Can dashboard switch be used to interupt or block a flow?

54 views
Skip to first unread message

timothym...@gmail.com

unread,
Apr 20, 2018, 12:28:58 PM4/20/18
to Node-RED
Can I place a switch between two nodes in order to 1) in one position allow the flow, and 2) in another position stop the flow of information between them?  3) Can I load the switch with a value that it will then send to the second node instead of what was previously going to pass through the switch?

Mark Setrem

unread,
Apr 20, 2018, 12:37:20 PM4/20/18
to Node-RED


On Friday, 20 April 2018 17:28:58 UTC+1, timothym...@gmail.com wrote:
Can I place a switch between two nodes in order to 1) in one position allow the flow, and 2) in another position stop the flow of information between them? 

Yes, you can use a second flow where a dashboard switch changes the value of a global variable.  Which your first flow node uses to define the path taken in a switch node



3) Can I load the switch with a value that it will then send to the second node instead of what was previously going to pass through the switch?
No but you can follow a switch node with a change node to do that.

timothym...@gmail.com

unread,
Apr 20, 2018, 3:36:04 PM4/20/18
to Node-RED
Found this in the Node Red Library:


which gives an example of using a switch to inject a value into a function, that stops that function from passing through messages from another node.  I will see if I can modify it for my purposes.

timothym...@gmail.com

unread,
Apr 20, 2018, 4:10:35 PM4/20/18
to Node-RED
So I am tinkering with this example, and the switch provides true or false, which permits the function to pass on the time stamp to the output.  But if I try anything other than a time stamp, such as a node that injects a number, it doesn't work for me.  What am I not understanding?


[{"id":"53df21e9.a21a3","type":"ui_switch","z":"b94485da.7ea138","name":"","label":"Message over ride switch","group":"609a4324.b967cc","order":0,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"gate","style":"","onvalue":"false","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"true","offvalueType":"bool","officon":"","offcolor":"","x":153,"y":1016,"wires":[["334615e1.4220ca"]]},{"id":"334615e1.4220ca","type":"function","z":"b94485da.7ea138","name":"control","func":"if (msg.topic === \"gate\") {\n    context.pass = (msg.payload === true) ? true : false;\n    return null;  // exit out early as it's just the control\n}\nif (context.pass) {\n    return msg; // if enabled pass msg\n}\nreturn null; // or drop it","outputs":1,"noerr":0,"x":328,"y":1058,"wires":[["10d243f.a851cbc"]]},{"id":"d2316b66.880828","type":"inject","z":"b94485da.7ea138","name":"","topic":"","payload":"","payloadType":"date","repeat":"1","crontab":"","once":false,"onceDelay":"","x":162.5,"y":1100,"wires":[["334615e1.4220ca"]]},{"id":"10d243f.a851cbc","type":"ui_text","z":"b94485da.7ea138","group":"609a4324.b967cc","order":0,"width":0,"height":0,"name":"","label":"text","format":"{{msg.payload}}","layout":"row-spread","x":533.5,"y":1111.3499755859375,"wires":[]},{"id":"609a4324.b967cc","type":"ui_group","z":"","name":"Column two","tab":"2b7805f9.7f5ada","disp":true,"width":"6","collapse":false},{"id":"2b7805f9.7f5ada","type":"ui_tab","z":"b94485da.7ea138","name":"Home","icon":"dashboard"}]

Colin Law

unread,
Apr 20, 2018, 4:31:57 PM4/20/18
to node...@googlegroups.com
On 20 April 2018 at 21:10, <timothym...@gmail.com> wrote:
So I am tinkering with this example, and the switch provides true or false, which permits the function to pass on the time stamp to the output.  But if I try anything other than a time stamp, such as a node that injects a number, it doesn't work for me.  What am I not understanding?

A timestamp is a number so there should be no difference.  Try changing the inject node to inject a number and see if it works then.  There must be something more subtle going on, put debug nodes on the outputs of all the relevant nodes and see if that helps to work out what the problem is.  If you still can't get it to work post a failing flow that shows the problem.

Colin
 


[{"id":"53df21e9.a21a3","type":"ui_switch","z":"b94485da.7ea138","name":"","label":"Message over ride switch","group":"609a4324.b967cc","order":0,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"gate","style":"","onvalue":"false","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"true","offvalueType":"bool","officon":"","offcolor":"","x":153,"y":1016,"wires":[["334615e1.4220ca"]]},{"id":"334615e1.4220ca","type":"function","z":"b94485da.7ea138","name":"control","func":"if (msg.topic === \"gate\") {\n    context.pass = (msg.payload === true) ? true : false;\n    return null;  // exit out early as it's just the control\n}\nif (context.pass) {\n    return msg; // if enabled pass msg\n}\nreturn null; // or drop it","outputs":1,"noerr":0,"x":328,"y":1058,"wires":[["10d243f.a851cbc"]]},{"id":"d2316b66.880828","type":"inject","z":"b94485da.7ea138","name":"","topic":"","payload":"","payloadType":"date","repeat":"1","crontab":"","once":false,"onceDelay":"","x":162.5,"y":1100,"wires":[["334615e1.4220ca"]]},{"id":"10d243f.a851cbc","type":"ui_text","z":"b94485da.7ea138","group":"609a4324.b967cc","order":0,"width":0,"height":0,"name":"","label":"text","format":"{{msg.payload}}","layout":"row-spread","x":533.5,"y":1111.3499755859375,"wires":[]},{"id":"609a4324.b967cc","type":"ui_group","z":"","name":"Column two","tab":"2b7805f9.7f5ada","disp":true,"width":"6","collapse":false},{"id":"2b7805f9.7f5ada","type":"ui_tab","z":"b94485da.7ea138","name":"Home","icon":"dashboard"}]

On Friday, April 20, 2018 at 2:36:04 PM UTC-5, timothym...@gmail.com wrote:
Found this in the Node Red Library:


which gives an example of using a switch to inject a value into a function, that stops that function from passing through messages from another node.  I will see if I can modify it for my purposes.


On Friday, April 20, 2018 at 11:28:58 AM UTC-5, timothym...@gmail.com wrote:
Can I place a switch between two nodes in order to 1) in one position allow the flow, and 2) in another position stop the flow of information between them?  3) Can I load the switch with a value that it will then send to the second node instead of what was previously going to pass through the switch?

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/66702f20-6297-4a5b-b888-9e0074a8dd24%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

timothym...@gmail.com

unread,
Apr 20, 2018, 4:45:28 PM4/20/18
to Node-RED
I have tried it with just injecting a number instead of a time stamp, and I find that unless I cycle the switch once, on and then off, it won't pass thru the injected number.

Actually just tried it with the time stamp example and it does the same thing.  Unless you first cycle the switch on, which is what is supposed to block the message, then switch it off, it starts off in blocking mode.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

timothym...@gmail.com

unread,
Apr 20, 2018, 4:49:00 PM4/20/18
to Node-RED
It's probably because I thought I could just switch true and false on the switch and get a reversal of the switch, where it by default passes on the message, unless turned on to block it.  Seems the code doesn't work that way.  Once again, my fault.

Colin Law

unread,
Apr 20, 2018, 4:51:14 PM4/20/18
to node...@googlegroups.com
That is because on startup context.pass is undefined (since you have not yet written to it). If you want it to always pass on initial deploy or restart then change
if (context.pass) {
to
if (context.pass !== false) {
then it will pass if context.pass is undefined or true.

Colin

To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

timothym...@gmail.com

unread,
Apr 20, 2018, 5:00:36 PM4/20/18
to Node-RED
Thanks Colin, 

I promise that I will some day become more self sufficient in this.  Slowly.

Tim
Message has been deleted

Colin Law

unread,
Apr 21, 2018, 3:36:26 AM4/21/18
to node...@googlegroups.com
Examine the first line of your Threshold Distance function node.

Colin

On 21 April 2018 at 01:25, <timothym...@gmail.com> wrote:
Can you take a look at this flow?  For some reason, I can't get any output from the last control that the message override switch controls.  The debug feeding into it shows there is input of 90 when triggered, but the debug on the output shows nothing.  Maybe its the naming of the message (gate : msg.payload : number) ?

[{"id":"77f298e7.40cb18","type":"function","z":"fe166526.3bb3b8","name":"+ Check  Deviation","func":"if (msg.payload > 30)\n{\nvar temp = \"Warning! \";\nmsg.payload = temp.concat(msg.payload);\nvar tag = \" Zero Distance\";\nmsg.payload = msg.payload.concat(tag);\nmsg.topic = 'check3';\nreturn msg;\n}","outputs":1,"noerr":0,"x":520.9000244140625,"y":2342.800048828125,"wires":[["9bffa656.ee9ed8"]]},{"id":"ebdbbb15.2db2c8","type":"function","z":"fe166526.3bb3b8","name":"control","func":"if (msg.topic === \"gate\") {\n    context.pass = (msg.payload === true) ? true : false;\n    return null;  // exit out early as it's just the control\n}\nif (context.pass) {\n    return msg; // if enabled pass msg\n}\nreturn null; // or drop it","outputs":1,"noerr":0,"x":335.8999938964844,"y":2345.800048828125,"wires":[["77f298e7.40cb18"]]},{"id":"8533b0e7.234a9","type":"function","z":"fe166526.3bb3b8","name":"send reset","func":"if (msg.payload === true)\n{\n    return null;\n}\nelse if (msg.payload === false)\n{\n    msg.payload = 'Distance OK';\n    return msg;\n}\n","outputs":1,"noerr":0,"x":317.8999938964844,"y":2397.800048828125,"wires":[["9bffa656.ee9ed8"]]},{"id":"9bffa656.ee9ed8","type":"trigger","z":"fe166526.3bb3b8","op1":"","op2":"0","op1type":"pay","op2type":"str","duration":"-5","extend":false,"units":"s","reset":"","bytopic":"all","name":"","x":529.9000244140625,"y":2398.800048828125,"wires":[["87fb84a5.83cdb8"]]},{"id":"87fb84a5.83cdb8","type":"ui_text","z":"fe166526.3bb3b8","group":"df51a64f.b86e98","order":0,"width":0,"height":0,"name":"Dist Warn","label":"","format":"{{msg.payload}}","layout":"row-spread","x":635.4000244140625,"y":2456,"wires":[]},{"id":"86431c0c.ebaf7","type":"arduino in","z":"fe166526.3bb3b8","name":"Sharp Distance Sensor Front","pin":"7","state":"ANALOG","arduino":"5204a4b7.37058c","x":135.89999389648437,"y":2263.800048828125,"wires":[["f7958914.8d6dd8"]]},{"id":"995de55f.f29c88","type":"ui_text","z":"fe166526.3bb3b8","group":"df51a64f.b86e98","order":7,"width":0,"height":0,"name":"","label":"IR dist frt (cm)","format":"{{40-msg.payload}}","layout":"row-spread","x":516.9000244140625,"y":2261.2001953125,"wires":[]},{"id":"f7958914.8d6dd8","type":"range","z":"fe166526.3bb3b8","minin":"100","maxin":"450","minout":"0","maxout":"40","action":"clamp","round":true,"property":"payload","name":"","x":342.9000549316406,"y":2260.60009765625,"wires":[["995de55f.f29c88","ebdbbb15.2db2c8","c35b473.964a6b8"]]},{"id":"c35b473.964a6b8","type":"function","z":"fe166526.3bb3b8","name":"threshold distance","func":" msg.topic = 'gate';\nif (msg.payload > 30)\n{\n    msg.payload = true;\n   return msg;\n}\nelse if (msg.payload < 29)\n{\n    msg.payload = false;\n    return msg;\n}\n","outputs":1,"noerr":0,"x":113,"y":2346,"wires":[["ebdbbb15.2db2c8","8533b0e7.234a9","f637a7a3.7237f8"]]},{"id":"f637a7a3.7237f8","type":"function","z":"fe166526.3bb3b8","name":"threshold distance","func":" msg.topic = 'gate';\nif (msg.payload === true)\n{\n    msg.payload = 90;\n   return msg;\n}\nelse if (msg.payload === false)\n{\n }\n","outputs":1,"noerr":0,"x":127,"y":2440,"wires":[["83f50445.ad3958","206520d.bf860e","966a157b.86eab8"]]},{"id":"83f50445.ad3958","type":"arduino out","z":"fe166526.3bb3b8","name":"Throttle Servo","pin":"24","state":"SERVO","arduino":"5204a4b7.37058c","x":631,"y":2554,"wires":[]},{"id":"206520d.bf860e","type":"debug","z":"fe166526.3bb3b8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":452.5,"y":2482.2999877929687,"wires":[]},{"id":"a3168d0f.09a74","type":"ui_switch","z":"fe166526.3bb3b8","name":"","label":"Message over ride switch","group":"df51a64f.b86e98","order":0,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"gate","style":"","onvalue":"false","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"true","offvalueType":"bool","officon":"","offcolor":"","x":135,"y":2607,"wires":[["966a157b.86eab8"]]},{"id":"966a157b.86eab8","type":"function","z":"fe166526.3bb3b8","name":"control","func":"if (msg.topic === \"gate\") {\n    context.pass = (msg.payload === true) ? true : false;\n    return null;  // exit out early as it's just the control\n}\nif (context.pass !== false) {\n    return msg; // if enabled pass msg\n}\nreturn null; // or drop it","outputs":1,"noerr":0,"x":341,"y":2609,"wires":[["770df7e9.ca0498","b5610c38.a34f7"]]},{"id":"770df7e9.ca0498","type":"ui_text","z":"fe166526.3bb3b8","group":"df51a64f.b86e98","order":0,"width":0,"height":0,"name":"","label":"text","format":"{{msg.payload}}","layout":"row-spread","x":528.5,"y":2631.35009765625,"wires":[]},{"id":"b5610c38.a34f7","type":"debug","z":"fe166526.3bb3b8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":574.5,"y":2716.300048828125,"wires":[]},{"id":"df51a64f.b86e98","type":"ui_group","z":"","name":"Column 3","tab":"4a3b55c0.22eccc","order":4,"disp":false,"width":"3","collapse":false},{"id":"5204a4b7.37058c","type":"arduino-board","z":"","device":"/dev/ttyACM0"},{"id":"4a3b55c0.22eccc","type":"ui_tab","z":"","name":"Robot Dashboard","icon":"dashboard"}]

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Colin Law

unread,
Apr 21, 2018, 6:28:55 AM4/21/18
to node...@googlegroups.com
On 21 April 2018 at 08:35, Colin Law <cla...@gmail.com> wrote:
Examine the first line of your Threshold Distance function node.


There are other problems with that node too. You don't seem to be returning anything if msg.payload is false, unless that is your intention.

Colin

timothym...@gmail.com

unread,
Apr 21, 2018, 10:46:17 AM4/21/18
to Node-RED
Yes, it took me until 1:00 am last night to figure that out.  I should not have left the topic as gate, as that is the topic that is rejected.  I changed the topic name, and it worked.  I didn't realize that a topic assigned to a message, continues that characteristic after it leaves the node.

timothym...@gmail.com

unread,
Apr 21, 2018, 10:48:26 AM4/21/18
to Node-RED
It's intentional, because the servo has already been loaded with an unpredictable speed value, and all I want to achieve is to change the value to 90, which is cease motion, if an obstacle is within the defined range.  This function needs to work whether the robot is going slow or fast.

Colin

Colin Law

unread,
Apr 21, 2018, 12:23:51 PM4/21/18
to node...@googlegroups.com
On 21 April 2018 at 15:46, <timothym...@gmail.com> wrote:
Yes, it took me until 1:00 am last night to figure that out.  I should not have left the topic as gate, as that is the topic that is rejected.  I changed the topic name, and it worked.  I didn't realize that a topic assigned to a message, continues that characteristic after it leaves the node.

That's the way node-red works. You set properties on a message and then pass the message on.  The next node gets the message so if you have set the topic then the message still has the topic set.  Topic is no different to payload as far as node red is concerned, it is just convention that payload is used for the data and topic is used to say something else.

Colin

 
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
Reply all
Reply to author
Forward
0 new messages