In Node-Red: how to address a global variable with a dot in the name?

Hi,
In Node-Red, I want to read the value of a global variable that has a dot in the name
As an example, I want to:
global.get("homeassistant.MyHomeAssistant.states.sun.sun.state");
The problem is the name of the variable sun.sun
I tried several ways to escape that dot, but without any luck.
Any tips are welcome,
thanks,
chrisV

Problem solved:
I’m re-posting the answer here in case someone else has the same issue.
I had also posted this to the Node-Red forum and user @knolleary suggested to use an alternate syntax like this:
global.get("homeassistant.MyHomeAssistant.states['sun.sun'].state")
…which solved the issue
Learned 2 things here:

  1. there is an alternate syntax to address the “nodes” in a JSON object
  2. single quotes can also be used and they don’t terminate the double quotes
    chrisV
1 Like

I have same issue , want to access state of specific element but I cant do that from the flow.

Tried your code (adjusted to my needs)
var haCtx = global.get(“homeassistant.Home_Assistant.states[‘switch.control_ac_emilly’].state”);

but no results like its coming null.

How can I debug/check ? @chrisV

Can’t get your example to work @chrisV

Trying two ways. Function and Switch.

Function

var item = global.get("homeassistant.homeAssistant.states['input_select.livingroom_mode'].state");
node.warn("find it 1 "+ item);

var item1 = global.get("homeassistant").homeAssistant.states['input_select.livingroom_mode'].state;
node.warn("find it 2 "+ item1);

return msg;

This will log find it 1 undefined and "TypeError: Cannot read property 'state' of undefined".

Switch

Here I don’t get any error, nor does the debug node run. I’m trying to check global and the value

homeassistant.homeAssistant.states['input_select.livingroom_mode'].state

Looking at the vars they should be there. Would be really nice to run a switch to grab existing value instead of having to setup a current state node everytime.

image of context.

Figured it out

# get global variable
var g = global.get("homeassistant");
# get states variable
var states = g.homeAssistant.states;
# get the actual entity that we want
var player = states["media_player.livingroom"];
1 Like

My apologies, @Evgeny_Golubov and @fbacker I must have missed your earlier replies.
@fbacker Glad you got it working
@Evgeny_Golubov to troubleshoot, you can add a function node and send output to a debug node (see also the commented lines in the function node)

[{"id":"3c0ba1d6.c8ad9e","type":"tab","label":"dummy test","disabled":false,"info":""},{"id":"9b57ce79.ae3158","type":"function","z":"3c0ba1d6.c8ad9e","name":"","func":"\n\n// msg=global.get(\"homeassistant.homeAssistant.states['sun.sun'].state\");\n//msg=global.get(\"homeassistant.homeAssistant\");\n\n\nmsg=global.get(\"homeassistant.homeAssistant.states['sun.sun']\");\nreturn msg;","outputs":1,"noerr":0,"x":272,"y":271,"wires":[["6e5e08ee.1fec3"]]},{"id":"6e5e08ee.1fec3","type":"debug","z":"3c0ba1d6.c8ad9e","name":"Debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":507,"y":273,"wires":[]},{"id":"7f2e8e7d.82e158","type":"inject","z":"3c0ba1d6.c8ad9e","name":"now","topic":"","payload":"now","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":"","x":124,"y":127,"wires":[["9b57ce79.ae3158"]]}]

hope this helps,
ChrisV

1 Like

Hi, I copy all. Then no happen. but
I change like this

//msg.payloadmsg=global.get(“homeassistant.homeAssistant.states[‘sun.sun’].state”);
//msg.payload=global.get(“homeassistant.homeAssistant”);

msg.payload=global.get(“homeassistant.homeAssistant.states[‘sun.sun’]”);
return msg;

-> return: “undefined”

Where am I wrong?

Hi @Kydan
I’m trying to understand what you did.
Were you able to:

  1. Import the flow (that I posted), in Node-Red?
  2. Can you share a print-screen of the debug message that you get? Click on the square witch is attached to the left of the Inject Node, to trigger an event. And then open the debug output to display the debug message.

Here is a screenshot of what you should see

hope this helps,
chris

Thanks. I just fix problem. Just rename “server” to any other name, in Node red. That’s all.

Hi @Kydan
I’m sorry, but I don’t understand what you mean with “server”
Anyway, I’m glad that you got it working
chris


This

yes, that entry has to match with your home assistant deployment
chrisV