# Use global variable from homeassistant in function node

**URL:** <https://discourse.nodered.org/t/use-global-variable-from-homeassistant-in-function-node/47864>\
**Category:** General\
**Created:** [30 June 2021 21:04 UTC](https://discourse.nodered.org/t/use-global-variable-from-homeassistant-in-function-node/47864 "2021-06-30T21:04:30Z")\
**Posts on this page:** 18\
**Page:** 1

<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:** [30 June 2021 21:04 UTC](https://discourse.nodered.org/t/use-global-variable-from-homeassistant-in-function-node/47864/1 "2021-06-30T21:04:30Z")

</div>

Hi Node-Red Pros 🙂

I´m trying to create a function node that makes use of a value coming from a global variable of my home assistant.

This is what I receive when I copy the path from the corresponding global variable:

> homeAssistant.states["cover.esstisch\_jalousie"].attributes.current\_position

First I tried to use it exactly as is which would make the function node look as follows:

```auto
if ((global.get("homeAssistant.states["cover.esstisch_jalousie"].attributes.current_position")<80))
{  
    msg.payload = 99;
}
else{
    msg.payload = 0;
}
return msg;

```

As you might imagine, Node red doesn´t accept this format and claims "Invalid properties". Then I tried to tweak it on my own for the function node to look like this:

```auto
if ((global.get("homeAssistant.states[cover.esstisch_jalousie].attributes.current_position")<80))
{  
    msg.payload = 99
    return msg;
}
else{
    msg.payload = 0
    return msg;
}

```

Now Node-Red would accept the format but when running the flow the debug gives me the following error notice:  
`Error: Invalid property expression: unexpected c at position 21`

I don´t know what I am doing different from usual when using global variables, which works just fine. Anybody has an idea?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [30 June 2021 21:23 UTC](https://discourse.nodered.org/t/use-global-variable-from-homeassistant-in-function-node/47864/2 "2021-06-30T21:23:41Z")

</div>

I **think** it is as problem with the " and their structure.

```auto
("homeAssistant.states["cover.esstisch_jalousie"].attributes.current_position")<80)

```

I have fallen for this time and time again and still don't quite get it.  
How about you post the payload and I may be able to help you.

Breaking it down it is like this - as best I can make it.

`"homeAssistant.states[**number**].attributes.current_position"`

and you want to index the `number` part.

Anyway, waiting for you to post the `msg.payload` and I will be better informed.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [30 June 2021 21:37 UTC](https://discourse.nodered.org/t/use-global-variable-from-homeassistant-in-function-node/47864/3 "2021-06-30T21:37:00Z")

</div>

Surround the string with single quotes, then you can use double quotes inside.

```auto
global.get('homeAssistant.states["cover.esstisch_jalousie"].attributes.current_position')

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [30 June 2021 21:44 UTC](https://discourse.nodered.org/t/use-global-variable-from-homeassistant-in-function-node/47864/4 "2021-06-30T21:44:36Z")

</div>

Thanks. I couldn't remember 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:** [30 June 2021 22:03 UTC](https://discourse.nodered.org/t/use-global-variable-from-homeassistant-in-function-node/47864/5 "2021-06-30T22:03:32Z")

</div>

Alright, this already helped much @E1cid as I'm at least receiving a value now 🙂 However, the message payload which gets returned is always "0" where it should actually be "99" as defined below.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/b/6bad3ba38c595ea3047956df6a69b136c8e4215e.png)

Anything more that I´m doing wrong now?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [30 June 2021 22:17 UTC](https://discourse.nodered.org/t/use-global-variable-from-homeassistant-in-function-node/47864/6 "2021-06-30T22:17:05Z")

</div>

It may help if you copy/paste the payload rather than a screen shot.

To do this, in the `debug` window - right side of the screen - at the top line of the message to the right you will see a little clipboard icon when you move the mouse over it.

Click that and it will copy the `msg.paylaod` to the clipboard.

Paste it here and _we_ see it as text and can better help you with it as it is easier to see/read.

---

<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:** [30 June 2021 22:43 UTC](https://discourse.nodered.org/t/use-global-variable-from-homeassistant-in-function-node/47864/7 "2021-06-30T22:43:07Z")

</div>

@Trying_to_learn , I´m not quite sure if we are on the same page here. What I´m trying to do is to use the function node to create the message payload. So if I copy what I created as payload it´s just "0".

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [30 June 2021 22:48 UTC](https://discourse.nodered.org/t/use-global-variable-from-homeassistant-in-function-node/47864/8 "2021-06-30T22:48:24Z")

</div>

Ok, before any of your nodes, you have the node which gives you the `msg.payload` you want to examine.

Put a `debug` node there and set it to show `full message`.

Look at that message via the `debug` node and paste the message as shown.

That way _we_ (others and I) can see the message better than looking at a screen shot.

So, this is how to do the `debug` node:

 ![Screenshot from 2021-07-01 08-46-11](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/f/0fbd61da09aeb88c05e6b7060dae31f7528dbdef.png)

and this is how to get the `payload`

 ![Screenshot from 2021-07-01 08-46-26](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/1/812de7817727394b7d3df6d1263d2380412e2582.png)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [1 July 2021 05:39 UTC](https://discourse.nodered.org/t/use-global-variable-from-homeassistant-in-function-node/47864/9 "2021-07-01T05:39:29Z")

</div>

I see no issues in the code.  
Try setting the global to a variable, then use `node.warn(variable); to output to debug.  
If that shows that the global is set and the value is 0 then post your flow.

---

<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:** [1 July 2021 10:07 UTC](https://discourse.nodered.org/t/use-global-variable-from-homeassistant-in-function-node/47864/10 "2021-07-01T10:07:27Z")

</div>

Not quite sure I understand correctly what you mean @E1cid but I know created this sample flow.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/8/e803a0e32e0ddffccd416af83192f28bd4b4cc48.png)

The code insight the function node is as follows:

```auto
var a = global.get('homeAssistant.states["cover.esstisch_jalousie"].attributes.current_position')
node.warn(a)

```

The result of the debug node is

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

The same happens btw. when using a change node:

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

With the change node configured like this:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/4/9421ff6ced7e12eabc69248862da862ecf5fb1b0.png)

---

<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:** [1 July 2021 10:40 UTC](https://discourse.nodered.org/t/use-global-variable-from-homeassistant-in-function-node/47864/11 "2021-07-01T10:40:27Z")

</div>

Does the global actually exist? You can check it in the sidebar

---

<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:** [1 July 2021 12:47 UTC](https://discourse.nodered.org/t/use-global-variable-from-homeassistant-in-function-node/47864/12 "2021-07-01T12:47:31Z")

</div>

Hi @zenofmud - It´s listed under "States" of the global "homeAssistant" object so I would assume it should be right?

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

---

<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:** [1 July 2021 13:02 UTC](https://discourse.nodered.org/t/use-global-variable-from-homeassistant-in-function-node/47864/13 "2021-07-01T13:02:37Z")

</div>

I don't see it there

I would start of by just picking up global.homeAssistant and feeding that into a debug node. If that works and shows the correct contents then work down till you find at what level the problem is.

---

<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:** [1 July 2021 13:51 UTC](https://discourse.nodered.org/t/use-global-variable-from-homeassistant-in-function-node/47864/14 "2021-07-01T13:51:21Z")

</div>

Alright, I tried it with several other entities of the homeAssistant and it never works.

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/0/f0a4084621d109586b9fb5bfa7a4544ecd547658.png)

However, if I´m using the global context information of my other Smart Home System (Homee) it just works perfectly fine.

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

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

So I guess the homeAssistant integration into Node-Red is just buggy.

---

<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:** [1 July 2021 14:48 UTC](https://discourse.nodered.org/t/use-global-variable-from-homeassistant-in-function-node/47864/15 "2021-07-01T14:48:49Z")

</div>

Try `global.homeassistant` all lower case.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [1 July 2021 14:56 UTC](https://discourse.nodered.org/t/use-global-variable-from-homeassistant-in-function-node/47864/16 "2021-07-01T14:56:04Z")

</div>

> [@RonMa](#):
>
> ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/2/92077f77c5fc34fe520ce9546c8e029d881e28c5.png)
> 
> image750×918 62.6 KB

```auto
homeassistant.homeAssistant. ***** etc

```

homeassistant is global name , then it has a property called homeAssistant.

When you copy path it never copies first property e.g. msg or in this case homeassistant

---

<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:** [1 July 2021 15:49 UTC](https://discourse.nodered.org/t/use-global-variable-from-homeassistant-in-function-node/47864/17 "2021-07-01T15:49:44Z")

</div>

Oh man - that was exactly it! They couldn´t have made it more complicated 😒 Thanks much again to @E1cid, @Colin and all the other contributors for taking your time to support!

---

<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:** [15 July 2021 15:50 UTC](https://discourse.nodered.org/t/use-global-variable-from-homeassistant-in-function-node/47864/18 "2021-07-15T15:50:04Z")

</div>

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