The pressing demand for an escalation in renewable energy production arises from the surge in power consumption, escalating environmental concerns, and the exhaustion of fossil fuels. Microgrids, featuring dispersed power sources, exhibit superior resilience, efficiency, and stability in contrast to conventional power grids. Noteworthy investments in eco-friendly initiatives have been made by various major corporations, particularly in institutional structures. The current research advocates for the integration of photovoltaics, energy storage, and electric vehicles (EVs) as pivotal elements in a smart grid. This study introduces an optimal energy management system (EMS) designed to efficiently allocate power from existing sources. To address the challenges within an uncertain and reliable environment, a stochastic framework leveraging the whale optimization algorithm and unscented transform has been formulated. All simulations have been executed using the digital twin of the smart grid components. Results suggest that the proposed EMS ensures uninterrupted power supply, contributing to a substantial 45% reduction in energy costs. Furthermore, the potential of EVs as storage devices is explored. When employed as energy sources, EVs led to a noteworthy 45.58% reduction in power prices, while their use as loads still yielded a significant 19.33% decrease. The study also delves into the analysis of outage effects on the continuous power supply cost.