Skip to content

Prevent low-value export and freeze-export slots leaking into Predbat plans#4088

Open
nickgee31 wants to merge 13 commits into
springfall2008:mainfrom
nickgee31:export-planning-changes
Open

Prevent low-value export and freeze-export slots leaking into Predbat plans#4088
nickgee31 wants to merge 13 commits into
springfall2008:mainfrom
nickgee31:export-planning-changes

Conversation

@nickgee31

Copy link
Copy Markdown
Contributor

This PR tightens how Predbat handles export windows around the optimiser’s final export threshold.
Previously, Predbat could still plan or continue export behaviour in slots that were below the effective export threshold. This showed up in a few ways:

  • A low-rate export period could be joined to a higher-rate export window and survive because the merged window average still looked worthwhile.
  • Existing/current inverter export settings could remain active even after the optimiser no longer wanted that low-rate slot.
  • Optimiser-created freeze-export slots could bypass the export threshold.
  • The UI/plan could show confusing export windows that started slightly before the genuinely valuable export period, for example exporting from 15:55 even though the export rate did not improve until 16:00.

Changes

  • Added a final export-window threshold check after optimisation.
  • Added pruning of optimiser-selected export windows below the final export threshold.
  • Added trimming of export windows so below-threshold edge periods are removed from the start/end of a larger export window.
  • Kept manual export and manual freeze-export overrides honoured even when below threshold.
  • Changed optimiser-created freeze export so it is also subject to the export threshold.

Predbat should now avoid planning or executing export/freeze-export actions below the optimiser’s final export threshold, unless the user explicitly requested them manually. This makes export behaviour more consistent with the intended meaning of the threshold and avoids low-value export periods being pulled into otherwise profitable export windows.

nickgee31 and others added 11 commits June 12, 2026 12:07
Add threshold-based pruning and trimming for optimiser-selected export windows. Plan: introduce export_threshold_for_window, export_window_is_manual, export_window_above_threshold, trim_export_window_to_threshold and prune_export_windows_below_threshold; call pruning during plan filtering and recompute. Execute: respect export_window_above_threshold to avoid starting export (or scheduling freeze-only) when window rates are below the final threshold, log decisions and disable forced export. Tests: add unit tests and setup changes covering pruning, trimming of low-rate edges, preservation of manual overrides, and execute behavior when below threshold.
Set my_predbat.rate_export = {} in the test fixture to ensure the attribute exists for tests. This prevents errors when tests or code expect a dict for rate_export and makes the setup explicit about the default test state.
Compute original_window_size from the window's original start and use it when scaling metric_min_improvement_export. Previously the scaled minimum improvement used window_size (based on adjusted start), which could mis-scale the threshold; this change preserves the original window duration so min_improvement_scaled is calculated correctly relative to plan_interval_minutes.
Add logic to undo optimiser tail-trimming for export windows when the skipped original start period remains above the final export threshold. Hook restore_export_windows_above_threshold into the full optimisation flow (guarded by calculate_best_export and export_window_best). The new method checks the export threshold, skips fully-exported windows, inspects per-minute rate_export (using PREDICT_STEP) or window average, logs the restoration, and resets the window start to start_orig when appropriate. Also add unit tests verifying restoration when skipped rates are above threshold and non-restoration when there are below-threshold rates.

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.

Pull request overview

This PR refines Predbat’s export-window handling so that optimiser-selected export and freeze-export slots do not “leak” into the plan/execution when their rates fall below the optimiser’s final export threshold, while still honoring explicit manual overrides.

Changes:

  • Added final post-optimisation pruning/trimming of export windows against the effective export threshold.
  • Added restoration logic to undo optimiser tail-trimming when skipped minutes are still above the threshold.
  • Updated execution and tests to ensure below-threshold export does not remain active unless manually requested.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
apps/predbat/plan.py Adds export-threshold helpers plus pruning/trimming/restoration of export windows below the final threshold.
apps/predbat/execute.py Prevents executing/scheduling export when the current export window is below the final threshold.
apps/predbat/tests/test_execute.py Adds execution coverage for below-threshold export vs manual override behavior.
apps/predbat/tests/test_discard_unused_export_slots.py Adds unit tests for pruning/trimming/restoring export windows around the final threshold.

Comment thread apps/predbat/plan.py
Comment on lines +2396 to +2406
if skipped_above_threshold:
self.log(
"Restore export window start {} - {} to {} using optimisation threshold {}{}".format(
self.time_abs_str(window_start),
self.time_abs_str(window["end"]),
self.time_abs_str(window_start_orig),
dp2(threshold),
self.currency_symbols[1],
)
)
window["start"] = window_start_orig
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.

2 participants