Global JSON variable

Hi, I'm pretty new to Node-red, I've been using it for about 2 years, but only to set things up. Im doing a mayor overhaull on my MQTT based smarthome, which has lights with many ways of controlling them, and I want my dashboard to display all the data changed somewhere else. I decided to use a global variable, since I can just send out a JSON object straight from the global var to MQTT. so I want to have a global JSON object, like this:
payload{
brightness:0-255
white_value:0-255
color{
r:0-255
g:0-255
b:0-255
}
transition:0-150
effect:string
state:string
}

now if I for example change the value of brightness, I want to change that in the object (in a function node) but keep the rest the same

you can retrieve the JS object from global context using global.get("name-of-object") and change 1 thing e.g...

var myObject = global.get("name-of-object") || {};
myObject.brightness = 123;
msg.payload =  myObject;
return msg

Important note the above function ^ WILL change the object stored in the global store (since it is a reference)

PS, "JSON object" is not really a thing.. JSON mans "JavaScript Object Notation" - i.e. it is a String representation of a JS object.

I understand that, but in my case I want to use global.set() for just the value myObject.brightness, and not affect any other value in the Object. If I simply use global.set("myObject", brightness), the whole object will just become the brightness.

then dont do that. put the whole object back in the store

var myObject = global.get("name-of-object") || {};
myObject.brightness = 123;
global.set("name-of-object", myObject)

As i alluded to earlier, it isnt really necessary (but is good practice) to call global.set BECAUSE the object it a reference & the globally stored object WILL be updated by simply setting myObject.brightness = 123

best thing you can do is give it a go - best way to learn :slight_smile:

1 Like

well all I make is shotty code, and since I'm too lazy to update all of the function nodes I have I'll just try the method stated below the proper way to do it, I'll never learn :sweat_smile:

thanks a lot for the answer!

You can also use dot notation in the string.
global.set('myObject.brightness', 255)
or set the global myObject.brightness in a change node.
i.e.

[{"id":"a284d8c1.e2dd","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"20","payloadType":"num","x":590,"y":1760,"wires":[["dd06c3ac.4874a8"]]},{"id":"c815e9db.a393f8","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"255","payloadType":"num","x":550,"y":1800,"wires":[["3be8855b.56d692"]]},{"id":"dd06c3ac.4874a8","type":"function","z":"8d22ae29.7df6d","name":"","func":"global.set(\"myObject.brightness\",msg.payload)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":750,"y":1760,"wires":[[]]},{"id":"3be8855b.56d692","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"set","p":"myObject.brightness","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":800,"y":1800,"wires":[[]]}]

I need to pull a global variable using JSON how do I do that
global. payload.room = msg.payload.room and add the value of the button that was pressed

@Steve-Mcl @E1cid

@Zbrooklyn - please don't post the same question in multiple threads. Please do as I said in the other thread - i.e. open a new thread and explain what you are doinf and what you have tried.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.