# Accessing payload from ui switch or button

**URL:** https://discourse.nodered.org/t/accessing-payload-from-ui-switch-or-button/19693
**Category:** Dashboard
**Created:** [28 December 2019 22:28 UTC](https://discourse.nodered.org/t/accessing-payload-from-ui-switch-or-button/19693 "2019-12-28T22:28:49Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![chrisko](https://avatars.discourse-cdn.com/v4/letter/c/f04885/32.png) [@chrisko](https://discourse.nodered.org/u/chrisko)
#### Post date: [28 December 2019 22:28 UTC](https://discourse.nodered.org/t/accessing-payload-from-ui-switch-or-button/19693/1 "2019-12-28T22:28:49Z")

</div>

## 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:

```auto
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

---

<div class="post-metadata">

### Author: ![chrisko](https://avatars.discourse-cdn.com/v4/letter/c/f04885/32.png) [@chrisko](https://discourse.nodered.org/u/chrisko)
#### Post date: [28 December 2019 23:23 UTC](https://discourse.nodered.org/t/accessing-payload-from-ui-switch-or-button/19693/2 "2019-12-28T23:23:11Z")

</div>

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

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [29 December 2019 00:03 UTC](https://discourse.nodered.org/t/accessing-payload-from-ui-switch-or-button/19693/3 "2019-12-29T00:03:36Z")

</div>

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

---

<div class="post-metadata">

### Author: ![chrisko](https://avatars.discourse-cdn.com/v4/letter/c/f04885/32.png) [@chrisko](https://discourse.nodered.org/u/chrisko)
#### Post date: [29 December 2019 00:12 UTC](https://discourse.nodered.org/t/accessing-payload-from-ui-switch-or-button/19693/4 "2019-12-29T00:12:25Z")

</div>

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

```auto
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:

```auto
[{"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}]

```

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [29 December 2019 06:51 UTC](https://discourse.nodered.org/t/accessing-payload-from-ui-switch-or-button/19693/5 "2019-12-29T06:51:41Z")

</div>

> 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](https://nodered.org/docs/user-guide/messages)

---

<div class="post-metadata">

### Author: ![chrisko](https://avatars.discourse-cdn.com/v4/letter/c/f04885/32.png) [@chrisko](https://discourse.nodered.org/u/chrisko)
#### Post date: [29 December 2019 11:56 UTC](https://discourse.nodered.org/t/accessing-payload-from-ui-switch-or-button/19693/6 "2019-12-29T11:56:03Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [29 December 2019 12:02 UTC](https://discourse.nodered.org/t/accessing-payload-from-ui-switch-or-button/19693/7 "2019-12-29T12:02:48Z")

</div>

> 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 ?

---

<div class="post-metadata">

### Author: ![chrisko](https://avatars.discourse-cdn.com/v4/letter/c/f04885/32.png) [@chrisko](https://discourse.nodered.org/u/chrisko)
#### Post date: [29 December 2019 12:12 UTC](https://discourse.nodered.org/t/accessing-payload-from-ui-switch-or-button/19693/8 "2019-12-29T12:12:57Z")

</div>

i see the payload is true:

```auto
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?

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [29 December 2019 12:31 UTC](https://discourse.nodered.org/t/accessing-payload-from-ui-switch-or-button/19693/9 "2019-12-29T12:31:14Z")

</div>

Try this flow:

```auto
[{"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}]

```

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)
#### Post date: [29 December 2019 12:51 UTC](https://discourse.nodered.org/t/accessing-payload-from-ui-switch-or-button/19693/10 "2019-12-29T12:51:10Z")

</div>

> [@chrisko](#):
>
> In simple words: How to read the state of the particular switch if several are needed?

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.

 ![sw-01](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/36cc27c68e5a7d504cb5e436be6c61bac8b1e007.png)

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:

```auto
[{"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}]

```

---

<div class="post-metadata">

### Author: ![chrisko](https://avatars.discourse-cdn.com/v4/letter/c/f04885/32.png) [@chrisko](https://discourse.nodered.org/u/chrisko)
#### Post date: [29 December 2019 13:10 UTC](https://discourse.nodered.org/t/accessing-payload-from-ui-switch-or-button/19693/11 "2019-12-29T13:10:32Z")

</div>

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

many thanks again,  
Chris

---

<div class="post-metadata">

### Author: ![chrisko](https://avatars.discourse-cdn.com/v4/letter/c/f04885/32.png) [@chrisko](https://discourse.nodered.org/u/chrisko)
#### Post date: [29 December 2019 13:28 UTC](https://discourse.nodered.org/t/accessing-payload-from-ui-switch-or-button/19693/12 "2019-12-29T13:28:03Z")

</div>

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

Thanks for giving me a new idea.

Best regards,  
Chris
