Problem with History Stats

I’m starting using a history stat sensor to measure my gas consumption. I don’t have a ‘smart meter’ yet, so i thought I could measure the time that the flame of my central heating system is on. I’m aware this is not very accurate, but at least it give me a rough indication.

However, the history stat is acting strange. At some moments you can see the value increase as the flame is on, but sometimes there is a sudden jump in the value. I’m clueless that causes this.

Code in sensor.yaml:

- platform:     history_stats
  name:         "stat_otgw_flame"
  entity_id:    binary_sensor.otgw_flame
  state:        "on"
  type:         time
  start:        "{{ now().replace(year=2022, month=5, day=14, hour=23, minute=59, second=0) }}"
  end:          "{{ now() }}"

(The starting point is some moment in the past where I noted down the meter reading. I’m curious if I can roughly predict future meter readings)

Here is an example:

(click here for larger image)

I had a very similar problem (see link to it, below.)

I thought I’d proven that it only happens when the underlying binary sensor data had rolled off of the Recorder database after the purge_keep_days but that doesn’t seem to be your situation. I also can’t be sure when it started happening, since it’s fairly infrequent.

There is certainly something wrong with history_stats. My post didn’t get any response. I hope you have better luck with this one.

I just found out that it happens when I restart home assistant. I had already seen in the history of the binary sensor that it occured when there were 2 ‘off’ signals in a row. Then it occured to me that I might have restarted on these moments. And when I restarted again, it indeed happened again.
Seems like a bug waiting to be fixed.

1 Like

Thanks, the more examples we can post here the closer we might get to understanding the problem.

I am sure restarting didn’t cause it the last time I saw it. I only restart HA about this time every month, when I do updates. My HA hardware is on a UPS and doesn’t get restarted in between updates even if there’s a power outage. The problem last occurred about 5-6 days ago, when HA had been running for about three weeks.

Of course that’s not to say restarting won’t make it happen again for me. I’ll be updating within the next few days and I’ll watch for this symptom.

I was also reminded (over on that other thread I linked to in post #2, above) that there’s an issue open on Github about this, too:

We probably should all update that thread, so our observations can be considered.

I’m not sure if it’s the same problem. In my case I see that restarting generates an ‘off’ signal and I suspect that the start time is uninitialized, because no ‘on’ signal has been seen yet. So a random time period is added to the sensor.

I’ve raised an issue too about it:

Thanks for raising that issue in Github again. It’s clear to me there is a bug somewhere.

I’m not so sure yours is a different problem from mine. In my case, at least the last time it happened, the start time was also uninitialized, because it had passed the purge_keep_days in Recorder.

Whether there is one cause or two, it results in a sudden and incorrect jump in the history_stats value, so it’s got to end up in the same place in the code somehow.

This happened to me again. This time it was right after updating the HA OS. Same as the OP. I updated the Github problem that @hneel linked, above, to add a screenshot of this.

I am another one with history_stats jumping around after a restart.

Much like the OP, I wanted to measure the time that the central heating system is on, both daily and from an arbitrary date.

  - platform: history_stats
    name: Boiler heating on
    entity_id: sensor.wiser_heating
    state: "On"
    type: time
    start: "{{ now().replace(year=2023, month=2, day=23, hour=0, minute=0, second=0) }}"  
    end: "{{ now() }}"
  - platform: history_stats
    name: Boiler heating daily on
    entity_id: sensor.wiser_heating
    state: "On"
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"  
    end: "{{ now() }}"

But whenever I restart - and there are three restarts below - the stats jump around:

Screenshot 2023-03-19 at 14-29-10 Overview – Home Assistant

The daily stats are, it appears, less affected:

Screenshot 2023-03-19 at 14-29-44 Overview – Home Assistant

FWIW, I kept track of by how much it had changed in order to calculate an offset back to the first recorded figure:

  • -62.5
  • +23.8
  • +74.2
  • -143.4
  • +291

I never found a fix for this, but with HA’s expanding Helpers, there’s a decent workaround.

First, implement a daily record:

  - platform: history_stats
    name: Boiler heating daily on
    entity_id: sensor.wiser_heating
    state: "On"
    type: time
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"  
    end: "{{ now() }}"

Then SettingsDevices & ServicesHelpersCreate HelperUtility Meter.

For the input sensor, enter the daily history stats sensor (in the example above: sensor.boiler_daily_heating_on). Then … wait a few days.

So far, this has survived multiple restarts:

image

Boiler heating on was the old long-term history stats sensor which failed after restarts, although only once it had been non-zero for more than 10 days. Interestingly, the daily stats are still prone to changing values after a restart, but only by a small degree so it barely affects the total.

There was an interesting observation posted on the other thread looking at this:

2 Likes

Hello and sorry for digging this old thread but I want to accomplish exactly what you created - calculate how long heater was on during a day, week and month and have the results displayed in a calendar format.

How one can accomplish it?