Water tank level sensor: 4–20mA current loop, ESPHome, and Home Assistant

https://blog.narf.ssji.net/2025/06/30/water-tank-level-sensor-4-20ma-current-loop-esphome-and-home-assistant/

Our house water comes from a rain water tank. I built an ESPHome device, based on an Olimex ESP32-PoE2, to interface a 4-20mA current-loop liquid pressure sensor, and keep track of the water level in Home Assistant.

#clkMode #electronics #ESPHome #HomeAssistant #OlimexESP32PoE2 #waterTankSensor

In search for a better filter

https://blog.narf.ssji.net/2025/05/10/in-search-for-a-better-filter/

Linear vs. Polynomial calibration to get water level from sensor reading are pretty much the same.

Low-pass filtering too slow to react to actual changes

Works well enough for immediate reading and daily consumption aggregate

Too noisy for hourly readings and other event detection

Water delivery took hours to register the full scale

Kalman?

I thought it was for sensor fusion, but it also works for single-sensor filtering.

Good videos: https://www.youtube.com/watch?v=HCd-leV8OkU https://www.youtube.com/watch?v=qCZ2UTgLM_g

Home Assistant has some integration for Kalman filters, but they seem unmaintained https://community.home-assistant.io/t/kalman-filter/58483

ESPHome: https://esphome.io/components/sensor/combination.html

Dug out and translated the sheet that came with the current loop liquid pressure sensor. It looked like an ad, but actually contained useful info on the characteristics of the sensor.

Likely useful to calibrate the Kalman filter

Also has a “debugging” / calibration method, which would allow to get a wider range.

Adding a Kalman-filtered entity in ESPHome

Error on the sensor: Got the process stddev from overnight data from Home Assistant (when the water level is expected not to change), and chucked it into LibreOffice’s STDEV function: ~600L

Standard deviation on the process: change per second. For lack of a better estimate, using the baseline of 400L/day use: 400/(24*3600) = 0.00462963

This seem very low as “A low value here will place high importance on the current state and be slow to respond to changes in the measured samples.”

Also trying a larger value for likely changes, e.g., one tap running for a minute at 9L/min: 0.15

[…]

#ESPHome #HomeAssistant #KalmanFilter #waterTankSensor

In search for a better filter - Narf

Linear vs. Polynomial calibration to get water level from sensor reading are pretty much the same. Low-pass filtering too slow to react to actual changes Works well enough for immediate reading and daily consumption aggregate Too noisy for hourly readings and other event detection Water delivery took hours to register the full scale Kalman? I

Narf - The blagosphere got me...

Finagling the water sensor parameters

https://blog.narf.ssji.net/2025/02/22/finagling-the-water-sensor-parameters/

Now that I have the hardware and ESPHome OtA setup for the water tank sensor, I can play a bit. The ESPHome dashboard did not detect the remote sensor until I set ESPHOME_DASHBOARD_USE_PING=”true” (or any non-empty value) in the docker-compose environment (as hinted by https://community.home-assistant.io/t/esphome-devices-offline-but-only-for-dashboard-status/403141)

First, with taking all possible built-in sensors for a spin:

uptime

internal_temperature (bad luck this one gives a well-known invalid value)

Then, I can try to find the right filter https://esphome.io/components/sensor/#sensor-filters. There are many combination just with the basic filters, so let’s start simple.

[…]

#ESPHome #HomeAssistant #smartHome #waterTankSensor #YAML

Finagling the water sensor parameters - Narf

Now that I have the hardware and ESPHome OtA setup for the water tank sensor, I can play a bit. The ESPHome dashboard did not detect the remote sensor until I set ESPHOME_DASHBOARD_USE_PING="true" (or any non-empty value) in the docker-compose environment (as hinted by https://community.home-assistant.io/t/esphome-devices-offline-but-only-for-dashboard-status/403141) First, with taking all possible built-in sensors for a spin:

Narf - The blagosphere got me...

Putting it all together

https://blog.narf.ssji.net/2025/02/15/putting-it-all-together/

I’m finally putting the water tank sensor together for a test

PIN 35 on the ESP32-POE(2) is not Vcc, but the battery level, when a jumper is in place. Rather than putting the jumper, I created a short cable from the 3.3V pin to pin 35. It’s at 3.14V when powered over Ethernet.

the adc needs attenuation: auto, which works pretty well for both VCC and the current loop sensing resistor (the default is 0dB, which doesn’t)

Found a nice Lovelace card for Home Assistant lovelace-fluid-level-background-card

Also using icon mdi:hydraulic-oil-level, which seems more telling than the plain mdi:water

Crimped and patched the CAT6 cable to the water tank. There are two standards for Ethernet connectors, but Australia mostly uses T-568A (green first)

Updated the sensor config to use Ethernet for connectivity, in addition to power (rather than Wi-Fi)

The ESP32-POE2 uses GPIO0 for the clock, same as the first version.

Does DHCP by default, like Wi-Fi did.

Took a leap of faith and OTA’d it. It still work. Pfew.

esphome upload –device water-tank-sensor.example.net water-tank-sensor.yaml

Also moved passwords and related parameters to .gitignored secrets.yaml

Got some data in! Pretty limited, so far, with one calibration point. Re-enabled the moving average filter to reduce the noise. Now checking with air pressure to see if changes have much of an impact.

Next steps: add calibration points, maybe add an air pressure sensor if needed, finish the PCB as a generic base for current loop sensors.

[…]

#ESP32 #ESPHome #HomeAssistant #waterTankSensor

Putting it all together - Narf

I'm finally putting the water tank sensor together for a test PIN 35 on the ESP32-POE(2) is not Vcc, but the battery level, when a jumper is in place. Rather than putting the jumper, I created a short cable from the 3.3V pin to pin 35. It's at 3.14V when powered over Ethernet. the adc

Narf - The blagosphere got me...

Clamping and back to ESPHome

https://blog.narf.ssji.net/2025/02/14/clamping-and-back-to-esphome/

Obtained a better understanding of clamp diodes thanks to Alan. When they start conducting, they prevent the current from getting into the ADC. So a clamp diode from the ADC line to +VCC will protect from over-voltages above Vcc+Vd, the same from GND to the ADC would protect from voltage reversals from -Vd. Zeners reverse voltage would provide a tighter clamping, but a 1N4148 would at least do the above

I have plenty of 1N4148, and found one with a notably low Vd at around 0.5v.

Used that as between the ADC input and Vcc.

Put the water tank sensor together and played in ESPHome

No Ethernet connection (power over USB for now), so no 24V for the sensor for real

Could test ADC reading using the adc platform for the sensor component

[…]

#ESP32 #ESPHome #waterTankSensor

Clamping and back to ESPHome - Narf

Obtained a better understanding of clamp diodes thanks to Alan. When they start conducting, they prevent the current from getting into the ADC. So a clamp diode from the ADC line to +VCC will protect from over-voltages above Vcc+Vd, the same from GND to the ADC would protect from voltage reversals from -Vd. Zeners reverse

Narf - The blagosphere got me...

Re-working the 4-20mA adapter

https://blog.narf.ssji.net/2025/02/02/re-working-the-4-20ma-adapter/

I finally got back to my water-pressure meter. As I fried the previous esp32-poe, and I didn’t want to do this to my brand new esp32-poe2. I’ll need better protection on the ADC input, and I’m flat out of the right Zeners for clamping.

I could, however, rewire the adapter around a 150 Ω parallel resistor, and a 1 kΩ into the ADC. Testing this in isolation into a jar of water show hopeful progress!

Next, I need more Zeners.

[…]

#waterTankSensor

Re-working the 4-20mA adapter - Narf

I finally got back to my water-pressure meter. As I fried the previous esp32-poe, and I didn't want to do this to my brand new esp32-poe2. I'll need better protection on the ADC input, and I'm flat out of the right Zeners for clamping. I could, however, rewire the adapter around a 150 Ω parallel

Narf - The blagosphere got me...

ESP32-POE-two

https://blog.narf.ssji.net/2024/11/09/esp32-poe-two/

Mouser added the ESP32-POE2 to their catalogue, but with no stock

Ended up ordering straight from Olimex https://www.olimex.com/Products/IoT/ESP32/ESP32-POE2/open-source-hardware

Playing around with esphome, it seems to work with the same config as the esp32-poe

the EXT1 header has both VPP (my 24v), and GPIO36 (my ADC) […]

#ESP32 #ESPHome #waterTankSensor

ESP32-POE-two - Narf

Mouser added the ESP32-POE2 to their catalogue, but with no stock Ended up ordering straight from Olimex https://www.olimex.com/Products/IoT/ESP32/ESP32-POE2/open-source-hardware Playing around with esphome, it seems to work with the same config as the esp32-poe the EXT1 header has both VPP (my 24v), and GPIO36 (my ADC) https://github.com/OLIMEX/ESP32-POE2/blob/main/DOCUMENTS/ESP32-POE2-user-manual.pdf VPP@1 GND@2,4,6,8 GPIO36@12 some doc on ESP32 ADCs https://randomnerdtutorials.com/esp32-adc-analog-read-arduino-ide/

Narf - The blagosphere got me...

New blog post: Random discoveries

Olimex now has the ESP32-POE2 https://www.olimex.com/Products/IoT/ESP32/ESP32-POE2/open-source-hardware, which has a nice configurable 12/24V line that would make it substantially easier to build my water tank sensor. Waiting for Mouser to carry it.

I finally settled for EH connectors for the CTCSS module for my AWA RT85. They were surprisingly hard to find. I […]

https://blog.narf.ssji.net/2024/05/04/random-discoveries/

#AWART85 #waterTankSensor #DIY #electronics #µblog

Random discoveries « Narf

New blog post: Power and protection

More work on the 4–20mA Current Loop Rain Water Tank Sensor

Need to drop the voltage for the 78L24

46 to 24 -> 22V

Drop 11V in each stage -> 11V zener

Splitting voltage drop to dissipate power equally in each component

Current control resistor: 5mA at 46V (before established regime): ~10k

Need to protect the ADC […]

https://blog.narf.ssji.net/2024/04/08/power-and-protection/

#waterTankSensor #µblog

Power and protection « Narf

New blog post: Converting 48v to 40v for 78L24

I got pretty much all my components wrong to power my 4–20mA Current Loop Rain Water Tank Sensor: PNP instead of NPN transistor, and non-power components (I may be able to get away with this one)

Though I should be able to swap the VCC and GND in the circuit and get the PNP to work

Other suggestions to test (and be more careful about component […]

https://blog.narf.ssji.net/2024/03/05/converting-48v-to-40v-for-78l24/

#ESP32 #ESPHome #waterTankSensor #electronics #µblog

Converting 48v to 40v for 78L24 « Narf