Skip to content

core: shorten horizon for short consumption forecasts instead of failing - #476

Merged
MaStr merged 1 commit into
mainfrom
fix/consumption-forecast-shorter-horizon
Oct 28, 2026
Merged

MaStr merged 1 commit into
mainfrom
fix/consumption-forecast-shorter-horizon

Conversation

@MaStr

@MaStr MaStr commented Oct 28, 2026

Copy link
Copy Markdown
Owner

Problem

With the Home Assistant consumption forecast the run failed:

RuntimeError: Not enough consumption forecast data available, requested 210, got 191

The HA provider is capped at 48h (MAX_FORECAST_HOURS, 192 slots at 15 min, minus 1 for the shift to the current interval). Price and production forecasts reached ~52.5h. The shortfall was larger than FORECAST_TOLERANCE (3 slots), so the run raised and the inverter fell back to Allow Discharging.

Change

  • A consumption forecast shorter than requested now reduces fc_period to the available data, as already done for prices and production.
  • Only an empty forecast (fewer than MIN_FORECAST_HOURS) is still an error.
  • Remove the now unused FORECAST_TOLERANCE.

Testing

Existing tests pass (tests/batcontrol, web tests and tests needing pytest-mock not run locally). No new test added.

🤖 Generated with Claude Code

A consumption forecast shorter than requested only raised an error when
it was more than FORECAST_TOLERANCE slots short. The Home Assistant
provider is capped at 48h, while price and production forecasts can
reach further, so the run failed and the inverter fell back to
Allow Discharging.

Reduce the forecast horizon to the available consumption data, as is
done for prices and production. Only an empty forecast is an error.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 28, 2026 19:10
@MaStr MaStr added the bug Something isn't working label Oct 28, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

An unresolved active finding flags that the broadened reduced-horizon forecast path lacks regression test coverage, leaving the fix unverified.

1 open issue
What changed in this PR

This PR makes batcontrol more resilient when the consumption forecast is shorter than the price/production forecast horizon. Previously, if the Home Assistant consumption provider (capped at 48h) returned fewer slots than the ~52.5h price/production horizon and the shortfall exceeded a fixed FORECAST_TOLERANCE of 3 slots, the control cycle raised RuntimeError and the inverter fell back to Allow Discharging. The change aligns consumption handling with how prices and production are already harmonized: a short forecast simply shortens fc_period rather than aborting the cycle.

Changes:

  • A consumption forecast shorter than requested now reduces fc_period to the available data instead of failing.
  • Only an effectively empty forecast (len < MIN_FORECAST_HOURS) still raises an error.
  • Removes the now-unused FORECAST_TOLERANCE constant.
File Description
src/​batcontrol/​core.py Replaces the tolerance-bounded error check with an unconditional horizon reduction for short consumption forecasts, keeps the empty-forecast error branch, and drops the unused FORECAST_TOLERANCE constant.

Review effort: Balanced

Comment thread src/batcontrol/core.py
@MaStr
MaStr merged commit dff8923 into main Oct 28, 2026
13 checks passed
@MaStr
MaStr deleted the fix/consumption-forecast-shorter-horizon branch October 28, 2026 19:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants