History stats reset after each upgrade?

The last few weeks, I think since around the 2023.10.1 upgrade, my history stats lose a bunch of counts every time HA upgrades.

I have two motion sensors (binary) that are counting walkers. It should be a constantly increasing count. But as you can see it drops, and it seems to correlate with an HA upgrade.

Capture

Here is the config:

sensor:
# Motion Sensors
  - platform: history_stats
    name: Park Walkers
    entity_id: binary_sensor.motion_sensor_motion
    state: "on"
    type: count
    start: "{{ 0 }}"
    end: "{{ now() }}"
  - platform: history_stats
    name: Bluff Walkers
    entity_id: binary_sensor.bluff_sensor_motion
    state: "on"
    type: count
    start: "{{ 0 }}"
    end: "{{ now() }}"

OK, I think what might be happening is I had HA recorder saving data for 60 days. It seems to purge only on reboot, so it is cutting off the early days each time, making the count “back up”.

Is there a way, when data is purged, to take the count from the days that are going to be purged and make that the first value in the un-purged days? That way my increasing count keeps working. I only need the count during a 60-day span, but I want the total each day to be accurate

Feed your count to a utility meter helper with no cycle defined. That should count up forever. Though you will only be able to see the last 60 days of history it should never reset, even when your source sensor does as it will only accumulate the positive going events.

Won’t that just do the same thing the count is doing, i.e. when the count drops because of purging the utility meter value will also drop?

I tried creating two counters, and using those, but Utility Meter Helper doesn’t take counters:

counter:                                         
  park_counter:                                  
    initial: 0                                   
    step: 1                                      
  bluff_counter:                                 
    initial: 0                                   
    step: 1                                      
automation:                                      
  - alias: "Park Counter"                        
    trigger:                                     
      - platform: state                          
        entity_id: binary_sensor.motion_sensor_motion
        # adding from: off ensures not "unwanted" count if HA was offline and sensor state goes from "unknown" to "on"
        from: "off"                                                                                                   
        to: "on"                                                                                                      
        action:                                                                                                       
          - service: counter.increment                                                                                
            target:                                                                                                   
              entity_id: counter.park_counter                                                                         
  - alias: "Bluff Counter"                                                                                            
    trigger:                                                                                                          
      - platform: state                                                                                               
        entity_id: binary_sensor.bluff_sensor_motion                                                                  
        # adding from: off ensures not "unwanted" count if HA was offline and sensor state goes from "unknown" to "on"
        from: "off"                                                                                                   
        to: "on"                                                                                                      
        action:                                                                                                       
          - service: counter.increment                                                                                
            target:                                                                                                   
              entity_id: counter.bluff_counter     

No. Please read what I wrote:

Use your history stats sensors.

1 Like

Thanks, I’ll try it. Won’t know if it’s working for the next few days

You can test it by setting your history stats sensor to a lower value in Developer Tools → States.

Note however that as soon as the history stats sensor updates and returns to its higher value the utility meter will count this increase.

You can use the Utility Meter Calibrate service in Developer Tools → Services to return the utility meter current reading to a sensible value.

Or just use a test utility meter that you later delete and replace with your permanent meters if the spike (which will be visible in history for 60 days) annoys you.

I ended up just creating an automation that increases the counter each time the binary sensor turns on. This count never drops, just goes up forever. Dispensed with history_stats altogether.