Here is a small portion of one of my flows that you can see the logic with
[{"id":"f81fa250.1486e","type":"mqtt in","z":"10b98a0a.175376","name":"Water Level in Tank","topic":"Tasmota/TankLevel/Distance/SENSOR","qos":"2","datatype":"json","broker":"e2ed99fb.aa45d8","x":890,"y":121,"wires":[["7fbc282b.c01c28"]]},{"id":"c4d66267.4e6b","type":"smooth","z":"10b98a0a.175376","name":"Average the Level of the tank readings","property":"payload","action":"mean","count":"20","round":"","mult":"single","reduce":false,"x":950,"y":180,"wires":[["8a33396e.6b9ad8","89b54359.9f211"]]},{"id":"8a33396e.6b9ad8","type":"function","z":"10b98a0a.175376","name":"Calculate Tank Percentage","func":"//Total Tank depth (TTD) = 153 CM\nvar OffSet = 0; //Deadspot in sensor with readings less than 20cm need to resolve by raising sensor\n//Sensor returns units in CM\n//Calculation needs to be \n//(TTD - Payload)/TTD\nvar SensorReading = msg.payload;\nvar TTD = 153; \nvar TankRemainingCapacity = (TTD - (SensorReading - OffSet))/TTD; //\nmsg.payload = Math.round(TankRemainingCapacity * 100);\nflow.set(\"TankRemainingCapacity\", msg.payload);\nnode.status({text:\"Sensor Reading \" + SensorReading});\nreturn msg;","outputs":1,"noerr":0,"x":920,"y":240,"wires":[["72920a79.7c5a34","4627097d.c66538","1db66ed6.4b9941"]]},{"id":"72920a79.7c5a34","type":"debug","z":"10b98a0a.175376","name":"Percentage Remaining","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","x":1200,"y":240,"wires":[]},{"id":"4627097d.c66538","type":"ui_gauge","z":"10b98a0a.175376","name":"Water Tank Level","group":"9252424c.768bd","order":0,"width":0,"height":0,"gtype":"wave","title":"Water Tank Level","label":"%","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":1190,"y":300,"wires":[]},{"id":"dbe1dbde.cda348","type":"comment","z":"10b98a0a.175376","name":"Get the Water Tank Level from the Ultrasonic Sensor","info":"","x":990,"y":80,"wires":[]},{"id":"1db66ed6.4b9941","type":"link out","z":"10b98a0a.175376","name":"LInk to re-enable Automatic scheduling","links":["b0dc343d.457298"],"x":1015,"y":300,"wires":[]},{"id":"89b54359.9f211","type":"debug","z":"10b98a0a.175376","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1230,"y":180,"wires":[]},{"id":"7fbc282b.c01c28","type":"change","z":"10b98a0a.175376","name":"","rules":[{"t":"move","p":"payload.SR04.Distance","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1160,"y":120,"wires":[["c4d66267.4e6b"]]},{"id":"e2ed99fb.aa45d8","type":"mqtt-broker","z":"","name":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"9252424c.768bd","type":"ui_group","z":"","name":"Water Tank Level","tab":"7ded32a1.cf8ccc","order":2,"disp":true,"width":"6","collapse":false},{"id":"7ded32a1.cf8ccc","type":"ui_tab","z":"","name":"Water Tank","icon":"","order":2}]
Craig