Getting Average of Sensor Value in ESPHome

I am creating a self-contained ESPHome device (mostly) and am trying to figure out how to account for sensor spikes but am not sure how to track that.

I’m using a soil moisture sensor and 99/100 times it deviates only a very small amount, but sometimes it will get a super low reading or a super high reading and then on the next refresh it returns to where it should be. Since I’m writing automation to trigger based on the value of the sensor I would like to throw away the lowest and highest readings so it doesn’t suddenly trigger on the surge when it shouldn’t.

I thought that perhaps a counter of 1, 2, 3 where the low value goes into 1, high into 3 and everything else in 2 and then trigger off the value of 2 instead of the value of the sensor but I’m not sure if that’s the best way to go about it.

Any suggestions?

1 Like