How to set global variable parameters?

Hi,

I would like to store 5 temperature values in one global object.
I have created the global object with several temp values:

var GlobalTemp = {};
GlobalTemp.a = 10;
GlobalTemp.b = 15;
GlobalTemp.c = 22;
global.set("Temp",GlobalTemp);

and I can read the Global Var and get the values I need:

var work_GlobalTemp = global.get("Temp");

msg.payload = work_GlobalTemp.b;
return msg;

but I have now Idea how to set a speciel value inside the global object with a new value.

Something like global.set ("GlobalTemp".b,19);

Does anyone know how to do this ?

BR
Gawan

Well you can do it in a Change node easy:
Screen Shot 2019-12-22 at 11.49.49 AM

You can modify properties of GlobalTemp the same way that you created them:

work_GlobalTemp.b = 19;
global.set("Temp",GlobalTemp);
msg.payload = global.get("Temp");

As well as using the Change node to access the property directly, you can also do:

global.set ("GlobalTemp.b",19);
2 Likes

Perfect, thats exactly what I was looking for !
I do not like using function-nodes ... writing code is more what I prefer:)
Thanks a lot !!

global.set("test.a",12)
global.set("test.b",33)
var d = global.get("test");
var e = global.get("test0");
if(e != d){
global.set("test0",d);
msg.payload=global.get("test0");
return msg
}

what I am trying to do is comparing two global.set values(test and test1). Based on the comparison if the values are equal then it shouldnt give an output. If they are not equal, it should set "test" values to "test0" and then after that it should give an output.
Can anyone help me with this..?
(the problem I have faced 1: it automatically sets the value to test0 without even giving global.set("test0",d) and I think its a bug )

You are setting ‘test.a’ and ‘test.b ‘but then testing ‘test’ and ‘test0’

Is that what you mean to do? If so what are in all four global?

Continuing the discussion from How to set global variable parameters?:

initially "test0" will be having null value.
after setting values for "test.a" and "test.b", test will be {"a":12,"b":33}(exactly copied from the debug window)
next, if values in "test" and "test0" are not equal, I want to copy the values to "test0" (thats why I gave "global.set("test0",d);" )
and give an output

[
    {
        "id": "9a2e60e52c7f98aa",
        "type": "tab",
        "label": "Flow 6",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "72957a0e1ad7b2ab",
        "type": "debug",
        "z": "9a2e60e52c7f98aa",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 830,
        "y": 260,
        "wires": []
    },
    {
        "id": "b15c5083e34e9a22",
        "type": "function",
        "z": "9a2e60e52c7f98aa",
        "name": "",
        "func": "msg.payload = {\"test\":global.get(\"test\"),\"test0\":global.get(\"test0\") };\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 420,
        "y": 360,
        "wires": [
            [
                "72957a0e1ad7b2ab"
            ]
        ]
    },
    {
        "id": "4b959870c9d26218",
        "type": "inject",
        "z": "9a2e60e52c7f98aa",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 180,
        "y": 360,
        "wires": [
            [
                "b15c5083e34e9a22"
            ]
        ]
    },
    {
        "id": "72baddb77d1b2a11",
        "type": "function",
        "z": "9a2e60e52c7f98aa",
        "name": "",
        "func": "global.set(\"test.a\",msg.payload.a)\nglobal.set(\"test.b\",msg.payload.b)\nvar d = global.get(\"test\");\nvar e = global.get(\"test0\");\nif(e != d){\nglobal.set(\"test0\",d)\nmsg.payload=global.get(\"test0\");\nreturn msg\n}",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 400,
        "y": 240,
        "wires": [
            [
                "72957a0e1ad7b2ab"
            ]
        ]
    },
    {
        "id": "2d50e21d02c814bb",
        "type": "inject",
        "z": "9a2e60e52c7f98aa",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"a\": 4,\"b\":5}",
        "payloadType": "json",
        "x": 170,
        "y": 280,
        "wires": [
            [
                "72baddb77d1b2a11"
            ]
        ]
    },
    {
        "id": "eeb5f39eb1cfd96e",
        "type": "inject",
        "z": "9a2e60e52c7f98aa",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": "10",
        "topic": "",
        "payload": "{\"a\":1001,\"b\":1213}",
        "payloadType": "json",
        "x": 170,
        "y": 200,
        "wires": [
            [
                "72baddb77d1b2a11"
            ]
        ]
    }
]

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

You can edit and correct your post by clicking the pencil :pencil2: icon.

See this post for more details - How to share code or flow json

1 Like

I have attached my flow, so you will get better understanding,
and in this , in the first function,

global.set("test.a",msg.payload.a)
global.set("test.b",msg.payload.b)
var d = global.get("test");
var e = global.get("test0");
if(e != d){
     global.set("test0",d);
     msg.payload=global.get("test0");
     return msg
}

if even if you remove the line "global.set("test0",d);" it tends to copy "test" to "test0" I think thats the bug here or may be my limited knowledge.

That that would be because e != d

What type are these variables? Numbers? Strings? Objects? Arrays?
NOTE: Arrays and Objects cannot be compared using != - they will ALWAYS be different. I will explain why if you need me too but first...

HINT: Use node.warn( [ "typeof d is: ", typeof d ] ) in your code. e.g...

global.set("test.a",msg.payload.a)
global.set("test.b",msg.payload.b)
const d = global.get("test");
const e = global.get("test0");
node.warn( [ "(test) typeof d is:", typeof d, d ] )
node.warn( [ "(test0) typeof e is:", typeof d, e ] )
if(e != d) {
    node.warn( "e does not equal d" )
    global.set("test0",d);
    msg.payload=global.get("test0");
    return msg
} else {
    node.warn( "e does equal d" )
}
1 Like

Thanks Steve,
Firstly I was comparing and writing object.
Secondly, even if you remove global.set("test0",d); , without even setting values to "test0", it tends to copy values of "test" to "test0"

That is because objects are stored as references (pointer to the original object).
So if you do this...

myObject = { value: "hello" } // an object with .value set to "hello"
global.set("test", myObject)
//some time later
const test = global.get("test")
test.value = "bye"

console.log(myObject.value) // outputs "bye"
// myObject and test are the SAME "thing"

This is a fairly good description of whats going on: Object references and copying

If you want to store a copy of an object that IS NOT the exact same object (i.e. a clone and not a reference/pointer to the original) then you need to duplicate the object.

there is a helper function in node-red for this:

2 Likes

Thanks a lot Steve,
I went through the link you have shared, and could identify the problem.
Once again, thankyou for the support.

1 Like

Hello every one, i have quite the same case here

let globalArray = ["value1","value8","value3"];
global.set("globalArray",globalArray);

let indexToChange = 1;
let newValue = "Value2";
global.set("globalArray[indexToChange]",newValue); // Not working 

How can i modify a value stored in an array in the global context using an index as parameters. Hope this is clear enough :sweat_smile:

And it throws me this :

"Error: Invalid property expression: unexpected i at position 12"

Actually, string concatenation simply does the trick !

global.set("globalArray["+indexToChange+"]",newValue); // Working

If string concatenation works with "set", why doesn't it with "get"?

var area = msg.payload; // greenroom
var old_presence = global.get('homeAssistant.homeAssistant.states["'+ area +'"].last_changed');
msg.data.old_pres = old_presence;

//result 
payload = greenroom
data.old_pres = *undefined*

1 Like