Skip to content

feat(control): judge a battery against what it can do now - #2068

Merged
frahlg merged 4 commits into
masterfrom
battery-can-do
Nov 22, 2026
Merged

frahlg merged 4 commits into
masterfrom
battery-can-do

Conversation

@frahlg

@frahlg frahlg commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Problem

"Are we in control?" judges a battery only against FTW's order. On the owner's home box (Sungrow SH-RT + SBR096 9.6 kWh, 3 × 20 A, 30 days of minute data and the control journal 8–22 Nov 2026):

  • 23 control.not_following notices in 14 days. 20 came from night grid charging: with a ≥ 4.5 kW charge order the battery met it 10 % of minutes; while short, grid import sat flat at 4,276–4,344 W (median 4,316) and the battery took that less the house load. With PV and grid under 3.8 kW it reached 5,004 W. So the inverter holds grid import at about 4.3 kW.
  • 3 came from discharge: against ≥ 4.5 kW the battery gave 4,049–4,383 W at a flat 21.6–23.1 A DC; power moved with pack voltage, current did not.
  • 14 times the inverter's running state went to standby for 2–9 min while an order stood (battery 0 W).
  • Journal totals for battery:sungrow: power_below_target 192 spans, 24.9 h.

Owner direction: learn what is normal for each device and tell "in control" from "limited". A 3 kW delivery against a 5 kW order is not loss of control if that is what the device can do. The learning lives in its own small code path and changes the verdict and nothing else.

What changed

All new logic is in two new files with small call sites.

go/internal/battery/plateau.go — a pure learner type (Plateaus), stored in the existing per-battery model JSON (battery_models, keyed by hardware device_id). No existing field removed. Nothing in control, dispatch, the fuse guard or clamps reads it.

  • Samples: one per 5 s journal sample, only while Core already says power_below_target (settled, accepted, fresh, right direction) and |actual| ≥ 300 W. Per sample: grid W from the site meter (import when charging, export when discharging) only when separateMeterSource holds, the meter is live, fault free, at most 15 s old and dispatch is not blocked; battery DC current (a/A) when reported; battery power; battery temperature when reported.
  • Runs: consecutive samples; a break up to 30 s does not end one; a direction or temperature band change does; a run is cut at 2 h. A run needs 12 samples (1 min).
  • Flat: an amount's IQR ≤ max(3 % of its median, 100 W or 0.5 A). The home box's grid import held within 68 W of 4.3 kW (< 2 %); its DC current ripple is a few tenths of an amp. Battery power under a grid cap moves with the house, so it is not flat, as wanted.
  • Kept: per battery × direction × amount × 10 °C band, the last 50 runs of the last 60 days.
  • Trusted: ≥ 3 runs whose medians agree within max(5 %, 100 W / 0.5 A), on ≥ 2 days (runs that start ≥ 12 h apart, so one night across midnight counts once). Of the groups that agree, the newest trusted one is the plateau; the one before is kept as earlier.
  • Sits on a plateau: the sample's amount within max(5 %, 100 W / 0.5 A) of it. One sample's DC current read 21.6–23.1 A on a 22.4 A limit (< 4 %). A battery well below (1 kW against 4.3 kW) is not explained. A new lower level is normal only after it repeats on two days.
  • A failing battery is never normal. A battery power or current plateau is learned from, and explains, only a battery that gives at least 50 % of its order. A limit inside the battery still gives most of an order: the home box's discharge cap gave 4.3 of 5 kW (86 %). A battery that gives a steady 0.4–1 kW of 5 kW (8–20 %) is failing. Grid plateaus have no such floor, because under the import cap the battery's share falls to 30 % when the house takes 3 kW. The cost: a battery power or current limit below 50 % of the order is never learned, so a cold BMS that gives, say, 46 % stays a warning unless the driver reports its limit (TestLowShareLimitNeedsTheDriversReport: 2.28 of 5 kW stays not_following for three nights, then reads battery_limit once available_discharge_W appears).
  • A grid run must show a live cap. The battery must move while the grid holds (a steady house with a steady battery says nothing about the inverter, and a failing battery looks the same). The meter's own measurement time must move on at least once per 20 s on average over the run, so a meter that measures every 10, 15 or 20 s against the 5 s journal still counts (tested), and one every 60 s does not. 20 s is about the slowest meter whose readings also stay inside the 15 s the verdict allows a grid reading. No single grid value may make up half the run: a driver that resends a cached value repeats it exactly, while the home box's 4.32 kW cap had a 41 W spread.
  • Self-tune runs are not learned from.

go/internal/api/api_control_capability.go — runs after every other verdict step and only relabels power_below_target or no_power_response from fresh readings of an accepted order:

  1. device_state standby/starting → not_following / device_standby / warning.
  2. Fresh available_charge_W / available_discharge_W (0–100 kW) below the order, with actual within the control tolerance (10 %, ≥ 100 W) of it → limited / battery_limit / info. A reported 0 explains a 0 W response (the driver guide says 0 means "not allowed now", e.g. charging below 0 °C).
  3. A trusted plateau the shortfall sits on → limited / learned_limit / info. Wrong direction, no response, power above target or while idle, setpoint changed, stale readings, faults, Core stops and readings_lost are never learned or explained by a learned limit.

Only power_below_target and no_power_response pass this step, so battery_calibrating from #2017 (open), which is set earlier in the same loop, can neither feed the learner nor be relabelled. TestCalibratingBatteryIsNeverRelabelled guards that reason name now; when #2017 lands, a test through SetDriverCalibrating should follow. Please keep the allowlist on rebase.

The journal feeds the learner from JournalVerdicts (every 5 s), never from HTTP reads; TestBatteryModelsShowPlateaus checks that status reads leave it untouched.

Feedback fields (omitted when absent, never zero): limit_w, limit_room_w (battery's room under a grid limit), limit_source (device|learned), limit_kind (grid_import|grid_export|battery_current|battery_power|battery_reported), limit_seen (runs), limit_since_ms, limit_earlier_w. A current limit also sets limit_a, and limit_w is that current at the pack voltage now (null without a voltage).

GET /api/battery_models gains plateaus: {known, series}. POST /api/battery_models/reset replaces the whole model, so it also forgets the learned limits.

Words (web/control-feedback.js, vendored by the web app): "At its usual limit" — e.g. "The inverter takes at most 4.3 kW from the grid, which leaves 3.6 kW for the battery. FTW has seen this 14 times since 8 Nov.", "The battery gives at most 22 A (4.2 kW now).", "… Earlier it was 4.3 kW."; "Limited by the battery" — "The battery reports that it can give at most 3.2 kW now. FTW asked for 5.0 kW discharge."; "On standby" — "Asked for 5.0 kW discharge, but the battery reports that it is on standby or starting up." The owner reviews this UI in a browser; I have not rendered it.

Notifications: control.not_following fires on status not_following only (notifications/control.go, unchanged). The new limited verdicts are limited; the night scenario checks ControlStates says limited on every explained sample.

Changeset minor (visible capability; the owner approved this work).

Tests

New, all on a fake clock:

  • battery/plateau_test.go: run length and breaks, direction change, only flat amounts make runs, trust (3 runs one night → no; plus a second day → yes), lower plateau after two days with earlier, temperature bands, bounds (50 runs, 60 days, 2 h cut), JSON round trip and an old model without plateaus.
  • battery/plateau_test.go also: a failing battery at 0.4, 0.7 and 1.0 kW over four nights, with a steady and a moving house, never becomes a plateau; a 60 % battery plateau does not explain 40 % of a larger order; a frozen meter (cached value resent, or the meter's own time standing still) and a steady battery under a steady grid make no grid run, while a live meter under a cap does, including one that measures every 10, 15 or 20 s (and a 10 s or 15 s meter's cap is trusted after three nights).
  • api/api_control_capability_test.go, replaying the home-box patterns: grid import flat at 4.3 kW with house 0.3–3 kW over three 5 h nights → night 1 ≥ 95 % not_following/power_below_target, night 3 ≥ 95 % limited/learned_limit with grid_import, limit_w ≈ 4,316, limit_seen ≥ 4, since night 1; then wrong direction stays power_wrong_direction and adds no runs; then 1 kW warns. Discharge at 22.4 A with 192→187 V → battery_current, run twice: with no reported limits, and with the BMS reporting its 30 A rating (available_*_W 5,973 W, 20 °C) as sungrow 1.7.0 did in a draft run on the home box. The rating is far above the 22.5 A / 4.3 kW the battery gives, so it explains nothing (no battery_limit on any day) and the learned current limit still does. Reported limits (12 cases incl. 0 W, out of range, above the order, a 5,973 W rating against 4.3 kW delivered, standby, starting). A plateau learned at −5…−1 °C explains −3 °C but not +15 °C. Three nights of a failing battery (steady house, moving house) and three nights behind a frozen meter all stay not_following and learn nothing. TestLimitsNeverExplainOtherVerdicts: with trusted plateaus and a matching reported limit in place, setpoint_changed, power_above_target, power_while_idle, power_wrong_direction, device_fault, readings_lost and a driver that says offline keep their own reasons and learn nothing; nothing is learned while self-tune runs. Grid only from a trusted meter (7 cases). GET /api/battery_models shows the plateau.
  • web/control-feedback-battery-limits.test.mjs: the words above, and no codes or invented numbers when fields are missing. The existing "every reason has words" test covers the three new reasons.

Run locally:

  • go test ./internal/api/ ./internal/battery/ -count=1: ok
  • make verify (GOFLAGS=-p=4): verify: vet + test + build clean
  • npm test: 962 pass, 0 fail
  • After the review fixes, on the current head: go test ./internal/api/ ./internal/battery/ ./internal/notifications/ -count=1: ok; make verify: clean; npm test: 962 pass, 0 fail

Not verified on hardware

Not run on the home box. To check on a beta: after two nights of grid charging, GET /api/battery_models → sungrow.plateaus.known should hold a grid_import plateau near 4.3 kW, and the night's journal spans should read limited/learned_limit instead of not_following/power_below_target.

Open questions

  • available_import_W / available_export_W on the inverter stream are not read. Core's host rejects that stream today (telemetry.ParseDerType has no inverter), so routing it is a host API change for a separate PR. The learned grid plateau covers this site meanwhile.
  • On the home box the reported BMS limit is the 30 A rating, not what holds the battery (grid import cap 4.32 kW, discharge about 22.5 A); the inverter's 8.0 kW AC rating is not the limit either. So on this site only the learned limits explain the shortfalls, and temperature (20 °C) explains nothing.
  • No bundled driver emits available_*_W or device_state yet (docs: what a battery can do now (temperature, available power, device state) device-drivers#250 documents them). The Sungrow running state the driver reads could become device_state there.
  • The apps owe words for battery_limit, learned_limit and device_standby; the web app copies web/control-feedback.js.

Base and overlap

Based on master, not on #2067; it does not need that change. Kept clear of the lines #2017, #2065 and #2050 touch: the new reasons sit at the top of setControlStatus's switch, the struct fields at its end, the words beside the battery taper words, and the web tests in a new file.

Refs srcfl/device-drivers#250

🤖 Generated with Claude Code

@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:40:37.543673Z 32d0aa9 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 4 commits November 22, 2026 11:59
A battery that gives 4.3 kW of a 5 kW order because that is all it can
do now is limited, not out of control. On the owner's home box the
inverter holds grid import at about 4.3 kW at night and the battery
gives a flat 22 A on discharge, which sent 23 not-following notices in
two weeks.

Learn those limits per battery from the control journal's 5 s samples:
while Core says power_below_target, keep each run where grid import,
grid export, DC current or battery power stays flat, and trust a level
seen three times on two days, per 10 C band when the temperature is
known. A shortfall that sits on a trusted level reads limited /
learned_limit. A fresh available_*_W the battery delivers reads
limited / battery_limit; device_state standby or starting keeps the
warning as device_standby. The plateaus live in the battery model JSON
and show in GET /api/battery_models. Only the verdict reads them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Fredrik Ahlgren <fredrik@sourceful-labs.com>
A battery that breaks and gives a steady 0.4-1 kW against a 5 kW order
became a trusted battery_power plateau, and a meter driver resending a
cached grid value looked like an import cap.

A battery power or current plateau now stands for, and explains, only
a battery that gives at least half of its order; the home box's
discharge cap gave 86 %. A grid run counts only while the battery
moves, the meter's own measurement time moves on, and no single value
makes up half the run. Add tests for these, for every verdict a limit
must never explain, for self-tune, and for a calibrating battery.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Fredrik Ahlgren <fredrik@sourceful-labs.com>
A grid run now asks the meter to measure at least once per 20 s on
average over the run, not in a third of the samples, so a meter every
10, 15 or 20 s still shows a live cap and one every 60 s does not. Add
tests for slow meters and for a 46 % shortfall, which stays not
following until the driver reports its limit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Fredrik Ahlgren <fredrik@sourceful-labs.com>
siteGridW read the cached meter reading directly. A cached reading survives
its driver going stale or faulted, so take the meter power from a new
telemetry.Store.LiveMeterPower that checks the driver's health itself.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Fredrik Ahlgren <fredrik@sourceful-labs.com>
@frahlg
frahlg merged commit 1584f59 into master Nov 22, 2026
21 checks passed
frahlg added a commit that referenced this pull request Nov 22, 2026
…m is gone (#2070)

* feat(control): read the inverter stream and its grid headroom

The driver contract defines an `inverter` emit stream: a battery or
hybrid driver's AC stage, with available_import_W / available_export_W,
how much more the inverter takes from or gives to the grid now. Core's
host passed the type to ParseDerType and refused it, so the Solis C&I
drivers lost it on every poll.

Keep the latest inverter reading per driver in its own map on the
telemetry store, apart from the DER readings, so no site sum, dispatch,
fuse guard, ledger or history reads it, and write no history samples
from it. The emit follows the poll rules of a DER emit but is no sign of
life. Show it in /api/status and /api/drivers/{name}.

LiveBatteryInverter returns the reading only while the driver is live
and unfaulted and the reading came within 2 s of its battery reading.
A battery shortfall in the right direction, or a zero response, while
that reading reports at most 100 W of headroom in the order's direction
reads limited / battery_limit with limit_kind inverter_import or
inverter_export, after the battery's own report and before learned
limits. The box page says "Limited by the inverter".

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

* fix(web): two battery limit phrases from #2068

A battery_limit row without limit_w or a target read "below the the
new target". It now reads "below what FTW asked for". The Numbers list
labelled a battery's learned current limit "Charger lets the car take";
a battery row now says "Battery current limit". Same words as
srcfl/ftw-webapp#144.

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

* fix(control): pair inverter headroom by poll, never explain no response

Review of the inverter stream found four gaps.

The pairing was a 2 s window, not a poll. On a 1 Hz driver a failed
inverter read left a reading a second old that still paired with the
next battery reading, and the reverse. The host now numbers every driver
poll, process-wide, and stamps each reading of the poll with it, held
readings included. LiveBatteryInverter needs the same number on both.

Headroom explained a zero response. A dead battery ordered to discharge
on a site that may not export also sees no export headroom, and read
limited/info. The inverter now explains only power_below_target; a zero
response keeps its warning unless the battery itself reports 0
available.

/api/status showed a stale driver's cached inverter values as live. It
now reads them through LiveInverter, live and unfaulted only, and adds
inverter_updated_at_ms.

One bad field failed the whole emit. Each field is now read on its own
by Blixt's exact names; a non-number, null or out-of-range value is left
out and the rest kept, and Lua drops NaN and Inf before encoding.

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

---------

Signed-off-by: Fredrik Ahlgren <fredrik@sourceful-labs.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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