# How to set global variable parameters?

**URL:** <https://discourse.nodered.org/t/how-to-set-global-variable-parameters/19470>\
**Category:** General\
**Created:** [22 December 2019 16:44 UTC](https://discourse.nodered.org/t/how-to-set-global-variable-parameters/19470 "2019-12-22T16:44:24Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [22 December 2019 16:44 UTC](https://discourse.nodered.org/t/how-to-set-global-variable-parameters/19470/1 "2019-12-22T16:44:24Z")

</div>

Hi,

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

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

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

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

```

Does anyone know how to do this ?

BR  
Gawan

---

<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:** [22 December 2019 16:50 UTC](https://discourse.nodered.org/t/how-to-set-global-variable-parameters/19470/2 "2019-12-22T16:50:12Z")

</div>

Well you can do it in a `Change` node easy:  
 ![Screen Shot 2019-12-22 at 11.49.49 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/2/25fdb4816d409bc53cbbde766031dde87f880da5.png)

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [22 December 2019 17:14 UTC](https://discourse.nodered.org/t/how-to-set-global-variable-parameters/19470/3 "2019-12-22T17:14:36Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [22 December 2019 18:36 UTC](https://discourse.nodered.org/t/how-to-set-global-variable-parameters/19470/4 "2019-12-22T18:36:46Z")

</div>

> [@Gawan](#):
>
> but I have now Idea how to set a speciel value inside the global object with a new value.
> 
> ```auto
> Something like global.set ("GlobalTemp".b,19);
> 
> ```
> 
> Does anyone know how to do this ?

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

```auto
global.set ("GlobalTemp.b",19);

```

---

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [22 December 2019 19:46 UTC](https://discourse.nodered.org/t/how-to-set-global-variable-parameters/19470/5 "2019-12-22T19:46:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![austine-aj](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/austine-aj/32/67751_2.png) [@austine-aj](https://discourse.nodered.org/u/austine-aj)\
**Post date:** [14 September 2022 13:00 UTC](https://discourse.nodered.org/t/how-to-set-global-variable-parameters/19470/6 "2022-09-14T13:00:17Z")

</div>

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 )

---

<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:** [14 September 2022 13:42 UTC](https://discourse.nodered.org/t/how-to-set-global-variable-parameters/19470/7 "2022-09-14T13:42:47Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![austine-aj](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/austine-aj/32/67751_2.png) [@austine-aj](https://discourse.nodered.org/u/austine-aj)\
**Post date:** [15 September 2022 06:32 UTC](https://discourse.nodered.org/t/how-to-set-global-variable-parameters/19470/8 "2022-09-15T06:32:56Z")

</div>

Continuing the discussion from [How to set global variable parameters?](https://discourse.nodered.org/t/how-to-set-global-variable-parameters/19470/7):

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

---

<div class="post-metadata">

**Author:** ![austine-aj](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/austine-aj/32/67751_2.png) [@austine-aj](https://discourse.nodered.org/u/austine-aj)\
**Post date:** [15 September 2022 06:45 UTC](https://discourse.nodered.org/t/how-to-set-global-variable-parameters/19470/9 "2022-09-15T06:45:50Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [15 September 2022 06:50 UTC](https://discourse.nodered.org/t/how-to-set-global-variable-parameters/19470/10 "2022-09-15T06:50:17Z")

</div>

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

````auto
``` 
   code goes here 
```

````

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

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

**Author:** ![austine-aj](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/austine-aj/32/67751_2.png) [@austine-aj](https://discourse.nodered.org/u/austine-aj)\
**Post date:** [15 September 2022 06:52 UTC](https://discourse.nodered.org/t/how-to-set-global-variable-parameters/19470/11 "2022-09-15T06:52:55Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [15 September 2022 06:59 UTC](https://discourse.nodered.org/t/how-to-set-global-variable-parameters/19470/12 "2022-09-15T06:59:43Z")

</div>

> [@austine-aj](#):
>
> if you remove the line "global.set("test0",d);" it tends to copy "test" to "test0"

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

```javascript
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" )
}

```

---

<div class="post-metadata">

**Author:** ![austine-aj](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/austine-aj/32/67751_2.png) [@austine-aj](https://discourse.nodered.org/u/austine-aj)\
**Post date:** [15 September 2022 07:17 UTC](https://discourse.nodered.org/t/how-to-set-global-variable-parameters/19470/13 "2022-09-15T07:17:41Z")

</div>

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"

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [15 September 2022 07:25 UTC](https://discourse.nodered.org/t/how-to-set-global-variable-parameters/19470/14 "2022-09-15T07:25:21Z")

</div>

> [@austine-aj](#):
>
> 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...

```auto
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](https://javascript.info/object-copy)

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:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/d/1d07ddcd2ff177b031c85b8f5a68db80a1a7a5e4.png)

---

<div class="post-metadata">

**Author:** ![austine-aj](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/austine-aj/32/67751_2.png) [@austine-aj](https://discourse.nodered.org/u/austine-aj)\
**Post date:** [15 September 2022 12:35 UTC](https://discourse.nodered.org/t/how-to-set-global-variable-parameters/19470/15 "2022-09-15T12:35:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![SkyMound](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@SkyMound](https://discourse.nodered.org/u/SkyMound)\
**Post date:** [15 September 2022 15:11 UTC](https://discourse.nodered.org/t/how-to-set-global-variable-parameters/19470/16 "2022-09-15T15:11:14Z")

</div>

Hello every one, i have quite the same case here

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

And it throws me this :

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

```

---

<div class="post-metadata">

**Author:** ![SkyMound](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@SkyMound](https://discourse.nodered.org/u/SkyMound)\
**Post date:** [15 September 2022 20:26 UTC](https://discourse.nodered.org/t/how-to-set-global-variable-parameters/19470/17 "2022-09-15T20:26:54Z")

</div>

Actually, string concatenation simply does the trick !

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

```

---

<div class="post-metadata">

**Author:** ![Sandmman](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Sandmman](https://discourse.nodered.org/u/Sandmman)\
**Post date:** [16 November 2022 17:20 UTC](https://discourse.nodered.org/t/how-to-set-global-variable-parameters/19470/18 "2022-11-16T17:20:54Z")

</div>

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

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

```
