Skip to content

feat(control): wire MPC pv_limit_w into per-driver curtail dispatch - #236

Merged
frahlg merged 1 commit into
masterfrom
feat/mpc-pv-curtail-dispatch
May 2, 2026
Merged

frahlg merged 1 commit into
masterfrom
feat/mpc-pv-curtail-dispatch

Conversation

@frahlg

@frahlg frahlg commented May 2, 2026

Copy link
Copy Markdown
Member

Summary

Wires the missing leg between mpc.annotateCurtailment (which has been computing PV curtail recommendations for a while) and the actual driver-side action. Operators with negative-spot pricing now see curtail commands fire when the plan says exporting unconstrained PV would lose money.

  • mpc.SlotDirective gains PVLimitW — surfaced through the main.go adapter into the control package's mirror struct.
  • New control.ComputePVCurtail allocates the site-wide cap proportionally to each PV-supporting driver's live |PV|. Returns one CurtailTarget per affected driver; LimitW=0 means "release any prior curtail" (curtail_disable).
  • New State.SupportsPVCurtail (per-driver opt-in) gates dispatch. Lua side has shipped curtail handlers in sungrow / ferroamp / deye / huawei / solis for a while — this just turns on the Go-side dispatcher per driver. Operators set supports_pv_curtail: true in YAML.
  • New State.LastCurtailedDrivers diffs across ticks so a previously-curtailed driver gets exactly one curtail_disable when it drops out of the active set (slot rollover, plan no longer asking for curtail). No silent caps left applied.
  • main.go dispatch tick now runs ComputePVCurtail after ComputeDispatch and sends each result through reg.Send.

Why

Real incident on 2026-05-02: spot at −5 öre/kWh, the operator's site exporting ~6 kW of PV, MPC plan reported pv_limit_w: 1338 for the active slot — but no Go code read it. The lua drivers have curtail handlers; the wiring just wasn't there.

Test plan

  • go test ./... — 782 passed (+7 new in pv_curtail_test.go)
  • On homelab-rpi after deploy: set supports_pv_curtail: true on sungrow and ferroamp drivers, switch to a planner mode during a negative-spot hour, confirm /api/mpc/diagnose shows pv_limit_w > 0 AND the inverters' export limit registers reflect the cap (sungrow holding 13073, ferroamp pplim MQTT echo).
  • Cross-check: after slot rollover when prices return to positive, drivers receive exactly one curtail_disable and PV inverter resumes uncapped output.

The MPC's annotateCurtailment has been computing PVLimitW for slots
where exporting more PV would lose money (negative-spot hours, no
positive feed-in tariff) for a while now — but the value only
surfaced in /api/mpc/diagnose. The dispatch loop ignored it, so on
2026-05-02 with spot at −5 öre the site happily exported 6 kW of
unconstrained PV at the operator's expense.

This wires the missing leg:

- mpc.SlotDirective gains PVLimitW (echoed via the main.go adapter
  into the control package's mirror struct).
- New control.ComputePVCurtail returns one CurtailTarget per affected
  driver. The site-wide cap is allocated proportionally to each
  driver's live |PV|, so a 1.5 kW limit splits 0.9 kW / 0.6 kW
  across a 6 kW + 4 kW PV pair.
- New State.SupportsPVCurtail (per-driver opt-in) gates dispatch.
  Drivers without the flag are silently skipped — an EV charger
  wouldn't know what to do with a `curtail` payload anyway. The
  lua side (sungrow / ferroamp / deye / huawei / solis) has shipped
  curtail handlers for a while; this just turns on the Go-side
  dispatcher per driver.
- New State.LastCurtailedDrivers tracks who got non-zero curtail
  last tick. ComputePVCurtail diffs against the new set and emits
  exactly one LimitW=0 release target when a previously-curtailed
  driver drops out — main.go translates that to `curtail_disable`
  so the cap is released on slot rollover instead of silently
  staying applied.
- main.go's dispatch tick now runs ComputeDispatch (batteries) and
  ComputePVCurtail (PV) sequentially, sending each result through
  reg.Send with the matching action.
- New config.Driver.SupportsPVCurtail (`yaml: supports_pv_curtail`)
  carries the opt-in. Hot-reloadable via the same path as
  InverterGroups + DriverLimits.

Regression suite covers: stale plan → no targets, PVLimitW=0 → no
targets, proportional split across two PV drivers, non-supporting
driver skipped, non-generating driver skipped, release-on-slot-
rollover, and deterministic ordering.
@frahlg
frahlg merged commit e65d964 into master May 2, 2026
1 check passed
@frahlg
frahlg deleted the feat/mpc-pv-curtail-dispatch branch May 2, 2026 11:50

@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: 631c2f0b7c

ℹ️ 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 +1091 to +1093
if len(next) == 0 {
state.LastCurtailedDrivers = nil
} else {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Retry curtail_disable until command is acknowledged

ComputePVCurtail clears LastCurtailedDrivers before main.go attempts to send the generated curtail_disable command, so a transient reg.Send failure leaves the inverter capped with no further release attempts. This occurs when a driver was curtailed in the previous tick, drops out of next, and the disable send fails once (e.g., brief MQTT/Modbus outage): subsequent ticks return nil because state was already cleared, so the one-shot release is lost permanently until manual intervention.

Useful? React with 👍 / 👎.

frahlg added a commit that referenced this pull request Nov 25, 2026
Move the pin to srcfl/device-drivers@28c771fd (#279). zaptec_cloud 0.3.0
uses host.set_command_timeout_s and driver_release from this PR. The pin
also carries the easee_cloud fixes merged since e7b94b11 (#183, #227, #194,
#236, #200), whose Core side (#2050, #2062) is already on master.

Tell the stopped instance's default apart from a replacement's startup
default in TestOtherStopsKeepTheSafeDefault (review finding).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
frahlg added a commit that referenced this pull request Nov 25, 2026
…val (#2079)

* fix(drivers): give slow cloud drivers time, hand devices back on removal

A Zaptec Cloud command that took longer than 5 s failed, and the safe
default then paused the charger at 0 A. A driver can now ask for up to
30 s per call with host.set_command_timeout_s; the control tick still waits
only its short deadline and the call finishes in the driver's queue.

Removing or disabling a driver calls its optional driver_release instead of
driver_default_mode, so a charger goes back to the owner's own current
rather than staying at FTW's 0 A. Restarts, shutdown and stale readings
keep the safe default.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(drivers): bundle zaptec_cloud 0.3.0 and easee_cloud 1.5.0

Move the pin to srcfl/device-drivers@28c771fd (#279). zaptec_cloud 0.3.0
uses host.set_command_timeout_s and driver_release from this PR. The pin
also carries the easee_cloud fixes merged since e7b94b11 (#183, #227, #194,
#236, #200), whose Core side (#2050, #2062) is already on master.

Tell the stopped instance's default apart from a replacement's startup
default in TestOtherStopsKeepTheSafeDefault (review finding).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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