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

Hi.
Is it possible to superimpose on the same graph statistical card sensor data with different units? I would love to see my energy consumption history (from long term statistics) superimposed with the external temperature.
Thanks.

I don’t think so, different entities are presented on different graphs.

I am personally using metric export to datadog to explore metrics in an interface dedicated to that. Others do the same with grafana.

Most of the energy companies propose that in their Dashboard because it makes sense to understand our consumption.
It’s a shame we are not able to do that natively in HA.