Skip to content

fix(control): stop one late poll from reading as lost readings - #2067

Merged
frahlg merged 3 commits into
masterfrom
readings-lost-debounce
Nov 22, 2026
Merged

frahlg merged 3 commits into
masterfrom
readings-lost-debounce

Conversation

@frahlg

@frahlg frahlg commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Problem

On the owner's home box (Sungrow SH-RT hybrid, battery polled once a second), the control journal for 8–22 Nov 2026 holds 341 readings_lost alarm spans for battery:sungrow, about 23 a day, and 171 for ev:easee. Each is one 5 s journal sample; none lasted over 40 s. They are noise, not lost control, but they show as "Lost control" (no_contact, alarm).

Root cause

Two ways one sample turned into readings_lost at once. Both fail on master in the new TestOnePollGapIsNotReadingsLost:

  1. A poll stored after the sample's clock. controlFeedback(now) treats a reading stamped after now as not fresh (!rd.UpdatedAt.After(now)). The journal passes its ticker's time, and GET /api/status takes its clock before it waits for CtrlMu. A driver can store a poll in between, so that sample sees a "reading from the future" and calls it telemetry_stale. The battery polls once a second and the Easee stream about every 2 s, and the battery had about twice as many spans, which fits.
  2. A driver that says it is offline for one poll (is_online:false). Among bundled drivers only easee_cloud sends it (Easee op_mode 0), so this can add to the charger's spans, not the battery's.

In both cases controlVerificationLost turned telemetry_stale into readings_lost at once, with none of the grace every other lost-proof path has. The other one-poll gaps (a failed Modbus read, control_power_available=false once, power older than its max age once) do not give readings_lost on master; they stay in the test as guards.

Change

  • rowClock: each device is judged at the latest of the caller's clock and the store's own stamps on what it read (UpdatedAt, the command's last observation). The store stamps with the box's clock, so this never runs ahead of the present.
  • controlVerificationLost: telemetry_stale no longer jumps the queue. It uses the rule every other lost-proof path already uses: readings are lost once both the last measured power and the current order step are older than the grace, 30 s for a battery and 2 min for a charger (cloud chargers re-read about once a minute). These are the graces already in this function. Measured proof needs 10 s of distinct fresh readings, and a reading without power or a new ≥ 500 W step restarts that window. Counting from the order step too means a target that steps every few seconds cannot make one late poll an alarm.
  • A reading over a minute old, or a driver offline by the 60 s watchdog, has been gone longer than the grace, so it is still lost at once, unless the order has just stepped.
  • Delayed by design: while Core keeps stepping a dead driver's order by 500 W or more, each step restarts the 30 s wait, so readings_lost waits until 30 s after the last step. The verdict says telemetry_stale (no_contact, warning) the whole time, so the owner still sees no contact. In the field the 60 s watchdog hands such a battery back to its own control, and the steps stop.

Safety is separate and unchanged: dispatch, the stale site-meter stop and the watchdog's autonomous fallback do not read this verdict. Changeset patch.

Tests

New go/internal/api/api_control_readings_lost_test.go (synctest, fake clock):

  • TestOnePollGapIsNotReadingsLost: five one-poll gaps on a following battery; no sample alarms and the verdict comes back to following. The "stored after the sample's clock" and "driver says offline once" cases fail on master.
  • TestRepeatedOnePollGapsAreNotReadingsLost: the gaps above, repeated every 25 s and every 8 s for 5 min, and every 12 s under 600 W steps every 4 s; no sample alarms. The stepping case failed on this PR's first head (36 s: no_contact/readings_lost/alarm).
  • TestSustainedGapIsReadingsLost: a driver that stores nothing, and one that says offline on every poll while its polls still count as good, are both lost from 30 s.
  • TestDeadDriverUnderSteppingOrders: a dead driver under 600 W steps every 10 s shows no_contact/telemetry_stale/warning throughout (after its last reading is a minute old) and never alarms; 30 s after the steps stop it is readings_lost.
  • TestCloudChargerOfflineIsLostAfterItsGrace: Easee op_mode 0 (is_online:false, unconfirmed power) is a warning for 2 min, then readings_lost.
  • TestControlVerificationLossAlarm gains rows for the new rule. Its old "device offline 20 s → alarm" row now reads "one stale sample → no alarm", and "stale just after a new step waits for its grace" covers the order step.

Run locally:

  • go test ./internal/api/ -run 'ReadingsLost|PollGap|SustainedGap|CloudChargerOffline|VerificationLoss|FailedCall' -count=1: ok
  • make verify (GOFLAGS=-p=4): verify: vet + test + build clean
  • npm test: 956 pass, 0 fail

Not verified on hardware

Not run on the home box yet. The way to check it on a beta: GET /api/control/journal?subject=battery:&severity=alarm should stop showing 5 s readings_lost spans, while a pulled network cable still gives readings_lost after about 30 s.

Overlap

Touches the row loop and controlVerificationLost in api_control_feedback.go, near the hunks of #2017, #2065 and #2050 but not on their lines. controlVerificationLost keeps its signature.

🤖 Generated with Claude Code

The control journal samples at its ticker's time, and GET /api/status
takes its clock before it waits for the controller's lock. A battery
poll the store took in between was newer than that clock, so the
verdict called the reading stale, and a stale reading became
readings_lost (no_contact, alarm) at once. On a Sungrow hybrid polled
once a second this gave about 23 one-sample alarms a day.

Judge each device at the latest of the caller's clock and the store's
own stamps on what it read. Count readings as lost once no measured
power has come for 30 s, or 2 min for a charger, the same grace a new
order gets, bounded by the driver's last good poll so cached values
from a driver the watchdog gave up on do not hold off the alarm.
Dispatch and the stale site meter stop are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Fredrik Ahlgren <fredrik@sourceful-labs.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-22T09:19:36.654454Z 4067ef7 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.

frahlg and others added 2 commits November 22, 2026 10:56
Drop the last-good-poll cap: every emit records a good poll, so it
never differed from the last measured power in production. Stale
readings now use the rule every other lost-proof path uses: lost once
both the last measured power and the current order step are older than
the grace. Measured proof restarts its 10 s window after a reading
without power or a new step of 500 W or more, so it can lag healthy
readings; counting from the step keeps one late poll under a stepping
target from alarming. Add repeated one-poll gap tests and rename the
new-step test row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Fredrik Ahlgren <fredrik@sourceful-labs.com>
While Core keeps stepping a dead driver's order by 600 W every 10 s,
each step restarts the 30 s wait, so readings_lost comes 30 s after
the last step. The verdict says no contact (telemetry_stale, warning)
the whole time. In the field the watchdog hands the battery back to
its own control after 60 s, so the steps stop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Fredrik Ahlgren <fredrik@sourceful-labs.com>
@frahlg
frahlg merged commit 989dd07 into master Nov 22, 2026
21 checks passed
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