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…
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…
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:
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)
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.