How to get global variable properties

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

That´s written in the function node:

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)

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

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

This finally worked for me:

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

Thanks much @knolleary

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

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

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

Jesus, I forget the " " :face_with_hand_over_mouth: - Thanks much @Colin

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

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

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;

flow.set("schutz_aktiviert", false)

"Writing Functions : Node-RED" Writing Functions : Node-RED

1 Like

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?

Expected ')' and instead saw 'sensor'
var value = global.get("homeAssistant.states["sensor.flue_temp"].state");
msg.payload = value;
return msg;

Without knowing the contents of your global context or this flue_temp value - a guess....

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

Thanks Steve though it's giving me

"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

Ok now I understand. I think.

Try...

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

Or...

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

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

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

The second example

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

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

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

Or...

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.

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

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