How to add "running hours" to long-term statistics [SOLVED]

I’m still a bit confused with the long-term statistics.

Some pumps and several heating devices I’m tracking the amount of usage. For this I created some history_stats sensors. For example “stats_pump1_today” counts the amount of time the pump was running today. I would like to include these values in the long term statistics to enable to trend usage over longer periods of time. I checked the long-term statistics page but it seems there is no one for time/running time. Is it possible to enable this?

Also, the long term stats are stored in the database but discards the purge period (of course), but is it ever purged? And is it possible to trend this data with other (custom) cards, so far I only had success with the default history-stats card. I would really like an equivalent to the energy dashboard with date selectors, week/month totalizers, etc…

+1
I need this for my heating burner… Any Idee?

Is it possible to play with state_classes while keeping the unit of measurement the same?
Or is it just not possible (yet) and the unit “running time” must be added first?

Hi, I am struggling with the same problem. Dit you get any further? I made a binary_sensor that is set when the pump is turned on. Then I made a statistics sensor that calculates the time the sensor is on that day. But if I use that in the statistcs card the rsult seems cumulative

  - platform: template
    sensors:
      cv_activiteit:
        friendly_name: "cv ketel aan"
        value_template: >
          {{ is_state_attr("climate.nest_thermostaat", "hvac_action", "heating") }}

  - platform: template
    sensors:
      cv_activiteit:
        friendly_name: "cv ketel aan"
        value_template: >
          {{ is_state_attr("climate.nest_thermostaat", "hvac_action", "heating") }}

Hi, I might have solved the problem. It might not be exactly how you want it but it does show me per hour how long the pump (in my case Central Heating ) as wel as per day. It builds on my previous post.
I’ll try to make a complete and consistant narrative.

First I constructed a binary sensor that is on when the pump is active:

  - platform: template
    sensors:
      cv_activiteit:
        friendly_name: "cv ketel aan"
        value_template: >
          {{ is_state_attr("climate.nest_thermostaat", "hvac_action", "heating") }}

With this I built a history stat sensor

- platform: history_stats
    name: CV ketel uur
    entity_id: binary_sensor.cv_activiteit
    state: "on"
    type: time
    start: "{{ now().replace(minute=0, second=0) }}"
    end: "{{ now() }}"
  - platform: history_stats
    name: CV ketel dag
    entity_id: binary_sensor.cv_activiteit
    state: "on"
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now() }}"
    
  - platform: history_stats
    name: CV ketel week
    entity_id: binary_sensor.cv_activiteit
    state: "on"
    type: time
    start: "{{ as_timestamp( now().replace(hour=0, minute=0, second=0) ) - now().weekday() * 86400 }}"
    end: "{{ now() }}"


This will calculate the amount of time the Central Heating is on in per clock_hour, day, week
This is at least the data that you want but the data gest lost every hour, day or week

Then I created a helper “cv aan”
and an automation which containes the value hist stats sensor.
Finally I made an automation that copies the value of the hist sta sensor in the helper.

alias: CV tik
description: vlak voor elk uur wordt de waarde sensor.cv_ketel_uur gekopieerd
trigger:
  - platform: time
    at: '00:59:50'
  - platform: time
    at: '01:59:50'
  - platform: time
    at: '02:59:50'
  - platform: time
    at: '03:59:50'
  - platform: time
    at: '04:59:50'
.
.
.
  - platform: time
    at: '22:59:50'
  - platform: time
    at: '23:59:50'
condition: []
action:
  - service: input_number.set_value
    data_template:
      entity_id: input_number.cv_aan
      value: '{{ states(''sensor.cv_ketel_uur'') }}'
mode: single

Actually I had another solution. Requirements for long term statistics are described here.

As duration or any device class exists using a time (minutes/hours) as supported unit I abused the aqi device class. I create a template sensor with this class and then it will be picked up by the statistics “engine”.

HOWEVER!!! Something changed. Currently my original timer is also part of the statistics :slight_smile:
Shown as device class DURATION and unit_of_measurement: h.
Although not in the documentation (yet) it appears to be a new device class specifically for this issue :slight_smile:


Containing both original (now working) and previous tricked “aci” template version called longterm…

image

1 Like

Here is what I use to have long term statistic about a duration of some binary sensor:

- name: "Heating duration"
  unique_id: solary_ohrev_doba_id
  device_class: duration
  unit_of_measurement: min
  state_class: total_increasing
  state: >
    {% if is_state('binary_sensor.solary_ohrev', 'on') %}
      {{ (as_timestamp(now()) - as_timestamp(states.binary_sensor.solary_ohrev.last_changed))/60 }}
    {% else %}
      {{ this.state }}
    {% endif %}

The duration is in minutes.

My above way of tracking long term duration statistics is not reliable - especially when you restart Home Assistant. I use workaround based on utility meter now - see history_stats sensor does not reset until slightly after midnight for the next day · Issue #75903 · home-assistant/core · GitHub