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

I had a post up about this "problem" a month ago, and thought it was solved. It turns out that there is still problems.

I'm doing this in a Function Node:
  var ha = global.get('homeassistant');
  ha.homeAssistant.states['input_number.x'].state = 1;
  global.set('homeassistant', ha);

Seen from the Context Tab in Home Assistant (under Global context) the variable changes as expected, but an Event-state node (Server-Change-State) monitoring input_number.x do not react to the change.

If input_number.x is altered using a Call-Service-Node (Api-Call-Service) everything works fine.

What I need, is to be able to alter input_number and input_boolean variables using a Function Node, instead of using a Call-Service-Node.

I'm running Node-Red on Home Assistant (Hassio) - all latest versions.

Any suggestions ?

Boja.

If you want to change the state of sensors from nodered from a function, then you would have to go via the API route. In the function node, you prepare the message and through an HTTP or HTTPS API call you can change the states.

Something like:

    sensor = 'something';
    m.url = host + "/api/states/sensor." + sensor;
    m.method = "POST";
    m.headers = {
        'Authorization': 'Bearer ' + token,
        'content-type': 'application/json',
    };    
    m.payload = {
        "state": whatever
    };
    node.send(m); //send a msg to next node

Followed by an HTTP(S) call.

Does it matter that the input_number variable is just a variable defined in configuration.yaml, not a device/sensor ?.

The purpose of doing it with a function, is to avoid having a Call-Service-Node for each input_number variable.

I have never done it. But, looking at the documentation for input_number, it seems that you can change it like any other "sensor".
Why not using the Call-Service in this situation?

There is many reasons to this. One is that handling very large numbers of Call-Service-Nodes in a flow, makes Node-red WAY to slow (on a Raspberry Pi 4).

The way I understand the communication between nodered and HA, the call service is in fact using the API under the hood (websocket call)... So, I can't imagine it is going to make a difference from that point of view between the "call service" and the API call I suggested. Never compared it though, so I might be wrong. You may want to post this on the HA community forum. "Experts" (I am not!) might have a different take on this.

Well, slow is not in regard to operation speed, that's probably fine.

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