Changing state of input_number in Function Node - is it possible?

One can read states with global.get ('homeassistant').homeAssistant.states['input_number.xyz'].state;
but it doesn't seem to have any effect if I use global.set.

Is it at all possible to change these values from a function Node ?

are you doing global.set correctly?

global.set("key", value)

so in your case...

var ha = global.get('homeassistant');
var oldValue = ha.homeAssistant.states['input_number.xyz'].state;
ha.homeAssistant.states['input_number.xyz'].state = 77; //change something
ha.homeAssistant.states.something_else = "a new string value"; //change something_else
global.set('homeassistant', ha);//store it

NOTE:

  • I did not check for existence of properties (so errors can occur)
  • I know nothing about HA so setting that state value might have side affects.
  • As homeassistant is an object, calls to global.set('homeassistant', ha) are not strictly necessary - but are definitely good practice (as homeassistant is an object it will be modified by reference, you actually modify the global instance - but if you persist context to file, these wont happen unless you call global.set()) In a nutshell, always call global.set after modifying the value.
  • I am assuming homeAssistant.homeAssistant.states['input_number.xyz'].state is a valid path (as per your post) - though i suspect there are one too many homeAssistants - but you would need to show us the actual content of the global context object to be sure.

Well I have tried using it like this:
global.set ('homeassistant').homeAssistant.states['input_number.xyz'].state, 1234);
and it looked like nothing happened to the value. The old value is still the same if read afterwards.

And yes, ('homeassistant').homeAssistant.states['input_number.xyz'].state is a valid path and the state can be read without any problems.

That definitely wont work.

The equivelant is this...
global.get('homeassistant').homeAssistant.states['input_number.xyz'].state = 1234;

This works because get gets the object & as it as a by reference affair, the sub properties are accessible & settable.

It works, great !
I think I have to dig into this a little deeper, I would probably never have solved this problem without you help.
I saw you comment on side affects and have to make sure I really understand what effects is has.

Thanks Steve-Mcl, you have really made my day.

You're welcome.

Please note, the "correct node-red way" to do this is...

var ha = global.get('homeassistant');
ha.homeAssistant.states['input_number.xyz'].state = 1234;
global.set('homeassistant', ha);

Noted.

Doing it this way is also much more inline with what I intend to do (setting many states).

1 Like

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