Issue with history stat

Hi all, I have a problem with a “history stat” platform which I set up to measure the amount of hours a binary sensor is active over time (its active when my oil burner is active). I have set up 2, one that resets every day and one that should just count and never reset. The daily one never fails, but the one that should just count always behaves very strange. Not always but often when I restart home assistant, it counts a massive peak of hours and I have no idea why.

Below you can see all the spikes upwards in the past week it made:

The daily one never does this. I also tried adding a unique Id to the one with the issue, thinking it might help, but it didn’t. Any idea’s why it is doing this?

Below you can find how I set up the daily and the one that never resets:

  - platform: history_stats
    name: Brander Active Time Daily
    entity_id: binary_sensor.doorcontact_technischeruimte_mazoutketelurenteller_contact
    state: "off"
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now() }}"

  - platform: history_stats
    name: Brander Active Time All Time
    entity_id: binary_sensor.doorcontact_technischeruimte_mazoutketelurenteller_contact
    state: "off"
    type: time
    start: "2024-10-01 00:00:00"
    end: "{{ now() }}"
    unique_id: 11566f00-3613-4ab3-820a-e4ddbb3653db

History Stats relies on the Recorder integration, so it can only count the"history" to the limit of the recorder’s purge. By default, recorder purges values that are 10 days old at 4:12am every day.

I don’t really need historical data in this case… I just need the sensor to add up whenever the binary sensor is active. So I don’t need data from 10 days ago unless I would want to count everything in the past, which is not what I want to achieve here.

I just need 1 sensor that adds up time when the binary sensor is on. Every time it updates, it goes up. So I’m not sure what it has to do with the recorder integration and how it would explain the spikes when home assistant restarts

If anyone would have an idea on how to get this to work properly that would be appreciated, I tried about everything I could think of. Either it is is not counting correctly or I get these spikes as explained above…

Make an automation if you don’t need historical data.

can you elaborate a bit? I don’t need historical data, just a total timer that keeps increasing

Make an automation that increments a counter helper every minute if the switch is on.

I assume this is only accurate up to the minute in that case? So if it’s active only 1 second in the counted minute, it’s still going to count a minute?

Might not seem like much but if that error accumulates over an entire year, the estimated consumption can be quite a bit off.

I can’t get around the fact that there is no easy way to just have a simple and accurate time counter for an active binary sensor…

There is, it’s history stats. If you set it to count just today, you’ll get all the other statistics with it using statistics cards in the frontend.

You’re trying to create something that has already been solved but you’re trying to do it your way, not HAs intended way.

Ok, now wait a couple of months with that daily entity, and put it in a statistics card, you can choose yearly, monthly, weekly totals if you want.

You may need to create it in the UI to get it to track statistics properly to see the totals over a period of time.

I’m not trying to do it “my way”, I’m asking for help here, “my way” did not work. My only goal is to have a single counter based on which I can launch other calculations like consumed liters of fuel.

The only thing what worked so far is to create a history stat for a daily counter. Which works without bizare spikes etc:

sensor:
* platform: history_stats
name: Brander Active Time Daily
entity_id: binary_sensor.doorcontact_technischeruimte_mazoutketelurenteller_contact
state: “off”
type: time
start: “{{ now().replace(hour=0, minute=0, second=0) }}”
end: “{{ now() }}”

This I used to have a visual graph of active hours per day:
image

This is fine, and no complaints there. All I now want is a counter that keeps track of the total time it was on (or off in this case, since it works inverted due to the fact I use a door sensor to have the signal) so I can do other calculations on it like this:

  - platform: history_stats
    name: Brander Active Time All Time
    entity_id: binary_sensor.doorcontact_technischeruimte_mazoutketelurenteller_contact
    state: "off"
    type: time
    start: "2024-10-01 00:00:00"
    end: "{{ now() }}"
    unique_id: 11566f00-3613-4ab3-820a-e4ddbb3653db

template:
  - sensor:
      - name: "Mazout Verbruik in Liters"
        unit_of_measurement: "L"
        state: >
          {{ (states('sensor.brander_active_time_all_time') | float(default=0) * 4.12) | round(1) }}
        unique_id: 350b98b2-ee69-4279-92f6-76d3c7638fa9

      - name: "Mazout Verbruik in kWh"
        unit_of_measurement: "kWh"
        state: >
          {{ (states('sensor.brander_active_time_all_time') | float(default=0) * 40) | round(1) }}
        device_class: energy
        state_class: total_increasing
        unique_id: c80e9b43-1759-47b3-a2f5-a76236aa64b8

This history stat, does not work since it spikes every time HA is restarted.

Now, how can I use HA’s intended way to calculate a live consumption in Liters and kWh as calculated above? So I can see at any given time what the consumed liter fuel (and use the kwh counter for my energy dash)

Create a daily consumption for both of those and let HA handle the statistics for longer durations.

Statistics are built into every numerical sensor that has a state_class. Utilize that to get the values you want.

What do you mean? Leave out the second “all time” platform? or how do you suggest to set it up?

Don’t include any all time sensors. Only make daily sensors that calculate liters and kWh. Make sure each entity has a device_class, state_class, and valid unit of measurement. HA will naturally collect statistics for those entities. You can then use statistics cards or graphs in the frontend to see any duration of data that you’ve collected.

Something like this?

# History statistics sensor voor het meer precies tellen van actieve tijd brander
sensor:
  - platform: history_stats
    name: Brander Active Time Daily
    entity_id: binary_sensor.doorcontact_technischeruimte_mazoutketelurenteller_contact
    state: "off"
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now() }}"

## Mazoutketel verbruik
# Template sensoren voor mazoutverbruik
template:
  - sensor:
      - name: "Mazout Verbruik in Liters"
        unit_of_measurement: "L"
        state: >
          {{ (states('sensor.brander_active_time_daily') | float(default=0) * 4.12) | round(1) }}
        device_class: volume
        state_class: total_increasing
        unique_id: 350b98b2-ee69-4279-92f6-76d3c7638fa9

      - name: "Mazout Verbruik in kWh"
        unit_of_measurement: "kWh"
        state: >
          {{ (states('sensor.brander_active_time_daily') | float(default=0) * 40) | round(1) }}
        device_class: energy
        state_class: total_increasing
        unique_id: c80e9b43-1759-47b3-a2f5-a76236aa64b8

Yep, then HA will build statistics as time marches on

1 Like

Ok, I will try and see what happens

So yeah it’s not working… The sensor is just resetting, just like the daily hour counter:

Same with the kWh counter:

I also used the statistics tool as you suggested and put it to “this year”, it is still showing only the liters of today:

Wait for a second day of data, it’ll work. It hasn’t been enough time.