How to retain history when replacing battery?

Hi. I’ve noticed I quite often lose all the device history when I replace a battery. Just now I had a Sonoff SNZB-02 (ZigBee Temperature & Humidity Sensor) that was showing as ‘unavailable’, and when I checked it, the battery was low. I replaced the battery and tried a reconfigure but no luck and ended up pairing it again. It retained the name I gave it but lost all the history. Is there a way to get it back and/or avoid losing it in the future?

If you make sure you name your entities precisely what they were before then your history should stay intact. I’ve unpaired old devices and I always make note of all the entity names on that device so that when I put in the replacement I don’t lose history.

That being said, I’ve never had a Zigbee device require re-pairing after a battery replacement, that’s odd - but if it is offline for a while perhaps that’s why.

As far as I can tell, everything is named the same. The problem is intermittent though. Maybe HA is designed to forget devices after a certain period?

Either way, is it possible to retrieve the old history? Or failing that, is there an integration that would save data to an external file?