Check status of multiple devices and act afterwards

Hey all,

I’m new to Home Assistant and NodeRed, but I’m pretty experienced in programming.
I do have a quite easy setup, two Lights in my living room that I want to control with a button.
Just a toggle call.
Now I realized that sometimes one Lights doesn’t react or my son just plays around and manually turns one off or on.

With my current setup I just have a node “button click” that calls Lamp1 and Lamp2 and does the toggle action. This results sometimes in one lamp turning on and the other off at the same time.

I would like to have a check like “if a is on and b is off, toggle both on, if a && b off, turn on, …”.
That should be doable with NodeRed, right?

I would appreciate some help.
Thanks in advance.

Try to post a read out from one of your sensors in the HA developer tools, then I can make an example.

Well, the sun integration is probably one you have, so check this function node out.

[{"id":"fcb8501ba4e0c02b","type":"function","z":"be03f9025a1f137a","name":"sun","func":"msg.sun = global.get('homeassistant').homeAssistant.states[\"sun.sun\"];\nmsg.sun_state = global.get('homeassistant').homeAssistant.states[\"sun.sun\"].state;\nmsg.sun_attr_next_dawn = global.get('homeassistant').homeAssistant.states[\"sun.sun\"].attributes.next_dawn;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":365,"y":75,"wires":[["36f148efe227cbac"]]}]

Find your other options by clicking the Context Data menu in the upper right corner and then click the refresh icon next to global.
You can then probably figure out the rest. :slight_smile:

Btw. this is just to check it, then you need to react to it, but I think your “problem” would be easier to solve with a group in HA with the “All entities” option enabled.

image

[{"id":"0c4b4398a7d4859d","type":"inject","z":"c89d915bdff0f798","name":"start","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":258,"y":928,"wires":[["37543723a3088839"]]},{"id":"37543723a3088839","type":"ha-get-entities","z":"c89d915bdff0f798","name":"","server":"","version":0,"rules":[{"property":"entity_id","logic":"includes","value":"light.light1, light.light2","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":406,"y":928,"wires":[["d21283f7e04df309"]]},{"id":"d21283f7e04df309","type":"switch","z":"c89d915bdff0f798","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":546,"y":928,"wires":[["254d8dda17b0273c"],["b00d44e0dd4287bb"]]},{"id":"254d8dda17b0273c","type":"change","z":"c89d915bdff0f798","name":"turn on","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"service\": \"turn_on\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":696,"y":928,"wires":[["b00d44e0dd4287bb"]]},{"id":"b00d44e0dd4287bb","type":"api-call-service","z":"c89d915bdff0f798","name":"","server":"","version":5,"debugenabled":true,"domain":"light","service":"toggle","areaId":[],"deviceId":[],"entityId":["light.light1","light.light2"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":710,"y":976,"wires":[[]]},{"id":"dad09a2516ed246d","type":"inject","z":"c89d915bdff0f798","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":258,"y":976,"wires":[["d21283f7e04df309"]]},{"id":"b06b6817420501ac","type":"inject","z":"c89d915bdff0f798","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":258,"y":1008,"wires":[["d21283f7e04df309"]]},{"id":"2de6b3a108b61cd9","type":"inject","z":"c89d915bdff0f798","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2","payloadType":"num","x":258,"y":1040,"wires":[["d21283f7e04df309"]]}]
1 Like