Skip to content

RCE on DST days: period labels from PSE's UTC fields - #54

Merged
piomar123 merged 1 commit into
mainfrom
fix/rce-dst-order
Oct 25, 2026
Merged

piomar123 merged 1 commit into
mainfrom
fix/rce-dst-order

Conversation

@piomar123

Copy link
Copy Markdown
Owner

Problem

On DST days, the start of PSE's period text doesn't tell you when the quarter starts. The text is a correct interval, but we only used its start as the label:

PSE period period_utc Starts (Warsaw) Old label Read as
02:45 - 03:00 00:45 02:45 CEST 02:45 02:45 CEST ✓
03:00 - 02a:15 01:00 02:00 CET (= 03:00 CEST) 03:00 03:00 CET ✗ (an hour late)
02a:15 - 02a:30 01:15 02:15 CET 02a:15 ✓
02a:45 - 03a:00 01:45 02:45 CET 02a:45 ✓
03a:00 - 03:15 02:00 03:00 CET 03a:00 sorted after 03:45 ✗
spring: 02:00 - 03:15 01:00 03:00 CEST 02:00 right only by accident (fold=0 on a nonexistent time)

rce_storage.get_cached_prices orders by the label as text. On 2026-10-25 the export bands sent to Predbat for 02:00-04:00 were scrambled:

  • a 75-minute band (02:00+02:00 → 02:15+01:00);
  • a zero-length band (03:00+01:00 → 03:00+01:00);
  • 03:00-04:00 priced from a single quarter.

Every band from 04:00 on was correct (checked against PSE by dtime_utc). The import tariff was flat all that Sunday, so in practice nothing went wrong.

Fix

  • rce.convert_to_series_15min now builds the label from dtime_utc (end of the period) minus the period_utc length, converted to Europe/Warsaw. The repeated hour gets HHa:MM (fold=1). Labels are 02:45, 02a:00, ..., 02a:45, 03:00. They sort chronologically as text, and export_price._parse_period_start already reads them. The existing export_price DST tests already assumed this shape. The query also selects period_utc,dtime_utc.
  • rce_storage.init_db relabels DST days that are already in the cache, once (PRAGMA user_version 0 → 1). This is done in place, not by dropping and re-fetching, because export_price only reads the cache. Dropping today would leave Predbat without export prices until midnight. The version is re-checked under BEGIN IMMEDIATE, so two threads can't relabel twice.
    • Fall-back day: 03:00 → 02a:00, 03a:00 → 03:00.
    • Spring day (has 02:00 but no 03:00): 02:00 → 03:00.

Tests

  • The real PSE responses for 2026-10-25 (100 quarters) and 2026-03-29 (92) are saved as fixtures in tests/fixtures/.
  • The fixtures go end to end, convert → cache → Predbat bands, at both granularities:
    • every band is 15 or 60 min long;
    • bands are contiguous;
    • fall-back day: 100 / 25 bands, spring day: 92 / 23 bands;
    • the night-hour values match PSE's UTC hours.
  • Migration tests: an old-format fall-back day, a spring day and a normal day; a second init_db doesn't relabel again; a new DB starts at the current version.
  • The 9 new tests fail on main. The full suite passes on Python 3.14 (606 passed).
  • I ran the migration on a .backup snapshot of the Pi's rce_prices.db. 2026-10-25 comes out as 100 / 25 contiguous bands. 02:00-03:00 CET = 0.4837 (mean 393.28 × 1.23) and 03:00-04:00 CET = 0.4244 (345.06 × 1.23), both matching PSE.

Not in this PR

On the fall-back day, inverter history timestamps still come from the inverter clock (naive, fold=0), so one hour of history is ambiguous. That needs a separate change: stamp samples with the Pi's NTP clock.

🤖 Generated with Claude Code

PSE's `period` text is a correct interval, but its start alone is
misleading on DST days: the fall-back day's first repeated quarter is
"03:00 - 02a:15" (03:00 CEST), the 03:00 CET quarter is
"03a:00 - 03:15", and the spring-forward day has "02:00 - 03:15".
Using that start as the label read the first repeated quarter an hour
late and sorted "03a:00" after "03:45", so on 2026-10-25 the export
bands for 02:00-04:00 were scrambled.

Labels now come from dtime_utc/period_utc converted to Warsaw time
('02a:00'..'02a:45' for the repeated hour), which sort chronologically
as text. rce_storage relabels DST days already in the cache once
(PRAGMA user_version 1), in place, since export_price only reads the
cache. Tests use the real PSE responses for 2026-10-25 and 2026-03-29.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@piomar123
piomar123 merged commit f331281 into main Oct 25, 2026
1 check 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.

1 participant