Skip to content

feat(planner): plan each battery within what it has shown it can do - #2072

Open
frahlg wants to merge 5 commits into
masterfrom
planner-slot-limits
Open

frahlg wants to merge 5 commits into
masterfrom
planner-slot-limits

Conversation

@frahlg

@frahlg frahlg commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Paired with srcfl/energyplan#87 (Energyplan 0.9.0, which takes the limits). Refs #2068.

Do not merge yet. The bundle should be pinned only after Energyplan's Linux and timed gates pass on the bundled commit; see "Not verified".

Why

#2068 taught Core each battery's real limits. On the owner's home box the inverter holds grid import near 4.32 kW while the battery charges from the grid, so a night hour gives the battery 4.32 kW less the house (about 2 to 3.8 kW). Its DC current stops discharge near 22.4 A (4.05 to 4.4 kW). With PV it charges at the full 5 kW. The planner still assumed 5 kW both ways, so night slots ended under-delivered (143 warnings in two weeks) and the plan changed every quarter.

What changed

  • Learned limits. onlineFleetParams asks Service.LearnedBatteryLimits (wired in main.go to the battery models, Plateaus.KnownAt) for each battery's trusted plateaus in the 10 °C band its temperature is in now, or the no-temperature band. A battery_current limit becomes W at the pack's voltage now. With no voltage it is left out. Under discharge the voltage sags, so that W is a little optimistic; the code says so. A battery_power limit is its W, and the lower of the two wins. A grid_import limit while charging is the inverter's hold. grid_export is not used, and a limit under 300 W (the least the learner samples) is left out.
  • Per slot. Charge = min(configured, battery limit, share × (hold − house load − PV)), clamped at 0, from the slot's own forecasts (the downside PV the plan uses). The hold belongs to the site, so batteries that learned one share its room by their configured charge limits; the room uses the least hold any of them learned. A battery without a hold keeps its own limits. Discharge takes only the battery's own limit. Nothing rises above the configured limit; with nothing learned every slot keeps it.
  • Worker. Sent as max_charge_w_by_slot / max_discharge_w_by_slot only to a worker that announces storage_slot_limits, and only for a storage where some slot falls below its limit. Without learned limits the request is the same, byte for byte (tested). Core keeps the scalar check in external_optimizer.go and also rejects a reply more than 2 W past a slot limit it sent. A worker without the feature gets no limits and is checked against the scalars only. Core's own planner still holds the limits.
  • Core DP. The fallback holds each slot's actions to the fleet's summed slot limits; an action past a limit runs at it.
  • Contract. Says the above, that Core omits a field it does not send, and that [] and null are invalid. It is byte-identical to Energyplan's copy.
  • Bundle. Energyplan 0.9.0 at 438aa26b, from the package stage of Energyplan's local CI on that commit. Only the two Linux workers and manifest.json change. THIRD-PARTY-NOTICES.txt needs no change: the package stage wrote the same bytes, since no dependency and no vendored microlp code changed (0.8.0's one-line change was the microlp note). Health checks expect 0.9.0.
  • Site sim. sitesim.Battery.GridImportCapW holds grid import like the inverter. The rig's watched paths now include go/internal/battery.
  • Changeset minor (owner approved: "fixa ovan och ta det som behövs").

Tests and results

The KnownAt and site-sim battery tests were written first and failed first; the planner tests came with the code.

  • TestCoreDPPlansTheHomeBoxNightWithinItsLearnedLimits (hold 4.32 kW, night load 0.6 to 1.5 kW, discharge 4.2 kW): without the limits the plan charges 4.5 + 4.5 kW in 2 night hours, past what the battery takes; with them 2.5 + 3.62 + 2.98 kW over 3 hours and −4.2 kW at the peak, all inside.
  • TestNativeEnergyplanPlansTheHomeBoxNightWithinItsLearnedLimits (darwin build of 0.9.0): 4.1 + 5.0 kW in 2 hours without; 3.12 + 3.62 + 2.36 kW over 3 hours and −4.2 kW with. Core accepts the worker's own plan.
  • TestSiteSimNightChargeWithinTheLearnedImportHold (closed loop, Core's planner and control): without the hold, 8 of 8 cheap quarters fell short (plan 1249 Wh, got 780); with it, 0 of 8.
  • TestBatteriesShareTheSitesGridHold: two batteries behind one hold get 5/8 and 3/8 of its room and never more than it together; a third without a hold keeps its own limit.
  • TestACarBesideTheGridHoldKeepsCoresImportLimits: with a car, 3 night slots charge the car and the battery together, so the battery's limit is optimistic there. ValidatePlan still holds the plan to the 11 kW import limit and a 9 kW hourly ceiling.
  • TestWorkerFailureFallsBackWithinTheSameSlotLimits: when the worker answers numerical_failure or deadline_exceeded on a request with slot limits, Core's DP fallback plans within the same limits it sent, and reaches one.
  • Unit tests for plateaus to limits, pack readings, slot limits, the unchanged request, reply rejection, KnownAt bands and param validation.
  • GOFLAGS=-p=4 make verify: pass. On this Mac it skips native execution, so I also ran -run 'Native|RustForecastHost' with a darwin build of the bundled commit: pass. verify.py: bundle integrity passed.

Not verified

  • No run on the home box or other hardware. The Linux workers ran only in Energyplan's packaging, not here.
  • Energyplan's Linux and timed gates have not run on the bundled commit; its PR says why.
  • A car charging in a slot takes import the battery then cannot get; the slot limit leaves the car out (pinned above as optimistic only).
  • The pack voltage is read now, not a recent average.

Overlap

Open #1956 changes the DP loop in mpc.go; there is a small text conflict at actionAt. #2017 and #2041 merge cleanly.

🤖 Generated with Claude Code

frahlg and others added 3 commits November 22, 2026 13:46
The battery model learns trusted limits per battery (#2068). On the
owner's home box the inverter holds grid import near 4.32 kW while the
battery charges, so a night hour gives the battery 4.32 kW less the house,
and its DC current stops discharge near 22.4 A. The planner still expected
5 kW both ways, so night slots ended under-delivered and the plan replanned
every quarter.

Core now reads each battery's trusted plateaus for the temperature band it
is in now (Plateaus.KnownAt) into learned limits: a DC current limit in W
at the pack's voltage now, a power limit, and the grid import the inverter
holds while charging. Each slot's charge limit is then that import plus
the slot's PV less its house load, from the forecasts the plan uses, never
above the configured limit; discharge takes the battery's own limit only.

A worker that announces storage_slot_limits gets them as
max_charge_w_by_slot and max_discharge_w_by_slot, and Core rejects a reply
past them. Core's own planner holds each slot's actions to them too. A
battery with nothing learned sends the same request as before.

The site simulator's battery can hold grid import like the inverter. In a
closed-loop night run every cheap quarter fell short of its plan without
the learned hold and none with it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Fredrik Ahlgren <fredrik@sourceful-labs.com>
Energyplan 0.9.0 (7bf34945) plans each storage within the per-slot power
limits Core sends and announces storage_slot_limits. A request without
them plans as 0.8.0 does. Protocol v1, site signs and the time budget are
unchanged. Only the two Linux workers and manifest.json change; the
bundle comes from the package stage of Energyplan's local CI on that
commit.

The health checks expect 0.9.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Fredrik Ahlgren <fredrik@sourceful-labs.com>
The planner now reads the battery model's learned limits, so a change in
go/internal/battery changes plans the rig runs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Fredrik Ahlgren <fredrik@sourceful-labs.com>
@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-22T12:56:07.724355Z 9e49eee 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9e49eee333

ℹ️ 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".

Comment on lines +47 to +48
case battery.LimitBatteryPower:
w = k.Value

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Distinguish battery limits from site-constrained power plateaus

When grid-limited charging coincides with steady household load, the learner can produce a trusted battery_power plateau that is not the battery's maximum power. For example, a 4.32 kW import hold and steady 1.5 kW load yield a flat 2.82 kW battery reading against a 5 kW order: Plateaus.Tick records that power because it exceeds the 50% share threshold, and repeated runs make it trusted. Converting it unconditionally into ChargeW now restricts every planning slot—including sunny slots where this battery can charge at 5 kW—to 2.82 kW. The existing learner identifies repeated shortfall levels, not their causal independence from site conditions; distinguish those cases before promoting them to unconditional planning limits.

Useful? React with 👍 / 👎.

frahlg and others added 2 commits November 22, 2026 14:10
The grid import hold belongs to the site, so two batteries that both
learned it each got the whole room, and the fleet could plan twice what
the inverter lets through. The batteries that learned a hold now share its
room in proportion to their configured charge limits; the room is the
least hold any of them learned plus the slot's PV less its house load. A
battery without a hold keeps its own limits.

Tests pin two cases the limits leave out. A car charging in the same slot
takes import the battery then cannot get: the plan is optimistic for the
battery only, and ValidatePlan still holds it to the site's import limit
and the hourly ceiling. When the worker fails with numerical_failure or
deadline_exceeded on a request with slot limits, Core's fallback plans
within the same limits.

The contract says a worker without storage_slot_limits gets no limits and
is checked against the scalars only, that Core omits a field it does not
send, and that [] and null are invalid. storage_limits.go notes that a
current limit read at rest is a little optimistic under discharge sag.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Fredrik Ahlgren <fredrik@sourceful-labs.com>
The bundle now carries the review fixes: limit ratios that differ only by
rounding stay on the battery library, and `[]` and `null` per-slot arrays
are invalid. It comes from the package stage of Energyplan's local CI on
438aa26b. Only the two Linux workers and manifest.json change.

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