Note: Install https://github.com/hassio-addons/app-sqlite-web/tree/main/README.md
Automating this is a great way to keep your database lean, but a quick word of caution: Always back up your home-assistant_v2.db file before running bulk updates. If something goes wrong, you want a way back.
Since Home Assistant uses a relatively modern version of SQLite, we can use the UPDATE ... FROM syntax. This allows us to use the window functions (LAG/LEAD) to identify the bad rows and fix them in a single command.
This script targets the short-term (last ~10 days) table. It identifies the spikes and overwrites the min, max, and mean with the interpolated averages.