Skip to content

fix(control): a battery that may not export holds the grid above 0 W - #1969

Merged
frahlg merged 2 commits into
masterfrom
fix/no-export-discharge-band
Nov 5, 2026
Merged

frahlg merged 2 commits into
masterfrom
fix/no-export-discharge-band

Conversation

@frahlg

@frahlg frahlg commented Nov 5, 2026

Copy link
Copy Markdown
Member

Problem

A field report on 0.158.0, planner_passive_arbitrage with battery export not allowed, a 20 kWh battery as site meter and no PV: while the battery covered about 2.4 kW of house load, the meter showed 100–270 W of export in 315 of 721 minutes, worst −1 387 W, mostly 16–19 h. The battery followed its order (−2 571 W against −2 575 W requested); the order itself was too large.

In planner modes the reactive path aims at a grid target of 0 W: passive arbitrage cover-load and idle slots call SetGridTarget(0), planner_self chases 0 W, and stale plans fall back to 0 W. site.grid_target_w does not apply there. The dead band (grid_tolerance_w, 42 W by default) is symmetric, and the meter clamp lands each correction on the target. So every drop in house load sold battery energy until the next correction, about as often as the site imported. With a larger tolerance a steady export inside the band was never corrected. The tolerance_w in control_feedback (about 10 % of the request) is only the display's "on target" band; it steers nothing.

Export from the battery is sold at spot and bought back later at the full price. Import while the battery discharges stays in the battery for later.

Change

Mode.batteryMayNotExport names planner_self, planner_cheap and planner_passive_arbitrage. In the reactive PI path, when the plain target would have such a battery discharge more than the dead band (currentTotal − errW < −grid_tolerance_w), the target is at least +grid_tolerance_w. The dead band then ends at 0 W.

  • planner_arbitrage, which may export, and the manual modes are unchanged.
  • A discharge inside the dead band keeps the plain target. Keying on the reading instead sent an order every tick for a −15 W standby reading at the SoC floor.
  • The condition looks at the planned discharge, not the reading, so an idle battery under a legacy plan with a negative grid target (meant for PV export) no longer discharges toward it on the first tick.
  • The bias never makes a discharge larger. When PV surplus appears, the plain target asks for charge and the bias is off.

The cost: while it discharges, the site settles about grid_tolerance_w above 0 W, and from idle the battery leaves import below 2 × grid_tolerance_w uncovered. At the default 42 W that is a small steady import; a site with a large tolerance imports more, and a smaller tolerance narrows it. A discharge smaller than the tolerance can still export up to its own size.

Evidence

  • New tests in no_export_band_test.go:
    • the report's snapshot (grid −164 W) and an export inside the old band (−20 W) settle at 0–84 W import, in passive arbitrage and planner_self; a 30 W import is left alone; planner_arbitrage keeps its band;
    • a legacy plan of −1 365 W with an idle battery and 800 W of load never exports, from the first tick, in planner_cheap and passive arbitrage;
    • PV surplus that appears mid-discharge is charged at once and never imports more than the tolerance;
    • a closed loop of 3 000 ticks (5 s) with a noisy load and steps: export below −50 W in 26 % → 12 % of ticks, exported 141 → 73 Wh, imported 140 → 248 Wh.
  • The band, plan and closed-loop tests fail on master (the plan test also on the first commit of this branch: 499 W export on tick 0). The surplus test is a guard that master also meets.
  • TestPlannerSelfIdleGateLeavesExistingDischargeAloneInsideDeadband now seeds a 20 W import, inside the new band; its point, that the idle gate leaves a discharge alone, is unchanged.
  • Golden corpus: 26 of 551 records change, in incident_scenarios, targeted_invariants, seeded_planner and slew_limiter. I compared each against master: in every one the plain target asked for discharge, and the order moves 15–36 W toward less discharge. seeded_planner/045_planner_cheap moves 855 W: a legacy plan of −1 365 W had the battery discharge toward export, and it now stops at +60 W. The other five families only picked up re-dump noise (0 → -0, float jitter) and keep master's files.
  • The site rig's criteria do not assume a 0 W grid.
  • make verify passes on this machine.

Review

An independent review (Sonnet) walked every path to this code for the three modes: passive cover-load and idle slots, planner_self, planner_cheap with and without a slot directive, the legacy plan target, stale-plan fallbacks, the EV surplus-only reserve, manual holds, and the fuse and import-ceiling rails after the PI. It confirmed that the bias never enlarges a discharge, that a switch to PV charge imports at most the tolerance for one tick, that the swapped PI setpoint is restored, and that loads between one and two tolerances settle without a limit cycle.

It found no blocking issue. Changed from it (6e2c18b): the first version keyed the bias on the battery's reading, so an idle battery under a negative legacy plan exported for one tick before the bias engaged. The bias now keys on the planned discharge, with a regression test.

Left as it is: the bias is not capped below the tolerance. A site with a 200 W tolerance holds 0–400 W import while discharging. The mode promises no battery export, so the band's lower edge stays at 0 W; such a site can lower its tolerance.

🤖 Generated with Claude Code

frahlg and others added 2 commits November 5, 2026 06:23
In planner_self, planner_cheap and passive arbitrage the reactive path
aimed a discharging battery at 0 W with a symmetric dead band. Each drop
in house load then sold battery energy until the next correction, about
as often as the site imported. While such a battery discharges more than
the dead band, the target is now at least grid_tolerance_w, so the band
ends at 0 W.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… the reading

The bias keyed on the battery's measured power, so an idle battery under
a legacy plan with a negative grid target discharged toward it for one
tick and exported. It now applies whenever the plain target would have
the battery discharge more than the dead band. Charging from PV surplus
no longer sees the bias at all.

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

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

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-05T05:38:31.789663Z 6e2c18b 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 a71fb80 into master Nov 5, 2026
22 checks passed
@frahlg
frahlg deleted the fix/no-export-discharge-band branch November 11, 2026 09:20
frahlg added a commit that referenced this pull request Dec 5, 2026
Review on #2012 (Fredrik, 14 Nov): planner_self, planner_cheap and
planner_passive_arbitrage never sell, but the boost cover aimed at
exactly 0 W and gave up the dead band the reactive path keeps (#1969).

- The cover and the 30 s hold use the same lifted aim as the reactive
  path, through one helper, noExportGridAimW. The reactive path now
  calls it too, so the two cannot drift.
- In those modes the cover goes no further than the latest meter and
  battery samples show, as the fast cut on export does (#2155).
  Without this the boost undid that cut while the smoothed meter
  caught up with a stopped car, and exported for three ticks.
- During a boost and the hold, a cut of discharge passes the slew
  limit, as the meter clamp's does. With the setup guide's 500 W slew
  the car's stop otherwise eased the battery down 500 W a tick, on the
  energy path in every mode.

Tests, with a 500 W slew where it matters:
- TestBatteryBoostKeepsTheNoExportBand: the three modes settle at the
  dead band, not 0 W; arbitrage keeps 0 W.
- TestBatteryBoostNoExportCutsAtOnceWhenTheCarStops: only the tick the
  car stops on exports, with and without charger lag.
- TestBatteryBoostFollowsTheCarBothWays: exact targets, a cover-load
  slot where the "car stopped" leg fails on master, and slew cases.
- TestBatteryBoostEndingAtAChargeSlotCharges: the hold does not hold
  back a planned charge, on the energy and legacy paths.

Golden: only seeded_ev_boost/043 is re-recorded, spliced into master's
file so the other records keep their values. planner_cheap, cheap
slot: -3157 W becomes -3097 W, projected grid 0 W becomes +60 W, the
record's GridToleranceW.

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