## 3 Homeostatic Control  
As it was explained before, the homeostatic control system which makes the energy homeostaticity scheme possible, comprises of two different parts, reactive and predictive homeostasis. In this sense, the flow diagram for the proposed homeostatic control can be seen in Figure 11.  
The predictive branch, which is divided into two separated parts, are mainly centered in the use of the thermal model of the room to keep the temperatures at adequate intervals. It must be noted that this function would take on more and more importance when the building has more mass (which implies more thermal energy storage) and also when the thermal isolation with the outside is better (which implies less losses), having then more thermal inertia and having his management more impact in the total consumption of the building. Following this idea, the temperature inside a room for the next hour will be obtained from the thermal model:  
\[T_{intproxh}=f(T_{null1},\ T_{null2},\ T_{null3},\ T_{null4},\ T_{int}), \tag{1}\]  
The reactive branch, which conforms the core of the control system, must keep the batteries with enough charge to maintain the microgrid running, even if a blackout occurs. Additionally, the storage capacity can be used to actuate when the utility advice a high energy price (usually applied when a certain power or energy consumption value is reached), or even a DR event which requires a high reduction in the consumption of the microgrid. This fact is reflected with the parameter \(\mathrm{Plim}\) (power limit), which depends on the received information from the utility (or the distribution system operator). The minimum or maximum level of battery charge depends on the characteristics of the event (the time in which the maximum power consumption from the grid is limited) the expected consumption, and the estimation of solar generation that could be obtained for the period (in order to assure that all that generated energy will be consumed, or stored). The calculation of the limit power in the context of a typical DR event (of the type used by some utilities in North America [40]), is usually dependent on two variables, the client baseline for the event time and the minimum power reduction needed to avoid penalties. For the calculation of the baseline for every hour (or other time period needed), the mean of the ten previous days which are similar to the day of the event (e.g., laborable day) can be used:  
\[P_{baseline(h)}=\frac{\sum_{i=1}^{10}P_{daysimilar\_n(h)}}{10}, \tag{2}\] \begin{table}
\begin{tabular}{c c c} \hline \hline
**Name** & **Type** & **Description** \\ \hline pow\_limit & Input & Power consumption limit (from the utility grid) \\ pow\_now & Input & Power consumption \\ soc\_batBU & Input & State of battery group 1 \\ soc\_batST & Input & State of battery group 2 \\ gridOK & Input & Indicates if the utility grid is available (no blackout) \\ Twalls & Input & Walls temperature \\ Text & Input & Outside temperature \\ Tint & Input & Internal temperature \\ contactOnGrid & Output & On/Off grid selector \\ ContactBat & Output & On/Off battery selector \\ ContactHVAC & Output & On/Off HVAC selector \\ \hline \hline \end{tabular}
\end{table} 
Table 1: Control block inputs/outputs. of course, this depend on the type of DR program and contract stablished between utility and customer (or aggregator). Following this idea, the power limit will be calculated applying the minimum power reduction needed to the baseline power:  
\[P_{\text{lim}(DR\text{ {event}})}=P_{baseline(h)}-P_{reductionrequired}, \tag{3}\]  
or, if the power reduction required is zero (penalties to the customer would only be applied if they consume more than their baseline):  
\[P_{\text{lim}(DR\text{ {event}})}=P_{baseline(h)}, \tag{4}\]  
Last, if during a specific period there is not any event active, then the power limit will not be necessary (no power restriction specified).