fix(teslemetry): correct _tesla_dow - Tesla's day-of-week is Monday=0, not Sunday=0 - #4611
Merged
Conversation
…, not Sunday=0 _tesla_dow() mapped Python's weekday() (Monday=0) to a wrongly-assumed Tesla convention of Sunday=0, via (python_weekday + 1) % 7. Tesla's tariff_content_v2 fromDayOfWeek/toDayOfWeek actually use the same Monday=0 convention as datetime.weekday(), so every ON_PEAK boost band (and the day's real-tier layout) landed one day late. During the actual export window the Powerwall saw only the ordinary off-peak tariff, so it never had a price reason to export - it just covered house load, which is why every prior symptom (load-following with grid=0 during "Exporting", raising the boost price changing nothing) looked like something else. optimization_strategy (#4600) was a red herring; this is the actual cause. Fixed by making _tesla_dow the identity function. Test changes pin absolute expected day indices instead of deriving them from _tesla_dow itself (the previous self-referential pattern passed under any mapping), plus a new resolver-style test that independently resolves the built tariff's price at a moment inside the boost window using Tesla's real day convention. Confirmed live: correcting only the day index took a Powerwall from grid=0 to grid=-5156 (full 5kW export) within 50 seconds, no other change. Fixes #4610 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes Teslemetry’s Tesla tariff day-of-week mapping so that fromDayOfWeek/toDayOfWeek align with Tesla’s Monday=0 convention (matching datetime.weekday()), preventing export/boost windows from being written one day late.
Changes:
- Updated
TeslemetryAPI._tesla_dow()to be an identity mapping (Python weekday already matches Tesla’s convention). - Refactored existing tests to avoid self-referential expectations derived from
_tesla_dow(). - Added targeted regression tests to ensure boost tiers resolve on the correct Tesla day index.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| apps/predbat/teslemetry.py | Corrects Tesla DOW mapping and documents the convention explicitly. |
| apps/predbat/tests/test_teslemetry.py | Makes DOW assertions independent of _tesla_dow() and adds resolver-style regression coverage for the boost window. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Collect every matching tier instead of returning the first, so an overlapping-period regression would fail the test instead of silently passing depending on dict insertion order; and assert the resolved price is genuinely the boosted maximum instead of a tautological self-comparison. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.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.
Summary
_tesla_dow()mapped Python'sweekday()(Monday=0) to a wrongly-assumed Tesla convention of Sunday=0, via(python_weekday + 1) % 7. Tesla'stariff_content_v2fromDayOfWeek/toDayOfWeekactually use the same Monday=0 convention asdatetime.weekday().grid=0during "Exporting", raising the boost price changing nothing (it was written to the wrong day), an overnight window importing at the cheap rate while sitting at 97% SoC.optimization_strategywas a red herring - this is the actual cause._tesla_dowthe identity function - Python'sweekday()already matches Tesla's convention.Testing
today_dow = api._tesla_dow(api.base.now.weekday())), so they passed under any mapping and never caught this. Fixed to pin absolute expected indices, computed independently of_tesla_dow(bypassing it entirely, e.g.api.base.now.weekday()/api._local_today_weekday()directly).test_teslemetry_tesla_dow_sunday_zero(which asserted the wrong Sunday=0 behaviour) withtest_teslemetry_tesla_dow_matches_python_weekday.test_teslemetry_build_tariff_boost_resolves_at_the_real_tesla_day_index: a resolver-style regression that independently resolves the built tariff's price at a moment inside the boost window using Tesla's real day convention, and asserts the boosted ON_PEAK price applies - the property that actually matters, and the one that would have caught this../run_all --quickand./run_pre_commitboth pass.grid=0togrid=-5156(full 5kW export) within 50 seconds, no other change.Fixes #4610
Test plan
./run_all --quick./run_pre_commit🤖 Generated with Claude Code