Question: setting global variables, then using their values as trigger

I’ve been searching around and trying to find an answer to the question how to best work with (global) variables for node red flows.

Let’s imagine an example: the sun.sun integration in HA has elevation and azimuth as its attributes. Let’s say I want to create several automations based on where the sun is (blinds tilting, covers closing, lights going on/off/changing intensity etc.). I could create for each automation an events:state node as the start of each one that would listen for attribute changes of sun.sun (so e.g. elevation getting lower) and a change node after it to set trigger conditions (if elevation > X, then do stuff), but that seems like duplication.

What I was thinking about is have event:state node update itself on all attributes changes and have a change node after it that would set a global variable (e.g. global.sun_elevation) to the value of of sun.sun. That way the elevation could be stored once and just checked/called in different places. Is my logic correct here? Yes there could be a way to create new entities in HA itself that would only have this elevation as its value, but it seems unnecessary to clutter HA with it.

What I don’t know is how this global variable could then be used as a trigger for automations, especially on when its value is checked. Does its value get stored if node-red/HA is restarted?

Not sure why you need to store the value.
If you have a events state node then use a switch node to look at the if that is above or below your trigger and do the actions after.
I don’t really see a point in saving these values since they are old as soon as they are saved anyways

It’s already stored as a global. All home assistant states can be accessed by using the global. drop down. The path for the sun’s elevation is


I’d like to know how to do that too ?

As noted above my Mikefila, this information is already there. No reason to store it again. Or just use the current state node and don’t worry about it, let the node handle going to the cache or the source and optimize performance for you.

Not by default but you can make it do that. See here for how to add a persistent store. If you use the addon your settings.js file should be in /config/node-red

But you can’t trigger off of changes to global variables, that’s not possible in Node RED. There are custom nodes that provide this capability but honestly don’t do that, especially for this use case. Just use an events:state or trigger:state node.

Even using global variables in general is probably something to avoid for users using Node RED in conjunction with Home Assistant. An entity node is going to be better most of the time. Then you can find them easily in HA, show the value in the UI and view change history.

1 Like

As an example I will use globals to bring states into a change node to perform math or build a message.

Or to bring a state into a function node with

var temp = global.get('homeassistant.homeAssistant.states["sensor.temp"].state');
var hum = global.get('homeassistant.homeAssistant.states["sensor.humidity"].state');

If you want to have a trigger fire according to an attribute value, use a trigger node. This will trigger when the sun’s elevation is >= 5.

1 Like