Units and Numerical Stability¶
HAEO uses specific unit conventions for numerical stability and solver performance.
Unit system¶
HAEO uses SI-derived units scaled for energy systems:
| Quantity | Unit | Rationale |
|---|---|---|
| Power | kilowatts (kW) | Typical household range: 0.1-20 kW |
| Energy | kilowatt-hours (kWh) | Battery capacities: 5-100 kWh |
| Time | hours (h) | Natural for energy calculations |
| Price | dollars per kWh ($/kWh) | Standard electricity pricing |
Why not watts and watt-hours?¶
Using watts would create numerical issues:
❌ Bad: Using Watts
✅ Good: Using Kilowatts
Numerical stability¶
Linear programming solvers work best when:
- Variables have similar magnitudes (0.01 to 1000)
- Avoiding very large numbers (> 10⁶)
- Avoiding very small numbers (< 10⁻⁶)
Our unit choice keeps values in the ideal range:
- Battery energy: 5-100 kWh
- Power flows: 0.1-20 kW
- Time steps: 0.0833-1 hours
- Prices: 0.05-0.50 $/kWh
Energy calculations¶
With our units, energy calculations are natural:
# Energy = Power × Time
energy_kwh = power_kw * time_hours
# Example: 5 kW for 0.5 hours = 2.5 kWh
energy = 5.0 * 0.5 # = 2.5 kWh
No conversion factors needed!
Cost calculations¶
# Cost = Energy × Price
cost = energy_kwh * price_per_kwh
# Example: 10 kWh at $0.25/kWh = $2.50
cost = 10.0 * 0.25 # = $2.50
Again, no conversions!
Converting external data¶
Sources are converted to base units as they load, in core/units.py.
base_unit_conversion() returns the UnitConversion from a unit to its base unit: W, MW, and GW to kW, Wh and MWh to kWh, minutes and days to hours, and a price per energy unit, such as €/MWh, to the same currency per kWh, with a price in cents or pence, such as c/kWh, converting to the main currency unit (ct to € and p to £, while c and ¢ name no currency and convert to HAEO's $ placeholder).
A unit stated as the reciprocal of its base unit, such as km/kWh or mi/kWh for energy per distance in kWh/km, is scaled and then inverted, and a zero in it raises ValueError, so the source has no reading rather than an infinite one.
UnitOfMeasurement.of() reads a unit ignoring whitespace, as some integrations write kWh/100 km.
It also ignores case, except where case tells units apart: an m or M starting a part, which starts the unit or follows a slash or a number, is milli or mega, and a one-letter unit keeps its case.
So KWH and KM read as kWh and km, while mW is never read as MW, nor Home Assistant's weeks unit w as W.
A currency symbol from CURRENCY_SYMBOLS, such as the forint's Ft, is never read as a unit.
A calendar event's unit is the longest leading part of the text after its number that is_unit() accepts, so kWh / night is kWh, while kWh / h is a rate HAEO does not read.
Other currencies cannot be converted, so the coordinator groups price sources with price_currency_sources() and raises a repair issue, without stopping the optimization, when they report more than one currency.
Each source's base unit must then fit the input field's unit, checked by core/data/util/field_units.py.
A source in a unit that does not convert raises an InputError, which the input reports instead of reading the value as the field's unit, naming every unit that does convert to it.
A sensor that states no unit passes.
The entity selector offers an entity for a field by the same test: its unit, converted to base units, must be the field's unit.
A value a user types, such as in a calendar event, must state its unit: to_field_unit() converts it to the field's unit and rejects a bare number for a field with a unit, so no number is ever taken to be in HAEO's own units.
Implementation guidelines¶
When adding new features:
- Store in kW/kWh/hours: Always use standard units internally
- Convert at boundaries: Convert only when interfacing with HA
- Document units: Comment units for all variables
- Validate ranges: Check values are in expected ranges
Example:
def add_battery(
self,
capacity: float, # kWh
max_power: float, # kW
period: float, # hours
) -> None:
"""Add battery to network.
Args:
capacity: Battery capacity in kWh (typical: 5-100)
max_power: Maximum power in kW (typical: 3-10)
period: Time step in hours (typical: 0.083-1.0)
"""
# Validate ranges
assert 0 < capacity < 1000, "Capacity should be in kWh"
assert 0 < max_power < 100, "Power should be in kW"
# ... rest of implementation
Related documentation¶
-
Battery Modeling
See units in context of battery formulation.
-
Grid Modeling
Price units and grid formulation.