Repository navigation
Battery Model
Every battery is its own unit in the planner. The planner sees each pack's power limits, its usable capacity, its losses and its wear, and decides per quarter what each one does. This page explains how a battery is described in the linear program (LP) and how ZenMaster books the money it saves.
Per battery and per quarter there are five columns:
| Column | Meaning | Upper bound |
|---|---|---|
| AC charge | power taken from the house grid into the pack | rated charge power |
| AC discharge | power delivered from the pack to the house | rated discharge power |
| DC solar in | the battery's own panels charging the pack | own solar forecast |
| DC solar out | the battery's own panels delivered through its inverter | own solar forecast |
| SoC | state of charge, 0 to 1 over the usable window | 1 |
The state of charge (SoC) is scaled to the usable window between the battery's soc_min and soc_max, so 0 means "at soc_min" and 1 means "at soc_max". The usable capacity is kwh * (soc_max - soc_min) / 100.
Two rows tie the solar and inverter together per quarter:
DC solar in + DC solar out <= own solar forecast-
AC discharge + DC solar out <= rated discharge power(the inverter carries both)
A battery that is offline, not under control, or has no pack gets zero capacity: all its flows are pinned to 0 and it drops out of the plan. The flows in the last quarter of the horizon are also pinned to 0, because nothing after it would account for them.
The SoC moves from one quarter to the next by this row:
SoC[t+1] = SoC[t] + gain * (AC_IN * charge[t] + DC_IN * solar_in[t]) - gain * discharge[t]
gain = 0.25 / usable capacity in kWh
| Constant | Value | Meaning |
|---|---|---|
AC_IN |
0.84 | of what a pack takes in over AC, this share comes back out |
DC_IN |
0.95 | of what a pack takes in from its own panels, this share comes back out |
The whole round-trip loss sits on the way in. Charging 1 kWh over AC adds 0.84 kWh that can be delivered later. That is why the plan only charges from the grid when the price difference covers the loss: a charge at price p is worth it only if a later kWh is worth more than p / 0.84.
Quarter 0 is pinned to the live SoC. A pack that reads above its window only has to stop charging, it is not forced to discharge the excess. A reading under soc_min never forces a charge.
Every kWh discharged over AC costs wear. For a pack that has worked as much as the fleet average this is WEAR_EUR = 0.03 EUR per kWh. A pack that has worked more pays more, one that has worked less pays less:
wear cost per kWh = WEAR_EUR * (1 + 0.6 * (wear / fleet mean wear - 1))
wear is the number of equivalent full cycles over the last 7 days (see below). The factor 0.6 is WEAR_BIAS. Only packs that are online and have capacity count in the fleet mean. The average cost over the fleet stays WEAR_EUR, but the plan turns to the less used packs first.
Energy left in a pack at the end of the 48 hours is not worthless. The last SoC column gets a negative cost (a reward):
end value per kWh = mean sell price of the 4 hours after the horizon + CALIBRATE_EUR * due days
Using the sell price, not the buy price, keeps the plan from buying energy just to hoard it.
A battery's SoC reading drifts when the pack never reaches 100 %. A Zendure SolarFlow battery therefore asks for a full charge now and then. The due days count from the last day the SoC reached 100 % (read from the recorder at start, and updated live):
| Days without 100 % | Due days |
|---|---|
| up to 23 | 0 |
| 24 | 1 |
| 30 or more | 7 |
Each due day adds CALIBRATE_EUR = 0.02 EUR per kWh to the end value of that pack, so the plan prefers to leave it full at the end of the horizon. A kWh that is charged and discharged again earns no discount.
While a calibration is due the device's own upper SoC limit is set to 100 %, and the user's soc_max is kept for afterwards. On the last day (30 days), above 80 % SoC, the battery leaves the fleet and charges on at full power until it is full. Once it has reached 100 % the user's soc_max goes back and the battery rejoins the fleet at rest. Batteries of other brands report no due days.
Some countries cap what plug-in batteries may deliver to the house, for example 800 W in Germany. The distribution setting max_output (in W, 0 is no cap) adds one row per quarter:
sum(AC discharge + DC solar out over all batteries) <= max_output
The same cap applies to the live setpoint, see Real-time control.
Once a day ZenMaster reads 7 days of hourly statistics of each battery's kWh counters:
wear = (kWh in + kWh out) / (2 * pack kWh) equivalent full cycles per week
RTE = (kWh out + SoC shift over the window) / kWh in * 100 in %, kept between 0 and 100
The round-trip efficiency (RTE) is published as a sensor on each battery so you can compare it with the 0.84 the plan assumes. The plan itself uses the fixed AC_IN.
ZenMaster treats the energy in the fleet as stock with a weighted average cost:
- A charge adds kWh at the price of that quarter: grid energy at the buy price, own solar at the sell price (the export income given up). The average cost of the stock is updated.
- A discharge saves the difference between the price it avoids and the average cost: the buy price for what the house uses, the sell price for what goes to the grid.
saving = (discharge - to grid) * (buy - average cost) + to grid * (sell - average cost)
This is settled once per quarter from the fleet's kWh counters, at the price of the quarter the energy was measured in. The result is the savings sensor (total, never reset). savings_today is the realised part since midnight plus the saving the plan expects over the rest of the day, so it moves from forecast at 00:00 to fact at 24:00.
Shifted consumers add their own saving to the same total:
- Heating: the energy used, at the price of its quarter, against the average buy price of the coming 24 hours.
- Appliances: the energy of a cycle at the price when it was armed, against what the cycle really cost.
In the code: custom_components/zenmaster/solver/battery.py, custom_components/zenmaster/distribution.py, custom_components/zenmaster/zendure/solarflow.py, custom_components/zenmaster/device.py, custom_components/zenmaster/solver/heating.py, custom_components/zenmaster/solver/consumers/appliance.py
ZenMaster on GitHub · Releases · Issues · Licensed under GPL-3.0
Getting started
Devices
How it calculates
- The planner
- Battery model
- Real-time control
- House model
- Hot water tank
- Price model
- Solar, base load and phases
- Appliances and EV
- Recorder
More