Migrate LTS history to replacement sensor

A question of how to deal with long-term data for sensors, whch I want to keep, even if I don’t have the sensor anymore.

I have been recording, e.g. the temperature in my apartment since I got into HA about 18 months ago. I guess I have about 12 months of long-term data for this and other sensors since Long-Term-Statistics was introduced in HA 2021.8.

I would now like to change the device recording the temperature. This will create a new entity which will describe the temp and basically have the same properties. The old device will be repurposed in some other location. I would then like to somehow move the sensor history to the new entity, since it will no longer correspond to the new data from the old sensor.

I do not believe that this is supported. But is it possible in principle by writing to the LTS database using the sqlite add-on or similar? One idea I’ve had would be take an entry in the statistics_meta database and replace the entity name there from being my old device to be the new one. Or, if that’s too dangerous, maybe replacing the id for every entry in the statistics database to be the one of the new device?

Is something like this possible? Would it work? In general it seems this is a feature that should be built in.

I think all you have to do is name the new sensor to have the same entity_id as the old one.

But if I have the old entity, I cannot rename the new one to be the same. If I first rename the old – the statistics move with it.

Are you suggesting I should delete the old integration/device, this will give me a bunch of orphan time series in statistics. Then add the new device and rename it to the old name?

Did you manage to find an answer to this question ?

So it seems that the LTS and the recent “recorder” history are only connected through the entity_id of the sensor.

So what you have to do:

  • Rename the sensor to what you want it to be in the future, “name1”. This will tie the statistics to the new name
  • Remove the device, restart
  • The entities are now dead. Delete them. This will retain the LTS orphaned until you go to dev tools/stats and “fix” them. So don’t do that.
  • Add your new device and rename the sensor to name1. The LTS will not know there was a change of device reporting. But be careful about units of measurement etc.

Seems to have worked for me as fat as I can tell.