Water Level Meter - SR04M-2

Hello, I am using the same sensor for my watertank and had initially a lot of reading errors especially when the tank was nearly full.

I'm using the sensor on an ESP running Tasmota but the code you shared is following the same approach. The ESP and the sensor is powered by a 12v Solar System with battery .

Tasmota provides you all the data you need out of the box .

Just connect the sensor to the pins and configure as below

The issue with this sensor is that it gives a WRONG/Incorrect reading when the distance from sensor to water is less the 21cm (it might be as well less than 23cm depending on tolerance of your sensor!)

I'm using this 3D printed case for my sensor and have it mounted above the maximum water level (~22cm above water).

The 3D print was rough in the inside of the cone and I used a 250 sanding paper to make it very smooth which improved the reading quality significantly (without sanding I still got false readings)

Now my readings are very stable for a while. The stepwise reduction is when my daily irrigation system is running. Than you see a spike at 01/05 where it goes to maximum, here we had a lot of rain which filled the tank to 100%. Than we had a few rainy days where no irrigation was running upto 01/12 where the irrigation started again.

Below are the RAW-Data from Tasmota for the sensor(no filter), and you can see that the readings are very stable

Below is my flow in which I use a filter to eliminate false readings

[{"id":"50e1ebdab3029328","type":"tab","label":"Tank","disabled":false,"info":"","env":[]},{"id":"9bd1a940ed96ee45","type":"debug","z":"50e1ebdab3029328","name":"Volume","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1540,"y":120,"wires":[]},{"id":"1c665f48aef699cf","type":"tank-volume","z":"50e1ebdab3029328","name":"","tankType":"cone_top","inputField":"payload","outputField":"payload","measurement":"above","inputUnit1":"cm","inputUnit2":"l","outputUnit":"l","topLimit":0,"bottomLimit":0,"diameter":0,"length":0,"width":0,"height":0,"length2":0,"width2":0,"height2":0,"coneHeight":"50","cylinderHeight":"119","diameterTop":"30","diameterBottom":"183","customTable":[],"x":1320,"y":80,"wires":[["9bd1a940ed96ee45","410fb8767fb446ed","9def827cb68ebe5e"]]},{"id":"410fb8767fb446ed","type":"ui_level","z":"50e1ebdab3029328","group":"e1c153f31de947f3","order":1,"width":3,"height":3,"name":"","label":"","colorHi":"#000fe6","colorWarn":"#00ff59","colorNormal":"#d8964b","colorOff":"#ff0000","min":0,"max":"3440","segWarn":"2000","segHigh":"3000","unit":"L","layout":"sv","channelA":"","channelB":"","decimals":0,"animations":"soft","shape":"3","colorschema":"fixed","textoptions":"default","colorText":"#eeeeee","fontLabel":"","fontValue":"","fontSmall":"","colorFromTheme":true,"textAnimations":false,"hideValue":false,"tickmode":"auto","peakmode":false,"property":"payload.filledVolume","peaktime":3000,"x":1530,"y":40,"wires":[]},{"id":"5d6fd1cd656d0b67","type":"mqtt in","z":"50e1ebdab3029328","name":"","topic":"stat/WaterTank/STATUS10","qos":"2","datatype":"utf8","broker":"ab8bdac8.bb6898","nl":false,"rap":false,"inputs":0,"x":190,"y":80,"wires":[["93aa3619443bfaa9"]]},{"id":"578af7c09a4fe7b3","type":"inject","z":"50e1ebdab3029328","name":"","props":[{"p":"topic","vt":"str"},{"p":"payload"}],"repeat":"300","crontab":"","once":true,"onceDelay":"60","topic":"cmnd/WaterTank/Status","payload":"10","payloadType":"str","x":180,"y":240,"wires":[["2294e2bba31a121a","86a1d90c5e385057"]]},{"id":"9898ad5a44ead344","type":"link out","z":"50e1ebdab3029328","name":"","links":["212504ad.d93e5c","83260501.ed4468"],"x":995,"y":240,"wires":[]},{"id":"16c8130f72b9d599","type":"ui_button","z":"50e1ebdab3029328","name":"","group":"e1c153f31de947f3","order":8,"width":0,"height":0,"passthru":false,"label":"Check","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"0","payloadType":"str","topic":"cmnd/WaterTank/Status","topicType":"str","x":250,"y":200,"wires":[["86a1d90c5e385057","2294e2bba31a121a"]]},{"id":"86a1d90c5e385057","type":"change","z":"50e1ebdab3029328","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"WaterTank/status","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":200,"wires":[["9898ad5a44ead344"]]},{"id":"2294e2bba31a121a","type":"delay","z":"50e1ebdab3029328","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":600,"y":240,"wires":[["9898ad5a44ead344"]]},{"id":"93aa3619443bfaa9","type":"json","z":"50e1ebdab3029328","name":"","property":"payload","action":"","pretty":false,"x":410,"y":80,"wires":[["ba43f34573f6edfc","9c6df35259e4d27e"]]},{"id":"ba43f34573f6edfc","type":"change","z":"50e1ebdab3029328","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.StatusSNS.SR04.Distance","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":80,"wires":[["a728bb6baefea3db","6c56968812dd883f"]]},{"id":"6c56968812dd883f","type":"ui_text","z":"50e1ebdab3029328","group":"e1c153f31de947f3","order":5,"width":0,"height":0,"name":"","label":"Level","format":"{{msg.payload}}","layout":"row-spread","className":"","x":830,"y":40,"wires":[]},{"id":"fa78d4cad29652fe","type":"ui_text","z":"50e1ebdab3029328","group":"e1c153f31de947f3","order":6,"width":0,"height":0,"name":"","label":"IP","format":"{{msg.payload.StatusNET.IPAddress}}","layout":"row-spread","className":"","x":590,"y":340,"wires":[]},{"id":"528fd9013e1ee4c7","type":"mqtt in","z":"50e1ebdab3029328","name":"","topic":"stat/WaterTank/STATUS5","qos":"2","datatype":"utf8","broker":"ab8bdac8.bb6898","nl":false,"rap":false,"inputs":0,"x":190,"y":340,"wires":[["efdc7d4357309cd4"]]},{"id":"efdc7d4357309cd4","type":"json","z":"50e1ebdab3029328","name":"","property":"payload","action":"","pretty":false,"x":410,"y":340,"wires":[["fa78d4cad29652fe","c0b99e6c6d6e985b"]]},{"id":"c0b99e6c6d6e985b","type":"debug","z":"50e1ebdab3029328","name":"Volume","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":600,"y":380,"wires":[]},{"id":"ab7b642f31ea4dd8","type":"mqtt in","z":"50e1ebdab3029328","name":"","topic":"stat/WaterTank2/PIR1","qos":"2","datatype":"utf8","broker":"ab8bdac8.bb6898","nl":false,"rap":false,"inputs":0,"x":200,"y":440,"wires":[["434ee9908e7e3965"]]},{"id":"434ee9908e7e3965","type":"debug","z":"50e1ebdab3029328","name":"Volume","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":600,"y":440,"wires":[]},{"id":"9c6df35259e4d27e","type":"ui_text","z":"50e1ebdab3029328","group":"e1c153f31de947f3","order":7,"width":0,"height":0,"name":"time","label":"time","format":"{{msg.payload.StatusSNS.Time}}","layout":"row-spread","className":"","x":590,"y":120,"wires":[]},{"id":"9def827cb68ebe5e","type":"influxdb out","z":"50e1ebdab3029328","influxdb":"a5b54fc0.d8c14","name":"WaterTank","measurement":"WaterTank","precision":"","retentionPolicy":"","database":"","retentionPolicyV18Flux":"","org":"","bucket":"","x":1550,"y":80,"wires":[]},{"id":"a728bb6baefea3db","type":"function","z":"50e1ebdab3029328","name":"< 23 ?","func":"let val=parseFloat(msg.payload);\n\nif (val<=23) val=23;\nmsg.payload=val\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":830,"y":80,"wires":[["64ea5cc51a0ec318"]]},{"id":"64ea5cc51a0ec318","type":"switch","z":"50e1ebdab3029328","name":"","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"23","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":970,"y":80,"wires":[["ba9034baeb0aaa00"],["b8a224846d219c50"]]},{"id":"b8a224846d219c50","type":"change","z":"50e1ebdab3029328","name":"get old","rules":[{"t":"set","p":"payload","pt":"msg","to":"Old_Value","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":1130,"y":100,"wires":[["1c665f48aef699cf"]]},{"id":"ba9034baeb0aaa00","type":"change","z":"50e1ebdab3029328","name":"set old","rules":[{"t":"set","p":"Old_Value","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1130,"y":60,"wires":[["1c665f48aef699cf"]]},{"id":"c8169110aa3c9ea7","type":"ui_spacer","z":"50e1ebdab3029328","name":"spacer","group":"e1c153f31de947f3","order":2,"width":3,"height":1},{"id":"c2e2d590ddd99e21","type":"ui_spacer","z":"50e1ebdab3029328","name":"spacer","group":"e1c153f31de947f3","order":3,"width":3,"height":1},{"id":"8a0453646a4267f1","type":"ui_spacer","z":"50e1ebdab3029328","name":"spacer","group":"e1c153f31de947f3","order":4,"width":3,"height":1},{"id":"e1c153f31de947f3","type":"ui_group","name":"Water1","tab":"51cd696686651003","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"ab8bdac8.bb6898","type":"mqtt-broker","name":"MQTT-org","broker":"192.168.2.199","port":"1883","clientid":"Node-Red","usetls":false,"compatmode":false,"protocolVersion":4,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"a5b54fc0.d8c14","type":"influxdb","hostname":"192.168.2.202","port":"8086","protocol":"http","database":"demo","name":"","usetls":false,"tls":"","influxdbVersion":"1.x","url":"http://192.168.2.195:8086","rejectUnauthorized":false},{"id":"51cd696686651003","type":"ui_tab","name":"Tank","icon":"dashboard","order":27,"disabled":false,"hidden":false}]

as said, the 3D printed mount did do the trick after it was smooth at the inside !!

good luck