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:

  icon: mdi:bike
  state_class: total_increasing

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

In configuration.yaml:

  - 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 statistics graph card allows you to display statistics data for other sensors that support it.

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

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.