The most basic of questions

#1

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?

#2

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

#3

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!

#4

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/
that will take you through the fundamentals of Node-RED and thousands of introductions to javascript objects online.

#5

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;

#6

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};

#7

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

#8

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

[{"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"]]}]
1 Like