How would you do this? (Dashboard Switch)

I would like to put a switch on the dashboard that simply interrupts a flow. I have a BigTimer that controls my lawn sprinkler flow. It works great- on schedule, the BigTimer starts a cascading set of timers to control my Sonoff 4CH relays connected to the sprinkler valves..

There are times that I don't want the sprinkler cycle to run. I want a switch on the dashboard to interrupt the flow from the BigTimer to the cascading timers- basically a manual "off" switch.

So, before I embark on developing functions to remember the switch position, (can I even determine the switch position without touching it?) I am wondering if there's an easier way. Here is what I would like to do:
_NR-DoStuff
Any ideas would be appreciated.

Separate the flows.

[Dashboard switch]--------[Change Node]

  • set Global.switch to "Off"/"On"

[Big Timer]------[Logical Switch Node]-------[Do Stuff]

  • In the logical switch Global.switch === "On"

The Big Timer triggers but the Dashboard switch via the Global Variable blocks messages from continuing dependant on the Logical Switch

1 Like

Many thanks- I needed that nudge. Here's how I implemented your suggestion:

Here's the flow if you wish to critique my coding. Really, feedback would be appreciated since I can probably count the functions that I've written on two hands.

[{"id":"62b3e433.50a51c","type":"inject","z":"f5516e25.5a314","name":"BigTimer analog","topic":"","payload":"SprinklerStart","payloadType":"str","repeat":"","crontab":"","once":false,"x":241.5,"y":2628.300048828125,"wires":[["108b42a2.365bed"]]},{"id":"e84548f4.14be88","type":"ui_switch","z":"f5516e25.5a314","name":"","label":"switch","group":"a2047e9a.89065","order":1,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":199.5,"y":2526.099853515625,"wires":[["eefd760d.7e7bf8"]]},{"id":"eefd760d.7e7bf8","type":"function","z":"f5516e25.5a314","name":"Save the state of the sprinkler system master switch","func":"global.set(\"SprinklerMaster\",msg.payload);  \n\n//For debugging\nvar SpMaster = global.get(\"SprinklerMaster\");\nmsg.payload = SpMaster;\nreturn msg;","outputs":"1","noerr":0,"x":481,"y":2526,"wires":[["fd25ee91.2c63b"]]},{"id":"108b42a2.365bed","type":"function","z":"f5516e25.5a314","name":"Test the master switch","func":"var SpMaster = global.get(\"SprinklerMaster\");\nvar SpTime = global.get(\"SprinklerOnTime\");\n\nif (SpMaster=== true){\n  if (msg.payload === \"SprinklerStart\") {\n    msg.payload = \"timer \" + SpTime;\n    return msg;\n  }\n}\n","outputs":1,"noerr":0,"x":487,"y":2628,"wires":[["def059dd.f79c08"]]},{"id":"fd25ee91.2c63b","type":"debug","z":"f5516e25.5a314","name":"","active":true,"console":"false","complete":"false","x":787.5,"y":2521.449996948242,"wires":[]},{"id":"def059dd.f79c08","type":"debug","z":"f5516e25.5a314","name":"Do Stuff analog","active":true,"console":"false","complete":"payload","x":746.5,"y":2620.449951171875,"wires":[]},{"id":"5b93aac3.7fa5c4","type":"function","z":"f5516e25.5a314","name":"Set global variable, \"SprinklerMaster\" defaults \"ON\"","func":"//Times are in seconds.\nglobal.set(\"SprinklerMaster\",true);\n\nmsg.payload=\"on\";\nreturn msg;\n","outputs":"1","noerr":0,"x":464,"y":2414,"wires":[["e84548f4.14be88"]]},{"id":"b10d7a89.c3f438","type":"inject","z":"f5516e25.5a314","name":"On start","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"x":162,"y":2409,"wires":[["5b93aac3.7fa5c4"]]},{"id":"a2047e9a.89065","type":"ui_group","z":"","name":"Lawn Sprinklers","tab":"466151d7.ba2ed","order":1,"disp":true,"width":"3"},{"id":"466151d7.ba2ed","type":"ui_tab","z":"","name":"Sprinklers","icon":"dashboard","order":7}]

My crappy tutorial video on context variables could be a good fit for this, conceptually speaking.

A little solution without using function nodes

[{"id":"4f2d1972.379178","type":"change","z":"d15171ff.d7b71","name":"","rules":[{"t":"set","p":"switch","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":690,"y":160,"wires":[[]]},{"id":"d155a4db.542898","type":"bigtimer","z":"d15171ff.d7b71","outtopic":"","outpayload1":"On","outpayload2":"Off","name":"Big Timer","lat":"53","lon":"18","starttime":5001,"endtime":1425,"startoff":0,"endoff":0,"offs":0,"outtext1":"On","outtext2":"Off","timeout":1440,"sun":true,"mon":true,"tue":true,"wed":true,"thu":true,"fri":true,"sat":true,"jan":true,"feb":true,"mar":true,"apr":true,"may":true,"jun":true,"jul":true,"aug":true,"sep":true,"oct":true,"nov":true,"dec":true,"day1":0,"month1":0,"day2":0,"month2":0,"day3":0,"month3":0,"day4":0,"month4":0,"day5":0,"month5":0,"d1":0,"w1":0,"d2":0,"w2":0,"d3":0,"w3":0,"d4":0,"w4":0,"d5":0,"w5":0,"suspend":false,"random":false,"repeat":true,"atstart":true,"odd":false,"even":false,"x":280,"y":300,"wires":[["33b5d5e2.f8ee8a"],[],[]]},{"id":"33b5d5e2.f8ee8a","type":"switch","z":"d15171ff.d7b71","name":"","property":"switch","propertyType":"flow","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":500,"y":340,"wires":[["7b7de667.db60b8"],["4c0e8c50.b7ba84"]]},{"id":"4c0e8c50.b7ba84","type":"debug","z":"d15171ff.d7b71","name":"Stuff to do (if any) when switch is OFF (sprinkler blocked)","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":820,"y":370,"wires":[]},{"id":"7b7de667.db60b8","type":"debug","z":"d15171ff.d7b71","name":"Stuff to do when switch is ON (sprinkler allowed)","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":790,"y":310,"wires":[]},{"id":"62f6b88f.cd3448","type":"ui_switch","z":"d15171ff.d7b71","name":"","label":"switch","group":"55bce2e9.0701dc","order":0,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":270,"y":190,"wires":[["88fb2f5b.3c"]]},{"id":"88fb2f5b.3c","type":"switch","z":"d15171ff.d7b71","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":500,"y":190,"wires":[["4f2d1972.379178"],["1d68fc17.6dcda4"]]},{"id":"1d68fc17.6dcda4","type":"change","z":"d15171ff.d7b71","name":"","rules":[{"t":"set","p":"switch","pt":"flow","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":690,"y":220,"wires":[[]]},{"id":"e20d234c.0d994","type":"inject","z":"d15171ff.d7b71","name":"","topic":"","payload":"test","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":270,"y":390,"wires":[["33b5d5e2.f8ee8a"]]},{"id":"495c0404.13ac1c","type":"inject","z":"d15171ff.d7b71","name":"Initial (sprinkler is allowed)","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":true,"onceDelay":"0.1","x":340,"y":110,"wires":[["62f6b88f.cd3448"]]},{"id":"55bce2e9.0701dc","type":"ui_group","z":"","name":"The Switch","tab":"7f04f84.36f2908","disp":true,"width":"6","collapse":false},{"id":"7f04f84.36f2908","type":"ui_tab","z":"","name":"My Switch","icon":"dashboard"}]

2 Likes

Thanks. I learned something today. I probably should look closer at the switch node before I write a function; I did not know that I could test a context variable there.

Steve

@SteveMann Take a look at node-red-contrib-simple-switch, just uploaded today. The second flow in the second example does pretty much what you want.

I don't see it in Manage Palette.

@SteveMann That's odd. I find it in the Install pane, searching on "simple-gate". You might need to refresh your browser if it's been running more than a day or so.

This is a variant using the simple-switch node, works fine, elegant, simplifies the flow and gives a better understanding of the functionality I think, no need for setting flow or global variables

The inject node is not needed for the gate itself, it is there to initially also synchronize the ui for the switch in the dashboard

[{"id":"9a30e0ea.904bf","type":"gate","z":"d15171ff.d7b71","name":"","controlTopic":"control","defaultState":"open","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","x":490,"y":670,"wires":[["6608f667.fc5a78"]]},{"id":"3c1afa.c0f75506","type":"ui_switch","z":"d15171ff.d7b71","name":"","label":"switch gate","group":"55bce2e9.0701dc","order":0,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"control","style":"","onvalue":"open","onvalueType":"str","onicon":"","oncolor":"","offvalue":"close","offvalueType":"str","officon":"","offcolor":"","x":290,"y":590,"wires":[["9a30e0ea.904bf"]]},{"id":"e36fb44a.28ad68","type":"inject","z":"d15171ff.d7b71","name":"","topic":"","payload":"test","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":270,"y":750,"wires":[["9a30e0ea.904bf"]]},{"id":"6608f667.fc5a78","type":"debug","z":"d15171ff.d7b71","name":"Stuff to do when switch is ON (sprinkler allowed)","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":780,"y":670,"wires":[]},{"id":"515a9cfd.982464","type":"bigtimer","z":"d15171ff.d7b71","outtopic":"","outpayload1":"On","outpayload2":"Off","name":"Big Timer","lat":"53","lon":"18","starttime":5001,"endtime":1425,"startoff":0,"endoff":0,"offs":0,"outtext1":"On","outtext2":"Off","timeout":1440,"sun":true,"mon":true,"tue":true,"wed":true,"thu":true,"fri":true,"sat":true,"jan":true,"feb":true,"mar":true,"apr":true,"may":true,"jun":true,"jul":true,"aug":true,"sep":true,"oct":true,"nov":true,"dec":true,"day1":0,"month1":0,"day2":0,"month2":0,"day3":0,"month3":0,"day4":0,"month4":0,"day5":0,"month5":0,"d1":0,"w1":0,"d2":0,"w2":0,"d3":0,"w3":0,"d4":0,"w4":0,"d5":0,"w5":0,"suspend":false,"random":false,"repeat":true,"atstart":true,"odd":false,"even":false,"x":280,"y":670,"wires":[["9a30e0ea.904bf"],[],[]]},{"id":"14246beb.8e8fa4","type":"inject","z":"d15171ff.d7b71","name":"Initial (sprinkler is allowed)","topic":"","payload":"open","payloadType":"str","repeat":"","crontab":"","once":true,"onceDelay":"0.1","x":340,"y":510,"wires":[["3c1afa.c0f75506"]]},{"id":"55bce2e9.0701dc","type":"ui_group","z":"","name":"The Switch","tab":"7f04f84.36f2908","disp":true,"width":"6","collapse":false},{"id":"7f04f84.36f2908","type":"ui_tab","z":"","name":"My Switch","icon":"dashboard","order":1}]

Another variant using only core nodes (besides BigTimer) and no flow context

 [{"id":"1287cdd4.e3b802","type":"debug","z":"e04ff427.d9a7b8","name":"Sprinkler On","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1050,"y":820,"wires":[]},{"id":"8df4f262.85a27","type":"change","z":"e04ff427.d9a7b8","name":"","rules":[{"t":"set","p":"timer","pt":"msg","to":"payload","tot":"msg"},{"t":"delete","p":"payload","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":840,"wires":[["6e22a391.e644bc"]]},{"id":"bc582185.a281e","type":"switch","z":"e04ff427.d9a7b8","name":"timer","property":"timer","propertyType":"msg","rules":[{"t":"eq","v":"On","vt":"str"},{"t":"eq","v":"Off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":870,"y":840,"wires":[["1287cdd4.e3b802"],["ce5f36c1.38d6e8"]]},{"id":"ce5f36c1.38d6e8","type":"debug","z":"e04ff427.d9a7b8","name":"Sprinkler Off","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1050,"y":860,"wires":[]},{"id":"1d39c644.94ad9a","type":"bigtimer","z":"e04ff427.d9a7b8","outtopic":"","outpayload1":"On","outpayload2":"Off","name":"Big Timer","lat":"53","lon":"18","starttime":5001,"endtime":1425,"startoff":0,"endoff":0,"offs":0,"outtext1":"On","outtext2":"Off","timeout":1440,"sun":true,"mon":true,"tue":true,"wed":true,"thu":true,"fri":true,"sat":true,"jan":true,"feb":true,"mar":true,"apr":true,"may":true,"jun":true,"jul":true,"aug":true,"sep":true,"oct":true,"nov":true,"dec":true,"day1":0,"month1":0,"day2":0,"month2":0,"day3":0,"month3":0,"day4":0,"month4":0,"day5":0,"month5":0,"day6":"","month6":"","d1":0,"w1":0,"d2":0,"w2":0,"d3":0,"w3":0,"d4":0,"w4":0,"d5":0,"w5":0,"d6":"","w6":"","xday1":"","xmonth1":"","xday2":"","xmonth2":"","xday3":"","xmonth3":"","xday4":"","xmonth4":"","xday5":"","xmonth5":"","xday6":"","xmonth6":"","xd1":"","xw1":"","xd2":"","xw2":"","xd3":"","xw3":"","xd4":"","xw4":"","xd5":"","xw5":"","xd6":"","xw6":"","suspend":false,"random":false,"repeat":true,"atstart":true,"odd":false,"even":false,"x":240,"y":840,"wires":[["8df4f262.85a27"],[],[]]},{"id":"6e22a391.e644bc","type":"ui_switch","z":"e04ff427.d9a7b8","name":"","label":"switch gate","group":"49d3ecbe.787174","order":0,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"control","style":"","onvalue":"open","onvalueType":"str","onicon":"","oncolor":"","offvalue":"close","offvalueType":"str","officon":"","offcolor":"","x":590,"y":840,"wires":[["6095eea4.66436"]]},{"id":"a4c9ef47.e1947","type":"inject","z":"e04ff427.d9a7b8","name":"","topic":"","payload":"On","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":720,"wires":[["8df4f262.85a27"]]},{"id":"2361f05a.80d1a","type":"inject","z":"e04ff427.d9a7b8","name":"","topic":"","payload":"Off","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":231,"y":769,"wires":[["8df4f262.85a27"]]},{"id":"5dfe6e.489f0194","type":"inject","z":"e04ff427.d9a7b8","name":"","topic":"","payload":"open","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":450,"y":720,"wires":[["6e22a391.e644bc"]]},{"id":"79c20e78.c6063","type":"inject","z":"e04ff427.d9a7b8","name":"","topic":"","payload":"close","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":450,"y":780,"wires":[["6e22a391.e644bc"]]},{"id":"6095eea4.66436","type":"switch","z":"e04ff427.d9a7b8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"open","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":730,"y":840,"wires":[["bc582185.a281e"]]},{"id":"49d3ecbe.787174","type":"ui_group","z":"","name":"The Switch","tab":"3257867d.17dfca","disp":true,"width":"6","collapse":false},{"id":"3257867d.17dfca","type":"ui_tab","z":"","name":"My Switch","icon":"dashboard","order":1}]

That worked.

But I find the meaning of "Open" and "Closed" in this node backwards. As a long-time electrician, "Open" means the switch is open and current cannot flow, "closed" means the switch is closed and current flows.

Whoever wrote the node must be a plumber.

That's why it's called a "gate", not a "switch". There already is a switch node. Sorry.

I won't touch that. :slightly_smiling_face: