How to completely delete a history_stats sensor

Home Assistant 2023.11.1 running via a docker container. Standard sqlite database.

I have created some history_stats sensors, one of which ended up with duff data due to a typo. So I thought I would just delete the sensor definition, delete the entity and then remove the associated statistic. However, as soon as the sensor is recreated it seems to have remembered the previous value rather than starting from zero, can anyone explain why?

For completeness the sensor definition.

  - platform: history_stats
    name: Monthly Boiler Usage
    entity_id: binary_sensor.oil_boiler_running
    unique_id: mbu0001
    type: time
    state: "on"
    start: "{{ now().replace(day=1, hour=0, minute=0, second=0, microsecond=0 ) }}"
    end: "{{ now() }}"

Here are the steps I took to delete the sensor.

  1. Commented out history_stats sensor from the yaml configuration
  2. Rebooted Home Assistant
  3. Go to settings / entities - sensor showing no longer being provided. Deleted sensor.
  4. Go to developer / statistics - no state for the sensor. Fix issue and remove.
  5. Rebooted Home Assistant.
  6. Download the database and opened
  7. From the states_meta table I find the metadata_id for the sensor - 1099
  8. From the states table I find the states associated with 1099 - there is one entry listed, the state is NULL. Attribute id is 462421 and there’s one entry in the state_attributes table.
  9. From the statistics_meta table I find no entry for sensor.

So, in theory the sensor has been deleted, although I’m surprised to see any entry in the states table? However, if I then create the sensor again its starting value is exactly the same value as it was before the delete rather than starting at zero. Can anyone explain what’s going on and why the sensor hasn’t started from zero? There must be a hidden value somewhere that I’m missing?

I’m loathed to start directly altering the database but as I can’t find any data pertaining to the issue I’m not sure what I would delete?

Can someone please shed some light on what’s going wrong.

Thanks

Richard

The history stats sensor obtains its state from the history of the source sensor stored in the database.

It is not clear from your description which entity’s history you deleted, but from the result you are getting I’m guessing it was not the history of binary_sensor.oil_boiler_running

1 Like

I was deleting the monthly_boiler_usage data but I can see now that recreating it will immediately scan through the historic data for the oil_boiler_running, so I’ve been looking in the wrong place…

Thank you, I couldn’t see the wood for the trees! I will take a look at the binary_sensor.oil_boiler_running as there must be some duff data there.

Richard

1 Like

Just to follow this up I think there’s an issue with history_stats after a HA restart. I cleared out the oil_boiler_running data and then recreated the monthly_boiler_usage template sensor and the value was then correct. However, after a HA restart the value has gone wayward again.

At 8.33.00 HA was restarted and the history_stats sensor jumped by 400 hours but the state of the sensor it should be checking has not changed.

Here’s a slightly bigger scale showing the events either side of the restart more.

Hello, I am seeing exactly the same thing, did you figure anything out?

I even deleted the entire HA database and values still returned, not sure where its pulling them from.

No I gave up using the history stats sensor, it has all sorts of weirdness especially during restarts, there are several bugs reports on GitHub including mine…

The data was being sourced from an ESPhome sensor so I coded the history stats functionality there instead and it has been 100% reliable.