Calculating rolling standard deviation or volatility

Hi,

I have a raw sensor value which varies over time. In my case it’s a force sensor, but I guess for this question it could be any type of sensor which returns a number within a range.

What I’d like to do is output a metric which represents the volatility of the sensor value over a certain time period (e.g. last minute or last hour). My high-school statistics knowledge is a bit rusty, but I think what I need is a ‘moving standard deviation’. Can anyone suggest how I could calculate this from a sensor value in ESPHome? (Note, I want to do this on the ESPHome device itself and not externally in HA or some other system).

I can see that ESPHome has a filter for a basic moving average, but I don’t see any filter for moving standard deviation. I’m guessing I might be able to use a lambda filter, but I don’t know how I could write one which could perform a calculation on a range of historical values…?

Any advice is much appreciated. Thanks

Is the problem that the drift on the force sensor, makes it difficult to set a static value to trigger an automation? This may work for you.

I don’t know which force sensor you have, this is for an hx711 but should work for any force sensor. It’s an auto/manual tare function. The hx711 yaml can be swapped with the sensor your using.