Skip to content

fix: period counters are TOTAL, not TOTAL_INCREASING - #46

Open
terafin wants to merge 4 commits into
tryfi:masterfrom
terafin:fix/state-class-total
Open

terafin wants to merge 4 commits into
tryfi:masterfrom
terafin:fix/state-class-total

Conversation

@terafin

@terafin terafin commented Oct 19, 2026 •

Copy link
Copy Markdown

Fixes #13.

steps and distance come from currentActivitySummary for the daily, weekly and monthly periods. All three reset to zero at the start of their period — local midnight for the daily pair — so TOTAL_INCREASING is the wrong state class: Home Assistant treats that as a monotonic counter and records every midnight reset as a counter reset, which shows up as an anomaly and breaks the long-term statistics.

Changing the state class alone is not enough. In components/sensor/recorder.py, a new statistics cycle starts for a TOTAL sensor only when its last_reset attribute changes; with no last_reset the first zero point is kept forever and every later reset is summed as though it were consumption. So these sensors now also publish last_reset: the start of the current day, week or month, in local time.

Sleep and nap are durations rather than period-resetting counters, so they keep their existing behaviour and report no last_reset.

Scope

Both sensors share one SensorEntityDescription per key and the entity is built per period in sensor.py, so this covers six period-scoped entities — daily/weekly/monthly × steps/distance — not just daily_distance. Issue #13 names daily_distance, but daily_steps and the weekly/monthly pairs reset the same way and have the same defect.

Test

Two tests: test_period_counters_use_total_state_class asserts both descriptions are TOTAL, and test_period_counters_report_a_last_reset asserts the daily/weekly/monthly counters return a last_reset at the start of their period while a duration sensor returns none. Both fail on the pre-fix tree.

Note on existing statistics

This corrects the state class and the reset signal going forward. Values already recorded under total_increasing may still show the old anomalies until those samples age out; that is not fixable from the integration side.

terafin and others added 4 commits October 19, 2026 15:21
`steps` and `distance` come from currentActivitySummary for the daily,
weekly and monthly periods, and all three reset to zero at the start of
their period (local midnight for the daily pair). They were declared
TOTAL_INCREASING, which Home Assistant treats as a monotonic counter: every
midnight reset is recorded as a counter reset and shows up as an anomaly in
long-term statistics, so the daily_history graphs are wrong.

TOTAL is the state class for "a running total that may reset", which is
what these actually are.

Both sensors share one SensorEntityDescription per key, so this covers all
six period-scoped entities (daily/weekly/monthly x steps/distance), not
just daily_distance.

Closes tryfi#13

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
TOTAL alone does not fix the statistics: the recorder starts a new cycle for a
TOTAL sensor only when last_reset changes, so without one the first zero point
is kept and every later midnight reset is summed as consumption. Report the
start of the current day, week or month.
TOTAL alone does not fix the statistics: the recorder starts a new cycle for a
TOTAL sensor only when last_reset changes, so without one the first zero point
is kept and every later midnight reset is summed as consumption. Report the
start of the current day, week or month.
SensorEntity.state_attributes is where Home Assistant publishes
last_reset, as an ISO string, and only for state_class TOTAL. Returning
it from extra_state_attributes bypassed that. The test now reads the
attribute Home Assistant publishes, and fails on the previous code.
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.

daily_distance has state class total_increasing, but its state is not strictly increasing

1 participant