Need help controlling tank level with node red

Hi,

Im trying to control the level in a tank with node red on the raspberry pi 3. I have a low and high switch at the bottem and top of the tank and a solenooid to let in water. Im very new to coding and node red so i need some help. Ive done up a flow (I know its ugly) and it kind of works but, if the high switch is open before the low switch opens the solenoid still fills the tank.

[{“id”:“8757536f.2115c8”,“type”:“rpi-gpio in”,“z”:“e4167a44.5763d8”,“name”:“Low Level SW”,“pin”:“40”,“intype”:“up”,“debounce”:“25”,“read”:true,“x”:130,“y”:580,“wires”:[[“26fd1d82.81ceb2”,“580f2422.bc118c”,“42c5da7c.9a4054”,“a1c56cf5.c4674”,“e0876d8c.a7238”]]},{“id”:“ca05192d.5c77b8”,“type”:“rpi-gpio in”,“z”:“e4167a44.5763d8”,“name”:“High level SW”,“pin”:“38”,“intype”:“up”,“debounce”:“25”,“read”:true,“x”:130,“y”:520,“wires”:[[“39d1f00e.2be2b”]]},{“id”:“ec6b2cda.ba1df”,“type”:“rpi-gpio out”,“z”:“e4167a44.5763d8”,“name”:“Solenoid”,“pin”:“37”,“set”:true,“level”:“1”,“freq”:"",“out”:“out”,“x”:800,“y”:580,“wires”:[]},{“id”:“9138080f.2f1f28”,“type”:“change”,“z”:“e4167a44.5763d8”,“name”:“AND”,“rules”:[{“t”:“change”,“p”:“payload”,“pt”:“msg”,“from”:“0”,“fromt”:“num”,“to”:“0”,“tot”:“num”}],“action”:"",“property”:"",“from”:"",“to”:"",“reg”:false,“x”:610,“y”:580,“wires”:[[“ec6b2cda.ba1df”]]},{“id”:“e0876d8c.a7238”,“type”:“trigger”,“z”:“e4167a44.5763d8”,“op1”:“1”,“op2”:“1”,“op1type”:“num”,“op2type”:“num”,“duration”:“250”,“extend”:false,“units”:“ms”,“reset”:"",“bytopic”:“all”,“name”:"",“x”:420,“y”:580,“wires”:[[“9138080f.2f1f28”]]},{“id”:“39d1f00e.2be2b”,“type”:“change”,“z”:“e4167a44.5763d8”,“name”:“Invert output”,“rules”:[{“t”:“change”,“p”:“payload”,“pt”:“msg”,“from”:“1”,“fromt”:“num”,“to”:“0”,“tot”:“num”}],“action”:"",“property”:"",“from”:"",“to”:"",“reg”:false,“x”:410,“y”:520,“wires”:[[“9138080f.2f1f28”]]}]

This is what i need it to do,
if high switch = 0 & low switch = 0
send = 0
if high switch = 1 & low switch = 0
send = 0
if high switch = 1 & low switch = 1
send = 0
if high switch = 0 & low switch = 1
send = 1, untill high switch = 1

Ive used the trigger node to latch the low switch output to 1 untill the high switch sends 0. I think it can be done in a function node but i dont know how to seperate the 2 switch payloads coming in to the function node

Any help wil be appreciated
Jeremy

Jeremy,
I cant copy your Node-Red JSON into mine, it tells me its not a valid JSON.

Based on the sparse information to hand and my recent learnings…

One thing I have learned is that I cant pass variables from one node to another. You may need to string a collection of basic Functions and Switch cases together… Either that or create your own Javascript function up front and define how many outputs you need (one for each switch case perhaps??)

The other thing Ive found incredibly useful is to connect a debug node to the end of every function. This allows you to make sure the answer you expect from a node is indeed what is being passed on.

hth
Jon

not sure if this format is any different.

{"id":"8757536f.2115c8","type":"rpi-gpio in","z":"e4167a44.5763d8","name":"Low Level SW","pin":"40","intype":"up","debounce":"25","read":true,"x":130,"y":580,"wires":[["26fd1d82.81ceb2","580f2422.bc118c","42c5da7c.9a4054","a1c56cf5.c4674","e0876d8c.a7238"]]},{"id":"ca05192d.5c77b8","type":"rpi-gpio in","z":"e4167a44.5763d8","name":"High level SW","pin":"38","intype":"up","debounce":"25","read":true,"x":130,"y":520,"wires":[["39d1f00e.2be2b"]]},{"id":"ec6b2cda.ba1df","type":"rpi-gpio out","z":"e4167a44.5763d8","name":"Solenoid","pin":"37","set":true,"level":"1","freq":"","out":"out","x":800,"y":580,"wires":[]},{"id":"9138080f.2f1f28","type":"change","z":"e4167a44.5763d8","name":"AND","rules":[{"t":"change","p":"payload","pt":"msg","from":"0","fromt":"num","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":610,"y":580,"wires":[["ec6b2cda.ba1df"]]},{"id":"e0876d8c.a7238","type":"trigger","z":"e4167a44.5763d8","op1":"1","op2":"1","op1type":"num","op2type":"num","duration":"250","extend":false,"units":"ms","reset":"","bytopic":"all","name":"","x":420,"y":580,"wires":[["9138080f.2f1f28"]]},{"id":"39d1f00e.2be2b","type":"change","z":"e4167a44.5763d8","name":"Invert output","rules":[{"t":"change","p":"payload","pt":"msg","from":"1","fromt":"num","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":520,"wires":[["9138080f.2f1f28"]]}]

i think the easy thing to do would be use a function node but i dont know how to define the 2 variables from the 2 switches, need help with the ‘?’

var high switch=?         //msg.payload from high switch
var low switch=?          //msg.payload from low switch
var output=''
if (low switch=0 && high switch=0) {
    output=false
} else
if (low switch=0 && high switch=1) {
    output=false
} else
if (low switch=1 && high switch=1) {
    output=false
}else
if (low switch=1 && high switch=0) {
    output=true
msg.payload=0utput
return msg;

Forum tip - wrap code in the preformatted tool

image

1 Like

Add a debug node to see the actual payload values, and a couple of change nodes to get the relevant data to process.

This page is a good place to start: https://nodered.org/docs/user-guide/messages

Yes I know you have the change nodes but I can’t read the code. :grinning:

1 Like
[{"id":"8757536f.2115c8","type":"rpi-gpio in","z":"e4167a44.5763d8","name":"Low Level SW","pin":"40","intype":"up","debounce":"25","read":true,"x":150,"y":720,"wires":[["e0876d8c.a7238"]]},{"id":"ca05192d.5c77b8","type":"rpi-gpio in","z":"e4167a44.5763d8","name":"High level SW","pin":"38","intype":"up","debounce":"25","read":true,"x":150,"y":660,"wires":[["12edd188.79e84e"]]},{"id":"ec6b2cda.ba1df","type":"rpi-gpio out","z":"e4167a44.5763d8","name":"Solenoid","pin":"19","set":true,"level":"1","freq":"","out":"out","x":820,"y":720,"wires":[]},{"id":"9138080f.2f1f28","type":"change","z":"e4167a44.5763d8","name":"AND","rules":[{"t":"change","p":"payload","pt":"msg","from":"0","fromt":"num","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":650,"y":720,"wires":[["ec6b2cda.ba1df"]]},{"id":"e0876d8c.a7238","type":"trigger","z":"e4167a44.5763d8","op1":"1","op2":"1","op1type":"num","op2type":"num","duration":"250","extend":false,"units":"ms","reset":"","bytopic":"all","name":"","x":440,"y":720,"wires":[["9138080f.2f1f28","26fd1d82.81ceb2","580f2422.bc118c","42c5da7c.9a4054"]]},{"id":"12edd188.79e84e","type":"change","z":"e4167a44.5763d8","name":"Invert output","rules":[{"t":"change","p":"payload","pt":"msg","from":"1","fromt":"num","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":660,"wires":[["9138080f.2f1f28"]]}]

Maybe i could try somthing like this but i dont know how assign the variables from the 2 switches,

 [{"id":"abfdc92b.0f7be8","type":"rpi-gpio in","z":"e4167a44.5763d8","name":"Low Level SW","pin":"40","intype":"up","debounce":"25","read":true,"x":510,"y":1100,"wires":[["c7cf5cde.303d3"]]},{"id":"d398d6cb.dc6c5","type":"rpi-gpio in","z":"e4167a44.5763d8","name":"High level SW","pin":"38","intype":"up","debounce":"25","read":true,"x":510,"y":1040,"wires":[["c7cf5cde.303d3"]]},{"id":"c7cf5cde.303d3","type":"function","z":"e4167a44.5763d8","name":"","func":"var high_switch= ? //msg.payload from high switch\nvar low_switch= ?  //msg.payload from low switch\nvar output;\n\nif (low_switch==0 && high_switch==0) {\n    output=false\n}else\nif (low_switch==0 && high_switch==1) {\n    output=false\n}else  \nif (low_switch==1 && high_switch==1) { \n    output=false\n}else\nif (low_switch==1 && high_switch==0) {\n    output=true\n}\nmsg.payload=output\nreturn msg;","outputs":1,"noerr":8,"x":730,"y":1080,"wires":[["d24cc080.e907f"]]},{"id":"d24cc080.e907f","type":"rpi-gpio out","z":"e4167a44.5763d8","name":"Solenoid","pin":"19","set":true,"level":"1","freq":"","out":"out","x":900,"y":1080,"wires":[]}]

if i use
var low_switch=msg.payload
var high_switch=msg.payload
how would the function node know which is which

Maybe i could try somthing like this but i dont know how assign the variables from the 2 switches,

[{"id":"abfdc92b.0f7be8","type":"rpi-gpio in","z":"e4167a44.5763d8","name":"Low Level SW","pin":"40","intype":"up","debounce":"25","read":true,"x":510,"y":1100,"wires":[["c7cf5cde.303d3"]]},{"id":"d398d6cb.dc6c5","type":"rpi-gpio in","z":"e4167a44.5763d8","name":"High level SW","pin":"38","intype":"up","debounce":"25","read":true,"x":510,"y":1040,"wires":[["c7cf5cde.303d3"]]},{"id":"c7cf5cde.303d3","type":"function","z":"e4167a44.5763d8","name":"","func":"var high_switch= ? //msg.payload from high switch\nvar low_switch= ?  //msg.payload from low switch\nvar output;\n\nif (low_switch==0 && high_switch==0) {\n    output=false\n}else\nif (low_switch==0 && high_switch==1) {\n    output=false\n}else  \nif (low_switch==1 && high_switch==1) { \n    output=false\n}else\nif (low_switch==1 && high_switch==0) {\n    output=true\n}\nmsg.payload=output\nreturn msg;","outputs":1,"noerr":8,"x":730,"y":1080,"wires":[["d24cc080.e907f"]]},{"id":"d24cc080.e907f","type":"rpi-gpio out","z":"e4167a44.5763d8","name":"Solenoid","pin":"19","set":true,"level":"1","freq":"","out":"out","x":900,"y":1080,"wires":[]}]

if i use
var low_switch=msg.payload
var high_switch=msg.payload
how would the function node know which is which

Jeremy,
This is a very basic demo of node red on the RPI, but I think it is just what you are trying to do.

https://projects.raspberrypi.org/en/projects/getting-started-with-node-red

It is the actual node red screens, so no need for copying code. Hope it helps.

Two input conditions from each pin.

2 Likes

Here is picture of what i would do if i were to hard wire it, this is the logic i am trying to but into node red.