How to clear the long term stats of a sensor

Assume you have this template sensor (which is created in UI & thus has some “unique_id”):
image

Then you “rename the entity” (i.e. “change it’s entity_id” in UI - which is only possible for entities with “unique_id”):
image

a new entry (“metadata_id”) will be added to “states_meta” table of DB for the new “entity_id”:
image

and then new entries will be added to “states” table for this new “metadata_id”:
states for “old entity_id”:
image

states for “new entity_id”:
image

Same about LTS.

Means - a history (& LTS) for that “renamed” entity becomes stored “anew”.

Assume you deleted that entity with “new entity_id”.
In “core.entity_registry” you will find this entry in “deleted entities”:

"config_entry_id":null,"created_at":"2024-12-08T20:25:56.560740+00:00","entity_id":"sensor.testing_ids_in_db_xxx","id":"0c6a1c8d5eef2dc0fe1d55a343a0fb2a","modified_at":"2024-12-09T02:01:37.488929+00:00","orphaned_timestamp":1733709697.4888752,"platform":"template","unique_id":"01JEKZ6FJ8WJ4D2ZRBZBEV0A6F"}

which is ONE entry - since you “merely renamed entity_id”.
Now create a new entity with same “old entity_id”.
And we got old history back:
image

with a gap in DB:
image

because “entity_id” is a key in DB.

Means:

  1. The whole point of easily changing “entity_id” in UI may easily cause loosing old history. Contrary, this helps to merge “old history for deleted entity” & “new history of new entity”.
  2. That mentioned proposal to “rename entity_id → delete entity → create new entity” will not work if you wish to “start anew”.
2 Likes