Write value to home assistant entity via node red function

Hi Guys,

I am using node red and try to read and write variables to and from Home Assistant via the function node. So I figured out that reading the variables kan be done by using this line:

var states = global.get('homeassistant.homeAssistant.states["sensor.xxxx"].attributes.next_rising');

This works fine, no problems. Now I also want to write to Home Assistant variables, but this doesn’t seem to work. I use this line of code:

global.set("homeassistant.homeAssistant.states['sensor.xxxxx'].state", temp);

Where “temp” is the variable I want to write to the state of this entity. I get no errors in node red here, so the code does get executed. For some reason the value is not being updated in HA.

Thanks!

As far as I know you can’t set sensor values like that.
But you can create a sensor that Node red controls and set it the way you want, but to set any given sensor is probably not possible.

So I made a little test with an input boolean and in Node Red it seems to work ok, but I don’t see any changes in Home Assistant itself.

var states = global.get('homeassistant.homeAssistant.states');
var present = states['input_boolean.guests_present'].state;
node.warn(present)
if (present== "on"){
    node.warn("off")
    states['input_boolean.guests_present'].state = "off";
    global.set("homeassistant.homeAssistant.states", states);
}else{
    node.warn("on")
    states['input_boolean.guests_present'].state = "on";
    global.set("homeassistant.homeAssistant.states", states);
}
return msg;

I look at the current state of the input boolean and then invert it. The debug window seems to tell me that with every trigger, the input boolean is inverted. But when I look in home assistant the last change was more than a week ago.

image

I don’t understand what this does given that you use an undefined variable in the if statement.
Then you debug output a fixed message. :man_shrugging:

Create a sensor Node red controls instead and it will work as expected.

sorry, my mistake. I translated the code, but missed the if statement… :slight_smile:

The thing is, node red is now occupied with a lot of nodes for reading and writing values to and from home assistant. I want to reduce the amount of nodes I use by putting as much as possible in function nodes. Therefore I would also like to use the global.set command for HA variables.

Globals have no influence on home assistant. They are simply values imported from ha and stored in a way that it’s accessible from nodered’s runtime.

1 Like

Hello, are you sure of that?
If so, you solved my problem…

I am having a similar issue (I want to change the brightness)

my code in node red function:

// set the object homeassistant as a js variable
var ha = global.get('homeassistant');

// set a new brightness level - lamp on my desk
ha.homeAssistant.states['light.led_scrivania_destro'].attributes.brightness = 255;

// update the global variable
global.set('homeassistant', ha);

this code works but only on the node red side, I can see the value changing into the homeassistant object (into the context data tab) but both (lamp and home assistant) doesn’t change and remain at the default level (50)

Thanks

So the globals FROM HA, that you can access within node-red, are a one way street.

Thanks. I have been looking for this specific bit of explanation for at least an hour.
(and it seems many other people have tried to change HA values from within a node-red function though those globals)
Thank you!!

You can set things in HA from node red, but you need to do it in a proper way.
You can’t do it in a function node. You need to use the nodes that is meant to set values, like call service node or sensor node if you wish to create a new sensor in HA.

1 Like