ESPHome water level sensor

In my opinion from the currents and voltage prospective, normal “telecom” cable (wires 0,5mm diameter) should be OK. The one I had in my sensor (which on the opposite side I had to cut sorter) has 2 small wires (appr. 0,5mm) and shield. It was thicker then normal as I believe it was ready to be submerged into different liquids and potentially outside so must be for “ouside use”.
The way I understand the mechanism of that sensor, voltage does not matter (therefore also supply voltage can be from 12 to 24 V) - and this is why you connect it to current->voltage converter. The sensor “provides” specific current depending on the pressure of liquid. So even small cable which can provide 500mA capacity should be OK.

2 Likes

Thanks a lot. I was thinking of using the typical 1.5mm² electricity cables we use for lighting here in Europe, so that should be fine then. I understand you use a different sensor than this one ? The one I linked has two wires and an air tube. As the air tube isn’t used I assume I only need to extend the two wires.

I trust our sensors are very similar (of not the same). Here is link to one I have. It looks like cable is pretty much the same:


Actually now I realized, that i have made my cable shorter and then I had extended it with regular 0,5mm par (only two wires). It works fine.

As mentioned before I am not happy with repeating values (there are differences up and down within 1 hour of 5-7%) and at this point I believe it is due to tolerance and stability of my “simple” current-voltage converter based on 1 resistor… :slight_smile: waiting for ordered converted (like yours) and then will see.

1 Like

FWIW I get a variety of values as well, but I have the ESP take the median over 15-readings, this has generally given me a very stable result. See filters below

sensor:
  - platform: adc
    pin: A0
    update_interval: 2s
    id: "water_tank_sensor_voltage"
    name: "Water Tank Sensor Voltage"
    accuracy_decimals: 3
    icon: "mdi:water-percent"
    filters:
    - multiply: 3.3
    - median:
        window_size: 15
        send_every: 15
        send_first_at: 15


(The big increase is from turning on my well pump)

2 Likes

@hummingbear, interesting that you are measuring every 2 seconds. Then you do median on 15 measures which “practically” makes measure every 30 seconds. Quite frequent…
In my case I do not see the need to measure so often, here is my config:

  - platform: adc
    pin: A0
    name: "liquid level"
    update_interval: 300 s
    unit_of_measurement: cm
    icon: 'mdi:arrow-expand-vertical'
    filters:
    - lambda: return (x-0.128) * (1000 + 50) ;
    - sliding_window_moving_average:
        window_size: 10
        send_every: 1
        send_first_at: 1

And my measures are like that:
obraz
I am looking forward for current-voltage converter to see how much it will be improved.

1 Like

Not quite, it’s a measurement every 2sec where I take the median every 15 readings. Taking a measurement every 30s would only be one data point. The values can slightly fluctuate on each read hence I take multiple and use a median.

You can see my full YAML higher up in this thread, but my sensor runs for 90s, then sleeps for 2-hours, so it’s not continuously taking a readings. I’m running off battery

1 Like

So I’ve measured that the tube from my basement to my water citern is 30 meters. Given that we seem to be talking about only 20mA, voltage drop indicators tell me that even an ethernet cable has sufficiently thick wires to not have a significant voltage drop.
However the large differences between individual measurements you are talking about have me worried that this pressure sensor might not be such a good solution after all?
Have you seen that on the DFRobot paghe for the pressure sensor, they recommend to use this device to have consistent measurements: https://www.dfrobot.com/product-1721.html

I mean I don’t have any issues with fluctuation, it’s solved by the 3-lines code of posted above. It gives me a reading with 1% of how much water is in my cistern. I’m working with 3000gal and I really don’t care if it’s off by +/-30gal. I’m sure if you wanted to fine tune it more you could.

If you’re looking for exact measurements of volume then sure you might want some more scientific instruments :slight_smile: - If you want to know quite accurately how much water you have in your cistern for $40 then this is great.

Oh OK, yes 1% variation is quite accetable :slight_smile:
Then I needn’t worry.

For those who don’t run this battery powered and thus won’t implement the deep sleep functionality you have: They will get a value coming in every 30 seconds with your code, which I think could actually be quite usefull when you are watering plants etc to have a near realtime look at how your water citern is doing. So I think your code is usefull for me as well. Spamming home assistant with results every 30 seconds is however perhaps a bit overkill for my database.
If I understand correctly I simply need to add to “filters:” the following:
- delta : X
Where X is some treshold value that ensures that Home Assistant will only receive a result when the change is higher than X.

In case of interest: I found this video which seems to confirm that you just need to ensure the air tube is above the water level (so that it gets atmospheric pressure) and that the length of the cable doesn’t really matter. So I should indeed be fine with extending it.

The link provided seems to be for Digital Analogue Converter which converts computer “digital” signal to nice analogue one. In our case we need to convert analogue signal (current in the sensor) into digital form. There are such converters and what we do now is first we convert current to voltage and then use ESP8266 on-board ADC (Analogue to Digital Converter) to convert voltage to digital measure.

1 Like

I think that amount of data is so small from each measure that for HA it is not “noticeable”. Also you may consider to exclude measures from this particular sensor from your database in case the size is important.
- delta: X will send the result to Home Assistant only if the next measure is greater then previous one by X. At least that is my understanding of the ESPHome description here.

1 Like

Thanks those are helpful insights.

You are correct and I misread ADC for DAC. Here is the page where they recommend to use a 12-bit ADC or higher.

A 16-bit ADC is supposed to give 0.5% accuracy while a 10-bit ADC would give 2% accuracy if I read it correctly.

I conclude that it is highly recommended to use an ESP32 rather than an ES8266:

  • The ESP8266 has one 10-bit ADC where the ESP32 has 12-bit ADCs.
  • Moreover by default an ESP8266 can only measure the 0-1V range, where the ESP32 can be configured up to 3.9V (see here).

So it seems like if you use an ESP8266 with the DFRobot supplied sensor (which measures 0-5m) the result would be:

  • You can measure 0-151cm as it should give a reading of 1v at 151cm which would be the max that many ESP8266 can read.
  • The accuracy of the reading would be 10cm (2% of the max range of the sensor of 500cm).

I think I will actually order the pressure sensor from Aliexpress, because then I can get one that measures 0-2meters, which should give me more accurate results for my tank (1.7m high) than the 0-5m sensor which is sold on DFRobot. Assuming that the ESP32’s 12-bit ADC will allow for an accuracy of about 1% that should give an accuracy of 200cm*1%=2cm.

The current to voltage converter is also available on Aliexpress.

EDIT: it turns out that it’s also ultra-cheap to add a 16-bit ADC to the ESP32 to get even more accurate results if you want it (probably not necessary) or I guess it can also be used on an ESP8266 to get the sensor working properly on that board as well. More info here.

1 Like

These very cheap current to voltage converters are also available:


Not sure whether it has any downsides to the DFRobot version.
In terms of set-up it is a bit confusing:
There are jumpers and the instructions say that in order to get 0-3.3V for 0-20ma you have to make sure both jumper caps are installed.
In addition, when the water tank is empty you have to turn one potentiometer until the output is 0V, then when the water tank is full you have to turn the other potentiometer until the output is 3.3V.

Also you would need to feed this one with the same power supply as you use for the water level sensor as it requires >7V so more than the ESP32 will provide.
In general it seems more flexible than the DFRobot version.

Downside seem to be related to ESP8266 - cannot limit output voltage to 1V.
I am waiting for delivery (in 1-2 weeks) of exactly those. Will connect to ESP8266 and since my sensor is for 5m and my tank has max depth of 1,4m then it should be fine to have output voltage within 1V limit. In worst case I move to ESP32.
I am hoping for more stable readings as it says (in my seller page) that this module is using “high precision low temperature drift ring resistor 0,1% 30ppm” which for sure is way better then resistor I have used.
Now, after reading this carefully and checking some data it may turn that NodeMCU ESP8266 has 0-3,3V input on A0. Only bare ESP8266 have 1V limit - will check this today.

Now for your calculation of accuracy I read that my sensor has 0,2-0,5% FS accuracy which is max 2,5cm. Then current-voltage converter also introduces error - not clear but I assume resistor tolerance 0,1% (another 0,5cm) and then comes ADC on ESP8266. With 10 bits it has resolution of 1024 which translates to 0,1% - then some tolerance has to be added to analogue conversion (could not find data for this) - I assume 0,2%.
So in total I have in worst case 0,9% (0,5+0,1+0,1+0,2) which means 5cm. In my case of 1,4m max level this translates to 3,5%… This is theory which I will soon verify (have easy access to my tank so I can measure the level and compare with reading).

1 Like

Thank you @quizzical !
With your posts I have discovered that my NodeMCU ESP8266 can measure up to 3,3V on A0!

Tested and confirmed with multimeter.
This is actually described on ESPHome page (for some reason I have not paid enough attention to it - or did not fully understand) that some NodeMCU’s have internal devider and in order to read real voltage the multiply: 3.3 filter has to be applied.
Now it will be easier to attach current-voltage converter and accuracy should be also better.

1 Like

Excellent, glad I was able to help!
In case anyone is looking for a 24V power supply to feed the pressure sensor, I’m planning to buy this one from Meanwell. To my very limited understanding it’s quite fine to buy low-voltage things from Aliexpress, but for devices that operate on 110-230V AC power it’s safest (electrocution/fire hazard) to buy from western countries (even if I have no doubt the actual production is in China).

You can also consider DC-DC step up converter to achieve from power supply of ESP (5V) by “stepping” it up to 24V. Or you plan to provide 24V and then step down to 5V to supply to ESP.

Yes my setup will be as follows:
I convert 230V AC to 24V DC and feed that into:

Mentally it somehow made more sense for me to convert 230V to 24V and then to 5V than to go from 230V down to 5V and then back up to 24V :slight_smile: Without any knowledge on the matter I assumed it would be more power efficient that way.

Do you feed a sufficiently powerful (or would 1A=5W be sufficient?) 5V USB power supply into your ESP8266 and then connect a DC-DC step up converter to the 5V and GND of your ESP to achieve 24V for the sensor and current to voltage converter?

2 Likes

Hey, I’m trying to adjust this to suit my little reservoir but I’m doing something wrong. I was going to keep the sensor 22cm above the full water line as recommended. The res is full at 21cm. Can you help me out with the equation? thanks

tank height: 30.5cm

# height from full to sensor: 22cm (essentially sensors minimum distance)
# full tank: 21cm
# current reading: x
# ergo: used = (x - 22)
# ergo: remaining = (21 - used)
# ergo: percent = (remaining / 21) * 100

filters:
  - lambda: return (1-((x-.22)/21))*100;
  - filter_out: nan
1 Like