Fix bug in dual rate tariff's when rates have changed#3933
Merged
Conversation
Contributor
There was a problem hiding this comment.
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_ratesto 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_toboundary 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 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 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}) |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.