ESPHome water level sensor

For those who, like me, would rather use a volume in their front-end than a percentage, here is what i use:

  - platform: ultrasonic
    trigger_pin: GPIO14
    echo_pin: GPIO26
    update_interval: 5s
    name: "Filter Tank Volume"
    unit_of_measurement: "l"
    icon: "mdi:water"
    accuracy_decimals: 0
    filters:
      - lambda: return 2.75 * 1.3 * (1.15-x) * 1000.0 ;
      - filter_out: nan

Replace with your tank’s internal dimensions, where 2.75m is the length, 1.3m is the width, 1.15m is the distance measured by the sensor when tank is empty and x is the distance measured by the sensor.

3 Likes
4111.25 - (1000 * x)

is simpler maths for the processor. But then again yours is self documenting.

1 Like

@nickrout Usually a C compiler will do that for you

True, I am too used to using templates in ha itself.

It seems to me like this would not work mathwise. Maybe:

3575 * (1.15-x)

The ESP32 seems fine with the maths every 5 seconds anyhow!

No, you are right, using my factoring it would be

4111.25 - 3575*x
1 Like

After a few days of using the ultrasonic sensor, I can confirm I would not be comfortable using this for automation. The big spike is me opening the tank, but there are false readings maybe due to condensation. Tried to use the sliding_window_moving_average filters with no luck. Maybe someone’s got an easy and clever solution???
The ultrasonic sensors still have a use for giving an indication of how much water is in the tank.


Much better to use simple (and cheap) binary sensors like these:


I have not given up on the idea to use pressure sensors for both automations and precise measurement… Wait & See as I have 2 more tanks and a well waiting for automations!!
1 Like

@monkey-house Well, even with the spikes the general performance is great to my eyes. The spikes can be caused by all sorts of things including insects, spiders, condensation, frogs swimming, etc.
As an indicator of current tank level your setup is thumbs up from me and your fixed float sensors allow you to accurately control filling. Great result!

PS: Restarting HA will give large spikes as well.

1 Like

Spikes can of course be filtered out.

2 Likes

I am using a statistics sensor in HA on the raw distance value from my ultrasonic sensor (reporting once per minute), with sampling_size: 180. This averages out all the spikes and noise.

1 Like

True @nickrout but it is not a bad idea to observe the behaviour of these sensors to help diagnose problems which can then be corrected often by some maintenance. I have worked in water treatment (recycled and desalinated) and the expensive ones (Endress+Hauser etc) will behave just the same as these cheaper devices.

2 Likes

Am using a moving average in Esphome that does the same. Maybe I need to increase the sampling size for better filtering.
I had to change the position of my binary sensor array due to some strange construction choices of my water tank. Things you only find out through automating… 3 inch pipe on the wall at the bottom of the tank prohibited my empty sensor to trigger. Dirty cut and paste on the PVC pipe did the job.
Now have a full, a middle and an almost empty sensor… Idea is to use excess solar production to fill the tank from mid to full when necessary. Almost empty triggers the pump any time of day or night. This is just a backup and should seldom be used. Unless the monkey forgets to turn the tap off of course…

1 Like

using your lambda equation there. What would my values be for a pit (sump pump) that is 18 inches diameter and about 20 inches deep with the sensor mount about 10 inches above the top edge of the tank?

If I understood your equation above: ```
lambda: return 2.75 * 1.3 * (1.15-x) * 1000.0 ;


To represent my tank (US) lambda: return 0.4725 * 0.4725 * (1.1303-x) * 1000.0 ;
18" diameter pit (so 18" by 18") * 46.5 inches (1.1303 meters - measurement) * 1000.0
I get: 116 litres, but should be about 6 US Gallons (22.7125 litres)

What do I need to adjust in the formula?
1 Like

First, translate all into metric units. Sorry, but the empire is long gone! I could help with international units, but won’t touch deprecated ideas…
Then, surface of a circle is not height x width: it’s Pi x radius squared! This should help get valid results.
So, assuming your imperial to metric conversions are correct:

  return 3.14 * 0.229 * 0.229 * (0.87 - x) * 1000.0

I think there is an additional space in your formula (second 0.236).

1 Like

Good spotting. Corrected! Twas’late…
Anyways, I believe the conversions from imperial are wrong.inch_conversion

Hi @monkey-house, could you please share your esphome configuration for you water level with the binary sensors , I been having trouble with my ultrasonic not been very reliable and you gave me the idea to switch to binary sensors

1 Like

Not a problem (although it was already posted here):

binary_sensor:
  - platform: gpio
    pin: GPIO36
    name: "Water Low"
    filters:
      - delayed_on: 500ms
  - platform: gpio
    pin: GPIO32
    name: "Water Empty"
    filters:
      - delayed_on: 500ms
  - platform: gpio
    pin: GPIO34
    name: "Water Full"
    filters:
      - delayed_on: 500ms

Unfortunately, I’ve lost the full code as my SD card with ESPHome got corrupted. Sensors still work, but can no more OTA. Need to plug into computer to reprogram (which sucks). Tried to open an issue with ESPHome with no luck!

One of my binary sensors is MIA (always on due to bad contact). I ended up reverting to using the ultrasonic mostly… Ultrasonics work ok, as long as you can prevent condensation to stick on the sensor. I remixed my holder with holes to ventilate and lessen this issue on this tank. Works good!

1 Like

thanks monkey for the info appreciate your help and you input

1 Like

Sorry to bring this back to life but I’ve been trying to get this working with accurate numbers and struggling, math was never my strong point.

@monkey-house I’m trying to use your formula but I think I’m missing something which is causing it not to calculate correctly…

My tank height at full level is 1.60m , width (diameter) is 1.4m and sensor is mounted at 1.65m


Edit:

I incorrectly assumed in the original post / formula was for a circular tank which it wasn’t and which is why I could never get the calculations to work, I eventually after re reading the posts saw the one about using Pi which I did and am now getting correct readings, my formula based on above (circular) tank size was as follows:

- lambda: return  3.14 * 0.7 * 0.7 * (1.65 - x) * 1000.0 ; 

i.e. 3.14 * radius * radius (sensor_height - x) * 1000.0 ;

Hope it helps someone else :wink:

2 Likes