Ways to make an AND node with true and false output?

Hi everyone, I’m trying to create a sequence that reacts based on multiple device states. This led me to install the node-red-contrib-bool-gate node, which triggers only if multiple conditions are met. So far so good.

[{"id":"fbef87b08305041b","type":"tab","label":"Flow 2","disabled":false,"info":"","env":[]},{"id":"46a5c4966a601184","type":"and-gate","z":"fbef87b08305041b","name":"","rules":[{"t":"eq","v":"on","vt":"str","propertyType":"msg","property":"payload","topic":"light.feet_light"},{"t":"eq","v":"on","vt":"str","propertyType":"msg","property":"payload","topic":"light.tv_light"},{"t":"eq","v":"on","vt":"str","propertyType":"msg","property":"payload","topic":"light.door"}],"outputTopic":"","gateType":"and","emitOnlyIfTrue":false,"x":520,"y":180,"wires":[["4dea3c3c03f81831","ebcce3cd6bb98ef1"]]},{"id":"6362bc6a36c76b8e","type":"server-state-changed","z":"fbef87b08305041b","name":"","server":"8a86b0cc.b2bac","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.feet_light","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":240,"y":180,"wires":[["46a5c4966a601184"],[]]},{"id":"15e6fe8d397c9b40","type":"server-state-changed","z":"fbef87b08305041b","name":"","server":"8a86b0cc.b2bac","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.tv_light","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"1","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":240,"y":240,"wires":[["46a5c4966a601184"],[]]},{"id":"ee29167b8a4fe096","type":"server-state-changed","z":"fbef87b08305041b","name":"","server":"8a86b0cc.b2bac","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.door","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":230,"y":300,"wires":[["46a5c4966a601184"],[]]},{"id":"ebcce3cd6bb98ef1","type":"debug","z":"fbef87b08305041b","name":"debug 23","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":720,"y":180,"wires":[]},{"id":"4dea3c3c03f81831","type":"api-call-service","z":"fbef87b08305041b","name":"","server":"8a86b0cc.b2bac","version":5,"debugenabled":false,"domain":"switch","service":"turn_on","areaId":[],"deviceId":[],"entityId":["switch.sonoff_ceiling_light"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":720,"y":260,"wires":[[]]},{"id":"8a86b0cc.b2bac","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30,"areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]

The thing here is that this node only has one output and it only reacts when it turns “TRUE”, but when it becomes FALSE (it shows a red dot and says FALSE) it doesn’t do anything. Can anyone recommend another way around this? Or am I missing something?

Thanks,
Rodrigo

Try to use this instead of the and-gate node.

[{"id":"d7fbda01ca7d258b","type":"function","z":"fbef87b08305041b","name":"function 3","func":"if (global.get('homeassistant').homeAssistant.states[\"light.feet_light\"].state==\"off\") {\n    msg.payload=\"false\";\n    return msg;\n}\nif (global.get('homeassistant').homeAssistant.states[\"light.tv_light\"].state == \"off\") {\n    msg.payload = \"false\";\n    return msg;\n}\nif (global.get('homeassistant').homeAssistant.states[\"light.door\"].state == \"off\") {\n    msg.payload = \"false\";\n    return msg;\n}\nmsg.payload=\"true\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":525,"y":360,"wires":[["68b538d379feccd6"]]}]

Ok @WallyR, thanks for writing that code! It’s working, I see that it outputs true or false. I’ll probably use this.

I still wonder, is there any other way to get something like this but without needing to use a function node? I’m migrating from Webcore and even though I love that NodeRED seems to be more graphic, I’m finding that it needs more coding than I expected :s

Thanks,
Rodrigo

1 Like

You could do the same with a chain of current state nodes.

[{"id":"d93cc9d675824595","type":"api-current-state","z":"be03f9025a1f137a","name":"","server":"541ade28.b4a62","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.feet_light","state_type":"str","blockInputOverrides":false,"outputProperties":[],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":375,"y":900,"wires":[["676ad6c663a4b579"],["fc0ed15429b70de3"]]},{"id":"676ad6c663a4b579","type":"api-current-state","z":"be03f9025a1f137a","name":"","server":"541ade28.b4a62","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.feet_light","state_type":"str","blockInputOverrides":false,"outputProperties":[],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":375,"y":960,"wires":[["2cf8cc986596990c"],["fc0ed15429b70de3"]]},{"id":"2cf8cc986596990c","type":"api-current-state","z":"be03f9025a1f137a","name":"","server":"541ade28.b4a62","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.feet_light","state_type":"str","blockInputOverrides":false,"outputProperties":[],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":375,"y":1020,"wires":[["7b4158cfa4bdcba3"],["fc0ed15429b70de3"]]},{"id":"fc0ed15429b70de3","type":"change","z":"be03f9025a1f137a","name":"false","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":960,"wires":[[]]},{"id":"7b4158cfa4bdcba3","type":"change","z":"be03f9025a1f137a","name":"true","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":1020,"wires":[[]]},{"id":"541ade28.b4a62","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"","connectionDelay":false,"cacheJson":false,"heartbeat":false,"heartbeatInterval":"","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]
2 Likes