Skip to content

feat(gridstate): the site's grid state from the inverters - #2132

Merged
frahlg merged 1 commit into
masterfrom
feat/grid-state
Nov 29, 2026
Merged

frahlg merged 1 commit into
masterfrom
feat/grid-state

Conversation

@frahlg

@frahlg frahlg commented Nov 29, 2026

Copy link
Copy Markdown
Member

Step 6 of the off-grid work in #2125. It reads grid_state from the drivers in srcfl/device-drivers#285. Based on master: it touches none of the planner stack.

Changeset: minor, for the owner to approve.

What it does

  • Parsing. telemetry.InverterReading.GridState reads grid_state from the inverter stream. Only "on_grid" and "off_grid" count; anything else is empty.
  • The site's state. Package gridstate folds the fresh reports into the site's state on every control tick:
    • Off grid once a report has said so for 30 s. FoxESS opens its grid relay for 9–20 s on its own fault checks while the grid is there, and one spurious reading has been seen (foxess_modbus#1314). Neither is an outage worth a notification. One islanded inverter is enough, because the house it feeds is off grid.
    • On grid once every report has said so for 60 s, since a returning grid can drop again in its first minute. From unknown, on grid applies at once.
    • Unknown with no report under 30 s old. That keeps every behaviour exactly as before grid state existed, so a site without such a driver is unchanged.
    • A state is dated from the first report that pointed to it.
  • A device fault does not hide the state. An islanding FoxESS sets 39067 to 0x0980, which its driver raises as a device fault, and LiveInverter hides a faulted device's reading. telemetry.Store.GridInverter reads the inverter of a driver whose telemetry is live, a fault included, and only for the grid state.
  • Visibility.
    • /api/status shows grid_state: {state, since_ms, reports}.
    • The control journal keeps a site:grid span, with an outage as a warning so its span starts at once.
    • lua.go's host API comment documents the field.

Not in this PR

  • Off-grid behaviour (step 7): hand the battery to the inverter's default mode, mark the plan stale, stop chargers, and release the reserve.
  • Push notifications "off grid" and "grid back". contract/push-catalogue.yaml must be copied byte for byte to ftw-webapp and ftw-app in paired PRs.
  • The bundled drivers' pin. Until it includes device-drivers#285, FTW's own FoxESS and Konja drivers report no grid_state, so a site stays unknown.

Evidence

  • go/internal/gridstate/gridstate_test.go:
    • unknown until an inverter reports;
    • a 5 s report and a 20 s trip do not count;
    • off grid at 30 s, dated from the first report;
    • back on grid only after a stable minute, where a grid that drops again keeps the site off grid;
    • one islanded inverter is enough;
    • no fresh report is unknown;
    • reports come from live drivers' inverter streams, a faulted FoxESS included, and stale, offline and invalid values are ignored.
  • go/internal/api/api_grid_state_test.go: the journal's site:grid verdict is unknown at info and off grid at warning; a nil tracker is unknown.
  • go test ./internal/gridstate/ ./internal/telemetry/ ./internal/api/ passes, and ./cmd/ftw/ passes with umask 022.

🤖 Generated with Claude Code

A hybrid inverter's driver reports grid_state on its inverter stream,
read from an explicit vendor status in the same poll (device-drivers
#285); Core had nowhere to read it. The inverter reading now parses it,
"on_grid" or "off_grid" and nothing else, and package gridstate folds the
fresh reports into the site's state every control tick: off grid once a
report has said so for 30 s, since FoxESS opens its relay for 9-20 s on
its own checks with the grid there; back on grid once every report has
said so for a minute; unknown with no report under 30 s old, which keeps
every behaviour as before. A state is dated from the first report.

An islanding FoxESS raises a device fault (39067 = 0x0980), which hides
its reading from LiveInverter. telemetry.Store.GridInverter reads the
inverter of a driver whose telemetry is live, a fault included, for the
grid state only.

/api/status shows grid_state with its start and the reports it rests on,
and the control journal keeps a site:grid span, an outage as a warning.
Nothing acts on it yet; the off-grid behaviour and the push
notifications, paired with the app, follow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 29, 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-29T15:12:33.111833Z cf298cd 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
frahlg merged commit d162291 into master Nov 29, 2026
23 checks passed
frahlg added a commit that referenced this pull request Nov 29, 2026
…e-drivers#285) (#2138)

Moves the pin to srcfl/device-drivers eeee2c48. Of the bundled drivers
only foxess_h3_smart changes: 0.11.0 reports grid_state on its inverter
stream from Status 3 bit 0 (39066), which #2132 reads into the site's
grid state. konja-261-svk 0.5.0 does the same but is not bundled.

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