Replace two sensors by each other and keep history?

Hi there.

I got a new thermometer and humidity sensor with display yesterday, which I already added to HA. I would like to replace my existing bathroom sensor (with no display) by the new one, keeping my existing historical data for the bathroom and assigning it to the new sensor, so that the bathroom data continues to flow into the same data sync.

How can this be (easily) accomplished?

I’m not willing to do any ugly database hacks. It should be possible via the web UI, or I will simply forget about it.

Many thanks for any help.

Kind regards,

Ralf

I just found out about this today, so I can’t comment on how well it works, how easy it is, or exactly how to do it…

There is an existing core action, recorder.get_statistics, to retrieve values and Frenck has added the action recorder.import_statistics to Spook.

Thank you for your response.

They both measure temp and humidity. Is that the same thing? I don’t know. They could be in a different internal format? I have no clue about these internals…

Also, how would I rename the entities? Would that be on the below page, simply entering the new entity ID?

https://ha.rmeb.de/config/entities

Also, what if I want to exchange the two sensors’ data? Would I simply rename “A” to be “B”, and “B” to be “A”?

Thank you.

That sounds risky, exactly not what I would like to do – especially as I’m still a HA rookie… :wink:

I’d be surprised if that works. There’s a metadata table in the recorder that maps entity ids to metadata_ids. The historical data is attached to the metadata id. It’s setup like this so you can rename an entity and not lose the history.

If you delete the old entity first then rename the new entity that may work.

What I usually do is shut down HA, manually edit the entity registry (.storage/config.entity_registry) and rename the entity ids there and that avoids the rename.

Just tried it - you’re right, it doesn’t work. Sorry. :flushed:

Exactly. Even with my limited experience I could tell that this would likely not work – I’ve renamed sensors a couple of times already, and every time I’ve kept my history…

Editing the registry (it’s an SQLite DB, isn’t it?) is something I don’t want to do. It’s not that I lack the relevant knowledge, it’s simply the risk about inconsistencies I’m not willing to take. This is why I would like to use only capabilities that have been built into HA to be used by end-users…

I think I’ll try deleting the old sensor and renaming the new one to the old entity name – if it fails and I lose my history, it’s not the end of the world… And the risk of “damaging” something seems close to zero…

No worries. I appreciate you’re trying to help. Thanks anyway.

I can confirm that this is working as I was hoping it would – the new sensor is now called as the old one used to be called, and I still see the historic temp and humidity values… :slight_smile:

This is not 100% what I wanted to achieve, but as the historic sensor values of the new sensor are of no use (it’s few days anyway), I’ll consider the case solved. :wink:

Thank you!

It’s a JSON file in the .storage directory. Just make a backup first.

Thanks, Pete. With my limited knowledge I consider even such “JSON hacking” risky, it could theoretically create inconsistencies anywhere… Maybe I’m “overcautious”, but I don’t want to run into problems later…

Anyway, as I wrote earlier, I’ve accomplished my main goal already…