# How to get global variable properties

**URL:** <https://discourse.nodered.org/t/how-to-get-global-variable-properties/9340>\
**Category:** General\
**Created:** [23 March 2019 00:54 UTC](https://discourse.nodered.org/t/how-to-get-global-variable-properties/9340 "2019-03-23T00:54:40Z")\
**Posts on this page:** 17\
**Page:** 2

<div class="post-metadata">

**Author:** ![RonMa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ronma/32/42531_2.png) [@RonMa](https://discourse.nodered.org/u/RonMa)\
**Post date:** [28 May 2021 06:13 UTC](https://discourse.nodered.org/t/how-to-get-global-variable-properties/9340/21 "2021-05-28T06:13:19Z")

</div>

Alright, here is a very simple example of a test - flow:

 ![example_flow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/3/33117d55ffc9517d2b44a15b35c2f40aca2b616c.png)

That´s written in the function node:

````auto
if (nodes[3].attributes[0].current_value === 0)
{
  return{payload: msg.payload.nodes[3].attributes[0].current_value}
}```

And that's the output I get on the debug node:

> ReferenceError: nodes is not defined (line 1, col 1)
````

---

<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:** [28 May 2021 06:39 UTC](https://discourse.nodered.org/t/how-to-get-global-variable-properties/9340/22 "2021-05-28T06:39:32Z")

</div>

@RonMa to get a global context value you have to use the `global.get()` function:

```auto
global.get("homee.nodes[3].attributes[0].current_value")

```

---

<div class="post-metadata">

**Author:** ![RonMa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ronma/32/42531_2.png) [@RonMa](https://discourse.nodered.org/u/RonMa)\
**Post date:** [28 May 2021 08:16 UTC](https://discourse.nodered.org/t/how-to-get-global-variable-properties/9340/23 "2021-05-28T08:16:26Z")

</div>

This finally worked for me:

```auto
var ObtainedData = global.get("homee.nodes[3].attributes[0].current_value");
msg.payload = ObtainedData;
return msg;

```

Thanks much @knolleary

---

<div class="post-metadata">

**Author:** ![RonMa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ronma/32/42531_2.png) [@RonMa](https://discourse.nodered.org/u/RonMa)\
**Post date:** [28 May 2021 15:28 UTC](https://discourse.nodered.org/t/how-to-get-global-variable-properties/9340/24 "2021-05-28T15:28:34Z")

</div>

@knolleary - Can u maybe help me with one more simple request. I´m trying to check the status of a flow variable in a function node but somehow the result is always "false" even if the actual status is true...

```auto
if (flow.get(schutz_aktiviert) === true){
    msg.payload = "put:/nodes/67/attributes/192?target_value=80";
   } else {
       msg.payload = false;
   }
    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:** [28 May 2021 15:41 UTC](https://discourse.nodered.org/t/how-to-get-global-variable-properties/9340/25 "2021-05-28T15:41:11Z")

</div>

> [@RonMa](#):
>
> `flow.get(schutz_aktiviert)`

Is `shutz_aktiviert` a variable containing the flow variable name string or did you mean  
`flow.get("schutz_aktiviert")`

---

<div class="post-metadata">

**Author:** ![RonMa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ronma/32/42531_2.png) [@RonMa](https://discourse.nodered.org/u/RonMa)\
**Post date:** [28 May 2021 15:48 UTC](https://discourse.nodered.org/t/how-to-get-global-variable-properties/9340/26 "2021-05-28T15:48:11Z")

</div>

Jesus, I forget the " " 🤭 - Thanks much @Colin

---

<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:** [28 May 2021 15:57 UTC](https://discourse.nodered.org/t/how-to-get-global-variable-properties/9340/27 "2021-05-28T15:57:23Z")

</div>

I am surprised you did not get an error from that.

---

<div class="post-metadata">

**Author:** ![RonMa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ronma/32/42531_2.png) [@RonMa](https://discourse.nodered.org/u/RonMa)\
**Post date:** [28 May 2021 16:38 UTC](https://discourse.nodered.org/t/how-to-get-global-variable-properties/9340/28 "2021-05-28T16:38:33Z")

</div>

And I´m surprised I´m still too stupid to set the flow variable "false" again 🙄 What am I doing wrong now:

```auto
if (flow.get("schutz_aktiviert") === true){
    msg.payload = "put:/nodes/67/attributes/192?target_value=0";
    flow.set("schutz_aktiviert") == false;
   } else {
       msg.payload = false;
   }
    return msg;

```

---

<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:** [28 May 2021 16:48 UTC](https://discourse.nodered.org/t/how-to-get-global-variable-properties/9340/29 "2021-05-28T16:48:45Z")

</div>

> [@RonMa](#):
>
> `flow.set("schutz_aktiviert") == false;`

`flow.set("schutz_aktiviert", false)`

"Writing Functions : Node-RED" [Writing Functions : Node-RED](https://nodered.org/docs/user-guide/writing-functions#storing-data)

---

<div class="post-metadata">

**Author:** ![mikefila](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikefila/32/95223_2.png) [@mikefila](https://discourse.nodered.org/u/mikefila)\
**Post date:** [28 May 2021 20:38 UTC](https://discourse.nodered.org/t/how-to-get-global-variable-properties/9340/30 "2021-05-28T20:38:52Z")

</div>

I'm using the node red with home assistant. Most HA paths have quotation marks `""` that when entered gives me an error. Is there a way to mitigate the paths quotation marks?

```auto
Expected ')' and instead saw 'sensor'

```

```auto
var value = global.get("homeAssistant.states["sensor.flue_temp"].state");
msg.payload = value;
return msg;

```

---

<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:** [28 May 2021 21:47 UTC](https://discourse.nodered.org/t/how-to-get-global-variable-properties/9340/31 "2021-05-28T21:47:02Z")

</div>

Without knowing the contents of your global context or this flue\_temp value - a guess....

```auto
var value = global.get("homeAssistant.states[" + sensor.flue_temp + "].state");
msg.payload = value;
return msg;

```

---

<div class="post-metadata">

**Author:** ![mikefila](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikefila/32/95223_2.png) [@mikefila](https://discourse.nodered.org/u/mikefila)\
**Post date:** [29 May 2021 02:47 UTC](https://discourse.nodered.org/t/how-to-get-global-variable-properties/9340/32 "2021-05-29T02:47:11Z")

</div>

Thanks Steve though it's giving me

```auto
"ReferenceError: sensor is not defined (line 1, col 50)"

```

I'm not sure what you mean by "contents of your global context or this flue\_temp value ".

`homeAssistant.states["sensor.flue_temp"].state` that is from copy path button. Context storage is enabled in `settings.js` and a screenshot from my context tab under the global heading

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/4/74ff46c7888a321a8790e1a1bc5c265cbf60bf39.png)

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/9/296a7946038312f50f5b6db172bfb8e1d083b0d4.png)

---

<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:** [29 May 2021 05:47 UTC](https://discourse.nodered.org/t/how-to-get-global-variable-properties/9340/33 "2021-05-29T05:47:00Z")

</div>

> [@mikefila](#):
>
> `homeAssistant.states["sensor.flue_temp"].state` that is from copy path button

Ok now I understand. I think.

Try...

```auto
var ha = global.get("homeAssistant");
var value = ha.states["sensor.flue_temp"].state;
msg.payload = value;
return msg;

```

Or...

```auto
var value = global.get('homeAssistant.states["sensor.flue_temp"].state');
msg.payload = value;
return msg;

```

---

<div class="post-metadata">

**Author:** ![mikefila](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikefila/32/95223_2.png) [@mikefila](https://discourse.nodered.org/u/mikefila)\
**Post date:** [30 May 2021 18:11 UTC](https://discourse.nodered.org/t/how-to-get-global-variable-properties/9340/34 "2021-05-30T18:11:00Z")

</div>

Unfortunately I'm still not able to get a value. It almost seems that this data, the naming scheme, wasn't meant to be used like this. I don't need this particular value, I was just getting familiar with storing and recalling context, which I am able to do just not these HA values.

I'm pretty sure the nodes provided by home assistant will let me extract it. That said if you have other ideas I'd be glad to try them.

The first example

```auto
5/30/2021, 2:03:19 PMnode: 3b0a1bf7.a4e3c4function : (error)
"TypeError: Cannot read property 'states' of undefined"

```

The second example

```auto
5/30/2021, 2:01:18 PMnode: c1bc3266.6aa02
msg : Object
object
_msgid: "3b4f99b2.ebc586"
payload: undefined
```

---

<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:** [30 May 2021 18:58 UTC](https://discourse.nodered.org/t/how-to-get-global-variable-properties/9340/35 "2021-05-30T18:58:32Z")

</div>

To get a property of a msg you use the copy path path button.

There’s a great page in the docs that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

![BX00Cy7yHi](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

[https://nodered.org/docs/user-guide/messages](https://nodered.org/docs/user-guide/messages)

To access the global items in your screen shot, you can use the copy path button on the item or the sub item. I looked a bit closer at your screenshot & believe this should work...

```auto
var ha = global.get("homeassistant");
var value = ha.homeAssistant.states["sensor.flue_temp"].state;
msg.payload = value;
return msg;

```

Or...

```auto
var value = global.get('homeassistant.homeAssistant.states["sensor.flue_temp"].state');
msg.payload = value;
return msg;

```

But I can't be 100% sure as your picture doesn't show the full object. As I said, use the copy path button.

---

<div class="post-metadata">

**Author:** ![mikefila](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikefila/32/95223_2.png) [@mikefila](https://discourse.nodered.org/u/mikefila)\
**Post date:** [30 May 2021 21:06 UTC](https://discourse.nodered.org/t/how-to-get-global-variable-properties/9340/36 "2021-05-30T21:06:49Z")

</div>

```auto
homeAssistant.states["sensor.flue_temp"].state

```

That is it, that is what I get from copy path. But the both of the 2 new examples work perfectly. Thank you this helpful. I'll use these values more often than globals I set, which in turn will give me more opportunities to use context.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [13 June 2021 21:07 UTC](https://discourse.nodered.org/t/how-to-get-global-variable-properties/9340/37 "2021-06-13T21:07:05Z")

</div>

Closed on 13 June 2021 21:07 UTC

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

[Previous page](https://discourse.nodered.org/t/how-to-get-global-variable-properties/9340.md?page=1)
