Skip to content

Count hours and price points from local midnight, not the wall clock - #1309

Merged
gskjold merged 1 commit into
mainfrom
fix/dst-price-index-hour-accounting
Nov 5, 2026
Merged

gskjold merged 1 commit into
mainfrom
fix/dst-price-index-hour-accounting

Conversation

@gskjold

@gskjold gskjold commented Nov 5, 2026

Copy link
Copy Markdown
Member

Why

On the day DST ends, local 02:00–03:00 happens twice and the day has 25 hours. On the day it starts, 02:00 is skipped and the day has 23 hours. Price point and hour of day were both taken from the wall clock (tm.Hour), which doesn't count those hours correctly.

#1296: prices one hour off on 2026-10-25. getCurrentPricePointIndex() returned (tm.Hour*60 + tm.Minute) / resolution in CET. After the change, the wall clock is one hour behind the time elapsed since CET midnight, so the cursor was 4 points (1 hour) short. The reporter's cursor: 56 at about 14:00 CET should have been 60. The UI starts the price plot at the cursor, so every price showed one hour late. The same index is used for getCurrentPrice() (MQTT current price, hourly cost), and getPriceForRelativeHour() had the same flaw. On the spring day it would run 4 points ahead instead. #1297 only fixed the day and month plot labels in the UI, not this.

#1299: hour accounting at fall-back.

  • EnergyAccounting::update() started a new hour only when local.Hour changed. Going from 02 to 02 isn't a change, so use/costHour ran on for two hours (26.44 kWh in the report), and the first 02 hour was never passed to updateMax(), so it was never checked as a monthly peak.
  • getUseToday()/getProducedToday()/calcDayCost() found each hour of the day with now - (local.Hour - i)*3600. That assumes local.Hour hours have passed today, so a 25-hour day lost its first hour, and a 23-hour day would include 23:00Z from the day before.

ENTSO-E fetch window. fetchPrices() computed local midnight as t - tm.Hour*3600 - … and the end as + SECS_PER_DAY. Fetching tomorrow's prices on 2026-10-24 requested 25 October from 23:00Z (should be 22:00Z) and always asked for 24 hours. This only affects devices that use their own ENTSO-E token.

Changes

  • src/LocalDay.h (new, header-only): localMidnightUtc(), nextLocalMidnightUtc() and hoursSinceLocalMidnight(), all based on elapsed time from local midnight.
  • PriceService: current price point index, getPriceForRelativeHour() and the ENTSO-E periodStart/periodEnd now use these helpers. getPricePoint()/getFixedPrice() already measured from midnight in elapsed time, so they are unchanged.
  • EnergyAccounting: currentHour now counts hours since local midnight, so it reaches 24 on a 25-hour day. Hour rollover, the day totals and calcDayCost() all use it.
    • On normal days the values are the same as before, and the layout of EnergyAccountingRealtimeData is unchanged, so data kept over a reboot or upgrade stays valid.
    • Peak entries are still labelled with the local hour, so the two 02 hours give two peaks labelled 02.
  • test/stubs/Timezone.h: now applies DST rules the same way the Arduino Timezone library does. Without DST rules it behaves exactly as before; the decoder tests only probe mid-January.
  • New native_localday env (test/test_local_day/), added to both CI workflows and to CLAUDE.md.

Verification

  • pio test -e native_localday: 9 tests on 2026-10-25 (25 h) and 2027-03-28 (23 h), for CET and EET:
  • pio test -e native, native_config and native_watchdog all pass. The decoder golden master is unchanged.
  • pio run for esp8266 (98.5% flash), esp32s2 and esp32: all build.
  • Not verified on a device. The test covers the helper functions; PriceService and EnergyAccounting can't run on native. The call sites were checked by review. Real confirmation comes with the next change on 2027-03-28.

Refs #1296, #1299

🤖 Generated with Claude Code

On the day DST ends the local hour 02 repeats, and on the day it starts
02 is skipped. Price point and hour of day were taken from the wall
clock, so on those days:

- the current price point was 4 points (one hour) behind after the
  change in autumn and ahead in spring, shifting the price shown, the
  MQTT current price and the hourly cost (#1296)
- the repeated hour was never closed: the hour accumulator ran on for
  two hours and the first one was never checked as a monthly peak (#1299)
- today's use/production summed the wrong UTC hours, missing the first
  hour of a 25 hour day or including one from the day before (#1299)
- the ENTSO-E request window for a DST day started an hour late and
  always covered 24 hours

Add LocalDay.h, which gives local midnight and the hours elapsed since
it, and use it in PriceService and EnergyAccounting. currentHour now
counts elapsed hours, so it reaches 24 on a 25 hour day; on other days
it has the same values as before and the realtime data layout is
unchanged.

The native Timezone stub now applies DST rules like the Arduino Timezone
library, and the new native_localday env tests 2026-10-25 and 2027-03-28.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@gskjold gskjold added the bug Something isn't working label Nov 5, 2026
@gskjold gskjold self-assigned this Nov 5, 2026
@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown

🔧 PR Build Artifacts

Version: 2760b70

All environments built successfully. Download the zip files:

Artifacts expire after 7 days. View workflow run

@gskjold
gskjold merged commit cc3a84f into main Nov 5, 2026
15 of 16 checks passed
@gskjold
gskjold deleted the fix/dst-price-index-hour-accounting branch November 5, 2026 15:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant