Use global variable from homeassistant in function node

Hi Node-Red Pros :slight_smile:

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:

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:

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?

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

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

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

global.get('homeAssistant.states["cover.esstisch_jalousie"].attributes.current_position')
2 Likes

Thanks. I couldn't remember that. :frowning:

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

Anything more that I´m doing wrong now?

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.

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

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:

and this is how to get the payload

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.

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

The code insight the function node is as follows:

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

The result of the debug node is

image

The same happens btw. when using a change node:

With the change node configured like this:

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

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

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.

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

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

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

Try global.homeassistant all lower case.

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

2 Likes

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

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