feat: add state_class so sensors get long-term statistics - #4
Open
alexandrepossebom wants to merge 1 commit into
Open
feat: add state_class so sensors get long-term statistics#4alexandrepossebom wants to merge 1 commit into
alexandrepossebom wants to merge 1 commit into
Conversation
Not one sensor declares a state_class, so Home Assistant never records long-term statistics for any of them: the recorder purges raw history after ~10 days and nothing survives past that. Nothing warns about it - the statistics graphs are simply empty. total_mileage and trip_meter are TOTAL_INCREASING rather than MEASUREMENT: they are counters, and averaging a counter says nothing. TOTAL_INCREASING reports distance driven per period and reads the trip meter being zeroed as the start of a new cycle instead of a drop. Everything else is a point reading, hence MEASUREMENT. The three mapped sensors (engine_state, park_brake, charger_connected) are deliberately absent: they report a label, and a state class over labels means nothing - HA would also reject the entity outright should they ever gain device_class ENUM. The state classes live in a table beside the icons and the diagnostic keys rather than as an eighth column in SENSOR_SPECS, whose rows are already 150 characters wide. tests/test_state_class.py reads both tables out of the source and fails if a new sensor arrives without a state_class, if a textual one gains one, or if a pair is invalid for its device class. The valid combinations are transcribed from HA 2026.7.4; all 21 pairs were also checked against a running install. Refs nitaybz#2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2.
No sensor declares a
state_class, so Home Assistant never records long-termstatistics for any of them: the recorder purges raw history after ~10 days and
nothing survives. There is no warning — the statistics graphs are just empty.
MEASUREMENTfor the point readings (battery, range, interior and exteriortemperature, speed, average speed, time to full charge, 12V battery and
voltage, average consumption, tyre pressures, days and distance to service).
TOTAL_INCREASINGfortotal_mileageandtrip_meter. They are counters;averaging a counter says nothing, and
TOTAL_INCREASINGreads the trip meterbeing reset to zero as the start of a new cycle instead of a drop.
engine_state,park_brake,charger_connected): they report a label, and statistics over labels meannothing. Should they ever gain
device_classENUM, HA rejects an entity thatdeclares a state class anyway (
DEVICE_CLASS_STATE_CLASSESmaps ENUM to theempty set), so the test below guards that direction too.
The table sits next to the icon and diagnostic tables rather than becoming an
eighth column in
SENSOR_SPECS, whose rows are already 150 characters wide.tests/test_state_class.pyreads both tables out of the source withast, so anew sensor added without a state_class fails the suite, as does a textual sensor
that gains one or a pair that is invalid for its device class. The allowed
combinations are transcribed from
DEVICE_CLASS_STATE_CLASSESin HA 2026.7.4,and the resulting entities were checked on a running install. It has no
dependencies —
python3 tests/test_state_class.pyruns it.The change is additive: no entity id, unique id or unit changes. Statistics
simply start being recorded from the upgrade on.