Skip to content

The Planner

Jan Brandsma edited this page Dec 4, 2026 · 1 revision

The planner

ZenMaster plans the next 48 hours of your house every quarter of an hour. The plan says, per quarter, how much to buy from and sell to the grid, how much each battery charges or discharges, and how much power a heat pump or other heat store draws. It is the cheapest plan that respects every physical limit the integration knows about.

A linear program, solved with HiGHS

The plan is a linear program (LP): a set of variables (called columns), linear constraints between them (called rows), and a linear cost to minimise. ZenMaster solves it with HiGHS, an open-source LP solver, through its Python package highspy.

Item Value
Horizon 192 quarters (48 hours)
History 12 quarters (3 hours) kept in front of the horizon, for charts and settlement
Solve moment every quarter at :00, :15, :30 and :45, plus once after a change in structure or fleet capacity
Solver time limit 30 seconds
Where it runs in an executor thread, so Home Assistant keeps running while HiGHS works

Every variable is a series: one column per quarter of the horizon. A battery's charge power, for example, is 192 columns, one per quarter.

The variables

Owner Columns per quarter Unit
Grid buy, sell kW
House solar the roof panels on the AC side, up to the forecast kW
Each battery AC charge, AC discharge, own panels into the pack, own panels out through the inverter, state of charge kW, and 0 to 1 for the state of charge
Fleet total charge, total discharge (sums of the batteries, for charts and the mode) kW
House heating (heat pump) electrical power, room air temperature, building mass temperature, plus helper columns for comfort and the efficiency knee kW, degrees C
Pool or electric boiler electrical power, water temperature, plus a comfort helper column kW, degrees C

The batteries are explained on Battery model, the heat stores on House model.

Some consumers are not in the LP at all. Appliances such as a dishwasher are placed in their cheapest window before the solve, an EV charge plan and a heat-pump hot water boiler are planned on their own. What they will draw is added to the fixed load per phase, so the LP plans around them.

The energy balance per quarter

Every quarter the power that comes in must equal the power that goes out:

grid_buy + house_solar + sum(battery discharge) + sum(battery own-panel output)
  - grid_sell - sum(battery AC charge) - sum(heat store power)
  = fixed load on L1 + L2 + L3

The fixed load is the background load of the house per phase (a weekday and hour profile from 28 days of history, see Solar, base load and phases) plus what the consumers outside the LP have planned. Grid buy and grid sell are each capped at the connection size max_kw.

The phase limits

A three-phase connection has a limit per phase: max_kw / 3. For each phase L1, L2 and L3 there is one row per quarter:

sum(draw on this phase) + 1/3 * sum(draw on all three phases) <= max(0, phase_limit - fixed load on this phase)

Only draws that can be planned count here: battery AC charging and heat store power. A device bound to one phase counts fully on that phase; a device on all three phases (or with an unknown phase) counts one third on each. Discharging stays out of these rows, because it only lowers the load. When the fixed load already fills a phase, the room is zero rather than negative, so the plan never becomes infeasible.

The objective

HiGHS minimises the sum of these costs over the 48 hours:

Term Cost
Grid buy buy price of that quarter, per kWh
Grid sell minus the sell price of that quarter, per kWh
Battery wear per kWh discharged, around EUR 0.03, see Battery model
Battery end value minus the value of what is left in each pack at the end of the plan
House below its comfort floor EUR 50 per degree per quarter (soft floor)
House above its maximum EUR 50 per degree per quarter (soft ceiling)
House below its target EUR 0.0125 per degree per quarter, so the plan only dips under the target when that saves more
Pool or boiler below its minimum EUR 50 per degree per quarter

Prices come from the Price model. Power columns are in kW over a quarter, so a price per kWh enters as price * 0.25.

What is refreshed every tick

The structure of the LP (which columns and rows exist) is built once, and again only when a device or consumer is added, removed or changes phase. Every tick only the numbers change:

  • The history shifts one slot per quarter that has passed.
  • Every entry refreshes its bounds and costs: prices, the solar forecast, the background load, each battery's live state of charge and power limits, the live temperatures.
  • Bounds written as functions of the quarter (such as the phase room) are evaluated again.
  • Rows whose coefficients change (a battery's kWh per percent, the heat pump's efficiency per quarter from the outdoor forecast) are rebuilt in place.
flowchart LR
    A[Quarter tick] --> B[Shift history]
    B --> C[Entries refresh bounds and costs]
    C --> D{Buy prices known?}
    D -- no --> F[No plan this quarter]
    D -- yes --> E[HiGHS solves in executor]
    E -- optimal --> G[Results into series, summary]
    E -- not optimal --> F
    G --> H[Plan handed to devices]
Loading

Without buy prices, or when HiGHS does not report an optimal solution, there is no plan for that quarter. With the optimize switch on, the fleet then stops instead of repeating the old plan.

Why the plan looks the way it does

After a solve HiGHS also returns the duals: per row a shadow price, the money one more unit of that limit would save. ZenMaster uses them to explain the plan (explain()):

  • Binding rows. A labelled row whose shadow price is not zero binds in that quarter. The shadow price is scaled to EUR per kWh (or EUR per degree for temperature rows).
  • Limits in the summary. A phase limit, an inverter maximum, the battery delivery cap, a comfort row and so on enters the summary when its shadow price reaches EUR 0.05 per kWh or per degree, with the number of quarters it binds and its highest value. The energy balance, the battery state of charge and the heat balances always carry a price, so they are not reported as limits.
  • Reduced costs. Per kW series, what one more kWh in that quarter would cost on top of what it brings. This is used to name the next best quarter for a battery window; one closer than EUR 0.01 per kWh is called equally good.
  • Placement reasons for the consumers outside the LP, such as the window an appliance got.

In the panel this is the text under the chart: which phase is full, when the battery charges and why, and what holds a cheaper plan back.

In the code: custom_components/zenmaster/solver/solver.py, custom_components/zenmaster/solver/battery.py, custom_components/zenmaster/solver/heating.py, custom_components/zenmaster/solver/baseload.py, custom_components/zenmaster/solver/forecast.py, custom_components/zenmaster/coordinator.py

Clone this wiki locally