Example #1
// 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
[{"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":[[]]}]