Make a Statistics helper

Can we please have a helper which can do at least mean average value of a sensor over a given period (e.g. last 24 hours). This would be really useful for automations. For bonus points you could select the statistic you want (e.g. min, max). You could add other statistics like mode, median, standard deviation, statistical range (similar to what we have in the combine sensor).
This could also simplify things by replacing both the derivative and integral helpers.

The UI could be something like:

  • Entity
  • attribute (optional)
  • time window (dd:hh:mm)
    • sampling interval (hh:mm:ss)
    • or sample on update
  • statistic (drop down list, eg. max / min / mean / median / mode…)
  • values to include:
    • all values (default)
    • only values between X and Y inclusive
    • only values within ± Z standard deviations from the mean

other statistics options could include:

  • range (max - min over time window)
  • trend (end value - start value)
  • sum of values
  • standard deviation
  • RMS
  • etc…

i just noticed something which makes this even more important to implement. When you look at the history chart of a sensor, it shows max, median and min. It doesn’t actually show (when hovering with mouse) the actual value ! Obviously, if you get a ruler out and find the corresponding value on the y-axis you can find out what it is, but if you don’t happen to have a ruler to hand, there you have three values, none of which can actually be used in a helper. The value which CAN be used is not displayed.
Confused yet ? Just a little bit…

In the meantime you could check the ‘History explorer card’. Maybe this is helpfull…

I need to calculate the monthly maximum of the 15min peak consumption. Turns out that is absurdly difficult in the current (.2024.4.2) HASS version. I tried many of the proposals that I found online, but could not make one of them work. HASS template syntax is really difficult !

So I strongly support this request !

To make things clear, the maximum here mean the maximum of the time series values of one sensor (not the maximum of different sensor values).
Time window should include week, month and year

You can with Statistics - Home Assistant if I understand the question right?

You’ve almost perfectly described the statistics integration as @gjohansson said. I’m seriously wondering how you could not have found it: home assistant statistics sensor mean - Google Search

Please correct me if I am wrong. Are you missing a feature?

Edit: Just realized you specifically asked for a “helper”, which is a UI feature. Is a yaml based configuration acceptable?

This would be nice. Sensors in yaml are nice and very configurable, but for small things, like a simple average/moving window average and other common things, would be nice to be able to do it via the UI. Would be easier to understand also for new users

I think it should be part of the core feature set as I envisage the principal use would be for scripts and automations. I don’t believe users should have to install an integration to obtain what ought to be basic functionality, It didn’t even occur to me that which is why I requested that be done as a helper. If Integration code can be used for this, more power to you.

Also, to clarify - as I mentioned in another reply, the idea of being a helper is that it would produce a dynamically changing value - ie. the value is recalculated each “sampling period” or when the sensor value is updated.
For calculating static, historical values in a time range, I think that is best handled by an integration designed for that purpose.

I believe that @emontnemery worked on something like that some time ago.

Another idea for this - it would be nice to be able to exclude statistical outliers from the calculations. Say I have a temp/pressure sensor which occasionally will produce a bad value that is either extremely high or low, then the next reading is normal - so you can see the odd spike on the history plot.
So it would be more accurate if I could set a + and - cut-off, and say exclude anything that varies by > 10 standard deviations, or else set an absolute valid range. Otherwise these values would overshadow min / max for the set of valid measurements.

Whilst I appreciate your support, the idea of the FR is to produce a dynamic value to use in scripts. IIUC your use case is to calculate historic values. I think in that case this is something more appropriate for an integration specifically designed for such things.

That’s available here:

Statistics is a complicated scientific area and Home Assistant could certainly be more user friendly about it. Yet again, there are powerful options provided already. You’d do good to explore them before suggesting improvements here. Would make for a more focused and therefore more heard feature request.