Methodology for Energy Management in a Smart Microgrid Based on the Efficiency of Dispatchable Renewable Generation Sources and Distributed Storage Systems

: This paper presents a methodology for energy management in a smart microgrid based on the efficiency of dispatchable generation sources and storage systems, with three different aims: elimination of power peaks; optimisation of the operation and performance of the microgrid; and reduction of energy consumption from the distribution network. The methodology is based on four steps: identification of elements of the microgrid, monitoring of the elements, characterization of the efficiency of the elements, and finally, microgrid energy management. A specific use case is shown at CEDER-CIEMAT (Centro para el Desarrollo de las Energ í as Renovables—Centro de Investi-gaciones Energ é ticas, Medioambientales y Tecnol ó gicas), where consumption has been reduced during high tariff periods and power peaks have been eliminated, allowing an annual reduction of more than 25,000 kWh per year, which is equal to savings of more than 8500 € . It also allows the power contracted from the distribution company by CEDER (135 kW) not to be exceeded, which avoids penalties in the electricity bill.


Introduction
The development and maturation of renewable energies are triggering a profound change in the current energy system, displacing and replacing traditional electric power systems based on fossil fuels [1][2][3].The implementation of renewable energies is becoming the backbone for meeting the objectives set for the energy transition [4][5][6] as clean, sustainable, and accessible energy sources.In this context of change, microgrids are postulated as a major element in the design and adjustment of the electricity system [7][8][9][10].Where they are implemented, microgrids represent a solution that guarantees continuous and secure access to electricity based on the use of distributed renewable generation sources and distributed storage system [3].Moreover, the recent development of communication, information, and artificial intelligence technologies and their integration into microgrids means a new evolution of the concept: smart microgrids [11][12][13].
Some major concerns about renewable energies still represent challenges for the operability and management of the SMG that must be solved [14,15].As in the grid, different phenomena can occur in the SMG [13,[16][17][18][19][20], such as mismatches between generation and generated demand, power consumption peaks, frequency variations, voltage level variations, blackouts, etc.Of these, the one that will be studied in this article is the phenomenon of the mismatch between generation and demand generated by prolonged consumption peaks [21,22].Consequently, in order to guarantee the correct operation of microgrids, energy management strategies/methodologies need to be implemented in the SMG [23].These strategies will vary according to the conditions of each microgrid: topology, available resources, needs, limitations. . .[24,25], and/or the criteria used by each manager, always having as the main objective to guarantee the energy balance between generation and demand while maximising the efficiency of the system [26,27].However, the energy management of an SMG is a complex problem of a non-linear and non-convex nature, requiring control, management, and optimisation algorithms that perform this task according to the established strategies and priorities.However, the use of these complex and highly sophisticated systems does not necessarily improve the performance and economic viability of the whole, and it is up to the SMG manager to develop and implement them [28].
The most widespread management strategy focuses on optimising various economic aspects using cost analysis indicators and metrics.This strategy seeks to maximise system efficiency while minimising costs.The operating cost is one of the most widely used and studied indicators for the energy management of microgrids.There are also microgrid EMSs, which seek to minimise operating costs through load scheduling based on the prediction of both wind generation and load consumption, supported by energy storage systems (ESSs) and diesel generation [29].Other EMSs employ minimising operating costs by reducing fossil fuel-based generation sources [30].There are methodologies where, in addition to minimising the life cycle cost, the annual cost of load loss, and the life cycle cost of the equipment that make up the microgrid, are further explored [31,32].On the other hand, the EMS can optimise costs according to existing operating conditions to effectively coordinate both dispatchable and non-dispatchable energy resources to maintain the stability of the microgrid [33,34].
Operational cost reduction can be complemented with other indicators and metrics, such as cost of energy or net present cost, among others.The combination of different indicators is a common practice in EMSs based on economic objectives.This combination was used in [35,36] to determine the best microgrid configuration by minimising cost of energy as the main indicator.
The cost of energy is used in other management strategies to compare the generation performance of a microgrid with the national electricity tariff [37].These EMSs connect the microgrid to the distribution network by consuming them from the distribution network when the price of energy is low.During these periods, the generation is used to load the ESSs, discharging them during periods when the price of electricity is high.This strategy based on market prices is called energy arbitrage [27] and is one of the main ways to increase system efficiency and optimise economic performance [38].
Energy management strategies in microgrids can be approached from perspectives other than economic aspects.However, in such cases and despite not being the main focus, profitability and economic efficiency are usually highly considered factors in most EMSs.
Another main microgrid operation strategy focuses on Demand Side Management.This methodology focuses on the integration and active participation of users in the management to optimise energy consumption [39].This optimisation and increase in energy efficiency can be achieved through EMS with different strategies: making the best possible resource allocation by coordinating the demand side [40,41].A different management approach is to develop baseload profiles from generation forecasts [42][43][44].On the other hand, in cases where both resource scarcity scenarios occur, EMS methodologies such as energy curtailment [45,46] can be applied.
The Demand Side Management extends to the demand response strategy [47,48], where management seeks to change the consumption patterns of users regarding economic issues.In this way, economic incentives are implemented on consumption, and bidirectional communication with the grid is established in real-time, connecting and disconnecting the microgrid according to the operating costs and electricity pricing period.This type of management interconnects the objectives of economic and energy efficiency, in addition to minimising energy demand during periods of high consumption and/or low generation [49].Examples of different EMSs proposed in microgrids that employ demand response to increase the economic efficiency of their microgrids are developed in [50][51][52], resulting in significant cost reductions.
Another management criterion used in microgrid energy management strategies is to maximise renewable generation.Therefore, it is necessary for these EMSs to seek the best combination of available renewable generation sources, integrating complementary technologies to avoid generation valleys [53] that would upset the energy balance.In parallel to other strategies, the maximisation of renewable resources in the microgrid can be oriented towards different objectives and criteria.In the framework of energy transition and decarbonisation, one of these objectives is to reduce dependence on fossil fuels in order to achieve 100% renewable microgrids [30,54].Furthermore, taking advantage of the economic competitiveness of renewable energies over conventional energy sources [1,4], implementing EMSs that maximise the use of renewables leads to a reduction in operating costs [30,37,55].Consequently, maximising generation and consumption from distributed renewable generation sources in microgrids results in the ability to operate over extended time intervals under a self-consumption regime [56][57][58][59].
The following methodology for energy management in SMG focuses on battery operation.In all the cases highlighted, the energy management of microgrids shares as a common element the use of ESS, with batteries being the most widespread technology.This equipment allows production and consumption to be balanced, provides flexibility to the system, and increases energy use [60].This results in an increase in economic efficiency and a reduction in total operating costs [61].This economic optimisation is achieved by matching the charging and discharging battery price gap between electricity price and remuneration rate [62].This case study, as well as those presented in [63], highlights how state of charge and usable battery capacity can limit the profitability of this EMS.This criterion is again of particular relevance [64], where different EMSs are compared, with the management methodology based on the battery charging cycle being the optimal one for this case study [65].Finally, there are also cases of EMSs where energy management is aimed at battery care, seeking to extend battery life and optimise performance over the life of the battery [66][67][68][69].
With stability of the microgrid as the main objective, the load following strategy is based on the adjustment of generation by the microgrid operator/manager to meet consumption demands at all times, responding in real time to variations and maintaining the energy balance.In the existing cases of isolated microgrids that have limited resources and generation sources [70], this type of strategy is the main management criterion for guaranteeing energy supply to loads/consumers [63,71,72].In grid-connected microgrids, stability is not as sensitive as in the isolated case.However, it is true that it can be affected by phenomena coming from the grid.In connected cases where ensuring stability is a critical factor, the implementation of a load-following strategy to cover demand variations may be the sole objective of the EMS [34,73].However, this strategy is often complemented by other management criteria such as minimising costs, increasing equipment lifetime [56], meeting consumption forecasts [74], or optimising battery charging for specific equipment such as electric vehicles [75].
After exemplifying various EMS in microgrids, two methodologies focused on consumption peaks are shown below.One management strategy implemented to reduce the effects of this phenomenon on the microgrid is load shifting.The approach of this methodology is based on the identification of flexible and programmable loads in the system and their use to globally adjust the energy balance.The operating principle is based on scheduling the activation/start-up of peak-generating loads, which have previously been identified as manageable, in different periods of low consumption in such a way that the increase in demand does not generate mismatch peaks in these periods and can be assumed by the generation [76].Therefore, as developed in the cases [77,78], through the implementation of this strategy, the aim is to establish the best organisation and scheduling of loads in the microgrid, maximising efficiency and stability.These EMSs are especially interesting in microgrids as they allow the efficient integration of new loads such as electric vehicles [79,80].
In parallel, this methodology can be combined with those seen above to meet criteria in addition to peak shaving.An EMS can be used to optimise load relocation in order to minimise operating costs [40,81,82].Moreover, the EMS can establish the operation scenario that provides the most flexibility by studying its impact on the cost and stability of the network [83].
On the other hand, the peak shaving strategy is based on the total elimination or, failing that, the reduction of consumption peaks by managing the energy stored in the ESS [27,77].These peaks can have particularly detrimental effects on the microgrid, as they represent a threat to its stability by significantly unbalancing the relationship between generation and demand during time lapses that can last up to several hours [21], being particularly characteristic of certain time slots.Similarly, the most significant effect of peaks is the large increase in operating costs as they occur in characteristic time slots that coincide with high pricing periods where the price of energy is higher.In order for the grid not to collapse, the energy differential between demand and generation is taken from the grid.Therefore, this management strategy is employed to avoid this effect, as it is especially beneficial in technical and economic terms [84].This application is the exact one implemented in EMSs, where cost of energy and operating cost have been minimised [85][86][87].Again, the technology that plays the role of ESS to counteract the peaks are batteries, being implemented in practically all the case studies [88][89][90][91][92][93].
This paper presents a four-step methodology for managing the energy in an SMG, based on the efficiency of dispatchable generation sources and ESS.The main contribution of this proposal is the definition of a methodology based on the efficiency of the ESS available in a microgrid to optimise its energy performance.ESSs are used on the basis of their performance, rather than other variables such as capacity or response time, which are the most commonly used criteria in other studies.This way of using storage systems not only maximises the use of energy from renewable sources but also eliminates consumption peaks (periods of less than one hour) and reduces the energy consumption of the distribution grid, especially during the most expensive pricing periods.To apply this methodology, two opensource and easy-to-implement software, such as Home Assistant and NodeRed, have been used.The methodology is developed in Section 2 and has been successfully implemented in the SMG of CEDER-CIEMAT.Section 3 presents the use case in CEDER-CIEMAT microgrid.Finally, the conclusions are presented in the fourth section.

Methodology
The objective of this document is to present a four-step methodology for the energy management of a smart microgrid based on the efficiency of dispatchable generation sources and storage systems.
The prescribed sequence of actions required to attain these desired outcomes is outlined as follows: Step 1. Identification of elements.First of all, it is necessary to identify all the elements necessary to carry out the energy management of a smart microgrid: 1.
Metre of the energy distribution company: To monitor the energy consumption/injection of energy to the distribution network from the microgrid.

2.
Renewable dispatchable generation sources: A smart microgrid can be made up of different sources of power generation, most commonly including photovoltaic and small wind energy, although they are not dispatchable from a resource point of view.However, there are other sources of renewable energy generation that can be managed, such as hydroelectric power, which will need to be monitored to issue operating commands (start-up and shut-down depending on the needs of the microgrid).

3.
Distributed energy storage systems: The most commonly used in microgrids are batteries (lead-acid, lithium, etc.), although many different storage systems can be used.The different storage systems that make up the microgrid must be monitored to provide them with charging and discharging commands.
Step 2. Monitoring of elements.After identifying the various components, a crucial next step involves monitoring and integrating them into a comprehensive control software.This software serves as a communication conduit, enabling the elements within the microgrid to interact with one another effectively.Through this integrated control system, commands are issued to dispatchable generation sources and distributed energy storage systems, contingent upon the consumption of the microgrid from the distribution network.It is necessary to monitor the following parameters: 1.
Instantaneous power of the microgrid: This parameter serves as a vital indicator of the energy dynamics of the microgrid.When the value of this power parameter exceeds zero, it means that the microgrid is consuming energy from the distribution grid.
Conversely, if the value falls below zero, it implies that the energy generated by the renewable generation sources integrated within the microgrid surpasses the energy demands of its loads.This negative value indicates a surplus of energy, resulting in the microgrid exporting energy back to the distribution network.

2.
All parameters necessary to operate the dispatchable renewable generation sources: instantaneous power, start and stop commands, power setpoints, etc.

3.
All parameters necessary to operate the stored energy systems: instantaneous power, on/off commands, charge and discharge commands, etc.
In order to continuously track and analyse the aforementioned parameters and effectively establish a control system, implementation will rely on the utilisation of two open-source software programmes: NodeRed and Home Assistant [94,95].This strategic choice represents a noteworthy advantage associated with the proposed methodology.The incorporation of NodeRed and Home Assistant offers a comprehensive and robust framework for monitoring and controlling various parameters within the system.
NodeRed, a free, open-source programming tool, provides a visual programming interface that enables the creation of flows for data processing and communication between different elements of the microgrid.
The Modbus protocol is the most widely used protocol in the industry, and practically all equipment has it.If any element of the microgrid does not have it, other protocols such as MQTT, HTTP, etc. will be used.To communicate with a device using the Modbus communication protocol from NodeRed, it is necessary to configure a Modbus node for that device, defining the IP address, port number (default is 502), and the communication mode (Modbus TCP/IP or Modbus RTU).Additional parameters such as connection timeout and update frequency also need to be adjusted [94].
Once the communication is established, it is necessary to configure input and output nodes (for reading values or sending commands, respectively).This requires determining the device's identification number (Unit-ID), the Modbus register address, the number of addresses occupied by the register, and the register type (input, holding, etc.) [94].
Finally, after reading the value from the corresponding Modbus address (which is a value between 0 and 65,536), it needs to be interpreted using a JavaScript function programmed to obtain the actual value of the variable being measured, based on the information collected in the Modbus frame regarding the scaling factor and data type (integer: int, unsigned integer: uint, floating point: float, etc.) [94].
Additionally, Home Assistant, another free open-source platform based on the Internet of Things architecture, serves as a pivotal component for automation and control purposes.It offers a user-friendly interface and supports the integration of a wide array of smart devices and protocols.By leveraging Home Assistant's extensive capabilities, the proposed methodology enables the development of a sophisticated control system that can effectively regulate the parameters under scrutiny.
Home Assistant receives information about the different parameters from NodeRed and allows the creation of automations based on events, schedules, or any other conditions by programming in YAML or using the visual automation editor.
The Home Assistant control panel enables the creation of customised views by configuring cards and panels to display real-time information, such as device status, graphs of measured variables, etc.
The adoption of these open-source programmes demonstrates the methodology's commitment to accessibility and affordability.Leveraging NodeRed and Home Assistant ensures flexibility and adaptability and allows for further customizations and enhancements based on specific needs.
Figure 1 shows the flow of data and commands between Home Assistant, NodeRed, and the elements of the microgrid.NodeRed communicates with all the elements of the microgrid to collect all the necessary data and sends them to Home Assistant, which works as a human-machine interface and allows their visualisation in real time (1 data per second).The operation commands are activated from Home Assistant and sent to the different elements of the microgrid through NodeRed.Step 3. Characterise the efficiency of the elements of the microgrid.Once all the elements of the microgrid have been identified and monitored, it is necessary to characterise them in order to subsequently classify them according to their efficiency.
To characterise dispatchable generation sources, it is necessary to run them at different powers and see the resource consumption associated with them.In order to characterise storage elements, their efficiency has to be calculated in terms of the energy they consume or supply in charging and discharging processes (according to Equation (1)) at different instantaneous powers.
Performance: η = P out /P in × 100% where: η: is the performance.P out : is the output power (injected by the element under consideration into the microgrid).P in : is the input power (consumed from the microgrid by the element under consideration).
Step 4. Microgrid energy management.Energy management in a microgrid encompasses many aspects, and it is always difficult to single out one over another.In this methodology, the main objective is to optimise the performance of the microgrid by prioritising the commissioning of dispatchable generation sources and the charging and discharging of storage systems according to their efficiency, reducing the energy consumption of the distribution grid and eliminating power peaks.
Figure 2 shows a flowchart of the control process that will be part of the microgrid energy manager and will run recursively.This means when it reaches the end of the process, it starts again and therefore runs again.
The diagram starts by checking whether the microgrid is consuming energy from the distribution network or injecting it.
If the microgrid is consuming energy from the distribution network, the possibility of increasing the output of renewable generators needs to be considered.If possible, renewable generation will be increased.If not, electricity storage systems will come into play.If there is capacity in these systems, they will deliver energy to cover the demands of the microgrid.If the microgrid is injecting energy into the distribution network, it is necessary to see if the storage systems have stored energy capacity.If there is storage capacity, priority will be given to charging the storage systems in order to reduce the amount delivered to the outside, and the limit of the load will be marked by the existing excess.
In order to implement adequate energy management, it is necessary to establish a strategy for charging and discharging stored energy, which, in this methodology, is based on using storage systems according to their performance in order to achieve the greatest efficiency in the operation of the microgrid.
Figure 3 shows a flow diagram of how the charging of the electrical storage systems of the microgrid is carried out according to their efficiency.
When the microgrid has surplus energy, the energy management system will start charging from the storage systems according to their efficiency, as seen in Figure 3, starting with the most efficient system.If it can be charged, it will be charged up to its limit as long as the surplus allows it.If, while this system is being charged or once charging is complete, there is still surplus energy, it will begin charging the next more efficient system, and so on until it reaches the least efficient storage system.Therefore, the process will be repeated n times, where n is the number of energy storage systems available in the microgrid.
The process ends when there is no longer surplus energy or when all storage systems are fully charged.
The process carried out to supply energy to the microgrid from manageable renewable generation sources or storage systems when it is consuming from the distribution network is very similar to that shown in Figure 3.It begins when the microgrid consumes energy from the distribution network.The generation sources or storage systems will be used to meet the energy needs based on their efficiency.

Use Case. CEDER-CIEMAT Microgrid
According to the four-step methodology described in the previous section, the first step is to identify the elements that are part of the microgrid and are involved in energy management.

Identification of Elements
In the CEDER-CIEMAT use-case, the elements to be considered in order to apply the methodology defined in this paper are the following:

Metre of the Energy Distribution Company
The energy distribution company's metre is not an element upon which we can act, but it is necessary to include it because it will be the one that tells us whether the microgrid consumes or injects energy into the distribution network.

Dispatchable Renewable Generation Sources
The CEDER microgrid includes different renewable generation systems, with a total installed capacity of over 400 kW:

•
Photovoltaic: There are 11 distributed photovoltaic systems throughout the centre, with a total installed capacity of 181.9 kW.They are non-dispatchable from the resource point of view.• Wind: There are five small-scale wind turbines with a total installed capacity of 162.2 kW.They are non-dispatchable from the resource point of view.

•
Hydraulic Turbine: There is a 60 kW mini Pelton turbine.It is dispatchable.
Photovoltaic modules and small wind turbines are not dispatchable from a resource point of view, but we can modify their output by sending commands to their inverters if there is sufficient radiation or wind speed, respectively.

Energy Storage Systems
CEDER microgrid incorporates several distributed energy storage systems, including the following:

•
Pumping system associated with the Pelton turbine, with a capacity of 30 kW.

•
Lead-acid battery PEPAI: 120 cells of 2 V each, capacity of 1080 Ah per cell.

•
Lead-acid battery PEPAII: 120 cells of 2 V each, capacity of 765 Ah per cell.

•
LFP batteries: Two racks with 196 cells of 3.2 V each, with a capacity of 50 Ah

Monitoring of Elements
The second step of the methodology consists of monitoring the elements involved in energy management and integrating them into a control system that allows operating commands to be issued to each of the dispatchable generation sources and storage systems.As explained in the previous section, NodeRed is used to monitor all these parameters, and Home Assistant is used to develop the control system.
Figure 4 shows one of the screens of the human-machine interface of the control sys-tem for Lead-acid batteries PEPA I, where the following information is displayed: Main values (battery power; battery voltage level; and power setpoint); Charging/Discharging (type of charge: normal, deep, floating, or equalisation; selection of the type of charge; selection of power setpoint; and button to send power setpoint command); State of charge in %; State (information about whether the battery is on or off; boolean button to select On/Off, indication of stopped, charging, or discharging; Temperature, state of charge, and indication of full load); Errors (shows the different errors that can occur in the battery).Figure 5 shows one of the screens of the human-machine interface of the control system for LFP batteries, where the following information is displayed: Rack (battery power; battery voltage level, battery current; and buttons to reset the battery and start); Temperatures (shows maximum and minimum temperatures and the cell in which they occur); Inverter (shows state of the inverter: on/off; and buttons to turn the inverter on and off); Command (to introduce the power setpoint and button to send the value); Voltage (shows maximum and minimum voltage levels and the cell in which they occur).Figure 5 also shows two graphs with the maximum and minimum voltage levels of the cells and the power of the battery.

Characterising the Efficiency of the Elements of the Microgrid
In order to be able to classify the elements that form part of the microgrid according to their efficiency, it is necessary to know in detail how they work.This means that it will be necessary to operate them at different power regimes and see the results obtained.

Hydraulic System
The hydraulic system consists of a Pelton turbine connected to a 60 kW electric generator, four pumps of 7.5 kW each, and two water storage tanks with a total capacity of 2000 m 3 and a difference in level of 70 m between them.
In order to characterise the turbine, a series of tests were carried out by operating them at different power levels and checking the consumption of the resource (in this case, water stored in the tanks) for each one of them.The results obtained are shown in Table 1: The powers at which the turbine has been characterised are 7.5 kW, 15 kW, 22.5 kW, and 30 kW, since these are the powers at which we can operate the pumping system (four pumps of 7.5 kW each).The system will return the turbined water that is collected by the lower reservoir to the upper reservoir.
The water consumption per kW produced is lowest in the 30 to 35 kW range, where less than 9 m 3 of water is needed for each kW of power generated by the turbine.Therefore, as far as possible, the turbine should ideally operate in this power range.
In order to characterise the pumping system, it is necessary to know the amount of water that the pumps are capable of lifting to the upper reservoir and their levels of electricity consumption.To accomplish this, water was pumped from the lower tank to the upper tank at the four possible power levels (7.5 kW, 15 kW, 22.5 kW, and 30 kW), i.e., using one, two, three, or all four pumps simultaneously.The results obtained are shown in Table 2: Table 2. m 3 of water lifted to the upper tank with one, two, three, and four pumps.

Pumps/Power (kWh)
Water Rising (m 3 ) m 3 /kWh To calculate the efficiency of the hydraulic system, we will use Formula 1 described in the previous section, obtaining the results shown in Table 3.The efficiency increases as the power increases due to the fact that the nominal power of the turbine is 30 kW, and therefore, its efficiency is higher at values close to that value, dropping significantly at lower values (7.5 kW).We can estimate the efficiency of the installation to be close to 32%, given that the turbine will not be used at power levels lower than 15 kW, as its efficiency decreases significantly within this range of values.

Lead-Acid Battery PEPAI: 120 Cells of 2 V Each, Capacity of 1080 Ah per Cell
To calculate the performance of the different CEDER-CIEMAT battery systems, a discharge from 100% to 20% at different power levels was carried out, followed by a complete recharge.
The energy obtained during the discharge process was calculated and compared with the energy required to recharge the battery to its initial state.With these values and using Equation (1), the efficiency at each power was calculated.The values obtained are shown in Table 4.The efficiency of these batteries is practically the same for all power ratings, at 83%.The values obtained for this battery system are shown in Table 5.The efficiency of these batteries varies between 73% for a power of 16 kW and 88% for 12 kW.An average efficiency of 80% is considered.
3.3.4.LFP Batteries: Two Racks with 196 Cells of 3.2 V Each, with a Capacity of 50 Ah Repeating the same process for Lithium Ferro-Phosphate batteries produces the values shown in Table 6.The efficiency of these batteries varies between 58 and 62%, with a higher efficiency observed at higher discharge powers (limited to 20 by the inverter).An average efficiency of 60% is considered.
Table 7 shows a summary of the average performance of each dispatchable generation source and storage system connected to the CEDER-CIEMAT microgrid.
With these values and according to the methodology defined in this paper, priority will be given to the use of PEPA I lead-acid batteries, followed by PEPA II lead-acid batteries, then LFP batteries, and lastly, the turbine and hydraulic pumps, which, in addition to having the lowest performance, have much higher operating and maintenance costs than the other systems.Normally, LFP batteries would have higher charging efficiency than lead-acid batteries.However, in our case, the opposite happens.This is due to the fact that the PEPA II batteries are new, so it is normal that their performance is higher the PEPA I batteries, which are about five years old, and the LFP batteries, which are more than 10 years old.It also makes sense that the performance of the hydraulic system is lower, since it is not designed to be optimised but rather depends on the orography of the terrain at CEDER and the available pumps.LFP batteries require less maintenance, are more modular, allow for a higher depth of discharge, and have lower self-discharge and faster charging than lead-acid batteries.In addition, LFP batteries do not emit gases during the charging and discharging process.

Microgrid Energy Management
According to the methodology defined in this paper, when the microgrid consumes power from the distribution network, if possible, the output of renewable generators should be increased.If this is not possible, the dispatchable storage and generation systems will be started up according to the performance calculations we conducted in the previous section.When the microgrid injects energy into the distribution network, the storage systems will be recharged (if they have the capacity to store more energy) according to their performance.
Energy management for recharging the ESS will be such that batteries will only be charged or the pumping system will only be used when there is surplus energy in the microgrid, so that the energy used is free.This recharging will be conducted according to the performance of the systems, starting with charging the lead-acid PEPA I batteries, then the lead-acid PEPA II batteries, followed by the LFP batteries, and lastly, the pumping system will be used.With this strategy, enough energy is stored during the months of April to October when there is abundant solar radiation, but not during the winter months when there is less radiation and therefore lower surplus generation.
Energy management for discharging the ESS is carried out with two different objectives: on the one hand, to reduce consumption from the distribution network for periods of several hours, and on the other hand, to eliminate power peaks (maximum 1 h), always depending on their efficiency.This is always conducted on the basis of efficiency, so that the most efficient ESS is used first, followed by the second most efficient system, and so on.Each type of management will be explained in the following section, Results.

Results
This section will explain the results obtained in the energy management of the CEDER microgrid in the two cases considered: reducing the consumption of the distribution network (up to 8 h) and eliminating power peaks (up to 1 h).
Out of all the days when the methodology was applied, two days of data are presented on which its benefits can be clearly seen (16 February 2023 and 12 January 2023)

Reducing Consumption of the Distribution Network
Consumption from the distribution network can be reduced at any time of the day, in consecutive or alternate hours, depending on what is of most interest from the point of view of microgrid management.
From the point of view of this paper, consumption is reduced when it is above the CEDER average (40-60 kWh) and during hours with higher cost per kWh (higher pricing periods).These maximum pricing periods change from one month to the next, as shown in Figure 6.There are six different periods (P1, P2, P3, P4, P5, and P6) in the CEDER-CIEMAT tariff, and the price of each period is negotiated with the distribution company.Table 8 shows the Eur/kWh prices for each of these periods.The use case was carried out with data from 16 February 2023.So, the maximum charging periods are P1 (from 9:00 to 14:00 and 18:00 to 19:00) and P2 (from 8:00 to 9:00, 14:00 to 18:00, and 22:00 to 24:00).So, consumption will be reduced during those hours when it is above the CEDER average.
Figure 7 shows the consumption data of the CEDER microgrid on 16 February 2023, without using the storage systems.There is above-average consumption between 8:00 (the start time of the working day at CEDER) and 15:00 (the end time of the working day at CEDER).Between 8:00 and 9:00 and 14:00 and 15:00 correspond to the second most expensive pricing period (P2), and between 9:00 and 14:00 corresponds to the most expensive pricing period (P1).
To reduce consumption, the ESSs are used according to their efficiency, i.e., first, the most efficient, then, the second most efficient, etc.According to step three of this methodology, to characterise the efficiency of the microgrid elements, the hierarchy of operation is as follows: Lead-acid battery PEPA I, Lead-acid battery PEPA II, LFP batteries, and Hydraulic system (Turbine/Pumps).The power generated by each of the storage systems to compensate for the power peak can be seen in Figure 8.The green line represents the total energy stored, which is the sum of the energy stored in PEPA I lead-acid battery (dark red line), the energy stored in PEPA II lead-acid battery (red line), the energy stored in the LFP battery (yellow line), plus the energy stored in the Pumping-Turbine system (blue line).Figure 9 shows the reduction of consumption using ESS, and there is a flatter consumption in the microgrid.The red line represents the consumption of the microgrid if energy stored systems were stopped, the yellow line represents the real consumption of the microgrid with energy storage systems working, and the green area is the energy saved due to the use of storage systems.

Eliminating Power Peaks
Figure 10 shows the consumption data of the CEDER microgrid on 12 January 2023, without using the storage systems.There is a consumption peak between 9:19 and 10:25, and it can be seen how consumption goes from levels of around 80-90 kW to values of up to 180 kW.This peak has been reduced using the methodology described in this article.In order to eliminate the peak, the storage systems have been started up according to their efficiency: battery PEPA I, battery PEPA II, LFP batteries, and the Hydraulic system.The power generated by each of the storage systems to compensate for the power peak can be seen in Figure 11.The green line represents the total energy stored, which is the sum of the energy stored in PEPA I lead-acid battery (dark red line), energy stored in PEPA II lead-acid battery (red line), energy stored in LFP battery (yellow line), plus energy stored in the Pumping-Turbine system (blue line).Figure 12 shows how the power peak has been eliminated using ESS, and the energy savings obtained by eliminating the peak power are depicted in green, totaling 51.6 kWh.The blue line represents the consumption of the microgrid if energy stored systems were stopped, the yellow line represents the real consumption of the microgrid with energy storage systems working, and the green area is the energy saved due to the use of storage systems.

Conclusions
The results of this study show that the methodology defined in this work for the smart management of a microgrid based on the efficiency of the available ESSs allows optimisation of the operation of the microgrid by reducing energy consumption of the distribution grid during the highest tariff periods and eliminating power peaks (of up to 2 h).This methodology was carried out using an energy management system developed by CEDER based on open-source software (and therefore free) and easy to implement (Home Assistant and NodeRed).
The procedure for reducing energy consumption of the distribution network during the highest tariff periods saves up to 288.38 kWh in a day (245.31kWh P1 and 43.07 kWh P2).Taking into account the capacity of the storage systems and the time needed to recharge them, as well as the fact that consumption similar to that of the target day in this case study occurs almost every working day in winter and autumn, we can estimate that this methodology can be applied about 80 days per year, which means a saving of 23,070.4kWh (19,624.8kWh P1 and 3445.6 kWh P2).The price of energy (year 2023) during the highest tariff periods is 0.331692 €/kWh in P1 and 0.319483 €/kWh in P2, which means an estimated annual saving of 7610.2 € (6509.4€ in P1 and 1100.8 in € P2).
The peak shaving procedure saves 51.6 kWh of consumption each day it is carried out.Taking into account that similar situations occur about 60 times a year, this allows for a saving of 3009 kWh in period P1, which is equivalent 855.8 € per year.In this case, the annual saving is lower, but it avoids exceeding the contracted power limit by CEDER (135 kW peak power), which would entail high penalties in the electricity bill.
Overall, the application of this methodology allows for annual savings of approximately €8500, optimising the operation of the energy storage systems and the management of the microgrid.
Table 9 shows the savings achieved in terms of energy and money (according to current tariffs in Table 8) by applying this methodology to the CEDER-CIEMAT microgrid (reducing energy consumption plus peak shaving).

Figure 1 .
Figure 1.Data and command flow between microgrid elements.

Figure 3 .
Figure 3. Flowchart of the charging of the electrical storage systems of a microgrid.

Figure 4 .
Figure 4. Lead-acid batteries PEPAI Control panel in Home Assistant.

Figure 5 .
Figure 5. LFP Batteries control panel in Home Assistant.

Figure 9
Figure 9 also shows in green the energy savings obtained by reducing consumption in periods P1 (245.31kWh) and P2 (43.07 kWh).

Table 1 .
Turbine water consumption at different operating powers.

Table 3 .
Performance of the hydraulic system at different operating powers.

Table 4 .
Lead-acid battery PEPA I discharging and charging at different powers.

Table 5 .
Lead-acid battery PEPA II discharging and charging at different powers.

Table 6 .
LFP battery discharging and charging at different powers.

Table 7 .
Performance of generation sources and storage systems in the CEDER microgrid.

Table 8 .
kWh prices (in euros €) for each period according to CEDER's electricity bill (Contract 6.1 TD).

Table 9 .
Savings achieved in terms of energy and money at the CEDER microgrid (reducing energy consumption).