Repository navigation
feat(control): judge a battery against what it can do now - #2068
Merged
Merged
Conversation
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
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
force-pushed
the
battery-can-do
branch
from
November 22, 2026 11:01
006fe75 to
9125def
Compare
This was referenced Nov 22, 2026
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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):
control.not_followingnotices 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.battery:sungrow:power_below_target192 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.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 whenseparateMeterSourceholds, 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.earlier.TestLowShareLimitNeedsTheDriversReport: 2.28 of 5 kW staysnot_followingfor three nights, then readsbattery_limitonceavailable_discharge_Wappears).go/internal/api/api_control_capability.go— runs after every other verdict step and only relabelspower_below_targetorno_power_responsefrom fresh readings of an accepted order:device_statestandby/starting→not_following/device_standby/ warning.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).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_targetandno_power_responsepass this step, sobattery_calibratingfrom #2017 (open), which is set earlier in the same loop, can neither feed the learner nor be relabelled.TestCalibratingBatteryIsNeverRelabelledguards that reason name now; when #2017 lands, a test throughSetDriverCalibratingshould follow. Please keep the allowlist on rebase.The journal feeds the learner from
JournalVerdicts(every 5 s), never from HTTP reads;TestBatteryModelsShowPlateauschecks 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 setslimit_a, andlimit_wis that current at the pack voltage now (null without a voltage).GET /api/battery_modelsgainsplateaus: {known, series}.POST /api/battery_models/resetreplaces 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_followingfires on statusnot_followingonly (notifications/control.go, unchanged). The new limited verdicts arelimited; the night scenario checksControlStatessayslimitedon 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 withearlier, temperature bands, bounds (50 runs, 60 days, 2 h cut), JSON round trip and an old model without plateaus.battery/plateau_test.goalso: 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_limitwithgrid_import,limit_w≈ 4,316,limit_seen≥ 4, since night 1; then wrong direction stayspower_wrong_directionand 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_*_W5,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 (nobattery_limiton 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 staynot_followingand 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_lostand 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_modelsshows 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: okmake verify(GOFLAGS=-p=4):verify: vet + test + build cleannpm test: 962 pass, 0 failgo test ./internal/api/ ./internal/battery/ ./internal/notifications/ -count=1: ok;make verify: clean;npm test: 962 pass, 0 failNot 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.knownshould hold agrid_importplateau near 4.3 kW, and the night's journal spans should readlimited/learned_limitinstead ofnot_following/power_below_target.Open questions
available_import_W/available_export_Won theinverterstream are not read. Core's host rejects that stream today (telemetry.ParseDerTypehas noinverter), so routing it is a host API change for a separate PR. The learned grid plateau covers this site meanwhile.available_*_Wordevice_stateyet (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 becomedevice_statethere.battery_limit,learned_limitanddevice_standby; the web app copiesweb/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 ofsetControlStatus'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