Repository navigation
feat(control): read the inverter stream and say when its grid headroom is gone - #2070
Conversation
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>
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>
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. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 997a3e01a6
ℹ️ 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".
| if apart := r.UpdatedAt.Sub(bat.UpdatedAt); apart > InverterPollSlack || apart < -InverterPollSlack { | ||
| return InverterReading{}, false | ||
| } |
There was a problem hiding this comment.
Match inverter and battery readings by poll identity
For drivers polling every second—a supported interval also used by the new inverter-stream test—the two-second window accepts inverter data from previous polls. If a poll emits zero inverter headroom and the next poll updates the battery but omits the inverter after a failed read, LiveBatteryInverter still returns the previous headroom. explainBatteryShortfall then suppresses the non-following warning as an informational inverter limit despite lacking current inverter evidence. Track a shared poll identity or explicitly invalidate the decision-facing inverter reading when its poll does not emit one; timestamp proximity cannot establish the promised same-poll relationship. This also conflicts with the requirement to expose freshness and failures in the normal experience.
AGENTS.md reference: AGENTS.md:L22-L23
Useful? React with 👍 / 👎.
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>
Why
The driver contract (srcfl/device-drivers
docs/WRITING-A-DRIVER.md, "What a battery can do now", andspec/host-api-profile.json,streams_vs_ders) defines aninverteremit stream. A battery or hybrid driver sends its AC stage onhost.emit("inverter", {...}):W,VA,Hz, per-phase values,rated_W, andavailable_import_W/available_export_W, how much more the inverter will take from or give to the grid now. Blixt L1 already reads these.Core's host refused the stream.
emitTelemetrypassed the type totelemetry.ParseDerType, which knows only the six DER types, sohost.emit("inverter", …)returned an error string and the reading was lost. The Solis C&I drivers (50-125k-svk,-slew,-ac-slew) andkonja-261-svkalready emit it on each poll whose AC read answers.#2068 judges a battery against what it can do now, but only from the battery's own
available_*_Wand learned plateaus. An inverter that reports no grid headroom left is the same kind of evidence, from the device itself.What
The stream reaches Core, apart from site power.
host.go: aninverteremit is routed before the DER envelope to a newemitInverter. It follows the poll rules of a DER emit (for a driver whose polls must read fresh, a poll whose reads failed drops it with the rest), but it is not a sign of life: it never callsRecordDriverSuccess, so it cannot keep a driver's held battery reading live in dispatch.DerReading.PollandInverterReading.Pollcarry it;Store.Updateis unchanged and stamps 0.telemetry/inverter.go:InverterReadingholds the latest reading per driver in its own map on the store, not inreadings. SoReadingsByType,ReadingsByDriver, theSumOnline*helpers, the live and history measurement, dispatch, the fuse guard and the energy ledgers never see it, and it writes no samples to history.Removeclears it.ParseInverterReadingreads each field on its own, by Blixt's exact names (W, or the catalog'sw;available_import_W;available_export_W). A field that is not a finite number, a null, or a headroom outside 0 to 100 kW is left out and the rest kept. In Lua, a NaN or infinite value in an inverter emit is dropped before encoding, so one failed register does not lose the emit.Inverter(driver): the last known reading, for the driver detail view, which shows its age andstale.LiveInverter(driver): only while the driver is live and its device reports no fault.LiveBatteryInverter(driver):LiveInverterfor a reading with the same poll number as the driver's latest battery reading. When one of the two reads fails, the other is left from an earlier poll however close in time, and the pair is refused. Readings from outside a poll never pair./api/statusdrivers map:inverter_w,inverter_available_import_w,inverter_available_export_wandinverter_updated_at_ms, throughLiveInverter, so a stale or faulted driver's cached values never show as live./api/drivers/{name}: aninverterobject with the same values, the emit as sent,updated_at_msandstale.lua.go: the host API comment lists the stream.The verdict uses the headroom. In
explainBatteryShortfall, after the battery's own reported limit and before learned limits: apower_below_targetbattery (it gives part of its order, in the right direction) whose same-poll inverter reportsavailable_import_W(charging) oravailable_export_W(discharging) at no more than 100 W (Core's smallest control tolerance) becomeslimited/battery_limit/info, withlimit_source: "device",limit_kind: "inverter_import"or"inverter_export", andlimit_w= the headroom left.no_power_responsekeeps its warning. A dead battery on a site that may not export also sees no export headroom. Only the battery's own report of 0 available (feat(control): judge a battery against what it can do now #2068's path) explains a zero response.power_below_targetis relabelled, so wrong direction, power above the order, power while idle,setpoint_changed,telemetry_stale,readings_lostanddevice_faultkeep their own reasons.device_standbyand the battery's ownbattery_reportedlimit come first.Words (
web/control-feedback.js, next to #2068's): title "Limited by the inverter"; text "The inverter reports that it can take no more from the grid now. FTW asked for 5.0 kW charge." or "…send only 60 W more to the grid now…". The reason staysbattery_limit, sosetControlStatus, the journal and notices already treat it as limited.Two fixes to #2068's words, found by the web app agent and fixed there in srcfl/ftw-webapp#144; the box now uses the same words:
battery_limitrow with nolimit_wand no target read "below the the new target FTW asked for". It now reads "The battery reports a limit below what FTW asked for."controlNumberslabelled a battery'slimit_a(its learned current limit) "Charger lets the car take". For a battery row it now reads "Battery current limit"; chargers keep their labels.These have their own
patchchangeset.After review (third commit): pairing by poll number instead of a 2 s window;
no_power_responseno longer explained by headroom;/api/statusgated on a live driver, with the reading's time; one bad field no longer fails the emit.Tests and results
telemetry/inverter_test.go: parsing, with each bad field left out and the rest kept, and exact key names only; storing a 9 kW inverter reading leaves the live and history measurement, every per-type read, the site sum and the history samples unchanged; pairing at 1 s intervals refuses an inverter reading from the poll before and a battery reading from the poll before, pairs an every-other-poll driver on its inverter polls only, and never pairs readings from outside a poll;LiveInverterandLiveBatteryInverterrefuse a faulted or offline driver;Removeclears it.drivers/inverter_stream_test.go: a Lua driver'shost.emit("inverter", …)returns no error and is stored; it creates no DER reading and no history sample; it is no sign of life; a failed held poll drops it and a good one pairs; a back-to-back every-other-poll Lua driver pairs only on its inverter polls; a NaN or infinite value is dropped and the rest kept.control/inverter_stream_test.go: two identical sites, one with a −20 kW, zero-headroom inverter stream on both batteries, get identical dispatch targets over five ticks in self-consumption, charge, idle and peak-shaving, with fuse clamping in play.api/api_control_inverter_test.go: a table of verdicts (no headroom explains a partial delivery both ways; a zero response staysno_power_responsewith no headroom either way, and readsbattery_reportedonly when the battery reports 0 available; headroom above 100 W, the wrong direction's headroom, missing or out-of-range headroom, and no stream do not explain; standby comes first); a 1 Hz stream whose inverter read stops is not following from the very next poll; the never-explain table with zero headroom both ways (setpoint changed, above target, while idle, wrong direction, no response both ways, device fault, readings lost, driver says offline, driver offline);/api/statustotals are unchanged, the new fields show with the reading's time, and vanish for an offline or faulted driver.no_power_responseagain fails four cases; going back to the 2 s window fails the 1 Hz API test, the telemetry pairing test and the Lua every-other-poll test; reading the ungated reading in/api/statusfails the status test; turning off the verdict rule fails the "limited" cases.web/control-feedback-battery-limits.test.mjs: the new words, the inverter title, no codes,undefinedor doubled "the", a check that everyinverter_*kind inapi_control_capability.gohas words, and two tests for the feat(control): judge a battery against what it can do now #2068 fixes that fail on the old code.make verify(GOFLAGS=-p=4, bundled drivers synced withscripts/sync-bundled-drivers.sh): clean on 847b276. An earlier run, before the review fixes, failed once ingo/cmd/ftw(TestChargeNowSurvivesOCPPSocketBlip:bind: address already in use, a port taken on the shared laptop); it passed 10 times alone.npm test: 966 pass, 0 fail..github/scripts/check_telemetry_reads.py: 133 reads outsidego/internal/telemetry, none added.Not verified
web/control-feedback.js(its header says byte for byte). ftw-webapp and ftw-app owe the inverter words; ftw-webapp#144 already has the two fixes.Changeset:
minor, for the owner to approve.Refs #2068
🤖 Generated with Claude Code