Skip to content

[codex] Change battery actions to policy states - #15

Merged
LordMike merged 7 commits into
mainfrom
battery-policy-states
Apr 22, 2026
Merged

[codex] Change battery actions to policy states#15
LordMike merged 7 commits into
mainfrom
battery-policy-states

Conversation

@LordMike

Copy link
Copy Markdown
Owner

Issue

WattPlan exposed battery action states that looked like raw flow states (hold, discharge, charge_pv, charge_grid_pv). That made hold especially misleading: the planner could mean "no modeled battery flow", while inverter automations often interpret it as "block discharge". In a real system, PV may cover forecast load while the battery should still be allowed to cover unexpected load spikes.

Fix

This changes the public battery action contract to inverter-control policy states:

  • preserve: prevent battery discharge because the model shows spending energy now is bad or infeasible.
  • self_consume: normal operation; allow battery discharge for real load and do not request grid charging.
  • grid_charge: request/allow grid charging and avoid spending the battery while charging.

PV charging remains implicit/normal behavior. PV export is intentionally not represented as a battery action state and is deferred for future site-level policy design.

What Changed

  • Removed old public action states from sensor options, tests, docs, and plan projection mappings.
  • Updated HA projection action codes to gc, p, and sc for the three policy states.
  • Removed user-facing prefer_pv_surplus_charging configuration/docs where it conflicted with the policy model, leaving it documented only as internal/deferred optimizer behavior.
  • Added a model-backed battery_preserve signal to optimizer state and warm-start reuse.
  • Replaced target/future-flow preserve inference with a counterfactual MPC check: if forcing a small discharge now is infeasible or worsens the objective, the slot is marked preserve; otherwise it defaults to self_consume unless grid charging is active.
  • Updated docs with the new policy contract and a generic Solar Assistant/MQTT/Deye-compatible automation example.

Validation

  • ./scripts/run_tests.sh
  • Result: 135 passed in 4.65s

Caveats

  • preserve now requires additional counterfactual solver calls for candidate battery slots.
  • Targets do not directly create preserve; they only affect preserve through model constraints/objective impact.
  • PV export remains deferred as a site-level policy, not a battery state.

Comment thread docs/extras.md Outdated
Comment thread docs/extras.md Outdated
Comment thread docs/extras.md Outdated
Comment thread custom_components/wattplan/optimizer/mpc_power_optimizer.py Outdated
@LordMike
LordMike marked this pull request as ready for review April 22, 2026 13:09
Comment thread README.md Outdated
Comment thread docs/extras.md Outdated
Comment thread docs/extras.md Outdated
Comment thread docs/extras.md
@LordMike
LordMike merged commit 95b57ed into main Apr 22, 2026
1 check passed
@LordMike
LordMike deleted the battery-policy-states branch April 22, 2026 13:53
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