
This paper proposes an Energy Management System (EMS) for rural microgrids in Morocco. The EMS is designed to optimize renewable energy self-consumption and reduce energy costs while considering Battery Energy Storage System (BESS) degradation. The proposed EMS focuses on optimizing power flow while ensuring full satisfaction of load demand. It aims to maximize the use of renewable energy sources through selfconsumption while effectively balancing supply and demand. Additionally, the EMS minimizes power exported to the grid, thereby enhancing system efficiency and sustainability. The effectiveness of the proposed EMS is validated through realtime Hardware-in-the-Loop testing using Speedgoat. The results demonstrate that the proposed system is easy to implement, enhances self-consumption, and mitigates BESS degradation.