Skip to content

feat: add state_class so sensors get long-term statistics - #4

Open
alexandrepossebom wants to merge 1 commit into
nitaybz:mainfrom
alexandrepossebom:feat/state-class
Open

feat: add state_class so sensors get long-term statistics#4
alexandrepossebom wants to merge 1 commit into
nitaybz:mainfrom
alexandrepossebom:feat/state-class

Conversation

@alexandrepossebom

Copy link
Copy Markdown

Fixes #2.

No 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. There is no warning — the statistics graphs are just empty.

  • MEASUREMENT for the point readings (battery, range, interior and exterior
    temperature, speed, average speed, time to full charge, 12V battery and
    voltage, average consumption, tyre pressures, days and distance to service).
  • TOTAL_INCREASING for total_mileage and trip_meter. They are counters;
    averaging a counter says nothing, and TOTAL_INCREASING reads the trip meter
    being reset to zero as the start of a new cycle instead of a drop.
  • Nothing for the three mapped sensors (engine_state, park_brake,
    charger_connected): they report a label, and statistics over labels mean
    nothing. Should they ever gain device_class ENUM, HA rejects an entity that
    declares a state class anyway (DEVICE_CLASS_STATE_CLASSES maps ENUM to the
    empty 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.py reads both tables out of the source with ast, so a
new 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_CLASSES in HA 2026.7.4,
and the resulting entities were checked on a running install. It has no
dependencies — python3 tests/test_state_class.py runs it.

The change is additive: no entity id, unique id or unit changes. Statistics
simply start being recorded from the upgrade on.

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

Sensors have no state_class, so Home Assistant never records long-term statistics

1 participant