Water Tank Level and Water Volume with ESPHome

1 - Esp and sensors powered by USB and close to the router.
Are all the sensors sending the readings (looking in the logs)?
Does esp reconnect to HA? Even if it takes a longer time for that to happen.
Everything is OK? Next step…

At this stage dallas (x3) sensors have been removed although without issues after (each) reboot. I had readings for the temperature and updats regularly.
Wifi coverage is fine all around the house (Ubiquiti AP) except the “engine room” space where is the underground tank and the hot water tank along with machinery.

ESP board is conned to ESPHome, from there I can see the readings and the logs (until today that realized that I can see the same from ESP flasher). After all this adventure and trials of today, most of the times (after reboot) I have readings in the board’s page. If not then turning on the logger of ESP flasher (if connected to this pc) and resuming the operation (or removing and reinserting any of the TX/Rx pins).

My observations are 1) sensor board flashing blue immediately after reboot despite f having or not having reading, something that wasn’t happening before 2) accuracy with these settings are slightly worst than before. Just now measuring 82.6 cm in an actual distance of 85 cm (not big deal) 3)I have seen messages of “safe boot” mode after reaching a number of reboots, as well

2 - Connect the esp and sensors using the power supply that you will use in the final project, but close to the router.
Everything is OK? Next step…
No? Change the power supply. I use jumper wires, but they also give a lot of problems.

Results are not different with the other source of power or USB. The random stall/delay is there.

3 - Everything ok, but when you take the project to the final destination, it doesn’t work.
Probably the network signal is not good.

Not even the smallest chance this issue related to the wifi coverage, could be anything else but not this as the wifi is very powerful in my home lab where Im here.

Could you help me how to add a “sensor” where the “Distance” will be translated into “Liters” (capacity). Its an underground tank of water with dimensions 2,35 x 1,43 x 2,29 mtrs?

This

You can do a linear calibration:

https://esphome.io/components/sensor/index.html#calibrate-linear

It’s in the first post

1 Like

Its impossible to do it with correct syntax!!!
I can duplicate the sensor but I can"t manipulate the “return” for giving me anything useful.
The formula is Q=TQ-(return * 32.75) where Q is the remaining quantity in liters, Total Quantity (TQ) is 7,696 liters and the constant 32.75 is the liters per centimeter.

Height (h)
Width (w)
Length (l)
sensor measurements (x)

(l*w*(x - h))

Why don’t you do something like that?

1 Like

Thanks again for your time, more or less is the same function.
My problem now is the syntax of the command

#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: "Quantity"

I can assume that the formula goes at

return {my_sensor};

but I can’t achieve even a simple multiplication. Most of the times, compilations failing

If compiling fails you are blessed with error message which actually tells us why you failed compiling :raised_hands:

Thanks for the clarification but into my eyes it is similar to the King Hammurabi tablets!

/config/esphome/esp32-sensors.yaml: In lambda function:
/config/esphome/esp32-sensors.yaml:37:34: error: expected ';' before '}' token
       return (235*143*({my_sensor};-229));
                                  ^
                                  ;
/config/esphome/esp32-sensors.yaml:37:35: error: expected ')' before ';' token
       return (235*143*({my_sensor};-229));
                       ~           ^
                                   )
/config/esphome/esp32-sensors.yaml:37:22: error: invalid operands of types 'int' and 'AJ_SR04M_Sensor*' to binary 'operator*'
       return (235*143*({my_sensor};-229));
               ~~~~~~~^~~~~~~~~~~~~~
/config/esphome/esp32-sensors.yaml:37:35: error: expected ')' before ';' token
       return (235*143*({my_sensor};-229));
              ~                    ^
                                   )
/config/esphome/esp32-sensors.yaml:37:40: error: expected ';' before ')' token
       return (235*143*({my_sensor};-229));
                                        ^
                                        ;
*** [/data/esp32-sensors/.pioenvs/esp32-sensors/src/main.cpp.o] Error 1
========================= [FAILED] Took 27.74 seconds =========================

I assume that now you can help!

Remove the ; after the } from the config/esphome/esp32-sensors.yaml
File on line 37 character 35 as per this error.

/config/esphome/esp32-sensors.yaml:37:35: error: expected ')' before ';' token
       return (235*143*({my_sensor};-229));
Compiling /data/esp32-sensors/.pioenvs/esp32-sensors/src/main.cpp.o
/config/esphome/esp32-sensors.yaml: In lambda function:
/config/esphome/esp32-sensors.yaml:37:34: error: expected ';' before '}' token
       return (235*143*({my_sensor}-229));
                                  ^
                                  ;
/config/esphome/esp32-sensors.yaml:37:35: error: expected ')' before '-' token
       return (235*143*({my_sensor}-229));
                       ~           ^
                                   )
/config/esphome/esp32-sensors.yaml:37:22: error: invalid operands of types 'int' and 'AJ_SR04M_Sensor*' to binary 'operator*'
       return (235*143*({my_sensor}-229));
               ~~~~~~~^~~~~~~~~~~~~~
/config/esphome/esp32-sensors.yaml:37:41: error: expected ')' before ';' token
       return (235*143*({my_sensor}-229));
              ~                          ^
                                         )
*** [/data/esp32-sensors/.pioenvs/esp32-sensors/src/main.cpp.o] Error 1
========================= [FAILED] Took 27.80 seconds =========================

It looks like you are trying to do a math function on a variable that us not an integer value.

{my_sensor}

Really, I dont know what I’m doing.
What I’m trying to achieve is converting measured distance of the void space to the capacity of the existing water.
Few posts above there is the (proposed) formula of

(l*w*(x - h))

where x is the return value of measurement and the rest the dimensions of the tank.

Pretty sure x is your sensor in home assistant that is created by 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

1 Like
  - 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