Skip to content

feat(control): inverter-affinity charge routing prefers DC-local battery - #144

Merged
frahlg merged 1 commit into
masterfrom
143-inverter-affinity
Apr 20, 2026
Merged

frahlg merged 1 commit into
masterfrom
143-inverter-affinity

Conversation

@frahlg

@frahlg frahlg commented Apr 20, 2026

Copy link
Copy Markdown
Member

Closes #143.

Summary

  • New optional `inverter_group: ` per driver in `config.yaml`. Tag the hybrid-inverter driver and any co-located PV driver with the same string.
  • `distributeProportional` now takes a `groupPV` map. When the fleet is charging, it routes up to `min(desiredTotal, ΣPV_g)` preferentially to batteries whose inverter also has live PV output (DC-local), then overflows the rest proportionally. Discharge and no-PV cases skip the locality math and collapse to today's capacity-proportional split.
  • `control.State.InverterGroups` wires driver→group from config; swapped under `ctrlMu` on hot-reload.

Why

Cross-inverter charging (PV on A → AC bus → inverter B → battery B) eats ~3-4 pp round-trip vs DC-local routing on the same unit. Over a sunny season at ~2 MWh routed through batteries that's 60-80 kWh silently lost. No config change needed for single-inverter sites — untagged drivers keep current behaviour exactly.

Worked example

```
drivers:

  • name: ferroamp
    inverter_group: ferroamp
    battery_capacity_wh: 15200
  • name: sungrow
    inverter_group: sungrow
    battery_capacity_wh: 9600
    ```

Fleet charging at 3 kW. Ferroamp PV = 4 kW, Sungrow PV = 0 kW:

  • Old: Ferroamp 1.84 kW + Sungrow 1.16 kW (capacity split).
  • New: Ferroamp 3.00 kW + Sungrow 0.00 kW (all DC-local).

Fleet charging at 6 kW. Same PV:

  • Old: Ferroamp 3.68 kW + Sungrow 2.32 kW.
  • New: Ferroamp 5.00 kW (4 local + 1.23 overflow, clamped at MaxCommandW) + Sungrow 0.77 kW (overflow share).

Backcompat

Untagged sites → `InverterGroups` map is empty → `groupPV` map is empty → `distributeProportional` falls through to the legacy capacity-proportional split. Five new tests + 51 existing control tests pass. `make test` + `make e2e` green.

Test plan

  • Unit tests for local-only, overflow, discharge, ungrouped, end-to-end.
  • `make test` + `make e2e` — 485 tests green.
  • Smoke on the Pi after merge: tag both inverters in config.yaml, wait for a charging slot (or force mode = `charge`), verify that Ferroamp battery gets the full share of Ferroamp PV and Sungrow battery only takes what the mixed AC-bus demands require.

Out of scope

  • DP-side per-group awareness. The MPC still optimises against the aggregate fleet; per-group state would grow the DP's state space and isn't worth it until we measure the locality-loss recovery in production. Referenced in Safe update + rollback UX (master) #140's follow-up backlog.

🤖 Generated with Claude Code

Closes #143.

Each physical inverter unit (hybrid PV + battery or separate
PV + battery pair on the same string) can keep incoming PV energy
DC-coupled to the same-unit battery, avoiding the
DC→AC→AC→DC round-trip that cross-inverter charging incurs —
roughly 3-4 pp of round-trip efficiency on a typical two-inverter
site over a sunny season.

The dispatcher now accepts an optional `inverter_group: <tag>` per
driver. At ComputeDispatch time we sum PV telemetry per group, then
feed that map into distributeProportional. When the fleet is charging
(desiredTotal > 0), the function routes up to `min(desiredTotal, ΣPV_g)`
preferentially to batteries whose group also has live PV output,
and spreads any overflow capacity-proportionally across the whole
fleet — the existing behaviour. Discharge and the no-PV case skip
the locality math and collapse to the capacity-proportional split,
so no-group sites see no behaviour change.

Within a group with multiple batteries, the local share is split by
that group's capacity ratio (same rule as fleet-wide). With only one
group represented across the fleet, the formula is mathematically
identical to today's proportional split — so a single-inverter
installation gets zero behaviour change from tagging.

Wiring:
- config.Driver gains optional `inverter_group` YAML key.
- control.State.InverterGroups holds the driver→group map; wired
  from config at startup and swapped under ctrlMu on config-reload
  so the control loop's 5 s tick can't race a live edit.
- main.go's inverterGroupsFrom helper builds the map from cfg.Drivers.

Tests (go/internal/control/control_test.go):
- TestInverterAffinity_PrefersLocalBatteryForLocalSurplus
- TestInverterAffinity_FallsBackToProportionalOnOverflow
- TestInverterAffinity_UngroupedBehavesAsBefore  (backcompat)
- TestInverterAffinity_DischargeStillProportional (discharge path)
- TestInverterAffinity_EndToEndViaComputeDispatch (wiring sanity)

Docs:
- docs/configuration.md: new "Inverter affinity" subsection with
  concrete example tags for a ferroamp + sungrow two-inverter site.
- go/internal/control/CLAUDE.md: "What NOT to do" updated with the
  groupPV guardrail.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@frahlg
frahlg merged commit cc3f1f3 into master Apr 20, 2026
1 check passed
@frahlg
frahlg deleted the 143-inverter-affinity branch April 20, 2026 08:18

@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: 13270cdaca

ℹ️ 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 +607 to +610
var totalPV float64
for _, w := range groupPV {
totalPV += w
}

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 Exclude PV-only groups from locality denominator

This sums totalPV across every tagged PV group, including groups that have no online battery capacity in bats; later, those groups are skipped by the capG > 0 check, so their portion of localCap is never reassigned. In scenarios like a PV-only tagged group (or a group's battery going offline), the dispatched targets add up to less than desiredTotal, which leaves persistent grid error and under-charging until PI winds up. totalPV should be computed only from groups that have dispatchable battery capacity.

Useful? React with 👍 / 👎.

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.

feat(control): inverter-affinity routing — prefer local battery for each inverter's PV surplus

1 participant