Skip to content

Fix bug in dual rate tariff's when rates have changed#3933

Merged
springfall2008 merged 2 commits into
mainfrom
fix/day_night_rate
May 16, 2026
Merged

Fix bug in dual rate tariff's when rates have changed#3933
springfall2008 merged 2 commits into
mainfrom
fix/day_night_rate

Conversation

@springfall2008
Copy link
Copy Markdown
Owner

No description provided.

Copilot AI review requested due to automatic review settings May 16, 2026 19:04
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 addresses incorrect rate selection for dual-rate (day/night) tariffs when Octopus returns multiple historical rates and the tariff rates have changed, ensuring the schedule uses the correct rate in force for each slot.

Changes:

  • Added a reusable rate lookup helper (_get_rate_for_time) to select the most recent applicable rate for a given timestamp.
  • Updated async_get_day_night_rates to resolve day/night rates per schedule day/slot (supporting rate changes within the synthetic 8-day window).
  • Expanded/adjusted tests to cover multiple historical rates, mid-window rate changes, and valid_to boundary behavior.

Reviewed changes

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

File Description
apps/predbat/octopus.py Adds per-timestamp rate selection helper and applies it to day/night schedule construction.
apps/predbat/tests/test_octopus_url.py Adjusts day/night test fixture timing to align with the synthetic schedule start.
apps/predbat/tests/test_octopus_day_night_rates.py Extends tests for historical rate ordering, in-window rate changes, and _get_rate_for_time edge cases.

Comment thread apps/predbat/octopus.py
Comment on lines +1237 to +1249
best_rate = None
best_valid_from = None
for rate in rates_list:
valid_from_str = rate.get("valid_from") or ""
valid_to_str = rate.get("valid_to") or ""
try:
valid_from_stamp = datetime.strptime(valid_from_str, DATE_TIME_STR_FORMAT) if valid_from_str else _EPOCH
except ValueError:
valid_from_stamp = _EPOCH
try:
valid_to_stamp = datetime.strptime(valid_to_str, DATE_TIME_STR_FORMAT) if valid_to_str else _FAR_FUTURE
except ValueError:
valid_to_stamp = _FAR_FUTURE
Comment thread apps/predbat/octopus.py
Comment on lines +1304 to +1307
if night_rate is not None:
mdata.append({"valid_from": night_start_time.strftime(DATE_TIME_STR_FORMAT), "valid_to": night_end_time.strftime(DATE_TIME_STR_FORMAT), "value_inc_vat": night_rate})
if day_rate is not None:
mdata.append({"valid_from": day_start_time.strftime(DATE_TIME_STR_FORMAT), "valid_to": day_end_time.strftime(DATE_TIME_STR_FORMAT), "value_inc_vat": day_rate})
Comment thread apps/predbat/tests/test_octopus_day_night_rates.py Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@springfall2008 springfall2008 merged commit 293af15 into main May 16, 2026
1 check passed
@springfall2008 springfall2008 deleted the fix/day_night_rate branch May 16, 2026 19:24
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