How to get value into switch


#1

image

[{"id":"ac63b74.7550548","type":"ui_slider","z":"9501be91.4aa278","name":"","label":"slider","group":"2f45f695.6435ba","order":0,"width":0,"height":0,"passthru":true,"topic":"","min":0,"max":10,"step":1,"x":220,"y":60,"wires":[["b2ec8bb7.2fe658","a9b73294.97a8a8"]]},{"id":"b2ec8bb7.2fe658","type":"ui_text","z":"9501be91.4aa278","group":"2f45f695.6435ba","order":0,"width":0,"height":0,"name":"","label":"slider","format":"{{msg.payload}}","layout":"row-left","x":570,"y":60,"wires":[]},{"id":"a9b73294.97a8a8","type":"ui_switch","z":"9501be91.4aa278","name":"","label":"switch","group":"2f45f695.6435ba","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":370,"y":100,"wires":[["ad39b480.d4092"]]},{"id":"ad39b480.d4092","type":"ui_text","z":"9501be91.4aa278","group":"2f45f695.6435ba","order":0,"width":0,"height":0,"name":"","label":"text","format":"{{msg.payload}}","layout":"row-spread","x":570,"y":100,"wires":[]},{"id":"bf3399cb.da6f1","type":"comment","z":"9501be91.4aa278","name":"set default value","info":"","x":240,"y":20,"wires":[]},{"id":"74fdcdb0.b281c4","type":"comment","z":"9501be91.4aa278","name":"only be on when switch is on","info":"","x":440,"y":160,"wires":[]},{"id":"2f45f695.6435ba","type":"ui_group","z":"","name":"row 1","tab":"568a2c54.1e76f4","order":2,"disp":false,"width":"15","collapse":false},{"id":"568a2c54.1e76f4","type":"ui_tab","z":"","name":"Deathstar","icon":"dashboard","order":1}]

can anyone tell me why i dont get the value from the silder into the switch node?


[NEW FEATURE] node-red-contrib-queue-gate (version 1.1.0)
#2

To set the switch position by passing in a message the payload sent has to be one of the values specified for the switch, otherwise it does not know which position you mean. Since you have configured the switch to send boolean true and false then you must pass it one of those values.
[Edit] I wonder whether you are confused about what a dashboard switch does. It does not pass inputs through based on the switch setting. It only ever sends the values you tell it in the configuration. The purpose of the input is to allow setting the switch by passing it a message.


#3

If you join nodes together and it doesn’t give you the result you expect, the best thing to do is to attach debug nodes to see what the content of messages actually being passed.

But it would appear from your notes that you are mistaking the dashboard switch node with the standard switch node. The standard switch node can be configure to only let messages that match through which would appear to be what you are looking for


#4

The widget can be updated by an incoming msg.payload with the specified values, that must also match the type of the input (number, string, etc).

How do I get it to do this?

if slider is set to 4 then when I switch the button on I what 4 if I set the slider to 5 then I want 5
when I turn off I know how to set to false (easy) or could set to a different slider if only i know how!


#5

@ukmoose i get the same weather i use the debug or the text so not sure what you mean


#6

You can do this with node-red-contrib-simple-gate. Feed the switch and the slider separately into the gate node and you should be able to achieve what you want


#7

@Colin any reason why i cant, just using what i got as info seems to imply you can?

(" The widget can be updated by an incoming msg.payload with the specified values, that must also match the type of the input (number, string, etc).")


#8

Which means that if a switch is off on the display you can change to on by sending the correct value
and v.v


#9

In the screenshot of your flow you cannot see the messages going between the nodes. The debug node gives you this ability and is v useful when debugging flows


#10

That says that (for the way you have configured it) if you feed in a value of true the switch will be set on, if you feed in a value false it will be set off. You are feeding it numbers between 0 and 10 from the slider so the input messages will be ignored by the switch. As I said before the switch does not do what you think. It is not a gate that allows things through or not, all it does is send true or false when you switch it.


#11

@Colin ok so if its set to flow. or global. or even the JSON{} how does it work then?
I see now, what it does with the numbers, you are right when the number matches, the switch does turn on


#12

Presumably if it is set to flow or global then again the message coming in would have to match the current context value (I don't know because I virtually never use flow or global context). Presumably also with JSON it would have to match, but I am not entirely certain what that means. Nor can I think of a use for that.


#13

[{"id":"d81194ef.313d6","type":"gate","z":"389e6e43.7ac652","name":"gate 1","controlTopic":"control","defaultState":"open","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","x":450,"y":140,"wires":[["f2823cc0.7548c"]]},{"id":"a2c1bfc0.3be47","type":"status","z":"389e6e43.7ac652","name":"status 1","scope":["d81194ef.313d6"],"x":590,"y":180,"wires":[["4e7accb1.6a1244","c7ba6981.1c5ca8"]]},{"id":"ffac76df.157138","type":"inject","z":"389e6e43.7ac652","name":"timestamp","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":100,"wires":[["717d6cd.b967594"]]},{"id":"5f1d51af.3f9ef8","type":"gate","z":"389e6e43.7ac652","name":"gate 2","controlTopic":"control","defaultState":"open","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","x":450,"y":320,"wires":[["9b2a6a4.c99b098"]]},{"id":"10481de0.41060a","type":"ui_switch","z":"389e6e43.7ac652","name":"open/close","label":"open/close","group":"5cfe21d2.307938","order":3,"width":0,"height":0,"passthru":false,"decouple":"false","topic":"control","style":"","onvalue":"open","onvalueType":"str","onicon":"","oncolor":"","offvalue":"close","offvalueType":"str","officon":"","offcolor":"","x":290,"y":340,"wires":[["5f1d51af.3f9ef8"]]},{"id":"dbc56c5c.9a499","type":"ui_button","z":"389e6e43.7ac652","name":"toggle","group":"fdb6c31c.4f2818","order":3,"width":0,"height":0,"passthru":true,"label":"toggle","color":"","bgcolor":"","icon":"","payload":"toggle","payloadType":"str","topic":"control","x":310,"y":140,"wires":[["d81194ef.313d6"]]},{"id":"fb3862a9.febaa8","type":"ui_button","z":"389e6e43.7ac652","name":"reset","group":"fdb6c31c.4f2818","order":4,"width":0,"height":0,"passthru":true,"label":"reset","color":"","bgcolor":"","icon":"","payload":"default","payloadType":"str","topic":"control","x":310,"y":180,"wires":[["d81194ef.313d6"]]},{"id":"717d6cd.b967594","type":"ui_button","z":"389e6e43.7ac652","name":"message 1","group":"fdb6c31c.4f2818","order":5,"width":0,"height":0,"passthru":true,"label":"Send","color":"","bgcolor":"","icon":"","payload":"","payloadType":"date","topic":"anything","x":290,"y":100,"wires":[["d81194ef.313d6"]]},{"id":"cf3d94f3.62e01","type":"ui_button","z":"389e6e43.7ac652","name":"message 2","group":"5cfe21d2.307938","order":4,"width":0,"height":0,"passthru":false,"label":"Send","color":"","bgcolor":"","icon":"","payload":"","payloadType":"date","topic":"","x":290,"y":300,"wires":[["5f1d51af.3f9ef8"]]},{"id":"f2823cc0.7548c","type":"ui_text","z":"389e6e43.7ac652","group":"fdb6c31c.4f2818","order":2,"width":0,"height":0,"name":"display 1","label":"Message","format":"{{msg.payload}}","layout":"row-spread","x":600,"y":140,"wires":[]},{"id":"4e7accb1.6a1244","type":"ui_text","z":"389e6e43.7ac652","group":"fdb6c31c.4f2818","order":1,"width":0,"height":0,"name":"status 1","label":"Status","format":"{{msg.status.text}}","layout":"row-spread","x":740,"y":180,"wires":[]},{"id":"9b2a6a4.c99b098","type":"ui_text","z":"389e6e43.7ac652","group":"5cfe21d2.307938","order":2,"width":0,"height":0,"name":"display 2","label":"Message","format":"{{msg.payload}}","layout":"row-spread","x":600,"y":320,"wires":[]},{"id":"9fdf32be.f66568","type":"ui_text","z":"389e6e43.7ac652","group":"5cfe21d2.307938","order":1,"width":0,"height":0,"name":"status 2","label":"Status","format":"{{msg.status.text}}","layout":"row-spread","x":720,"y":360,"wires":[]},{"id":"d3b465d7.ab11d","type":"inject","z":"389e6e43.7ac652","name":"timestamp","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":300,"wires":[["cf3d94f3.62e01"]]},{"id":"7378029e.0e7004","type":"status","z":"389e6e43.7ac652","name":"status 2","scope":["5f1d51af.3f9ef8"],"x":590,"y":360,"wires":[["9fdf32be.f66568"]]},{"id":"66b1fc61.8e356c","type":"inject","z":"389e6e43.7ac652","name":"toggle","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":140,"wires":[["dbc56c5c.9a499"]]},{"id":"c7bd73e3.460f","type":"inject","z":"389e6e43.7ac652","name":"reset","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":180,"wires":[["fb3862a9.febaa8"]]},{"id":"c7ba6981.1c5ca8","type":"debug","z":"389e6e43.7ac652","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"status","x":750,"y":220,"wires":[]},{"id":"5cfe21d2.307938","type":"ui_group","z":"","name":"Gate #2","tab":"3616a722.b89bc8","order":2,"disp":true,"width":"6","collapse":false},{"id":"fdb6c31c.4f2818","type":"ui_group","z":"","name":"Gate #1","tab":"3616a722.b89bc8","order":1,"disp":true,"width":"6","collapse":false},{"id":"3616a722.b89bc8","type":"ui_tab","z":"","name":"Home","icon":"dashboard"}]

this does not work because you can move the slider then switch the switch & it will not have the correct value
surley there must be a easy way to do this?


#14

Hi @gbond

the issue is the ui_switch node does not let you change what message it sends for on/off events. The messages are fixed to whatever you have configured.

So to achieve what you want, you'll need a bit more logic in your flow.

[{"id":"b399d5d3.5c76e8","type":"ui_slider","z":"79b3f480.2a819c","name":"","label":"slider","group":"273bd9a3.7a5d06","order":0,"width":0,"height":0,"passthru":true,"outs":"all","topic":"","min":0,"max":10,"step":1,"x":150,"y":440,"wires":[["5516ad29.76fbb4"]]},{"id":"5516ad29.76fbb4","type":"change","z":"79b3f480.2a819c","name":"","rules":[{"t":"set","p":"slider","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":440,"wires":[[]]},{"id":"a6ae4a88.a63af8","type":"ui_switch","z":"79b3f480.2a819c","name":"","label":"switch","group":"273bd9a3.7a5d06","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":150,"y":500,"wires":[["e0595bec.57d3e8"]]},{"id":"e0595bec.57d3e8","type":"switch","z":"79b3f480.2a819c","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":290,"y":500,"wires":[["7de402e3.831e9c"],["f7abf5e4.255708"]]},{"id":"7de402e3.831e9c","type":"change","z":"79b3f480.2a819c","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"slider","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":480,"wires":[["3b4d3aa0.284b56"]]},{"id":"3b4d3aa0.284b56","type":"debug","z":"79b3f480.2a819c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":650,"y":480,"wires":[]},{"id":"f7abf5e4.255708","type":"debug","z":"79b3f480.2a819c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":650,"y":520,"wires":[]},{"id":"273bd9a3.7a5d06","type":"ui_group","z":"","name":"Default","tab":"5f290822.a5e418","disp":true,"width":"6","collapse":false},{"id":"5f290822.a5e418","type":"ui_tab","z":"","name":"Home","icon":"dashboard"}]

Whenever the slider value changes, it updates a flow context value (flow.slider).

The UI_Switch is configured to emit true/false. It is connected to a normal Switch node that tests for true/false. The true branch (at the top) then sets msg.payload to the value of flow.slider. (The second output of the switch node is the off/false branch).


#15

thanks that seems to work.

[{"id":"22af34a8.2c6844","type":"rpi-gpio out","z":"fb0d00b6.ddad3","name":"","pin":"7","set":true,"level":"0","freq":"","out":"out","x":940,"y":240,"wires":[]},{"id":"696f35da.80e84c","type":"comment","z":"fb0d00b6.ddad3","name":"Room Lights","info":"","x":70,"y":240,"wires":[]},{"id":"7c769adb.955ccc","type":"change","z":"fb0d00b6.ddad3","name":"set timings","rules":[{"t":"set","p":"lighton","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"lightoff","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"numberofblinks","pt":"msg","to":"1","tot":"num"},{"t":"set","p":"delaystart","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":770,"y":140,"wires":[["65ba55c3.5546c4","fd2c8c22.e2cb18"]]},{"id":"65ba55c3.5546c4","type":"function","z":"fb0d00b6.ddad3","name":"The Light Show","func":"//get the values of msg.* to var's\nvar numberofblinks = msg.numberofblinks;\nvar light_on_for_ms = msg.lighton;\nvar light_off_for_ms = msg.lightoff;\n\n//how long to wait before starting the light show\nvar time_in_ms_to_wait_before_first_execution = msg.delaystart || 0;\n\n//total run time of light on and light off\nvar time_in_ms_to_wait_between_recursive_setTimeout = (light_on_for_ms + light_off_for_ms);\n\n//set the count to 1\nvar count = 1;\n\nvar tostopit;\n\n//function to count the number of blinks and stop timmer\nfunction blinks() {\n    if (numberofblinks <= count) {\n        //stop the recursive setTimeout\n        clearTimeout(tostopit);\n    } else {\n        count++;\n    }\n}\n\n//function to set payload to true\nfunction payload_true() {\n    //set payload to true\n    msg.payload = true;\n    node.send(msg);\n\n    //display green dot when light is on\n    node.status({fill: 'green', shape: 'dot', text: ' '});\n}\n\n//function to set payload to false\nfunction payload_false() {\n    //set payload to false\n    msg.payload = false;\n    node.send(msg);\n\n    //clear status when payload false\n    node.status({});\n}\n\n//run the light show!!!!\nsetTimeout(function run() {\n    //turn the light on by setting msg.payload to true\n    payload_true();\n\n    //turn the light off by waiting to set the payload to false by the ammount of time light is on\n    setTimeout(payload_false, light_on_for_ms);\n\n    //total ammount of time light can be on or off before it runs again\n    tostopit = setTimeout(run, time_in_ms_to_wait_between_recursive_setTimeout);\n\n    blinks();\n\n}, time_in_ms_to_wait_before_first_execution);\n\nreturn null;","outputs":1,"noerr":0,"x":780,"y":200,"wires":[["22af34a8.2c6844"]]},{"id":"fd2c8c22.e2cb18","type":"debug","z":"fb0d00b6.ddad3","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"lighton","x":950,"y":140,"wires":[]},{"id":"99c4d986.26bcc","type":"change","z":"fb0d00b6.ddad3","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"(payload*60)*1000","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":160,"wires":[["7c769adb.955ccc"]]},{"id":"800a1073.06b13","type":"ui_slider","z":"fb0d00b6.ddad3","name":"","label":"slider","group":"2f45f695.6435ba","order":0,"width":0,"height":0,"passthru":true,"topic":"","min":"1","max":"20","step":1,"x":70,"y":60,"wires":[["8bfc1f70.42d9b8","18e4ec.21fce315"]]},{"id":"8bfc1f70.42d9b8","type":"change","z":"fb0d00b6.ddad3","name":"","rules":[{"t":"set","p":"slider","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":60,"wires":[["2bfbabcb.f61294"]]},{"id":"c7631f18.3486e","type":"ui_switch","z":"fb0d00b6.ddad3","name":"","label":"switch","group":"2f45f695.6435ba","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":290,"y":120,"wires":[["15c2736d.b9788d"]]},{"id":"15c2736d.b9788d","type":"switch","z":"fb0d00b6.ddad3","name":"","property":"payload","propertyType":"msg","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":350,"y":180,"wires":[["d8129041.41de68"],["22af34a8.2c6844"]]},{"id":"d8129041.41de68","type":"change","z":"fb0d00b6.ddad3","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"slider","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":100,"wires":[["bc4b71cc.22852","99c4d986.26bcc"]]},{"id":"bc4b71cc.22852","type":"debug","z":"fb0d00b6.ddad3","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":100,"wires":[]},{"id":"2bfbabcb.f61294","type":"ui_text","z":"fb0d00b6.ddad3","group":"2f45f695.6435ba","order":0,"width":0,"height":0,"name":"","label":"slider","format":"{{msg.payload}}","layout":"row-left","x":470,"y":60,"wires":[]},{"id":"18e4ec.21fce315","type":"change","z":"fb0d00b6.ddad3","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":120,"y":120,"wires":[["c7631f18.3486e"]]},{"id":"2f45f695.6435ba","type":"ui_group","z":"","name":"row 1","tab":"568a2c54.1e76f4","order":2,"disp":false,"width":"15","collapse":false},{"id":"568a2c54.1e76f4","type":"ui_tab","z":"","name":"Deathstar","icon":"dashboard","order":1}]

This seem to work