Edit History states via Database/Dev Tools

Hi there,
i tried to reset/edit the state of this sensor.

 - platform: history_stats
   name: Test Flips
   entity_id: binary_sensor.openclose_10 #The aqara sensor
   state: 'off'
   type: count
   start: '{{ now().replace(hour=0, minute=0, second=0) }}'
   end: '{{ now() }}'

Actual State :“10”

  1. I tried to set the state of this sensor via Devtools/State/SET STATE to “1”
    That worked and is reflected in the UI, but the state is updated back to “10” after some seconds or at the next update of this sensor.

  2. I tried to edit the state via databse (edited via state table). Deleted all old values from “statistics” and “statistics_short_term” for this entity.
    That is not reflected in the ui. Sensor gets updates after some seconds or at the next update of this sensor back to “10”.

I also tried to stop HA completely when i do the changes in the Databse, but after restarting HA, the state gets set back to old value.

So my question:
Can someone explain to me, where the Value of “10” is stored in the databse?
Whats the best practice to edit those Values?

Example of Events Output after i set the state via Devtools to “1” and triggert the opensclose sensor.
Where is the “pre value” saved?
cant find it :frowning:

    "event_type": "state_changed",
    "data": {
        "entity_id": "sensor.test_flips",
        "old_state": {
            "entity_id": "sensor.test_flips",
            "state": "1",
            "attributes": {
                "state_class": "measurement",
                "unit_of_measurement": "",
                "device_class": "duration",
                "icon": "mdi:chart-line",
                "friendly_name": "Test Flips"
            "last_changed": "2022-08-02T09:59:40.625905+00:00",
            "last_updated": "2022-08-02T09:59:40.625905+00:00",
            "context": {
                "id": "01G9EZYBAH7FYR3AM5TW5Y99FX",
                "parent_id": null,
                "user_id": "54bff3c13dd445658793d488c000e082"
        "new_state": {
            "entity_id": "sensor.test_flips",
            "state": "11",
            "attributes": {
                "state_class": "measurement",
                "unit_of_measurement": "",
                "device_class": "duration",
                "icon": "mdi:chart-line",
                "friendly_name": "Test Flips"
            "last_changed": "2022-08-02T09:59:42.774741+00:00",
            "last_updated": "2022-08-02T09:59:42.774741+00:00",
            "context": {
                "id": "01G9EZYDDP445CQ1WDWYCZTMGS",
                "parent_id": null,
                "user_id": null

Debug logs show following: (see old_state=<state sensor.test_flips=10 )
Cant find this DB entry

2022-08-02 12:27:24 DEBUG (Recorder) [homeassistant.components.recorder.core] Processing task: EventTask(event=<Event state_changed[L]: entity_id=sensor.test_flips, old_state=<state sensor.test_flips=10; state_class=measurement, unit_of_measurement=, device_class=duration, icon=mdi:chart-line, friendly_name=Test Flips @ 2022-08-02T12:25:47.933074+02:00>, new_state=<state sensor.test_flips=1; state_class=measurement, unit_of_measurement=, device_class=duration, icon=mdi:chart-line, friendly_name=Test Flips @ 2022-08-02T12:27:24.891774+02:00>>)

Thank you for your help :slight_smile:

If you overwrite a state via the states dev tool or the API, it will not impact the actual device. If the device state is being polled, it will overwrite the state in the state machine the next polling.

Can that have something to do with this?

That value is not stored in the database anywhere. Well it is but not until after the state is updated then based on your recorder settings it gets stored or not in your history db.

the value you see when it gets changed back is the ACTUAL state of the sensor being updated by the device itself in real time.

It’s not coming from anywhere other than the device itself telling HA what it’s own value is in that instant. And it writes that value into the state machine.

I see,
but the sensor platform is actually “history_stats”.
So it should be anywhere in the database.

Why is it so important to change this value?
Is it because you want to reset it during the day by some reason?

yes the history stats sensor reads the historical data from the monitored sensor.

but both the history stats sensor and the sensor it monitors have their states determined by the backend of HA itself on an update interval determined by the sensor integrations.

So as soon as the state machine gets updated for both of those sensors the states of each of them will revert back to what the real values should be.

Its just annoying if you have some skuffed sensor data like this, when u tested this rainsensor before and calibrated it.
2022-08-02 21_00_50-Window

Now i have to create new sensors with different names to set them back to “0”.
Or is there another way of resetting them?

Not that I know of.