Water Tank Level and Water Volume with ESPHome

Hey guys,

Was thinking of implementing something similar with ESP-home. Re the weatherproof sensor that has been used, I am wondering if this is the same sort of thing and/or would it work with esp-home?

https://www.dfrobot.com/product-1503.html

Looks very similar…

1 Like

That’s the one

1 Like

Great thanks.

Can’t say I’ve found this successful. Sensor seems to under report distance. Sensor reads 0.23m where the actual distance is 0.5m. Randomly reports 0.23m when the actual distance is 1.8m. Adjusting the potentiometer doesn’t seem to have any effect.

I swapped the pins to GPIO’s 1 & 3 as per the original post. Seems much more stable now.

Checking the logs, there is one thing I’m curious about. I get a time outs from ultrasonic.sensor:026 while I receive correct reports from ultrasonic.sensor:030. Not sure why I have two sensors when there is only one specified in the config. Here’s a log extract.

[10:37:12][D][ultrasonic.sensor:026]: 'Tank Level' - Distance measurement timed out!
[10:37:17][D][ultrasonic.sensor:030]: 'Tank Level' - Got distance: 1.78 m
[10:37:17][D][sensor:131]: 'Tank Level': Sending state 1.77708 m with 1 decimals of accuracy
[10:37:22][D][ultrasonic.sensor:026]: 'Tank Level' - Distance measurement timed out!
[10:37:27][D][ultrasonic.sensor:030]: 'Tank Level' - Got distance: 1.78 m
[10:37:27][D][sensor:131]: 'Tank Level': Sending state 1.78103 m with 1 decimals of accuracy
[10:37:32][D][ultrasonic.sensor:030]: 'Tank Level' - Got distance: 1.78 m

Can anyone shed some light on this?

Still reports 0.23m randomly for some reason though…


Thanks.

Hello Omar, maybe my question is stupid, but I will ask it. Is the code in question for the ESPhome platform?

Thank you and congratulations for making this post available, as the sensor I bought from TUYA did not meet my needs.

Did you get the timeout issue resolved? By default the pulse time used for Ultrasonic sensors is 10usec whereas these sensors need 20usec (from the spec sheets). I changed this parameter plus the timeout parameter (default is 2.0m), but I was getting erroneous readings with the HC-SR04 above 1.80m.

Full details here ESPHome water level sensor - #190 by Britespark.

No timeout errors since I made these changes months ago. Still to resolve in code the random readings I get when it’s actually raining (so the water surface is rippled) which really mess up the charts (negative levels and percentages creep in), and temperature stabilisation (tank level inflates a few percent through the day as it gets warmer).

Thanks, I didn’t know about the pulse time though it is in the docs I didn’t think to try it. I’ll give that try. Thanks for that. My tank is way smaller that 1.8m.

Hey, that seems to be working now. Thanks a lot.

No drama - glad to help.

1 Like

would you mind sharing your code because i’m getting exactly the same problems as you, me it’s 0.25m… kinda stuck on that number almost…

thanks

Remember de JSN-SR04T has a minimum measuring range which is about 20cm

BTW, here’s my code:

sensor:
  - platform: ultrasonic
    trigger_pin: D6
    echo_pin: D5
    name: 'Nivel cisterna'
    unit_of_measurement: 'm'
    accuracy_decimals: 2
    update_interval: 10s
    pulse_time: 20us
    timeout: 2.0m
    filters:
      - filter_out: nan

After a month of stable operations (still to resolve temperature-based variations, and stable readings when it’s raining), my level sensor started playing up two weeks ago - often reading a distance greater than the height of my tank, frequent time-outs, etc. I figured I had a moisture or corrosion problem, but after replacing the Wemos D1 mini and the sensor board and sensors…same problem!

My fix? Upgrade ESPHome and HASSOS to current versions. I was running 2021.08 versions, not on 2021.10 and everything seems good.

I’m using the JSN-SR20 (samre as HC-SR04 with 2x waterproof sensors), it has a much better range than the JSN-SR20:

Specifications and parameters :

pulse output the serial output
working voltage DC:3.0-5.5V
working current <8mA
the probe frequency 40kHz
the farthest range 500cm
recently range 2cm
long-distant accuracy ± 1cm
resolution 3mm
measurement angle 60 degree
the input trigger signal 20us

My code (assumes tank height is 200cm):

  - platform: ultrasonic
    trigger_pin: D7
    echo_pin: D6
    timeout: 4m
    pulse_time: 20us
    update_interval: 60s
    name: "Water level"
    unit_of_measurement: "cm"
    accuracy_decimals: 0
    filters:
    - filter_out: nan
    - sliding_window_moving_average:
        window_size: 10
        send_every: 5
    - lambda: return (200 - (x * 100));

I do multiple readings / calls presently, to return other calculations for litres and %full. I’d guess there’s an easier (smarter!) way to do this but I’m learning as i go!

Has anyone the solution of implenetation two ultrasonic sensors in different tanks on one esp32/8266?

Maybe you could use two different GPIOs on the same board for the trigger and echo pins on the sensor

I’m trying to do this to measure a sump pit because I want to know if my pump fails before its too late.

I’m using a d1 mini as my MCU and the same sonar sensor as everyone else.

I have the following sensor code

sensor:
  - platform: ultrasonic
    trigger_pin: GPIO5
    echo_pin: GPIO4
    name: "Water Level"
    icon: "mdi:water"
    update_interval: 30s
    unit_of_measurement: "%"
    accuracy_decimals: 0
    timeout: 3m
    filters:
    - filter_out: nan
    - lambda: return ((((x*100)-10)-(76-10))/(76-10))*-100;

Right now in the winter my pit is almost completely empty there is a little bit of water down there. The sensor however is reporting in that the pit is at 80%. I’ve drilled 2 different holes into the lid of my pit to try and figure out if maybe the sensor was hitting off of something else in there but I don’t think thats the case. I live in the US but I converted all of my measurements over to metric for this. My pit is actually 76.2CM deep but I knocked off the decimal and I figure that the exclusion distance is about 10CM which is about the highest level I’d ever like my water to get in that pit anyways. If it ever got above 70% I’d be worried. Thank you all in advance for your help with this.

And, have you confirmed that the distance measurement is working in prinicple? For example tried the sensor outside of your pit just against a wall? And if installed in the top of the pit, is the distance measured correctly in metres (i.e. without that lambda conversion)?

Please note that the sensor is still returning the actual distance, it just can’t measure correctly if the distance is outside its operating range, so in your case you would expect values up to ~0.76 metres.
You could then simplify the lambda:

   - lambda: return (0.76 - x) / 0.76 * 100;

Yeah I sit here at my desk with it and point it at various things in my office :smiley:
So I took your code and its now returning that its 70% full which is still not right so I’m still poking around at it trying to determine what its issue is. Thank you for your help this.

ok so I think I figured out my issue. The minimum reading for the sensor is 23cm and with my tank being 76cm deep I think that I’ll either need to change over to the other ultrasonic sensors or see if there is some tweaks I can make to my formula to get a better reading.

EDIT: This is what happens when I work until some odd hour in the morning and my mind isn’t firing on all cylinders. The min reading has nothing to do with my tank depth ugh.
So still attempting to tinker with settings for this thing but its still reporting in that the measurement is 0.23m regardless of where I put the sensor inside of the pit. I’ve tried to lower it down to see if it would change, I’ve held it above the pit and that changes the value but there is something that is causing it to report 0.23m once its placed inside the tank regardless of where its at.

I haven’t tried this myself, but some people try to overcome the minimum measuring distance by adding a cone like this: Waterproof ultrasonic sensor (JSN SR-04T) holder for water tank by Monkeyhouse - Thingiverse

However, this may still not help with your immediate issue…

yeah I’ll have to get my direct drive on my printer fixed (having issues not feeding the filament) and print one of these and give it a whirl