Javascript in node red

since i can’t find a way to do what i want (like variables in automations) i’m thinking about doing it in javascript in node red. but there are some questions about variables:

  • is it possible to access input booleans and input numbers directly from javascript?
  • if i have some variables, will their values be kept from one run to the next?

First question: yes
2nd question: dunno. However, you can use globals or flow variables in node red, so yes.

Here is an example of a flow variable, and how to get access to your HA entities.


var MyTimer = flow.get('kitLightTimer');
const states = global.get("homeassistant").homeAssistant.states; 
const delay = parseFloat(states['input_number.kit_light_delay_off'].state);

function StopTimer() {
    clearTimeout(MyTimer);
}

function StartTimer() {
    MyTimer = setTimeout(function(){
        msg['payload'] = 'off';
        node.send(msg);
    }, 60000*delay);
    flow.set("kitLightTimer", MyTimer)
}

StopTimer();
StartTimer();

If by variables you mean normal js variables then no, they are scoped like normal.

But nodered flow and globals can survive restarts and redeploys if you set up a file store. They don’t by default though, you have to modify settings to add a new store and then use it as per that documentation.