Water Tank Level and Water Volume with ESPHome

- lambda: return (l*w*(x - h));

for this

#experiment
  - platform: custom
    lambda: return (235*143*(x - 229));
    sensors:
     unit_of_measurement: Ltrs
     accuracy_decimals: 1
     name: "Quantity"

I’m having this

/config/esphome/esp32-sensors.yaml: In lambda function:
/config/esphome/esp32-sensors.yaml:34:24: error: 'x' was not declared in this scope
     lambda: return (235*143*(x - 229));
                        ^
*** [/data/esp32-sensors/.pioenvs/esp32-sensors/src/main.cpp.o] Error 1
========================= [FAILED] Took 27.29 seconds =========================

Guys, I have no idea what else to try! Changing the slightest is giving me error!
Could anybody help me?

Here are a couple links that should help.

Salt level measurement

  - platform: custom
    lambda: |-
      auto my_sensor = new AJ_SR04M_Sensor(id(uart_bus));
      App.register_component(my_sensor);
      return {my_sensor};
    sensors:
      unit_of_measurement: Ltrs
      accuracy_decimals: 1
      name: "Quantity"
      filters:
      - lambda: return (235*143*(x - 229));

2 Likes

Thanks to @ walberjunior and everybody else that helped me to deal with this challenge.
History6– Home Assistant(1)
This is the result! I did some rectification into the formula (as 1 liter is equal to 10x10x10cm) and added one more sensor that is showing the percentage of the tank that is filled with water. Probably later, I’ll add one more sensor showing the percentage of the useful capacity, as there is a part of the tank that never is filled up and another that is never emptied due to precautions.

Here is my “sensors.yaml”, the “AJ_SR04M_Sensor.h” is unchanged as above post and I’m posting this hoping that somebody else will find it useful. I’ll come back in due time, when I’ll try to fit a LCD screen showing locally the measurements.

esphome:
  name: esp32-sensors
  friendly_name: ESP32-sensors
  includes:
    - AJ_SR04M_Sensor.h
 
esp32:
  board: esp32dev
  framework:
    type: arduino

dallas:
  - pin: GPIO22
uart:
  id: uart_bus
  tx_pin: GPIO32
  rx_pin: GPIO33
  baud_rate: 9600
  stop_bits: 1
  rx_buffer_size: 4

sensor:
  - platform: custom
    lambda: |-
      auto my_sensor = new AJ_SR04M_Sensor(id(uart_bus));
      App.register_component(my_sensor);
      return {my_sensor};
    sensors:
     unit_of_measurement: cm
     accuracy_decimals: 1
     name: "Tk Ullages"
#experiment
  - platform: custom
    lambda: |-
      auto my_sensor = new AJ_SR04M_Sensor(id(uart_bus));
      App.register_component(my_sensor);
      return {my_sensor};
    sensors:
      unit_of_measurement: Ltrs
      accuracy_decimals: 1
      name: "Tk Quantity"
      filters:
      - lambda: return ((235*143*(229 - x))/1000);
  - platform: custom
    lambda: |-
      auto my_sensor = new AJ_SR04M_Sensor(id(uart_bus));
      App.register_component(my_sensor);
      return {my_sensor};
    sensors:
      unit_of_measurement: pct%
      accuracy_decimals: 1
      name: "Tk space pct"
      filters:
      - lambda: return ((((235*143*(229 - x))/1000)/7695.5)*100);
#up to here

  - platform: dallas
    address: 0xea0722b0dcbabc28
    name: "Boiler 1" 
  - platform: dallas
    address: 0xd30822b0f48ffd28
    name: "Boiler 2"
  - platform: dallas
    address: 0xa90722b0e5ac1328
    name: "Boiler 3"

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

ota:
  password: "xxxxxxxxxxxxxxxxxxxxxxxxxx"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

captive_portal:

web_server:
  port: 80

globals:
  - id: last_state
    type: float
    restore_value: no
    initial_value: '0.0'    
2 Likes

Few days ago finished the installation in the place of operation. There are few challenges in the installation process but finally was ok.
My sensor (AJ-SR04M) is working quite well with accurate results although is reporting, constantly, a difference of +0.4 to +0.6 cm between each reading (per second). This is resulting a constant update of related “sensors” and causing a kind of flooding of useless information/updates.
I’m digging around to reduce the update time or something similar.

So far, the sensor is working from 40 cm up to 110 cm range and I’m intending to leave it until the max (150-160cm).

AJ_SR04M_Sensor(UARTComponent *parent) : PollingComponent(5000), UARTDevice(parent) {}

Update every 5 seconds.

1 Like

It seems that the refresh rate is remaining the same.



These two screenshots are showing the performance of the sensor.
in a certain point I did use of the filter/delta but without any improvement.

Any idea why sometimes are having this margin/error in measurements? As we are standing is reporting 57 to 88 cm. It is a range of 31 cm which is practically is making it useless

When i was looking at water level reporting the more i read indicated that using any form of ultrasonic sensor would be problematic due to a a number of factors including, high moisture environments, signal bounce due to water surface, signal bounce due to internal tank designs etc
This led me down the path of a throw in type sensor.
I have installed about 3 weeks ago a throw in sensor linked to a Shelly uni. The only issue i am now dealing with is scaling my battery/solar power supply to ensure that winter months do not result in the shelly switching off.
The value received goes through a median filter and then a calculation shows volume.
If you are interested look at a video from Lars Klint on youtube.
Pat

1 Like

https://www.youtube.com/watch?v=N90C9Xge8Z4 I think this is the video that you are referring to…
My objections are 1) supply voltage 24Vmake it difficult 2)Shelly Uni is something new for me (thinking the time spending to understand esp/esp32 3) pricing

My thoughts about the ultrasonic sensors? very sensitive to the voltage supplying these boards, moisture and the surface reflections.

really I’m confused, I think I should try few things before condemning ultrasonic sensors.
I’m thinking
a) a pipe (holed pipe) with the sensor on top of this
b) a floating surface underneath the sensor eg a plastic plate.
These are something theoretical, on the application may have different results.

Thanks for the feedback

Foufoutos,
I supply only 12v power with a battery/solar setup. I have a dc step up to power the sensor. On the test bench it was using about 30mA when running. Shelly uni - connect power, connect wire from sensor. HA picked up immediately (using Shelly add-on native).
I was just making suggestions as I went down this rabbit hole over the past 4 years.
I know some people made 3d printed cones for their ultrasonic sensors - can’t use a tube as that then deflects and distorts signals. I don’t own a 3d printer.
Anyway give it a go and best of luck
Pat

1 Like

yeah, I have that feeling!
These days we have a temp of near 40C outside and I’m doing an experiment related to the ventilation of the tank. I believe that there is a concentration of moisture in the empty space which is affecting the measurements

I recommend using sliding average median, this removes the erroneous readings.

sensor:
  - platform: ultrasonic
    trigger_pin: D1
    echo_pin: D2
    name: "Tank Level Sensor"
    unit_of_measurement: "L"
    device_class: volume_storage
    icon: "mdi:water"
    accuracy_decimals: 0
    update_interval: 1s
    filters:
      - median:
         window_size: 59
         send_every: 60
         send_first_at: 3
      - lambda: return (1-x) * 1000.0 - 55;
      - filter_out: nan
3 Likes

Good morning,
I have indicated your information correctly but it does not work?
I saw that some talk about mqqt?
THANKS

Thank you for this information. After reading about the HopperHawk monitor for pellet grills, I started looking at using an ESP32 device in the same fashion. This is looking at water levels, but I see no reason to think that it won’t work for wood pellets. This looks just like what I need to setup a device to monitor my pellet grill bin.

Wow, this is awesome! Definitely going to use the info in this thread.

I have a water purifier (Berkey). And I want HA to refill the tank when empty. ESPhome will measure the amount of water left and HA will refill. I already have a Zigbee water valve that works. And as a bonus I will be able to monitor when the filters need replacement (they last about 22.000 liters I believe).

Only challange now is ti actually get a waterpipe to the Berkey. But with a little luck, I am able to fix that.

@nawafbana as above - any chances of sharing the solution?

Can you please share your complete setup, which hardware you use, how you connect it and the code?
I’m struggling with getting this working.
I’ve tried a ESP8266 and also a D1 mini, both don’t give any information through.
I don’t know if i have to get a LED signal on the AJ-SR04M board, but I didn’t see that.
Thanks in advance!


Check this diagram if you need the code ping me :wink:

1 Like