Statistics sensor for non-updating sensor

Hi, I want to observe a water container for water being pumped out and the water level dropping. The HW is fully working, and I can get the water level with a voltage reference of 0.00V…10.00V. This is converter to a volume 0…1000 liters.

Whenever the water is being pumped out, the volume drops ~20 liters/minute. The water volume sensor updated roughly every 10-15 seconds in this case. But when there’s no water being pumped, the volume sensor might stand in a constant reading for several minutes, even 30mins. There’s some noise of course, and during 10h time period the volume sensor might fluctuate +/- 2 liters. But the updates are very sparse.

I have an automation to trigger and send me a notification if the water level drops below 150 liters. This is working fine, but there are occasions when the water level is exactly 150 liters overnight, and because of the noise the value will fluctuate 148…152 liters. And the automation has false triggers.

What I tried is making a Statistics sensor to monitor the “change” in the volume:

- platform: statistics
  name: "Water level change"
  entity_id: sensor.water_tank_fill_level
  state_characteristic: change
  sampling_size: 5
  max_age:
    seconds: 30

This way I could know if water is really being pumped out, and it’s not just random noise that’s causing the water level changes.

But there is a problem. The statistics sensor only updates when the source sensor updates. With my example the sensor goes into ‘unknown’ state after 30 seconds if the water level stays unchanged. If I remove the “max_age”, the “change” might stay in e.g. “5 liters” over a very long period, because it will reset to “zero” only when there are 5 updates for the sensor, and that might take even 1 hour.

Why couldn’t we have a statistics sensor where you could force the sampling even if the sensor doesn’t change.

Don’t use the statistics sensor. Use the Threshold binary sensor with a bit of hysteresis (larger than your noise level):

You can set it up via the frontend or YAML.

binary_sensor:
  - platform: threshold
    entity_id: sensor.water_tank_fill_level
    lower: 150
    hysteresis: 3
    name: Tank at 150L

This binary sensor will turn on when the level goes below 150L but wont turn back off until the level rises above 153L. Use that to trigger your automation or if you are only messaging, an Alert.

2 Likes

Thanks, this was the perfect solution

1 Like