## 2 Models and Methods
### Battery Management Optimization  
Electricity price varies according to the period of the day, \(p\), in which the consumption is established. Usually, these periods have been divided into peak, with high prices, and valley hours, with low prices, however, with the proliferation of the electric vehicle, a new period has appeared, the flat hours, with intermediate prices. The benefits of machine learning techniques allow a model to be obtained with which the electrical demand can be predicted in subsequent periods of time, \(\delta t\). The energy storage system benefits from this model, being able to recognize at any time, \(t\), the trend in consumption. In this way, it is possible to predict the best option that the storage system can take in order to maximize the electrical energy consumption in periods with lower electricity costs. The Figure 3 shows the storage flowchart and following paragraphs describe the mathematical model performed in order to minimize the electricity bills.  
Figure 3: Storage flowchart.  
The first stage is to apply the first principle of thermodynamics by balancing the power demand in the building, \(D_{B}\), with the power consumed from the electrical network, \(P\). The effects of the battery in both directions are also considered, i.e., the charging and discharging powers, \(P_{Ch}\) and \(P_{Disch}\) respectively. Equation (12) shows this principle applied to each period of time \(t\).  
\[P(t)+P_{Disch}(t)=D_{B}(t)+P_{Ch}(t) \tag{12}\]  
Subsequently, the battery state of charge at time \(t\), \(SoC(t)\), is computed from the previous state, \(SoC(t-1)\), and the charging and discharging needs in the building. In order to optimize the storage according to the needs of the building, in Equation (13) the charging is multiplied by a function that establishes the amount of power consumed from the electrical network needed at each time \(t\), \(f(SoC\), \(p\), \(t\), \(\delta t)\). This function is carried out from the predefined neural network model and allows the estimating of whether an overcharging, cover only the demand, a charging defect or a zero charging period is necessary.  
\[SoC(t)=SoC(t-1)-\frac{P_{Disch}(t)}{C_{nom}\cdot\eta_{Disch}}+\frac{P_{Ch}(t) \cdot\eta_{Ch}}{C_{nom}}\cdot f(SoC,\ p,\ t,\ \delta t) \tag{13}\]  
where \(\eta_{Disch}(t)\) and \(\eta_{Ch}(t)\) are the battery discharging and charging efficiencies, and \(C_{nom}\) is the capacity.  
In order to avoid irregularities in battery usage, its boundary conditions are stablished. On the one hand, the energy stored in the battery cannot exceed the capacity, due to physical limits, neither can be completely emptied, to protect and extend its life cycle. Equation (14) shows this restriction using \(SoC_{min}\) as the minimum allowed state of charge.  
\[SoC_{min}\leq SoC(t)\leq 1 \tag{14}\]  
On the other hand, the battery cannot be discharged or charged outside of the maximum ranges established by the manufacturer, \(P_{Disch,\ max}\) and \(P_{Ch,\ max}\) respectively. Equations (15) and (16) show these conditions.  
\[P_{Disch}(t)\leq P_{Disch,\ max} \tag{15}\]  
\[P_{Ch}(t)\leq P_{Ch,\ max} \tag{16}\]  
Finally, the cost function is determined from the calculations of power and energy consumed from the electrical network at each time \(t\), \(P(t)\) and \(E(t)\) respectively, shown in Equation (17).  
\[Cost=\sum_{t}[P(t)\cdot C_{P}(t,\ p)+E(t)\cdot C_{D}(t,\ p)] \tag{17}\]  
where \(C_{P}(t,\ p)\) and \(C_{D}(t,\ p)\) are the power and energy costs in period \(p\) and time \(t\) respectively.