Battery model¶
This page explains how HAEO models battery storage systems using linear programming.
Overview¶
A battery in HAEO's model layer is a single-section energy storage device with:
- Energy capacity: Maximum stored energy
- Cumulative energy tracking: Monotonically increasing charge/discharge variables
- State of charge constraints: Energy bounds within capacity, and min and max charge kept with priced slack
- Power balance: Integration with network through connections
Multi-Section Batteries
Requirements that span batteries, such as holding back energy for an outage, are Buffer elements measured from a level or the min and max charge. For explicit multi-section modeling, compose Battery Section elements with Connection elements.
Model formulation¶
The battery model follows the fence post pattern used throughout HAEO's optimization. Power variables (charge/discharge rates) represent average power over each period and have \(T\) values indexed as \(t \in \{0, 1, \ldots, T-1\}\). Energy variables (stored energy) represent instantaneous values at time boundaries and have \(T+1\) values indexed as \(t \in \{0, 1, \ldots, T\}\). Power is calculated from the change in energy between consecutive boundaries divided by the period duration.
Decision variables¶
For each time step \(t \in \{0, 1, \ldots, T\}\) (note: \(T+1\) time points for energy state):
-
\(E_{\text{in}}(t)\): Cumulative energy charged into battery (kWh, monotonically increasing)
-
\(E_{\text{out}}(t)\): Cumulative energy discharged from battery (kWh, monotonically increasing)
-
\(s_{\min}(t), s_{\max}(t) \geq 0\): Energy below the min charge and above the max charge (kWh), for \(t \in \{1, \ldots, T\}\)
Initial conditions:
- \(E_{\text{in}}(0)\): Initial charge level (constant, not a variable)
- \(E_{\text{out}}(0) = 0\): Battery starts with zero cumulative discharge
Parameters¶
Required parameters:
- \(C(t)\): Battery capacity (kWh) at time boundary \(t\) -
capacity - \(E_{\text{initial}}\): Initial charge in kWh -
initial_charge - \(\Delta t\): Time step duration (hours) -
period
Optional parameters:
- \(E_{\min}(t)\): Min charge in kWh, default 0 -
min_charge - \(E_{\max}(t)\): Max charge in kWh, default \(C(t)\) -
max_charge - \(c_{\text{limit}}(t)\): Holding cost for energy outside the min and max charge ($/kWh/h), required with either limit -
charge_limit_cost - \(v_{\text{salvage}}\): Terminal value of stored energy ($/kWh) -
salvage_value outbound_tags: Tags that discharged power can be placed on (see Tagged Power)inbound_tags: Tags this battery can consume (charge from) — None means all tags
Constraints¶
1. Energy flow constraints¶
Cumulative energy variables can only increase over time (prevents energy flowing backward):
This ensures energy flows are unidirectional (charging increases \(E_{\text{in}}\), discharging increases \(E_{\text{out}}\)).
Shadow prices: The energy_in_flow and energy_out_flow shadow prices represent the marginal value of relaxing these constraints. Non-zero values are rare in practice since batteries naturally flow energy in one direction at a time.
2. State of charge constraints¶
Net energy must stay within capacity:
Net energy stays within the min and max charge, or the slack carries the difference:
The slack is priced at the limit cost \(c_{\text{limit}}\) for each hour it lasts (see cost contribution), far above any price difference, so the optimizer treats the min and max charge as hard limits. The slack exists so that a battery reported outside its limits, for example below its min charge after a long outage, still gives a feasible plan, one that returns it within its limits as quickly as possible.
Shadow prices:
soc_max: Marginal value of a higher max charge. Negative values indicate the battery is held at its max charge and more room would reduce costs.soc_min: Marginal value of a lower min charge. Positive values indicate the battery is held at its min charge and the ability to extract more energy would reduce costs.
3. Power balance constraint¶
Net battery power equals the power from network connections:
Where:
- \(P_{\text{connection}}(t)\) is the power from network connections (positive = charging, negative = discharging)
- \(P_{\text{charge}}(t) = \frac{E_{\text{in}}(t+1) - E_{\text{in}}(t)}{\Delta t}\) is the charging power (consumed)
- \(P_{\text{discharge}}(t) = \frac{E_{\text{out}}(t+1) - E_{\text{out}}(t)}{\Delta t}\) is the discharging power (produced)
Shadow price: The power_balance shadow price represents the marginal value of energy at the battery element, which is the stored-energy side of any connection efficiency.
4. Tag balance¶
The Element base class creates per-tag power balance constraints for all elements with tagged connections.
Discharge power is placed on outbound_tags; charge power draws from inbound_tags.
See the tagged power formulation for details.
Cost contribution¶
Energy outside the min and max charge costs the limit cost for each hour it stays there:
The battery model can include an optional terminal salvage value. When configured, it credits the final stored energy at time \(T\):
Other costs (efficiency losses, degradation penalties) are applied through Connection elements in the device adapter layer.
Physical interpretation¶
Cumulative energy tracking¶
The model tracks cumulative energy flows rather than absolute stored energy:
Net stored energy = Cumulative charged (\(E_{\text{in}}\)) - Cumulative discharged (\(E_{\text{out}}\))
This approach:
- Keeps charging and discharging as separate, non-negative quantities
- Uses only linear constraints (no binary variables required)
- Enables efficient LP solving
Power calculation¶
Power is derived from energy changes:
Energy flow example¶
Consider a 10 kWh battery section with initial charge of 4 kWh:
Initial state (\(t=0\)):
- \(E_{\text{in}}(0) = 4.0\) kWh (constant)
- \(E_{\text{out}}(0) = 0.0\) kWh (constant)
- Net energy = 4.0 kWh
After charging 2 kWh over 1 hour (\(t=1\)):
- \(E_{\text{in}}(1) = 6.0\) kWh (increased by 2)
- \(E_{\text{out}}(1) = 0.0\) kWh (unchanged)
- Net energy = 6.0 kWh
- \(P_{\text{charge}}(0) = (6.0 - 4.0) / 1.0 = 2.0\) kW
After discharging 3 kWh over next hour (\(t=2\)):
- \(E_{\text{in}}(2) = 6.0\) kWh (unchanged)
- \(E_{\text{out}}(2) = 3.0\) kWh (increased by 3)
- Net energy = 3.0 kWh
- \(P_{\text{discharge}}(1) = (3.0 - 0.0) / 1.0 = 3.0\) kW
Power balance integration¶
The battery participates in network power balance through the connection power:
Where:
- Positive \(P_{\text{connection}}\): Net power flowing into the battery (charging)
- Negative \(P_{\text{connection}}\): Net power flowing out of the battery (discharging)
The battery exposes separate produced (discharge) and consumed (charge) power for per-tag decomposition. See Tagged Power for how production and consumption are routed to specific tags.
Numerical considerations¶
Units¶
HAEO uses kW for power and kWh for energy:
- Capacity: 10 kWh (not 10000 Wh)
- Power: 5 kW (not 5000 W)
- Time: hours (not seconds)
This keeps variables in similar numerical ranges (0.001 to 1000) which:
- Improves solver performance
- Reduces numerical errors
- Makes debugging easier
See the units documentation for detailed explanation.
Device layer integration¶
The single-section battery model is a building block for more complex battery behavior:
- Multi-section batteries: Multiple Battery instances connected through a node
- Efficiency losses: Applied through Connection efficiency parameters
- Power limits: Applied through Connection max_power constraints
- Cost penalties: Applied through Connection price parameters
- Reserves and headroom: Applied through Buffer elements that read stored energy
See the Battery Device Layer documentation for how these are composed.
Next steps¶
-
User configuration guide
Configure batteries in your Home Assistant setup.
-
Device layer
Understand how single-section batteries are composed into multi-section behavior.
-
Connections
Power flow paths between elements.
-
Implementation
View the source code for the battery model.