Accessing payload from ui switch or button

Dear all,
i'm very frustrated due to missing ideas how to read the payload from a switch or button.
I tried like this in the function node:

newmsg = {};
if (global.get("Available")===true && msg.payload.Night===true) {
    newmsg.payload = "Night";
} else {
    newmsg.payload = "to be continue...";
}
return newmsg;

My problem looks like msg.payload.Night===true doesn't have be recognized.
So how can i read the payload of a button (Topic= Night and pressed status=true) in the best way?
The global variable works perfectly.

Many thanks in advance,
Chris

Anyone who can push me in the right direction?
What i'm doing wrong?

Put a debug node on the output of the switch or button so you can see what the payload is.

Yes, the payload says 'true' as expected.
As well when i take out the "Available" term, it doesn't work...

newmsg = {};
if ( msg.payload.Night===true) {
    newmsg.payload = "Night";
} else {
    newmsg.payload = "to be continue...";
}
return newmsg;

The result is always "to be continue..."

The flow is:

[{"id":"5db0857b.89ed9c","type":"tab","label":"Flow 7","disabled":false,"info":""},{"id":"f63ea628.2712e8","type":"ui_switch","z":"5db0857b.89ed9c","name":"","label":"Guests","tooltip":"","group":"59070349.3e117c","order":5,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"Besuch","style":"","onvalue":"on","onvalueType":"str","onicon":"","oncolor":"","offvalue":"off","offvalueType":"str","officon":"","offcolor":"","x":180,"y":180,"wires":[["e4620cdc.f3aa18"]]},{"id":"d73610b3.de36c8","type":"debug","z":"5db0857b.89ed9c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":530,"y":140,"wires":[]},{"id":"13256bf2.471a5c","type":"ui_button","z":"5db0857b.89ed9c","name":"Night","group":"59070349.3e117c","order":5,"width":0,"height":0,"passthru":false,"label":"Night","tooltip":"","color":"","bgcolor":"","icon":"","payload":"true","payloadType":"bool","topic":"Night","x":190,"y":120,"wires":[["e4620cdc.f3aa18","518135a3.072eac"]]},{"id":"e4620cdc.f3aa18","type":"function","z":"5db0857b.89ed9c","name":"","func":"newmsg = {};\nif ( msg.payload.Night === true) {\n    newmsg.payload = \"Night\";\n} else {\n    newmsg.payload = \"to be continue...\";\n}\nreturn newmsg;","outputs":1,"noerr":0,"x":350,"y":140,"wires":[["d73610b3.de36c8"]]},{"id":"518135a3.072eac","type":"debug","z":"5db0857b.89ed9c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":370,"y":220,"wires":[]},{"id":"59070349.3e117c","type":"ui_group","z":"","name":"Homestatus","tab":"17fa1844.87e8b8","disp":true,"width":"4","collapse":false},{"id":"17fa1844.87e8b8","type":"ui_tab","z":"","name":"System","icon":"dashboard","order":5,"disabled":false,"hidden":false}]

The result is always "to be continue..."

Correct for both shown flows.

msg.payload.Night

There is no "msg.payload.Night"
There is msg.topic with a value of "Night" and msg.payload value of true/false.

You need to test for the topic and then the payload.

But hold on, first explain what you are trying to do.

You have 2 dashboard switches, both are connected to the function node.
Then you are checking if it is night.
But the second switch 'Besuch' does not do anything.

On top of that you are trying to test a global variable that does not exist.

I would recommend to read the documentation and how to work with messages.

https://nodered.org/docs/user-guide/messages

Firstly many thanks for taking your time to help me.
What i'm trying is simply reading the state of the switch or button (pressed on or off).
Finally the flow shall control my home status. F.e. the light control during the night is different when i have guests or not.
Right, on top it's still with global variable, but this point is not my problem. Only how to read the state is unclear for me and that's why i took out the global variable from the top.
In simple words: How to read the state of the particular switch if several are needed?
I'm really new with NR and for better understanding one example would be helpful for me.

How to read the state of the particular switch if several are needed?

By reading it ?

Sorry I don't understand the problem. If you read the documentation: working with messages, I thought it was clear.

switch -> debug node (set it up with output: complete msg object)

When you click it, what do you see ?

i see the payload is true:

object
payload: true
topic: "Night"
socketid: "LAwRHvnZn1TGwVTZAAAN"
_msgid: "f81477e8.5798a8"

But i don't know how to use it in the function node to read the state...
The documentation doesn't give me an answer for this?

Try this flow:

[{"id":"a7bbe565.f9bf4","type":"ui_switch","z":"1b5bc2fe.d98add","name":"","label":"Night","tooltip":"","group":"37a8895f.f920ce","order":2,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"night","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":146,"y":480,"wires":[["a78ca813.7b24b8"]]},{"id":"2e047e3.15ba282","type":"ui_switch","z":"1b5bc2fe.d98add","name":"","label":"Guests","tooltip":"","group":"37a8895f.f920ce","order":2,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"guests","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":132,"y":528,"wires":[["a78ca813.7b24b8"]]},{"id":"a78ca813.7b24b8","type":"function","z":"1b5bc2fe.d98add","name":"","func":"t = msg.topic\np = msg.payload\n\n\nif(t === \"night\"){\n    if(p){\n        msg.payload = \"Night\"\n    }\n    else{\n        msg.payload = \"Not night\"\n    }\n}\n\nif(t === \"guests\"){\n    if(p){\n        msg.payload = \"Guests\"\n    }\n    else{\n        msg.payload = \"No Guests\"\n    }\n}\nreturn msg","outputs":1,"noerr":0,"x":314,"y":504,"wires":[["c37bfc5.8b7a48"]]},{"id":"c37bfc5.8b7a48","type":"debug","z":"1b5bc2fe.d98add","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":478,"y":504,"wires":[]},{"id":"37a8895f.f920ce","type":"ui_group","z":"","name":"switches","tab":"9cdbdac.dba2428","disp":true,"width":"6","collapse":false},{"id":"9cdbdac.dba2428","type":"ui_tab","z":"","name":"test","icon":"dashboard","order":4,"disabled":false,"hidden":false}]
1 Like

I guess @zenofmud and @bakman2 already clarified the point but let me show an alternative approach. The way they explained requires the function node to be inserted in the very same flow as the ui_switch node.

Let´s say you want to read the status of the ui_switch inserting a function node in a separate flow (or separate tab). In such case you can bind a global context to the switch. Whenever you toggle the switch in the dashboard the global context value will follows.

In order to bind the ui_switch to the global context you use a status node like shown below.

Then you can read the value of the global context from any function node.

The good thing about this second approach is that you can recover the switch status at any time whereas the first approach will allow you to recover the switch status only after a toggle event.

Testing flow:

[{"id":"6fe46f15.c0819","type":"tab","label":"Flow 4","disabled":false,"info":""},{"id":"2e1eb3b8.d447ec","type":"ui_switch","z":"6fe46f15.c0819","name":"Switch 1","label":"switch 1","tooltip":"","group":"f62442d8.46a67","order":3,"width":"3","height":"1","passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":500,"y":80,"wires":[[]]},{"id":"12fa0c02.036b34","type":"status","z":"6fe46f15.c0819","name":"Status 1","scope":["2e1eb3b8.d447ec"],"x":120,"y":80,"wires":[["ab076d0b.7add6"]]},{"id":"ab076d0b.7add6","type":"change","z":"6fe46f15.c0819","name":"","rules":[{"t":"set","p":"sw1","pt":"global","to":"status.text","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":80,"wires":[[]]},{"id":"2e86a72f.c8af38","type":"function","z":"6fe46f15.c0819","name":"Read Switch1","func":"msg.payload = global.get(\"sw1\");\nreturn msg;","outputs":1,"noerr":0,"x":340,"y":240,"wires":[["d734b5c6.a9db18"]]},{"id":"1af28f3f.cb1d11","type":"inject","z":"6fe46f15.c0819","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":240,"wires":[["2e86a72f.c8af38"]]},{"id":"d734b5c6.a9db18","type":"debug","z":"6fe46f15.c0819","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":530,"y":240,"wires":[]},{"id":"477ec6f7.030f28","type":"ui_switch","z":"6fe46f15.c0819","name":"Switch 2","label":"switch 2","tooltip":"","group":"f62442d8.46a67","order":3,"width":"3","height":"1","passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":500,"y":140,"wires":[[]]},{"id":"bd2aa4df.3e2038","type":"status","z":"6fe46f15.c0819","name":"Status 2","scope":["477ec6f7.030f28"],"x":120,"y":140,"wires":[["4a2c0e1a.27d59"]]},{"id":"4a2c0e1a.27d59","type":"change","z":"6fe46f15.c0819","name":"","rules":[{"t":"set","p":"sw2","pt":"global","to":"status.text","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":140,"wires":[[]]},{"id":"21425a8d.29ecf6","type":"function","z":"6fe46f15.c0819","name":"Read Switch2","func":"msg.payload = global.get(\"sw2\");\nreturn msg;","outputs":1,"noerr":0,"x":340,"y":300,"wires":[["5d7060c0.8fa11"]]},{"id":"23c4b3c7.ad4d9c","type":"inject","z":"6fe46f15.c0819","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":300,"wires":[["21425a8d.29ecf6"]]},{"id":"5d7060c0.8fa11","type":"debug","z":"6fe46f15.c0819","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":530,"y":300,"wires":[]},{"id":"f62442d8.46a67","type":"ui_group","z":"","name":"Default","tab":"43bbb3c.f37894c","disp":true,"width":"22","collapse":false},{"id":"43bbb3c.f37894c","type":"ui_tab","z":"","name":"Home","icon":"dashboard","order":6,"disabled":false,"hidden":false}]
2 Likes

bakman2, Great!
Now it's more understandable for me :slight_smile:

many thanks again,
Chris

Hej Andrej,
yes, it is a good solution as well.

Thanks for giving me a new idea.

Best regards,
Chris

1 Like