Compare new sensor value to all values in the last hour

Hi everyone,
I recently installed an ultra sonic water meter in my water tank to track the water level. The problem is that it is quite unstable, going from 0,50m to 0,95 to 0,62 back to 0,50, etc…All this in few seconds. I managed to stabilize it with templates and statistic sensors by getting the minimum value over a period of time and it seems to do the trick, but still my sensor sometime reports like this:

height of water: 0,50m
30 minutes later: height of water: 0,51m
10 seconds later: height of water: 0,50m

My problem is that I would like to create a sensor that reports the water usage based on water level in my tank.
So i need an automation like:
If water_level in tank has decrease by 0,01m, increase water usage by 87 liters (87 is the number i calculated based on my tank size)
But if the water height keeps changing over time, the water usage is obviously going to be wrong.

So I would like to compare the new water_height sensor value to all its previous values in the last 3 hours and increment water_usage only if last water_height value is lower.

As of now, here’s my automation:

alias: test water
description: ""
  - platform: state
      - sensor.water_level_minimum
      hours: 0
      minutes: 1
      seconds: 0
  - condition: template
    value_template: >-
      {{ trigger.to_state.state|is_number and trigger.from_state.state|is_number
  - condition: template
    value_template: "{{ trigger.to_state.state|float < trigger.from_state.state|float }}"
  - service: counter.increment
    data: {}
      entity_id: counter.water_usage_helper
mode: single

water_usage_helper is a counter with my actual water usage as an initial value and 87 as the increase step.

I really hope it is clear enough despite my approximate english, any ideas?

Instead of the statistics sensor try using a low_pass filter and/or a time_simple_moving_average to smooth your data:

An even better option would be to investigate what is wrong with your sensor.

Thanks for the reply,

The statistic sensor with value_min really works well as the only problem with that ultra sonic sensor is that it gives wrong values only above the good one, and the minimum value it reports is always accurate, except that sometime, it it still fluctuates a little (0,50m to 0,51m to 0,50) and that’s my problem.

About what is wrong with that sensor, you’re absolutely right! And I already know the main issue: it’s cheap :wink: But unfortunately, I had to do with the means at hand.

I tried with low_pass filter and simple_moving_average, but most of the tests I did give a kind of average result for the value:

Capture d’écran 2023-10-04 à 15.17.48

I get a more accurate result with statistic and value_min, as you can see the sensor gives most of the time the right water level height (its minimum value). I checked with a ruler inside my tank and it’s pretty accurate!

I really would like to give a try to my initial plan but for this I need to be able to compare the new water_height sensor value to all its previous values in the last 3 hours.

If anyone can help that would be great.

You were right ! I installed en external wifi antenna on my water level sensor and everything is ok now. The instability of the sensor was du to a poor wifi signal apparently.
Thank you.

1 Like