Skip to content

Add solar_surplus_wh and solar_active to MQTT output#370

Merged
MaStr merged 4 commits into
mainfrom
claude/solar-surplus-wp-feedin-Gr5vC
May 28, 2026
Merged

Add solar_surplus_wh and solar_active to MQTT output#370
MaStr merged 4 commits into
mainfrom
claude/solar-surplus-wp-feedin-Gr5vC

Conversation

@MaStr
Copy link
Copy Markdown
Owner

@MaStr MaStr commented May 28, 2026

After each control cycle, publish two new MQTT topics:

  • /solar_surplus_wh — expected solar overflow in Wh (energy that would be fed to grid)
  • /solar_active — boolean, true iff solar is producing in the current slot (binary_sensor in HA discovery, retained)

Surplus formula

When solar is active (solar_active = true, production[0] > 0):

surplus = max(0, -sum(net_consumption[:production_end_current+1]) - free_capacity)

Covers the first production window only (scan stops at the first zero slot after production starts, preventing multi-day forecast contamination).

When solar is inactive (solar_active = false):

bridge_wh  = sum(net_consumption[:production_start])   # night consumption
solar_net  = -sum(net_consumption[production_start:production_end_current+1])
surplus    = max(0, solar_net - free_capacity - bridge_wh)

Answers: "given current battery state and expected night consumption, how much of tomorrow's solar production will overflow?" — rein forecast-basiert, ohne reserved_energy oder stored_usable_energy.

If no solar exists in the forecast, surplus_wh = 0.

Key implementation details

  • Arrays are already Wh/slot (not W) — no wh_per_slot scaling needed, correct at 15-min and 60-min resolution.
  • free_capacity is taken from calc_input (same snapshot as the optimizer decision).
  • The three-phase string (before/during/after) was collapsed to a boolean: before and after used identical formulas; only the label differed.
  • No before_phase_threshold_h config key — the boolean needs no threshold.
  • HA MQTT auto-discovery entries added for both topics.

https://claude.ai/code/session_01WQKwRRnW55Tr2mBkKao573

After each control cycle, publish two new topics:
- /solar_surplus_wh: expected usable solar surplus in Wh
- /solar_production_phase: 'before', 'during', or 'after'

Phase detection uses a None sentinel to correctly distinguish "no
production in forecast" from "production starting at slot 0", fixing
the ambiguity in the wp-feedin heuristic. Production window boundaries
track the last non-zero slot to tolerate single-slot forecast gaps.

Surplus calculation:
- during/before: net production surplus over the forecast window minus
  free battery capacity (what would be exported/wasted)
- after: unreserved stored usable energy minus expected consumption
  until next solar production starts (what the battery can spare)

The corrected (elapsed-time-adjusted) production/consumption arrays are
used together with calc_output.reserved_energy from the just-completed
optimizer run, so the values are consistent with the control decision.

HA MQTT auto-discovery entries added for both sensors.

https://claude.ai/code/session_01WQKwRRnW55Tr2mBkKao573
Copilot AI review requested due to automatic review settings May 28, 2026 04:49
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds MQTT reporting for expected solar surplus and solar production phase after optimizer runs, helping downstream MQTT/HA consumers react to PV availability.

Changes:

  • Adds core calculation for solar phase and surplus energy.
  • Publishes two new MQTT topics for surplus Wh and production phase.
  • Adds Home Assistant discovery entries for the new sensors.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
src/batcontrol/core.py Computes and publishes solar phase/surplus after optimization.
src/batcontrol/mqtt_api.py Adds MQTT publishers and HA discovery entries for the new sensors.

Comment thread src/batcontrol/core.py Outdated
Comment thread src/batcontrol/core.py Outdated
Comment thread src/batcontrol/core.py Outdated
Comment thread src/batcontrol/mqtt_api.py Outdated
claude added 2 commits May 28, 2026 04:59
Fix surplus calculation for during/before phase: sum the net energy
balance over the entire production window (production minus consumption)
instead of only counting slots where production exceeds consumption.
Positive consumption slots in the window now correctly reduce the
reported surplus.

Fix free_capacity and stored_usable_energy consistency: pass
calc_input.free_capacity and calc_input.stored_usable_energy into the
helper so the published values are based on the same inverter snapshot
as the optimizer decision, avoiding a second inverter read.

Add tests/batcontrol/test_solar_surplus.py: 19 focused tests covering
phase detection (during/before/after, gaps, 15-min resolution) and
surplus calculation for all three phases including edge cases (fits in
battery, exceeds capacity, negative surplus clamped to zero, reserved
energy, 15-min scaling).

Extend tests/batcontrol/test_mqtt_api.py: 8 tests covering
publish_solar_surplus (topic, format, zero, disconnected) and
publish_production_phase (all three values, disconnected).

https://claude.ai/code/session_01WQKwRRnW55Tr2mBkKao573
… scoping

Three bugs fixed in _compute_solar_surplus_and_phase:
- Remove wh_per_slot multiplier (arrays are already Wh/slot, not W)
- Use first production window only (not last slot in multi-day forecast)
- Replace reserved_energy/stored_usable with forecast-based bridge calculation

New formula for 'before'/'after': expected solar overflow at end of next
production window = max(0, solar_net_next - free_capacity - bridge_wh),
where bridge_wh is forecast consumption until solar starts. Identical
formula for both phases; phase label differs only by threshold distance.

Add before_phase_threshold_h (default 4h) to battery_control_expert config.
Copilot AI review requested due to automatic review settings May 28, 2026 07:03
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

Comment thread src/batcontrol/core.py Outdated
Comment thread src/batcontrol/core.py Outdated
Comment thread src/batcontrol/core.py Outdated
Comment thread src/batcontrol/core.py Outdated
Comment thread src/batcontrol/core.py Outdated
'before' and 'after' used identical formulas; only the label differed.
Replace the 3-state string with a binary signal: solar_active=True iff
production[0] > 0. Drops the unused before_phase_threshold_h config.

MQTT topic renamed: solar_production_phase -> solar_active (binary_sensor
in HA discovery). Consumers should switch to solar_surplus_wh + solar_active.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

Comment thread src/batcontrol/mqtt_api.py
Comment thread src/batcontrol/core.py
@MaStr MaStr changed the title Add solar surplus and production phase to MQTT output Add solar_surplus_wh and solar_active to MQTT output May 28, 2026
@MaStr MaStr merged commit 3d78592 into main May 28, 2026
14 checks passed
@MaStr MaStr deleted the claude/solar-surplus-wp-feedin-Gr5vC branch May 28, 2026 07:29
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.

3 participants