Skip to content

feat(heat): plan the owner's heat zones in shadow, with heat pump zones and a named heat input - #2041

Open
hannesb90 wants to merge 15 commits into
srcfl:masterfrom
hannesb90:feat/heat-shadow-plan
Open

hannesb90 wants to merge 15 commits into
srcfl:masterfrom
hannesb90:feat/heat-shadow-plan

Conversation

@hannesb90

@hannesb90 hannesb90 commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Builds on #2040. The first two commits here are #2040's. Review those there; this PR is the thirteen commits after them, and it rebases cleanly once #2040 lands.

What

Heat zones can be planned by Core, in shadow unless the owner turns writing on (docs/adr/0012).

  1. heat config section (go/internal/config/heat.go). Each zone names its sensor, control, boundary (outdoor or a measured temperature, such as the room around a floor), source, heat output, target and hard band, plus the owner's price_comfort and peak_relief. heat.savings is the least saving a day's plans must reach together. No number has a default; a section left out changes nothing; a section that cannot be read one way is refused, naming the zone.
  2. heatrun service. Every quarter it plans each zone from its thermal twin, the stored total price and the weather forecast, applies the owner's rules, and weighs every zone's saving together against the least saving. GET /api/heat/plan shows the plan, the saving per day and what was written.
  3. Writing, off by default (heat.write). A zone's control is held at the plan's first value through the same drivercontrol.Holds the API uses. main now creates one set of holds and passes it to both, so a planned value and an operator's hold share one lock and one expiry. A zone writes only while its twin is trusted, the least saving is reached, the site meter is fresh, and no one else holds the driver; otherwise the device goes back to its own default. An operator's hold is neither overwritten nor released. A held value is renewed between plans before the hold or the device drops it (CTC resets its BMS registers five minutes after the last write).
  4. heat_input per zone for the twin. A room sensor on one device (an underfloor gateway) is heated by another (a heat pump). The twin used to take the sensor's own driver's power or the single other driver reporting one, so a site with several had none. A zone can now name "<driver>/<metric>", and the twin learns against that and nothing else. A sensor named in heat.zones is learned even if its driver declares no sensors.
  5. Heat pump zones. source: heat_pump takes the owner's efficiency and heat curve; zero fields take heatplan's starting point. A heat pump zone needs the outdoor forecast. A zone whose declared control is a curve_offset is planned through it with the owner's curve_effect until a twin learns it. heatplan.Source gains an exported Validate.
  6. Shadow log (heatrun/shadowlog.go, <data dir>/heat-shadow.jsonl, rotated at 32 MB). At every quarter boundary each zone gets one JSON line: what the plan says for the quarter starting now (plan and baseline temperature, kWh and cost) and how the quarter that just ended came out. The measured outcome has the start and end temperature, heat input in kWh and its coverage, electricity drawn and its cost at the stored price, and model_end_c. That field is the planner's model run on the heat that actually went in, so the model is tested whoever controls the zone. A heat pump zone is costed on its electrical draw, not its heat. A forecast is compared only for the very quarter it was made for, and a thinly covered quarter is not judged. A record that cannot be encoded is left out and reported, and the other zones are still written.

Why

This is the house that runs the planner's first version locally (a Python controller over a Wiser hub): three electric floors, and a CTC EcoZenith i255 with an EcoAir 610M feeding LK underfloor loops. Moving that control into Core needs exactly this: the owner's numbers in config, a plan visible before anything is written, writes through Core's holds, and a way to tie LK's room sensors to the CTC's heat.

Evidence

  • go build ./... and go vet are clean for the touched packages.
  • Tests pass in heatrun, heatplan, config, thermaltwin, api and drivercontrol, and in cmd/ftw for the twin zones. On the Raspberry Pi I built on, 7 cmd/ftw identity tests fail with key directory mode 0775. They pass with umask 022 and are unrelated.
  • heatrun tests cover:
    • shadow writes nothing;
    • a trusted zone is written once and held;
    • an untrusted twin, a closed gate or a stale site meter hands the device back;
    • an operator's hold is left alone;
    • turning writing off releases;
    • a 5-minute device hold is renewed between plans;
    • a heat pump needs the forecast;
    • a curve offset needs its effect.
  • A plan for a 6 kW heat pump zone under a 450 öre evening peak preheats +2 just before the peak and runs −5 through it. The house goes from 21.5 to 20.7 °C, inside its band.
  • Shadow log tests cover the ended-quarter comparison, a gap, partial input coverage, rotation, the planner's model temperature, heat pump cost on draw, a late first quarter, a 0.0 °C reading and a NaN record.
  • Running in shadow on this house since 21 November 2026: three Wiser floors and the CTC/LK zone, with writing off. The CTC/LK twin started from zero and has no physical model yet. There are too few days to compare planned saving with the meter; I will report that here once there are.
  • make verify was not run.

Not in this PR

  • A floor/air constraint across two zones.
  • Joint planning of zones on a shared source.
  • Writing to the CTC, whose register 1023 has no documented scale.
  • UI.

go/cmd/ftw/main.go is also touched by #1956, #1865, #1789, #1691 and #1364. The change here is three contained blocks: the shared holds, the heat service, and the catalog/twin reload line.

The changeset is minor, a visible new capability, so it needs the owner's approval.

🤖 Generated with Claude Code

hannesb90 and others added 7 commits November 17, 2026 15:33
The optimum is found first, with no cost on changing value. Each run of one
value is then folded into a neighbour when that costs less than a share of
what one slot at full output costs for every change it removes. Without it a
price that wobbles by a couple of percent from slot to slot made the plan
change value 74 times a day on a radiator house, each one a write to the
device and a relay cycle; with it, 11, at about 0.4 % higher planned cost.

A long preheat is still weighed as a whole, because the smoothing runs on the
finished plan and not inside the search.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Signed-off-by: hannesb90 <279736760+hannesb90@users.noreply.github.com>
Two rules the owner sets, beside PriceComfort. Both are off at zero and have
no default: what a saving or a peak is worth depends on the contract, the
house and the household.

SavingsGate is the least a day's plans must save together before they are
acted on. It was taken out in srcfl#2022 on the evidence that it never opened in
the house that ran it. The log of that house shows otherwise once the plans
are summed, as the limit is: it reached 5 kr on 15 and 16 November, partly
on a room model that overstated what a preheat saves. The ADR's Open section
now says so, and decision 9 stands unchanged until it is revisited. The gate
counts a plan that leaves its band as saving nothing, scales a short horizon
up to a day, and has a keep share so a saving near the limit does not open and
close it every slot. A test holds the case that went wrong: three rooms that
each save less than the limit and together save more.

PeakRelief lets a zone's target down to the bottom of its band in the dearest
hours of a sharp price peak: at least a minimum price, at least a factor times
the lowest price of the window ahead, and at most so many hours of one peak.
On a dear but flat day nothing is a peak. It moves the target only, never the
band.

Nothing calls either yet; the planner still only proposes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Signed-off-by: hannesb90 <279736760+hannesb90@users.noreply.github.com>
heat.zones gives each zone its target and hard band, price comfort and peak
relief; heat.savings the least saving worth acting on; heat.write whether a
plan is sent to a device at all. A section that is left out changes nothing,
write is off unless set, and a section that cannot be read one way is
refused with the zone it names. Nothing reads it yet.

The heatplan rules gain yaml tags so the file and /api/config spell them the
same way.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Signed-off-by: hannesb90 <279736760+hannesb90@users.noreply.github.com>
A heat service plans every zone in the heat section each quarter from its
thermal twin, the stored total price and the weather forecast, applies the
owner's price comfort and peak relief, and weighs every zone's saving
together against the owner's least saving. GET /api/heat/plan shows the plan,
what it would save a day and what was written.

Nothing is written unless heat.write is on. Then a zone's setpoint control is
held at the plan's first value through the same drivercontrol holds the API
uses, so a planned value and an operator's hold share one lock and one
expiry. A zone writes only while its twin is trusted, the least saving is
reached and the site meter is fresh; otherwise the device is handed back to
its own default. An operator's hold is neither overwritten nor released. A
held value is renewed between plans before the device or the hold drops it,
for controllers such as CTC's that reset a written register after five
minutes.

The heat section gains boundary (outdoor or a measured temperature, such as
the room around a floor), source and heat_kw, all the owner's. Only direct
electric setpoint zones are planned yet: a heat pump's efficiency and a curve
offset's learned effect have nowhere to come from in this section.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: hannesb90 <279736760+hannesb90@users.noreply.github.com>
A room sensor on an underfloor gateway is heated by a heat pump on another
device, and the twin could not tell: it takes the sensor's own driver's power
or the single other driver reporting one, and a site with more than one has
none. heat.zones[].heat_input names "<driver>/<metric>" for the zone; the twin
then learns against that and nothing else, and a silent named input is no
input rather than a fallback to a pump the owner did not name. A sensor named
in heat.zones is learned even when its driver declares no sensors.

A changed source restarts the twin, as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: hannesb90 <279736760+hannesb90@users.noreply.github.com>
…s effect

source: heat_pump takes the owner's efficiency and heat curve (zero or absent
fields take heatplan's starting point) and needs the outdoor forecast, which a
heat pump's COP and flow temperature depend on. A zone whose declared control
is a curve offset is planned through it with the owner's curve_effect, what
one unit does to the zone and the flow, until a twin learns it; the device's
own value is the control's declared default, else zero.

heatplan.Source gains an exported Validate so the config can refuse a curve or
an efficiency the planner would.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: hannesb90 <279736760+hannesb90@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: hannesb90 <279736760+hannesb90@users.noreply.github.com>

@miravoss26 miravoss26 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Builds on #2040 (first two commits are its, reviewed there). Adds the heat config section, heatrun service, GET /api/heat/plan and the write path through control holds. Shadow unless heat.write is on.

Partial review: this is ~2.9k diff lines and I read the API route, the holds writer, the config example, the changesets and heat_control.go closely. I did NOT read heatrun/service.go, thermal_twin.go or config/heat.go line by line.

  • GET /api/heat/plan is registered as Read. It exposes plan, savings and written values, no secrets. Reasonable.
  • HoldsWriter goes through the same drivercontrol.Holds as the operator endpoint, so one lock and one expiry. Good call. API and heat control share ControlHolds only when it is set, otherwise the API builds its own. Worth a test that main.go always sets it when heat is enabled, or an operator hold and a planned write could race on two locks.
  • Security screen: no secrets, no new deps, no new network hosts. The write path is the one place that touches devices. It is gated on heat.write, twin trust, the savings gate and meter freshness per the changeset.
  • CI green.

Needs a human decision before merge: this is the first path where Core writes setpoints from the planner. Someone who owns heat control should read service.go and confirm the hand-back-to-default behaviour on twin loss and meter staleness.

… of holds

main.go is read to check that drivercontrol.New is called once and that
the same holds go to heatrun.HoldsWriter and api.Deps.ControlHolds; the
API builds its own holds when given none. A second test runs both ways
over one set: each sees, and can release, the other's hold.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: hannesb90 <279736760+hannesb90@users.noreply.github.com>

@miravoss26 miravoss26 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heat zone planning in shadow, writing only when heat.write is on (ADR 0012). Adds the heat config section + validation, heatplan, heatrun, thermal twin wiring, and GET /api/heat/plan. Builds on #2040, so I only looked at the post-#2040 part and skimmed.

  • Security screen: nothing found in what I read. No new deps or network destinations; the new route is registered Read, so it follows the existing read-auth path. Writes go through the shared drivercontrol.Holds, so a planned value and an operator's hold take one lock and expire together.
  • Scope: this is ~2.6k lines. I read the API, config wiring, holds writer and the plan entry points, not the optimiser maths in heatplan/comfort.go line by line, so I'm not vouching for the savings logic.
  • Question: with heat.write on, what happens when the twin stops being trusted mid-hold? The changeset says writes happen only while it is trusted; I'd want to see the release path (SendDefault) tested for that case.

Needs a human decision: a domain reviewer should look at the planner before this goes in, since it controls heating once heat.write is enabled. CI is green.

Drives the service through the real HoldsWriter and drivercontrol.Holds,
so the release path is tested down to the registry's SendDefault, not
only against the fake writer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hannesb90

Copy link
Copy Markdown
Contributor Author

On the question of what happens when the twin stops being trusted mid-hold, with heat.write on:

write() hands the device back as soon as the zone's twin is not StatusTrusted (giveBack(..., "twin not trusted")). That releases our hold through HoldsWriter.Release, which calls drivercontrol.Holds.SendDefault. An operator's newer hold is left alone.

Tests:

  • TestUntrustedTwinOrClosedGateWritesNothingAndHandsBack covers it at the service level with the fake writer.
  • The stale-meter case is in the test at service_test.go:310.
  • New in e7f2808: TestUntrustedTwinMidHoldSendsTheDevicesDefault runs the service through the real HoldsWriter and drivercontrol.Holds. It checks that the registry receives exactly one SendDefault and that no hold is left armed. It fails if Release only clears the hold without sending the default.

The domain review of the planner is still open.

@miravoss26 miravoss26 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adds a heat config section and a heat planner (heatplan, heatrun, thermal twin), exposed read-only at GET /api/heat/plan. Nothing is written unless heat.write is on, and then only through the existing control holds, gated on a trusted twin, the least-saving rule and a fresh site meter. Stacked on #2040.

  • Security screen: no secrets, no new dependencies, no new network destinations. New HTTP surface is the one read-only GET handler. Writes go through the existing hold path, so it inherits the existing auth.
  • Shadow-by-default with no numeric defaults in config is the right shape for this.
  • CI is green on the checks that ran.
  • Needs a human decision: I did not verify the planner and comfort math or the twin-trust thresholds, and this is a 115 KB diff. A review of those from someone who knows the thermal model is worth more than mine. Also first two commits belong to #2040, so review those there.

My read: no security findings from a screen of the diff. Not a correctness sign-off on the planner.

@frahlg

frahlg commented Nov 21, 2026

Copy link
Copy Markdown
Member

Thanks for the test that runs the hand-back through the real holds writer. We have not reviewed the planner yet: heatplan, heatrun, the comfort maths and the twin trust thresholds. #2040 goes first, then #2041. To help, please share what the shadow plan did on a real house: the zone and heat source, how many days, and how the planned saving compared with what the meter showed. We need that before heat.write can be trusted.

hannesb90 and others added 5 commits November 21, 2026 14:28
The shadow plan was only in memory, so nothing showed whether its saving
rested on a prediction the house bears out. Each tick now appends one line
per zone to heat-shadow.jsonl in the data directory: the plan's and the
left-alone baseline's prediction for the quarter starting, the horizon's
saving and cost, and for the quarter that ended the forecast made for it,
the zone's temperature at both ends, the heat input's energy integrated
from 30 s samples and the share of the quarter those samples covered.

In shadow the device runs itself, so the baseline is what the readings can
confirm. heatplan.Plan keeps the baseline's slots for that. A forecast is
compared only with the quarter it was made for; after a gap it is left out.
The file moves to .1 at 32 MB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A twin that found its heat on its own records the bare driver as its
source. An owner who then wrote the same source as heat_input,
"<driver>/hp_power_w", gave a different string, and the twin restarted
and threw away what it had learned. Caught on the home box before it
happened: three floor twins with a week of samples each.

A named input that is a driver's own heat power is now recorded as the
bare driver, so the two spellings are one source.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A sample taken a few seconds after the boundary counted toward the slot
that ended, so a fully covered quarter read 1.0000005 on the home box.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The baseline in the shadow log only holds while the device runs itself.
On the home box another controller runs the floors, and with heat.write
on the plan runs them, so the baseline is never what happens. The log
now gives each quarter the temperature the planner's model, as it stood
at the quarter's start, reaches with the heat actually measured, beside
the reading. That tests the model FTW plans with under any control.

Each quarter also gets its electricity and its cost at the quarter's
price. For direct electric heat the electricity is the heat input; for a
heat pump, whose input is its space heat, it is the pump's hp_power_w,
hot water included, since the driver does not split the draw. Neither a
model temperature nor a cost is given for a quarter covered less than
90 % by fresh samples.

A test sets the measured heat to the plan's and checks the model
temperature is the plan's own, so the log and the planner cannot drift
apart; it fails with a different inertia.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The first plan after Core starts runs a minute into its quarter, so the
start temperature was read then while the model ran the whole quarter.
The model temperature now runs from when the start temperature was read.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@miravoss26 miravoss26 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adds a heat config section and a heatrun service that plans the owner's heat zones every quarter, with GET /api/heat/plan to view it. It only writes when heat.write is on, through the same drivercontrol.Holds the API uses. It also adds heat_input for the thermal twin and heat pump zones.

I built the branch (go build ./... clean) and the heatrun, heatplan, config, thermaltwin and drivercontrol tests pass. I did not run cmd/ftw. The write path matches the description: it needs a trusted twin, an open savings gate and a fresh site meter, and it doesn't overwrite or release an operator's hold.

Findings

  • Low, shadowlog.go:256: zp.StartC != 0 treats a real 0.0 °C reading as missing for unplanned zones. Log only.
  • Low, shadowlog.go:214: a NaN reading makes JSON encoding fail and drops that tick's records. Logged once, nothing else breaks.
  • Low, PR body: it says four commits after #2040, but there are twelve (shadow log, model test, late-slot fix and others). Worth updating so reviewers know the scope.
  • Info: make verify wasn't run, and 7 cmd/ftw identity tests fail on your Pi because of file mode. I read them as unrelated, but CI should confirm.

Security screen: no secrets, no new dependencies or hosts. The new route is Read-level like /api/thermal/twins, and heat.write goes through the existing config authz.

Safe to merge from my read once #2040 lands and the owner approves the minor changeset.

A NaN reading made JSON encoding fail and dropped every zone's record for
that tick. Append now leaves out only the record it cannot encode and
reports it. An unplanned zone's sensor reading is also checked for NaN
before it is logged.

The start temperature of an unplanned zone is now taken from the
planned flag alone. The old check already read the sensor again for a
0.0 °C start, so that log was right; the test now pins it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@miravoss26 miravoss26 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adds a shadow heat-zone planner (heatrun service, config heat:, GET /api/heat/plan) plus owner comfort rules. Everything is shadow by default; write: false sends nothing. ~3.9k diff lines, CI green.

  • Security: new route GET /api/heat/plan is Read-scoped, matches the neighbouring thermal route. No new dependencies or network hosts. Shared drivercontrol.Holds between Core and the API is a sensible way to avoid two locks.
  • Needs a human decision: when write: true is enabled, the plan starts controlling heat drivers. Validation covers 'write needs control' but I read only the diff skim, not the full control path in heatrun/service.go (694 lines). Someone who knows the drivers should read that file before write is turned on anywhere.
  • Mergeable state shows UNKNOWN, so recheck for conflicts.

Shadow part looks safe to me; the write path needs a human read.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants