ESPHome water level sensor

Hi @giovanne ,

I have some environmental monitoring on my water tank, tracking outdoor details as well as the water tank level. My config is as follows:

  • Wemos D1 mini as control unit. This is powered by an old DC plug-pack sheltered from the rain etc, connected to a cheap DC level converter to provide 5V DC to the D1 mini. The wifi connection to my home router is surprisingly reliable given the conditions.
  • A standard “rain monitor” daughter board and sensor connected to A0 and D4. This DOES NOT work reliably regardless of sensitivity setting; the unit false triggers on dew and high humidity (Sydney hits 100% humidity many mornings so not much use), the PCB gets a corrosive build-up quickly and needs cleaning, and the sensor reports “wet” conditions for a day or more after rainfall.
  • Temperature monitoring using a DS10B20, connected to pin D3.
  • Water tank level sensing using the JSN-SR20 connected to pins D6 & D7. I have dual sensors located directly on the top of the tank (it has a flat top, and I used exterior double-sided tape to stick them on which worked surprisingly well). This has been UNRELIABLE for me; I replaced the daughter board and dual ultrasonic sensors 4 times before I gave up. Additionally the water level can’t be read when it’s raining as the surface waves cause invalid readings (you could fix this by putting the sensors into a vented pipe within the tank), plus the reported levels vary with temperature (this might be the DC supply drifting with temp, haven’t really investigated).
  • Water level reported in cm, L, and %full.
  • All the above is in a capped 90mm PVC pipe enclosure, with entry and venting at the base and temp / rainfall sensors stuck to it. Despite the wettest first 3 months of the year on record here, I’ve had zero issues with moisture or condensation.
  • Tank is a 5000L circular tank, 2m tall.

I’m going to upgrade with humidity monitoring, a DIY rainfall sensor, and a 5MPa water pressure sensor (which I’ll connect via a 3mm x 2m tube to the tank outlet) to replace the JSN-SR20 but am still to test and config it. The drop-in sensors discussed previously sound like a better idea but I haven’t gone down that path yet.

My ESPHome yaml code:

esphome:
  name: tank-mon
  platform: ESP8266
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: "my_secret_password"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Tank-Mon Fallback Hotspot"
    password: !secret fallback_hotspot_pswd

captive_portal:

web_server:
  port: 80

dallas:
  - pin: D3

text_sensor:
# Platform parameters
  - platform: version
    name: 'ESPHome Version'
  - platform: wifi_info
    ip_address:
      name: "IP"
    ssid:
      name: "SSID"

### All sensors defined below

binary_sensor:
# Rainfall sensor - digital wet/dry output.
#switch:
  - platform: gpio
    pin: 
      number: D4
      inverted: true
    name: "Rain sensor"
    device_class: moisture

sensor:  
# Platform parameters
  - platform: wifi_signal
    name: 'Tank_mon WiFi Signal strength'
    update_interval: 60s
    accuracy_decimals: 0
  - platform: uptime
    name: "Tank_mon Uptime"
    unit_of_measurement: days
    update_interval: 300s
    accuracy_decimals: 1
    filters:
      - multiply: 0.000011574

# DS18B20 temperature sensor (enable once address is recorded)
  - platform: dallas
    address: 0xXXXXXXXXXXXXX 
    name: "Tank_mon air temp"
    
# Rainfall sensor - analogue voltage on A0 (0.0 to 1.0V), digital output D4 (wet/dry)
  - platform: adc
    pin: A0
    name: "Rainfall intensity"
    update_interval: 60s
    filters:
    - lambda: return (1 - x);


# JSN-SR20 ultrasonic sensor for water level monitoring
# Tank height: 2m (Th)
# Tank circumference: 1.6m
# Tank volume: 4000L (Tv)

# Return water level (as Th - x)
  - 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
    - lambda: return (200 - (x * 100));
    
# Return water level %
  - platform: ultrasonic
    trigger_pin: D7
    echo_pin: D6
    timeout: 4m
    pulse_time: 20us
    update_interval: 60s
    unit_of_measurement: "%"
    accuracy_decimals: 0
    name: "Tank full %"
    filters:
    - filter_out: nan
    - max:
        window_size: 15
        send_every: 5
    - lambda: return ((200 - (x * 100))/2);

# Return water level in L
  - platform: ultrasonic
    trigger_pin: D7
    echo_pin: D6
    timeout: 4m
    pulse_time: 20us
    update_interval: 60s
    name: "Water volume (L)"
    unit_of_measurement: L
    accuracy_decimals: 0
    filters:
    - filter_out: nan
    - sliding_window_moving_average:
        window_size: 10
        send_every: 5
    - lambda: return ((2 - x)/2)*4000;

In my dashboard, I’ve included all the sensors plus standard ESPHome wifi / device monitoring (to monitor uptime, wifi network & signal strength).

Good luck!

Britespark,
This is a project i am about to get onto. I am just waiting to get my hands on some 18650 cells so i can set up a 24v battery with solar feed in.
My tank is located about 60m from my house uphill and I get good wifi there so will be using an esp32 as my controller with a thrown in pressure sensor.
The plan is to have the measured values provide a reliable value that will then be sent to another esp32 to trigger the pump for refill.
Would be good to keep in contact on our respective projects. I am in Northern NSW.
Pat

1 Like

Sounds like fun, Pat. My brother has a very similar scenario, with a header tank on a hill he’d like to monitor and a bore to fill it when needed. If I get my system working reliably (I’m in residential Sydney, so if it fails there’s always mains pressure :slight_smile: ) I’m planning to install on his property.

Keep us posted!

I read this somewhere and I have been doing this to any outdoor electronics. Coat them with this nail polish liberally to seal them from the elements https://www.amazon.com/Sally-Hansen-Nails-Polish-Crystal/dp/B00AJKJ6JW

Thanks @OzGav that sounds sensible but I think that will defeat the purpose here - you need the tracks exposed for the rain / moisture to complete the circuit. It’s the actual tracks on the rain sensor, which look like solder-coated copper, that get the corrosion buildup on them. If the tracks were straight copper they’d probably be ok but I think the wave solder process in manufacture coats everything.

I haven’t really looked into what the impact of the buildup is, I guess over time it would reduce the conductivity. Or the buildup might become waterlogged and prevent the circuit drying out in a reasonable time. I’m thinking of replacing the stock sensor with some thin brass wire (easy to solder, or maybe stainless steel wire) wrapped as a closely-spaced double-helix around a piece of veroboard.

1 Like

hi everyone,

I also need to know how much water is in my cistern so I know when to fill it up again.
I bought an TL-136 Water Level Sensor and have an ESP8266 here.

this thread is miles long (with many different aprroaches) and I would like to know If anyone can share a step-by-step tutorial how to set everything up.

  • components used
  • how to wire everything up
  • how to setup up the software and the home assistant integration

Best regards
Tobias

hi, which component did you use for this one? looks very nice and simple.

  • LM2596
  • Water Level Sensor (TL-136?)
  • MCU 8266 (is this an esp8266?)
    -CurrentToVoltage Converter ?? which one is it?

Best regards

HIi @kimocal,

Just a few questions. I have the same setup, but looking to connect to a Wemos D1 mini

I assume that the VCC & GND of your INA219 connect to the 5V and GND of your ESP, right ?
I assume that the SDA of your INA219 connects to the A0 of your board ?
Where is the yellow one going to ? :slight_smile:

Thanks in advance !

Kr,

Bart

Hi Kimocal,

Would you mind sharing your yaml code ? I constantly receive errors in the way you wired

Thanks in advance !

Kr,

Bart

Hi,

Do you mind sharing your esphome yaml file used ? I’m having trouble using my TL sensor and the ina219

Much appreciated !

Jt,

Bart

Here’s the esphome yaml parts:

esphome:
  name: water-tank
  platform: ESP32
  board: m5stack-core-esp32

# Enable logging
logger:

# Enable Home Assistant API
api:


i2c:
  sda: 21
  scl: 22

sensor:
  - platform: ina219
    address: 0x40
    shunt_resistance: 0.1 ohm
    current:
      name: "INA219 Current"
      id: ina_current
      accuracy_decimals: 5
      filters:
      - multiply: 1000 #convert from Amps to mA
      unit_of_measurement: "mA"
    power:
      name: "INA219 Power"
      accuracy_decimals: 5
    bus_voltage:
      name: "INA219 Bus Voltage"
      accuracy_decimals: 2
    shunt_voltage:
      name: "INA219 Shunt Voltage"
      accuracy_decimals: 5
    max_voltage: 32.0V
    max_current: 400mA
    update_interval: 10s
    
    

Here is the Home Assistant configuration.yaml parts:

 
 template:
  - sensor:
      - name: "Water Tank Level"
        unique_id: "water_tank_level_sensor_inches"
        unit_of_measurement: "inches"
        state: >
          {% set height = ((states('sensor.ina219_current')|float * 5.0663) - 24.86) |round(2) %} #This equation was derived in a spreadsheet based upon actual water height and ina219 current values
          {% if 0.000 <= height <= 72.000 %}
            {{ height }}
          {% else %}
            unknown
          {% endif %}

The INA219 is connected to the ESP32 via the wires in my diagram. The red is VCC and black is Ground. The yellow and green wires are the SDA and SCL pins on the M5 Stack ESP32. You’ll have to look up the Wemos D1 mini pins. On the M5 Stack the SDA and SCL pins are 21 and 22 respectively per the yaml code I shared.

Hi,

Can you help ?

Thanks @kimocal,

Unfortunately I’m receiving following errors:

[16:48:31][I][app:102]: ESPHome version 2022.5.0 compiled on May 24 2022, 16:47:54
[16:48:31][C][wifi:491]: WiFi:
[16:48:31][C][wifi:353]: Local MAC: 4C:75:25:34:94:32
[16:48:31][C][wifi:354]: SSID: [redacted]
[16:48:31][C][wifi:355]: IP Address: 192.168.1.80
[16:48:31][C][wifi:356]: BSSID: [redacted]
[16:48:31][C][wifi:358]: Hostname: ‘watertank’
[16:48:31][C][wifi:360]: Signal strength: -64 dB ▂▄▆█
[16:48:31][C][wifi:364]: Channel: 1
[16:48:31][C][wifi:365]: Subnet: 255.255.255.0
[16:48:31][C][wifi:366]: Gateway: 192.168.1.1
[16:48:31][C][wifi:367]: DNS1: 192.168.1.1
[16:48:31][C][wifi:368]: DNS2: 0.0.0.0
[16:48:31][C][logger:274]: Logger:
[16:48:31][C][logger:275]: Level: DEBUG
[16:48:31][C][logger:276]: Log Baud Rate: 115200
[16:48:31][C][logger:277]: Hardware UART: UART0
[16:48:31][C][i2c.arduino:038]: I2C Bus:
[16:48:31][C][i2c.arduino:039]: SDA Pin: GPIO4
[16:48:31][C][i2c.arduino:040]: SCL Pin: GPIO5
[16:48:31][C][i2c.arduino:041]: Frequency: 50000 Hz
[16:48:31][C][i2c.arduino:044]: Recovery: bus successfully recovered
[16:48:31][I][i2c.arduino:054]: Results from i2c bus scan:
[16:48:31][I][i2c.arduino:056]: Found no i2c devices!
[16:48:31][C][ina219:133]: INA219:
[16:48:31][C][ina219:134]: Address: 0x40
[16:48:31][E][ina219:137]: Communication with INA219 failed!
[16:48:31][C][mdns:084]: mDNS:
[16:48:31][C][mdns:085]: Hostname: watertank
[16:48:31][C][ota:085]: Over-The-Air Updates:
[16:48:31][C][ota:086]: Address: watertank.local:8266
[16:48:31][C][ota:089]: Using Password.
[16:48:31][C][api:138]: API Server:
[16:48:31][C][api:139]: Address: watertank.local:6053
[16:48:31][C][api:141]: Using noise encryption: YES
[16:48:39][D][api:102]: Accepted 192.168.1.142
[16:48:39][D][api.connection:827]: Home Assistant 2022.5.5 (192.168.1.142): Connected successfully

Seems that my wemos is not able to communicate through the I2C. My SDA GPIO pin is connected to the SDA pin on my INA219, same goes for SCL.

Do you have any idea ?

Kr,

Bart

This is part of the issue. Without seeing which pins you have the SDA and SCL lines physically connected to on the Wemos, it’s hard to trouble shoot. If you have them connected to the actual SCL and SDA pins then change these in your esphome yaml:

i2c:
  sda: 21
  scl: 20

Also you may wait to add everything after those lines until your ESP can see the i2c devices. The ESP will scan the i2c bus and then give you the address of the INA219. That will show up in the logs. Then you can add the appropriate address in the remainder of the YAML.

sensor:
  - platform: ina219
    address: 0xZZ  <---- TO BE DETERMINED WHEN THE ESP SCANS THE I2C BUS

Hi Kimocal,

Thanks again for your swift answer. Please find below the code. The Wemos D1 mini on an ESP8266 platform doesn’t support the declaration of the 20 and 19 pins, I need to use (I think) the GPIOnumbers.
So my SDA from the INA219 is connected to the GPIO4, the SCL from my INA219 is connected to the GPIO4. The GND from my INA219 connected to GND from Wemos & the VCC of INA219 connected to the 5V pin.

I use code below:

esphome:
  name: watertank
  platform: ESP8266
  board: d1_mini

# Enable logging
logger:
 level: VERY_VERBOSE

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

ota:
  password: "532e35be5f3621ce8a33df0d057a0bda"

wifi:
  ssid: "xxxxxxx"
  password: "xxxxxxx"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Watertank Fallback Hotspot"
    password: "xxxxxxx"

i2c:
  sda: GPIO4
  scl: GPIO5
  scan: true
sensor:
  - platform: ina219
#    address: 0x76
    shunt_resistance: 0.1 ohm
    current:
      name: "INA219 Current"
      id: ina_current
      accuracy_decimals: 5
      filters:
      - multiply: 1000 #convert from Amps to mA
      unit_of_measurement: "mA"
    power:
      name: "INA219 Power"
      accuracy_decimals: 5
    bus_voltage:
      name: "INA219 Bus Voltage"
      accuracy_decimals: 2
    shunt_voltage:
      name: "INA219 Shunt Voltage"
      accuracy_decimals: 5
    max_voltage: 32.0V
    max_current: 400mA
    update_interval: 10s

I try to scan the I2C, but no devices found. I tried to put a delay ‘on boot’, but that doesn’t work…

To be complete: I connected the pins for the INA219 to the Wemos board directly using the breadboard cables, but without using a breadboard (so from pin to pin)

Any other suggestions ?

Thanks in advance !

Kr,

Bart

Try using the pin numbers first and report back.

i2c:
  sda: 21
  scl: 20

If you are using a d1_mini you can also use the D-numbers for your pins in the yaml file.

The i2c scan doesn’t find any device, so 99 out of 100 times there is a problem with the wiring, or a defect sensor. Do you have another i2c device which can be used for testing to exclude the sensor itself.

Can you post some pictures of your setup?

That doesn’t make sense when using a esp8266, that are the actual pins on the bare esp-chip, not the GPIO numbers.

I was going by other examples in a quick google search of using the pin #s I found online for esp8266. I have only played with ESP32s so not too familiar with the 8266 / Wemos idiosyncrasies.