WTH can't we perform calculations on long term statistics

Long term statistics are awesome, but rather than simply watching the history of any single entity, I would like do more analysis on the data. Say I have long term statistics for the electricity price and the long term statistics for the power consumption of a device. I would want to know how much does this appliance cost me, so what I would assume would be possible is to create a formula to calculate this (price/kwh * power) over time.

Yes, I know that template sensors exist, but they are useless when observing the history, as they start collecting data from the moment they are created and ignore the history. I do not, and frankly cannot know what I will be using my historical data for in the future.

There are few dimensions to this:

  1. we could have a long term statistics graph card, that takes a formula as an input, and calculates the result when loaded (this would be the first and easiest step I presume)
  2. we could have a scheduled service that would calculate the history and long term statistics for a template sensor after the template has been updated when there is available CPU time. This would also help with debugging template sensors.

These two dimensions are not mutually exclusive - there are calculations that depend on the time frame of the card (say if you try to create a wind rose for a given time period (you cannot average wind directions, you need to average the sine and cosine of the wind direction)). So both would be needed.

Closing as a duplicate of this:

Please vote there.