Benefit from long term statistics for any sensor

Recent home-assistant versions (2021.9) brought the ability to benefit from long-term statistics.

However, it only works for sensors that have a state_class of measurement, total or total_increasing. Some regular sensors (command line sensors for instance) don’t configure state_class by default and don’t allow you to do it.

There are two solutions.

Solution1: use customize.yaml

This works for command line sensors (at least). In customize.yaml, you can specify a sensor to have a state_class. Example:

sensor.bicycle_distance:
  icon: mdi:bike
  state_class: total_increasing

Solution 2: create a modern template sensor based on your sensor

In configuration.yaml:

template:
  - sensor:
      - name: Total cycled meter
        unit_of_measurement: "m"
        state_class: total_increasing
        state: >
          {{ states('sensor.bicycle_distance')|int }}

Using statistics

The energy dashboard automatically draws on statistics data to draw its charts. The cards can be used individually in your own dashboards.

In addition, the statistic card allows you to display min max and mean values for an entity.

And the statistics graph card allows you to display statistics data for sensors that support it.

Some custom cards, like Plotly, will do the same:

Starting with release 2023.12, the history dashboard combines state history with long-term statistics.

The darker part of the graph line on the right is provided by the state history, and the long-term statistics provide the lighter part of the graph line on the left.

For editors: don’t hesitate to adapt this based on the most recent stable version of home-assistant, it seems this evolves with each version.

11 Likes