I have written an AppDaemon app that reduces the size of the states table by averaging states over configurable intervals. The main reason was that Home Assistant’s long term statistics are way too coarse to be useful (at least for some of the sensors I have). And statistics also aren’t configurable at all. While increasing recorder’s purge_keep_days is certainly possible, keeping values of a sensor that is reporting at high frequency for many months/years isn’t really a good option.
That’s where this app come in. You can define how wide you want the interval to be and what entities to process. Then the app goes over the states table and replaces multiple states with a single average over the interval. So you can for example compact a sensor that reports every 10 seconds down to one value for every 10 minutes. The states stay in the states table, but you can now keep them for much longer (you actually do need to increase recorder’s purge_keep_days) without bloating the database.
More details on GitHub: