History Stats Not Working as Expected

I’ve created several History Stats sensors to track the total “on” time over the last 10 days for some ESP switches that control irrigation valves. When I first created the sensors in my sensors.yaml the result was a number that seemed excessively high - over 25 hours on-time, even though none of my valves have ever been on for that much time over a 10 day period. I also noticed that the value dropped steadily over the rest of the day, so I figured I’d need to let it settle for 24 hours. But today the value suddenly jumped from 1.11 hours to 25.02 hours. I can’t explain the values or the sudden changes. I have several sensors I just created that behave the same way. I also have a history stats sensor for tracking the on-time of my dehumidifier that’s been in-use for a long time and it seems to behave as expected.

Anyone seen anything like this before or have any suggestions? Thanks!

This one seems to work:

  - platform: history_stats
    name: 'Dehumidifier ON today'
    entity_id: switch.dehumidifier_switch
    state: "on"
    type: time
    end: "{{ now() }}"
    duration:
      hours: 24

This is one that doesn’t show expected results:

  - platform: history_stats
    name: 'Fruit Trees Valve 10 Day On Time'
    entity_id: switch.fruit_trees_valve
    state: "on"
    type: time
    end: "{{ now() }}"
    duration:
      days: 10

History stats behaves weird if your recorder doesn’t have data in it at the start of the window, this can happen if you have sensors that are off for a long time and then turn on.

In that case all the “off” states are purged, and the only thing it sees is an “on” state, which it then backdates to the start of the history stats window.

So if you have a sensor that’s off for longer than your recorder purge window, then as soon as it turns on history_stats treats this as if it’s always been on.

Not sure if that’s a bug or by design, but it’s bitten me several times :person_shrugging:

This was the issue! Knowing that I wanted to get the history stats for 10 days, but my Recorder purge_days was set to 7 days, I had changed the recorder to 10 days. But because of this bug? it caused those weird values until 3 days had passed and the Recorder had valid data to allow the history stats to work correctly.

I’ve opened a bug report/suggestion to see if this can be fixed, or at least documented so it doesn’t keep biting people.

Thanks!