Fetch entity state in a function node

In Node Red, sometimes, when I need to check the state of an entity, it is easier to check it inside a function node instead of in a “get entities” or “current state” node.
This code:

var states = global.get('homeassistant.homeAssistant.states');
var timerstate = states["timer.timer1"].state;

seems to do the job. Is this a reliable way to do it?

4 Likes

Should be. I use it quite a bit myself and haven’t had any issues. The other thing you can do is to create a events: state node in a separate flow and have that feed into a change node to set a global variable. That way, as soon as it changes, it will trigger that node and update a global variable.

Either way should work well and accomplish what you want to do.

1 Like

Great! Then I’ll start using it.

Instead of creating a new array of all the entites in global home assistant I just reference the exact location

var newLocation = global.get("homeassistant.homeAssistant.states['device_tracker.mkx_location'].state");

if you click the context tab on the right side (where debug is & probably under the down arrow)
you can see all the flow variables/ global variables and then search for your entity
sometimes it can be difficult to find, when that happens I copy and paste into notepad → search for what I want and then figure out roughly where it is in the list so I can narrow down the area I’m looking in… like… oh its 25% down from the top

5 Likes

Or use ‘find on page’ using the menu in your browser as ctrl-f opens the Node-Red find function that doesn’t seem to search that area.

Hi,

I am currently having a problem with a function. For now I just want to get the state of a helper and return it to the msg.payload.
The output I get is undefined. Where is the problem?

var temp1 = global.get("homeassistant.homeAssistant.states['input_text.temp_1'].state");

var newMsg = { payload: temp1};

return newMsg;

Try this:

var temp1 = global.get(“homeassistant.homeAssistant.states”)[‘input_text.temp_1’].state;

Instead of making 2 new vars – just say msg.psyload = “homeassistant state”
Return msg.payload

Hi guys,

I want to get the latest values from my powercalc states like sensor.licht_tv_power which delivers the value 4.87 Watt

What I have to do, to get this value read by Node-Red? My target is to write this value into InfluxDB and create some visualiation with Grafana.

Thank you and regards,
Lukas