Job to prune/downsample data in HA DB (no InfluxDB)

Hi there,
would it be possible to downsample data in HA DB that are older than 14 days for example? I have some energy meters that are sending data each 5s, I would like to keep just 1 minute samples in HA DB for records that are older than 14 days.

I was thinking about some job/automation that would prune older data and keep just single (ideally avg of the 1m window) record for each minute, but I am not sure how to write it. I would like to apply this for all data/every sensor.

I do not want InfluxDB/Grafana because the data retrieval to the lovelace cards in dashboards is somehow complicated and I would like to keep HA homogenous as much as possible

Thanks.

I have yet to find a good way to do this :frowning: