Home assistant database gets purged at 4.00 and I am losing the Energy measuerment

Every day at exactly 04:00 Home Assistant purges or recreates part of the database and I lose all long-term Energy data (solar production, consumption, returned energy, water, gas). There are no faulty sensors in the Statistics diagnostics, all sensors have correct state_class/device_class/units, yet the statistics are wiped daily. After restoring a backup, the data disappears again the next night at 04:00. This makes the Energy dashboard useless because long-term history cannot be kept or compared between months or years. I need a way to prevent HA from deleting long-term energy/water/gas statistics and also a way to exclude unimportant sensors (like boiler burner ignition counters) from statistics so they don’t cause unintended purges. My recorder settings are default. Please investigate why HA removes validated statistics at 04:00 without reporting any faulty sensors.

Assuming this is true:

Then the nightly purge at 4am will delete all state data older than 10 days. It also deletes 5minute statistic data older than 10 days. It never deletes hourly statistics (that the energy dashboard uses).

It doesn’t. It is only your system that does that. You need to investigate it.

What errors do you see in your logs?

Have you tried enabling debug logging for the recorder?