# The most basic of questions

**URL:** <https://discourse.nodered.org/t/the-most-basic-of-questions/1961>\
**Category:** General\
**Created:** [1 August 2018 06:08 UTC](https://discourse.nodered.org/t/the-most-basic-of-questions/1961 "2018-08-01T06:08:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheRiddler](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@TheRiddler](https://discourse.nodered.org/u/TheRiddler)\
**Post date:** [1 August 2018 06:08 UTC](https://discourse.nodered.org/t/the-most-basic-of-questions/1961/1 "2018-08-01T06:08:06Z")

</div>

I'm sure the answer to this is so simple yet my lack of programming knowledge is letting me down.

I want to build a flow that collects the bollean status from the Switch component on a dashboard and passes that status, together a variable name, into a widget. For example, {"variable\_name": false}. I am guessing I should use the Function widget to do the magic but I can never seem to get the coding right as my debug outputs never produce the right syntax/structure.

What do I need to do?

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [1 August 2018 06:29 UTC](https://discourse.nodered.org/t/the-most-basic-of-questions/1961/2 "2018-08-01T06:29:04Z")

</div>

use a change node to set what you want or move an existing variable to a different location

---

<div class="post-metadata">

**Author:** ![TheRiddler](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@TheRiddler](https://discourse.nodered.org/u/TheRiddler)\
**Post date:** [1 August 2018 06:38 UTC](https://discourse.nodered.org/t/the-most-basic-of-questions/1961/3 "2018-08-01T06:38:50Z")

</div>

How would that work? I need to change the boolean status of the variable that passed to the widget dependant upon the status of the switch; I'd need to build a mini-script to do the manipulation. I'd also like to learn how to do that as I'm sure it will come in very handy as my use of the platform expands and hopefully saves me from posting such humiliating questions on here in the future!

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [1 August 2018 07:57 UTC](https://discourse.nodered.org/t/the-most-basic-of-questions/1961/4 "2018-08-01T07:57:12Z")

</div>

Add a debug node to the switch output to see what the output looks like, but it should be msg.payload  
I'm guessing variable-name is known you can then use the change node to set msg."variable" to msg.payload

But Node-RED generally works by passing a javascript object (msg) between nodes with the important part as msg.payload

There are several great tutorials on Node-RED online including this one:  
[http://noderedguide.com/node-red-lecture-5-the-node-red-programming-model/](http://noderedguide.com/node-red-lecture-5-the-node-red-programming-model/)  
that will take you through the fundamentals of Node-RED and thousands of introductions to javascript objects online.

---

<div class="post-metadata">

**Author:** ![TheRiddler](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@TheRiddler](https://discourse.nodered.org/u/TheRiddler)\
**Post date:** [1 August 2018 08:43 UTC](https://discourse.nodered.org/t/the-most-basic-of-questions/1961/5 "2018-08-01T08:43:50Z")

</div>

Thanks Moose - time to hit the books again!!

For now, I have created this horrible piece of code. I say horrible because I'm sure there's a better way of doing it but, for now, this works!

var truefalse = msg.payload;  
var lamp\_control\_variable='"on"';  
msg.payload = "{"+lamp\_control\_variable+":"+truefalse+"}";  
return msg;

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [1 August 2018 09:00 UTC](https://discourse.nodered.org/t/the-most-basic-of-questions/1961/6 "2018-08-01T09:00:18Z")

</div>

Do you really mean that you want the payload to contain a _string_ containing something like  
`{"on":1}`  
rather than a javascript object such as would be generated by the code  
`msg.payload = {on: msg.payload};`

---

<div class="post-metadata">

**Author:** ![TheRiddler](https://avatars.discourse-cdn.com/v4/letter/t/f9ae1b/32.png) [@TheRiddler](https://discourse.nodered.org/u/TheRiddler)\
**Post date:** [1 August 2018 09:07 UTC](https://discourse.nodered.org/t/the-most-basic-of-questions/1961/7 "2018-08-01T09:07:59Z")

</div>

Thank you so much. As I said at the beginning, the most basic of questions and I feel appropriately humiliated 🙄

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [1 August 2018 09:24 UTC](https://discourse.nodered.org/t/the-most-basic-of-questions/1961/8 "2018-08-01T09:24:58Z")

</div>

No need for humiliation, everyone starts somewhere. As previously mentioned you could also do this in a Change node set to Move msg.payload To msg.payload.on as in

```auto
[{"id":"4aaa252d.2990dc","type":"change","z":"acd8202b.36972","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.on","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":217.5,"y":1003,"wires":[["b9b8d6fe.834e1"]]}]

```
