Skip to content

fix(history): recognise a daily counter's reset at local midnight - #6519

Open
piomar123 wants to merge 2 commits into
springfall2008:mainfrom
piomar123:fix/minute-data-local-midnight
Open

piomar123 wants to merge 2 commits into
springfall2008:mainfrom
piomar123:fix/minute-data-local-midnight

Conversation

@piomar123

@piomar123 piomar123 commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

What this fixes

Wherever local midnight isn't 00:00 UTC, a daily *_today counter that ends a day below 1 kWh loses energy the next day. That includes every UK user during British Summer Time (local midnight = 23:00 UTC). Every daily counter read through minute_data(..., clean_increment=True) is affected (export_today, import_today, pv_today, load_today, iBoost and car charging energy, the ML load model's and Solcast's history), so on a dark day it hits PV and on most days export.

Why:

  • minute_data() with smoothing and clean_increment treats a drop as the counter's daily reset only if the drop is at least 1 kWh or the record is "near midnight" (Remove Minimum Drop for "Incrementing" Data near Midnight #3012).
  • "Near midnight" was read from last_updated_time.time(). Home Assistant stamps history in UTC, so in CET the reset at 00:00:03 local arrives as 23:00:03 and doesn't count as near midnight.
  • The drop then falls through to the "small dip" branch, which flattens the new reading to the previous day's total. Every reading after it stays flat until it passes that total. Because the flattened value becomes last_state, it also carries into the following day.

Example from a GoodWe export_today sensor in Europe/Warsaw (daily totals, Home Assistant vs Current data so far today in the Predbat log at 23:55):

Day Home Assistant Predbat
28 Nov 0.27 0.27
29 Nov 0.02 0.00
30 Nov 2.80 2.53
1 Dec 0.67 0.67
2 Dec 0.07 0.00
3 Dec 2.29 1.62

This is the separate minute_data() reset item noted (untested) at the end of #6083; the other items there are not part of this PR.

On 3 Dec the first 0.67 kWh of an evening export (1 Dec's total, carried through 2 Dec) was lost. The days after a total of at least 1 kWh (28 Nov, 1 Dec) were fine.

What changes

  • A small drop counts as the reset when it is near midnight on the local clock (now's time zone, the local zone Predbat passes as now_utc) or on UTC, as before. Counters that reset on UTC midnight (an inverter clock without DST) keep working. The check is only worked out for a drop, so the common path costs nothing extra.
  • Nothing else in the drop handling changes: a drop of 1 kWh or more is still a reset at any hour, and a small dip away from midnight on both clocks is still flattened.
  • Not covered (unchanged): a reset whose first recorded value after midnight is already above 0 (the 0 itself not recorded) is still missed by clean_incrementing_reverse, for UTC users too.

Tests

  • New minute_data_local_midnight (tests/test_minute_data_local_midnight.py):
    • a counter resetting at local midnight in CET from 0.67 kWh, 0.61 kWh counted since (was 0.0);
    • the same day in UTC (unchanged, already worked);
    • Europe/London in summer time, resetting at local midnight (was 0.0) and at UTC midnight (01:00 local, still works);
    • America/New_York (local midnight 05:00 UTC, was 0.0);
    • a glitch to 0 at midday is still ignored (fails if the near-midnight window is widened).
  • ./run_all --quick passes.

🤖 Generated with Claude Code

minute_data decided whether a drop in an incrementing sensor was the daily reset by
the record's own clock, which Home Assistant stamps in UTC. Ahead of UTC (CET: local
midnight = 23:00 UTC) a reset from below 1 kWh then looked like a small dip and was
flattened to the previous day's total, so the new day's readings counted only once
they passed it, and the flattened value carried on into the next day. A daily export
counter lost 0.67 kWh of an evening export this way. Judge "near midnight" on the
local clock of `now` instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the BOT_REVIEW Trigger an autotriage label Dec 4, 2026

@springfall2008 springfall2008 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated comment from the triage bot.

Verdict: 🟡 Amber - Recommend addressing the feedback, then approval

One sub-test is ineffective; the fix itself is correct. Reviewed at head ed237ce (1 inline finding, 0 in the body).

The change is the right root-cause fix: HA stamps history in UTC, a daily counter resets at local midnight, and near_midnight is now judged on now's own zone (now.tzinfo, which update_time() sets to local_tz). This is exactly the "zone-aware midnight window in minute_data" item that issue #6083 has been waiting on, and it agrees with gecloud's EMS near-midnight check, which already uses local now_utc_exact. last_updated_time always comes from str2time(), so it is tz-aware and astimezone() cannot raise; the naive-now path falls back to the old behaviour as the description says. Sub-test 1 is genuinely red without the change; sub-test 3 is not (see the inline note).

@springfall2008 springfall2008 removed the BOT_REVIEW Trigger an autotriage label Dec 4, 2026
… more zones

Review follow-up:
- A drop counts as a reset near midnight on the local clock or on UTC, so a counter
  that resets on UTC midnight (an inverter clock without DST, e.g. UK summer time at
  01:00 local) still works as it did before. Worked out only for a small drop.
- The test's dip case passed with or without the fix. It is replaced by a glitch to 0
  at midday, which fails if the window is too wide. Adds Europe/London in summer time
  (reset at local and at UTC midnight) and America/New_York, with pytz zones as Predbat
  passes them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

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

2 participants