Skip to content

fix(control): act only on new site-meter samples in battery dispatch - #1879

Merged
frahlg merged 2 commits into
masterfrom
a1-frozen-meter-holds-battery
Oct 27, 2026
Merged

frahlg merged 2 commits into
masterfrom
a1-frozen-meter-holds-battery

Conversation

@frahlg

@frahlg frahlg commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

Item A1 of the stable plan.

Problem

A failed site-meter poll keeps the old reading and its UpdatedAt (telemetry/store.go:222-229). The only gate before dispatch is the reading's age against the 60 s watchdog (site_dispatch_safety.go:43). Inside that window ComputeDispatch reads SmoothedW again on every tick (dispatch.go:1446-1449). The battery reports fresh power, so the meter clamp moves the target one more held error each tick, and the PI integrates the same error again.

Scenario: P1 meter stalls while a Sungrow battery keeps reporting, 5 s ticks, 10 kW battery.

  • Held +2000 W import, house 2000 W: the battery went to -10000 W in ten ticks. The site exported 8000 W while the meter still said +2000 W.
  • Held -3000 W export, house 1500 W: the battery went to +10000 W. The site imported 11500 W, and the fuse guard could not see it.

Change

State records the UpdatedAt of the meter sample the last battery dispatch used. When no newer sample exists:

  • The PI path returns the same early exit as the deadband: no new orders, no PI integration, fuse saver still active. It shares the deadband's carve-outs: when a battery discharges under max_discharge_w: 0, charges while blocked (charge_capable=false, charge cap 0, no-charge plan), or discharges against a no-discharge plan, the PI is skipped with no correction and the clamps, holdTowardZero and the safety pipeline cut that battery.
  • Every other path (planned energy, manual hold) may only move each battery between 0 W and its last order.

The EV fuse limit already follows this rule (ev_fuse_budget.go:375). Only the sample's age counts. A failed read whose sample is newer than the last dispatch still counts as new evidence, so a meter whose poll emits and then reports an error is not locked out, from a fresh start or later.

Three existing tests called ComputeDispatch several times on one meter sample. They now write a new sample between calls, as a running meter does.

Tests

  • go test ./internal/control/ -run 'Frozen|HeldMeter': fails on master (-10000 W / +11500 W, and new orders on a held sample), passes here.
  • From review: TestHeldMeterStillSendsExplicitZeroDischarge, TestHeldMeterStillStopsBlockedCharge and TestEmitThenErrorMeterIsNotHeld pass on master, failed on the first commit, and pass now.
  • go test -race ./internal/control/...: ok
  • go test ./cmd/ftw/ ./test/e2e/... ./internal/loadpoint/...: ok
  • go vet ./..., go build ./...: ok

The repro tests come from the audit (caa-51) under normal names, plus TestHeldMeterOnlyCutsTowardZero.

Risk

Medium. The battery now acts once per new meter sample rather than once per tick. Drivers write a sample on each poll, so this changes nothing for meters that poll at least as often as the control tick. A slower meter makes the battery react at the meter's pace. A manual hold that reverses direction waits for the next sample, normally one poll. Rig scenario R1 covers the stalled meter.

🤖 Generated with Claude Code

A failed meter poll keeps the old reading, and the dispatch gate passes
it until the 60 s watchdog expires. ComputeDispatch re-applied the same
held error every tick and wound the battery to full power in the wrong
direction. Track the meter sample the last dispatch used. Without a newer
one, the PI path holds its targets and skips integration, and every other
path may only move a battery toward 0 W.

Item A1 of the stable plan.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-27T13:08:39.912267Z 68bcf9c PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 68bcf9ca24

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread go/internal/control/dispatch.go Outdated
Comment on lines +1776 to +1777
if meterHeld {
return fuseSaverEarlyExit(store, state, driverCapacities, fuseMaxW)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Apply safety reductions before returning on a held meter

If the meter stops updating while a battery is running, this unconditional exit also suppresses safety reductions based on fresh battery telemetry or configuration. For example, hot-reloading max_discharge_w: 0 while the battery is discharging now leaves its previous negative setpoint active until another meter sample arrives or the watchdog expires. fuseSaverEarlyExit only handles fuse relief and full-battery charging stops; it does not enforce the updated power limits or direction blocks. Previously, the explicit-zero and blocked-charge predicates below allowed these cases through to the safety pipeline even inside the deadband. Hold the PI integration without bypassing commands that reduce an existing target toward zero.

Useful? React with 👍 / 👎.

The held-meter exit in the PI path returned before the cuts the deadband
exit keeps: a battery discharging under max_discharge_w=0, charging while
blocked, or discharging against a no-discharge plan. While the meter was
held, those cuts were not sent for up to 60 s. Share one condition with
the deadband exit. When it fires on a held tick, skip the PI with no
correction and let the clamps, holdTowardZero and the safety pipeline
cut the battery.

Also drop the startup clause that treated a failed latest read as held
before the first dispatch. A poll that emits and then errors leaves that
flag set on every tick, so a fresh start never dispatched although each
tick brought a new sample.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@frahlg
frahlg merged commit a217d77 into master Oct 27, 2026
23 checks passed
frahlg added a commit that referenced this pull request Oct 28, 2026
A site meter that repeats its values after FTW's devices moved more
than 500 W now counts as a held sample for battery dispatch from the
first repeat, as #1879 does for a sample with an old timestamp. Seed
207537 wound the battery from +5 kW to -5 kW in the 30 s before the
frozen verdict; the PI now holds it after its first followed move.

The freeze check reads the meter and every battery and charger through
health-gated telemetry accessors, so an offline driver's cached
reading never counts. Setup's check before it enables control applies
the same frozen verdict as the tick.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
frahlg added a commit that referenced this pull request Oct 28, 2026
…1921)

* fix(control): a site meter that repeats one reading counts as stale

A meter that answers every poll with the same values passed the dispatch
gate, which checks only UpdatedAt. In seed 207537 it repeated one sample
for 39.9 s; the battery wound from +4 kW to -5 kW on it and an EV rise
went out.

Track the meter's total and phase values against FTW's own batteries and
chargers. A device's move counts as the smaller of its order's move and
its own reading's move, so only orders a device followed count. Once they
move more than 500 W and the meter repeats the same values for 3 more
polls and 15 s, the gate reports site_meter_stale. Any changed value
reopens it. 500 W is far above meter resolution and inverter dead bands;
15 s covers a 10 s HAN or DSMR 4 refresh plus a poll.

Item C5 of the random-rig triage.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(control): frozen-meter check survives a lost telegram and sees every device

Review of #1921:
- Wait 30 s, not 15 s, after the move. A 10 s HAN or DSMR 4 meter that
  loses one telegram repeats its values for 20 s; 15 s tripped it.
- Count every battery and charger in telemetry, with a missing order as
  0 W. A meter frozen since start, or a car plugged in during a freeze,
  was never caught.
- Sum the moves with their sign, as the meter sees them. Two batteries
  trading 1.5 kW no longer count as 3 kW. A move that comes back before
  the meter changes no longer counts.
- One gate function, siteDispatchGateLocked, for the tick and operator
  setpoints, with a test that drives it to site_meter_stale.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(control): hold the battery on a meter repeat after FTW's own move

A site meter that repeats its values after FTW's devices moved more
than 500 W now counts as a held sample for battery dispatch from the
first repeat, as #1879 does for a sample with an old timestamp. Seed
207537 wound the battery from +5 kW to -5 kW in the 30 s before the
frozen verdict; the PI now holds it after its first followed move.

The freeze check reads the meter and every battery and charger through
health-gated telemetry accessors, so an offline driver's cached
reading never counts. Setup's check before it enables control applies
the same frozen verdict as the tick.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* docs(changeset): say what the frozen-meter fix holds and what it waits for

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
@frahlg
frahlg deleted the a1-frozen-meter-holds-battery branch November 11, 2026 09:12
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.

1 participant