Skip to content

logic: add forecast grid charge target strategy#361

Open
filiplajszczak wants to merge 1 commit into
MaStr:mainfrom
filiplajszczak:logic-grid-charge-target-strategy
Open

logic: add forecast grid charge target strategy#361
filiplajszczak wants to merge 1 commit into
MaStr:mainfrom
filiplajszczak:logic-grid-charge-target-strategy

Conversation

@filiplajszczak
Copy link
Copy Markdown
Contributor

@filiplajszczak filiplajszczak commented May 5, 2026

Adds an opt-in forecast grid-charge target strategy while keeping the default fixed behavior unchanged.

With forecast, min_grid_charge_soc remains a floor, and batcontrol raises the effective target from forecasted expensive-slot net demand, capped by max_charging_from_grid_limit.

Also publishes the runtime effective target via MQTT/Home Assistant discovery.

I’ll add the corresponding wiki documentation after merge.

@MaStr MaStr requested a review from Copilot May 5, 2026 07:31
@filiplajszczak filiplajszczak force-pushed the logic-grid-charge-target-strategy branch from 58656a4 to 21c2e72 Compare May 5, 2026 07:36
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 an opt-in forecast strategy for determining the effective minimum grid-charge SoC target at runtime, while keeping the default fixed behavior unchanged. The effective target is computed from forecasted net demand during upcoming expensive price slots (with optional PV forecast discounting) and is then passed into the logic layer and exposed via MQTT.

Changes:

  • Introduces batcontrol.logic.grid_charge_target.calculate_effective_grid_charge_soc() with fixed and forecast strategies.
  • Adds core config parsing and per-run computation of effective_min_grid_charge_soc, passing it into CalculationParameters and publishing it via MQTT.
  • Extends MQTT API + discovery and adds unit/integration-style tests for the new behavior.

Reviewed changes

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

Show a summary per file
File Description
src/batcontrol/logic/grid_charge_target.py New module implementing effective grid-charge SoC target calculation strategies.
src/batcontrol/core.py Parses new config, computes effective target each run, publishes it to MQTT, and feeds it into logic parameters.
src/batcontrol/mqtt_api.py Publishes the effective target and exposes it via Home Assistant MQTT discovery.
tests/batcontrol/logic/test_grid_charge_target.py Unit tests for fixed/forecast target computation and capping behavior.
tests/batcontrol/test_core.py Validates config parsing and verifies core passes/publishes the effective target.
tests/batcontrol/test_mqtt_api.py Tests MQTT publishing + discovery for the new effective target sensor.
config/batcontrol_config_dummy.yaml Documents the new optional config keys in the dummy config.

if isinstance(values, dict):
if not values:
return []
return [values[index] for index in range(max(values.keys()) + 1)]
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed in f5261de: sparse/non-consecutive forecast dict indices now raise a clear ValueError, covered by a regression test.

Comment thread src/batcontrol/core.py Outdated
@filiplajszczak filiplajszczak force-pushed the logic-grid-charge-target-strategy branch from 21c2e72 to f5261de Compare May 5, 2026 07:47
@filiplajszczak
Copy link
Copy Markdown
Contributor Author

Addressed the two Copilot comments in f5261de: forecast dict indices now fail with a clear ValueError/regression test, and grid_charge_target_strategy parsing now strips whitespace before lowercasing.

@filiplajszczak filiplajszczak force-pushed the logic-grid-charge-target-strategy branch from f5261de to e054693 Compare May 5, 2026 08:06
Comment thread src/batcontrol/core.py Outdated
else:
self.avoid_discharging()

def __calculate_effective_min_grid_charge_soc(
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I tried to move all these calculations over to the logic/ packages and have core.py more like a wiring and function router place.

With this context, does it make sense to have it here in the code, or not?

Copy link
Copy Markdown
Contributor Author

@filiplajszczak filiplajszczak May 23, 2026

Choose a reason for hiding this comment

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

Good point. Addressed in 690a32a: the grid-charge target config parsing and effective-target resolver now live in batcontrol.logic.grid_charge_target; core.py only builds CalculationInput, calls the logic helper, publishes the effective target, and passes it into CalculationParameters.

@filiplajszczak filiplajszczak force-pushed the logic-grid-charge-target-strategy branch from e054693 to 690a32a Compare May 23, 2026 15:23
@filiplajszczak filiplajszczak requested a review from MaStr May 23, 2026 15:26
@filiplajszczak filiplajszczak force-pushed the logic-grid-charge-target-strategy branch from 690a32a to 7b2bd3c Compare May 23, 2026 16:09
@MaStr
Copy link
Copy Markdown
Owner

MaStr commented May 23, 2026

Ok, I did another review and try to understand what you did there.
in the default logic, there is the recharge amount defined by the high price slot.
Why not just add the capacity on top of the recharge amount during the recharge energy calculation?

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