Getting Started with node red : different mindset

Hi All,
I have been messing with HA for a bit in January and had to stop because of other issues. Now, I am getting back to it !
I decided to use node-red as I read tons of positive feedback but I have been facing few issues with it :

  • autocomplete does not work (I gave up on that for now)
  • for me it is so not intuitive that I literally cannot do any real flow !

I mean, I have been able to turn on or off some lights, I can also know which switch was pushed, but I cannot do something as simple as if a light is on in a list of lights, then turn them all off, and if all are off, turn them all on.

With any programing language it would be quite straightforward :

  • Just make a table with all light you want to “group”
  • read all states (on/off) and do a “OR” on it.
  • if the OR output is true, then, at least one light is on --> turn them all off
  • else, they are all off, turn them all on

Something as basic as this I cannot achieve. I think I do not yet have a “node-red” way of thinking and I need a couple of example to really get started.

Would somebody be kind enough to explain how to implement such simple example using node-red ?

and I have a couple of questions because, to me, node-red is n

Example #1

image

// Just make a table with all light you want to “group”
const lights = ['light.light1','light.light2','light.light3'];

// read all states (on/off) 
const haCtx = global.get("homeassistant");
const entities = haCtx.homeAssistant.states;

// and do a “OR” on it.
const lightsOn = lights.filter(e => entities[e] && entities[e].state === "on");

// if the OR output is true, then, at least one light is on --> turn them all off
if(lightsOn.length) {
    msg.payload = {
        data: {
            "entity_id": lights
        }
    };
} else {
// else, they are all off, turn them all on    
    msg.payload = {
        service: "turn_on",
        data: {
            "entity_id": lights
        }
    };
}

return msg;
[{"id":"71813335.11179c","type":"inject","z":"311deaea.efb306","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":332,"y":704,"wires":[["baa25fe6.522d2"]]},{"id":"baa25fe6.522d2","type":"function","z":"311deaea.efb306","name":"","func":"// Just make a table with all light you want to “group”\nconst lights = ['light.light1','light.light2','light.light3'];\n\n// read all states (on/off) \nconst haCtx = global.get(\"homeassistant\");\nconst entities = haCtx.homeAssistant.states;\n\n// and do a “OR” on it.\nconst lightsOn = lights.filter(e => entities[e] && entities[e].state === \"on\");\n\n// if the OR output is true, then, at least one light is on --> turn them all off\nif(lightsOn.length) {\n    msg.payload = {\n        data: {\n            \"entity_id\": lights.join(\",\")\n        }\n    };\n} else {\n// else, they are all off, turn them all on    \n    msg.payload = {\n        service: \"turn_on\",\n        data: {\n            \"entity_id\": lights.join(\",\")\n        }\n    };\n}\n\nreturn msg;\n\n\n\n\n","outputs":1,"noerr":0,"x":462,"y":704,"wires":[["e3f97251.d0fd"]]},{"id":"e3f97251.d0fd","type":"api-call-service","z":"311deaea.efb306","name":"","version":1,"debugenabled":true,"service_domain":"light","service":"turn_off","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":630,"y":704,"wires":[[]]}]

Example #2

image

[{"id":"cbf87b25.7afb88","type":"inject","z":"311deaea.efb306","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":332,"y":896,"wires":[["748882b9.9eb76c"]]},{"id":"748882b9.9eb76c","type":"ha-get-entities","z":"311deaea.efb306","name":"","rules":[{"property":"entity_id","logic":"includes","value":"light.light1,light.light2,light.light3,light.bedroom_light","valueType":"str"},{"property":"state","logic":"is","value":"on","valueType":"str"}],"output_type":"count","output_empty_results":false,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":508,"y":896,"wires":[["50e5dddf.da11e4"]]},{"id":"50e5dddf.da11e4","type":"switch","z":"311deaea.efb306","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":654,"y":896,"wires":[["c3c083cf.97dc9"],["817e5e25.3514c"]]},{"id":"c3c083cf.97dc9","type":"api-call-service","z":"311deaea.efb306","name":"Turn Off","version":1,"debugenabled":true,"service_domain":"light","service":"turn_off","entityId":"light.light1,light.light2,light.light3","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":792,"y":880,"wires":[[]]},{"id":"817e5e25.3514c","type":"api-call-service","z":"311deaea.efb306","name":"Turn On","version":1,"debugenabled":true,"service_domain":"light","service":"turn_on","entityId":"light.light1,light.light2,light.light3","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":792,"y":928,"wires":[[]]}]

I do the grouping in HA using groups.yaml. Then I use those as inputs in nodered.

Also to get you even more into the nodered spirit, you can do recursive checks using [delay] and [join] nodes to get a list of lights on. Here’s an example to text a list of open windows:

That doesn’t do exactly what you need, but I posted to give one way to get a conditional list in nodered.

OTOH, what you want to do is probably quite simple to do in home assistant without node-red. If node-red doesn’t suit your programming style, don’t feel forced to use it.

I think you are overcomplicating this.

If you create a light group in home assistant for all the lights you want to use (ie the lights in your list).

Then in the node red flow you can call the “toggle” service against the light group. If all the lights in that group are off, then toggle will turn them all on. If any of the lights in the group are on, then the group is considered on and hence the toggle will turn all lights in the group off.

1 Like

Thank you all for your comments and Nicp in particular as you helped me get started.
As mentioned, one should not try to do everything in node-red (after all we are on a home assistant forum, we ought to rely on it!).

Making some light groups and using “toggle” was the solution. I was so focused on Node-red that I did not look to additional solutions ! My bad. Now, I am able to properly use my two switches and do different scenario based on, great !

Next step is controlling my radiators :star_struck: