Skip to content

fix(teslemetry): correct _tesla_dow - Tesla's day-of-week is Monday=0, not Sunday=0 - #4611

Merged
springfall2008 merged 3 commits into
mainfrom
fix/teslemetry-dow-4610
Aug 20, 2026
Merged

fix(teslemetry): correct _tesla_dow - Tesla's day-of-week is Monday=0, not Sunday=0#4611
springfall2008 merged 3 commits into
mainfrom
fix/teslemetry-dow-4610

Conversation

@springfall2008

Copy link
Copy Markdown
Owner

Summary

  • _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().
  • Every ON_PEAK boost band (and the day's real-tier layout) therefore 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.
  • This explains every symptom chased in Teslemetry: Powerwall never exports — tariff pushed without optimization_strategy, so TBC stays in Balanced #4600: load-following with grid=0 during "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_strategy was a red herring - this is the actual cause.
  • Fix: make _tesla_dow the identity function - Python's weekday() already matches Tesla's convention.

Testing

  • The existing day-of-week tests were self-referential (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).
  • Replaced test_teslemetry_tesla_dow_sunday_zero (which asserted the wrong Sunday=0 behaviour) with test_teslemetry_tesla_dow_matches_python_weekday.
  • Added 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 --quick and ./run_pre_commit both pass.
  • Confirmed live (see linked issue): 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

Test plan

🤖 Generated with Claude Code

…, 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>
Copilot AI lite review requested due to automatic review settings August 20, 2026 06:59

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 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.

Comment thread apps/predbat/tests/test_teslemetry.py Outdated
Comment thread apps/predbat/tests/test_teslemetry.py Outdated
springfall2008 and others added 2 commits August 20, 2026 08:07
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>
@springfall2008
springfall2008 merged commit bc343af into main Aug 20, 2026
2 checks passed
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.

Teslemetry: forced export never works — _tesla_dow writes the boost band one day late (Tesla uses Monday=0)

2 participants