Enable an input_boolean based on 3 others

I’m trying to setup a Sleep Mode using HA and NR. Basically, I already have input_booleans for Night (sunup/down), Occupied, and Armed (quasi security system). I want to enable Sleep Mode if all 3 of these states are true.

I’ve tried kicking off a flow with the Occupied mode, as it’s the one least likely to change, but this presents the problem that if I’m home while the others change, they won’t be picked up. Alternativly, I’ve tried putting the other variables earlier in the flow, with similar results.

I’ve also tried the Trigger-state node, but it only seems to change based on it’s input changing as well.

What would be the best way to set this up in Node-Red?

I think this node might work https://flows.nodered.org/node/node-red-contrib-boolean-logic-ultimate
i don’t have 3 input boolean, so I tested with inject nodes. maybe you need to put a change node in between so ‘on’ can be changed to ‘true’.

compares the topics and if there is a state for all 3 topics, gives the result. didn’t do extensive test but seems like a state change in any of the 3 inputs will yield a result.

image

Thanks for the reply. I implemented this as below, and it’s correctly outputting true/false depending on the 3 states.

My issue now, is that it’s not triggering sleep mode based on the true/false output from the switch. My flow export is below. Am I doing something wrong with the logic?

[{"id":"216685ea.a3fe1a","type":"tab","label":"Sleep Mode","disabled":false,"info":""},{"id":"f724c4c0.4872c","type":"api-call-service","z":"216685ea.a3fe1a","name":"Turn On Sleep Mode","server":"8ffc691d.25e1f8","version":1,"debugenabled":false,"service_domain":"input_boolean","service":"turn_on","entityId":"input_boolean.sleep_mode","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":840,"y":260,"wires":[[]]},{"id":"6d8898f5.672e5","type":"api-call-service","z":"216685ea.a3fe1a","name":"Turn Off Sleep Mode","server":"8ffc691d.25e1f8","version":1,"debugenabled":false,"service_domain":"input_boolean","service":"turn_off","entityId":"input_boolean.sleep_mode","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":840,"y":320,"wires":[[]]},{"id":"cc5656d0.ddb55","type":"server-state-changed","z":"216685ea.a3fe1a","name":"Occupied mode on","server":"8ffc691d.25e1f8","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.occupied_mode","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"x":130,"y":220,"wires":[["53d0b7f1.f97a5"],["557a8ab.a60adf4"]]},{"id":"955434f4.0d808","type":"server-state-changed","z":"216685ea.a3fe1a","name":"Night Mode on","server":"8ffc691d.25e1f8","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.night_mode","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"x":140,"y":300,"wires":[["85f83077.f58898"],["59ba28d7.16fb9"]]},{"id":"19175da4.bc7d0a","type":"server-state-changed","z":"216685ea.a3fe1a","name":"Armed Mode on","server":"8ffc691d.25e1f8","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.armed_mode","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"on","halt_if_type":"str","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"x":140,"y":380,"wires":[["5b889e01.ed3af8"],["15e8d6be.dd1629"]]},{"id":"85f83077.f58898","type":"change","z":"216685ea.a3fe1a","name":"True","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"},{"t":"set","p":"topic","pt":"msg","to":"night","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":280,"wires":[["808b4dfe.b02aa8"]]},{"id":"53d0b7f1.f97a5","type":"change","z":"216685ea.a3fe1a","name":"True","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"},{"t":"set","p":"topic","pt":"msg","to":"occupied","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":200,"wires":[["808b4dfe.b02aa8"]]},{"id":"5b889e01.ed3af8","type":"change","z":"216685ea.a3fe1a","name":"True","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"bool"},{"t":"set","p":"topic","pt":"msg","to":"armed","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":360,"wires":[["808b4dfe.b02aa8"]]},{"id":"557a8ab.a60adf4","type":"change","z":"216685ea.a3fe1a","name":"False","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"},{"t":"set","p":"topic","pt":"msg","to":"occupied","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":240,"wires":[["808b4dfe.b02aa8"]]},{"id":"59ba28d7.16fb9","type":"change","z":"216685ea.a3fe1a","name":"False","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"},{"t":"set","p":"topic","pt":"msg","to":"night","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":320,"wires":[["808b4dfe.b02aa8"]]},{"id":"15e8d6be.dd1629","type":"change","z":"216685ea.a3fe1a","name":"False","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"bool"},{"t":"set","p":"topic","pt":"msg","to":"armed","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":400,"wires":[["808b4dfe.b02aa8"]]},{"id":"960a7b7a.5dfb98","type":"debug","z":"216685ea.a3fe1a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":690,"y":200,"wires":[]},{"id":"808b4dfe.b02aa8","type":"BooleanLogicUltimate","z":"216685ea.a3fe1a","name":"","filtertrue":"both","persist":true,"sInitializeWith":"WaitForPayload","triggertopic":"trigger","outputtriggeredby":"all","inputCount":"3","topic":"result","x":480,"y":300,"wires":[["ee1dfa3e.e2ed58","960a7b7a.5dfb98"],[],[]]},{"id":"ee1dfa3e.e2ed58","type":"switch","z":"216685ea.a3fe1a","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"true","vt":"str"},{"t":"eq","v":"false","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":650,"y":300,"wires":[["f724c4c0.4872c"],["6d8898f5.672e5"]]},{"id":"8ffc691d.25e1f8","type":"server","z":"","name":"Home Assistant","addon":true}]

EDIT: Got it working! The switch needs to read direct True/False, not a string.

image

[{"id":"515882fb.03876c","type":"trigger-state","z":"ffbd7f06.4a014","name":"","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"input_boolean.occupied,input_boolean.night,input_boolean.armed","entityidfiltertype":"substring","debugenabled":false,"constraints":[{"id":"mmzcvxmcyx","targetType":"entity_id","targetValue":"input_boolean.occupied","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"},{"id":"7h6hm42ke1e","targetType":"entity_id","targetValue":"input_boolean.night","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"},{"id":"gcy625jytnh","targetType":"entity_id","targetValue":"input_boolean.armed","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"}],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":408,"y":912,"wires":[["233465af.2d65aa"],["d4a82b05.555188"]]},{"id":"233465af.2d65aa","type":"api-call-service","z":"ffbd7f06.4a014","name":"","version":1,"debugenabled":false,"service_domain":"input_boolean","service":"turn_on","entityId":"input_boolean.sleep_mode","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":840,"y":912,"wires":[[]]},{"id":"d4a82b05.555188","type":"api-call-service","z":"ffbd7f06.4a014","name":"","version":1,"debugenabled":false,"service_domain":"input_boolean","service":"turn_off","entityId":"input_boolean.sleep_mode","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":840,"y":960,"wires":[[]]}]
1 Like

The problem with the boolean logic node is that it must store the previous state of the topics it is looking for. This means that when deploying it will be incorrect until your input_booleans have switched and triggered a message. My personal preference is to keep all the state information in Home Assistant and use node red purely for the automations.

Node red is an event driven system. So when doing this logic I think about what are the events that would need to cause the change: the three input booleans changing state. And when one of those events fires what then needs to be checked: the other 2 input booleans.

image

[{"id":"3a7df645.1d2f7a","type":"trigger-state","z":"5f64b721.e82e98","name":"","server":"8ac442b3.df77c","entityid":"input_boolean.number1","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"id":"4s89bt4i0fu","targetType":"entity_id","targetValue":"input_boolean.number1","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"},{"id":"65kh788ro6b","targetType":"entity_id","targetValue":"input_boolean.number2","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"},{"id":"9vdl0xgvfab","targetType":"entity_id","targetValue":"input_boolean.number3","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"}],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":1250,"y":2280,"wires":[["3cc93480.c01e8c"],["d16a5e2.903e4a"]]},{"id":"15421c30.55d134","type":"trigger-state","z":"5f64b721.e82e98","name":"","server":"8ac442b3.df77c","entityid":"input_boolean.number1","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"id":"4s89bt4i0fu","targetType":"entity_id","targetValue":"input_boolean.number1","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"},{"id":"65kh788ro6b","targetType":"entity_id","targetValue":"input_boolean.number2","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"},{"id":"9vdl0xgvfab","targetType":"entity_id","targetValue":"input_boolean.number3","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"}],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":1250,"y":2340,"wires":[["3cc93480.c01e8c"],["d16a5e2.903e4a"]]},{"id":"18001c5c.499314","type":"trigger-state","z":"5f64b721.e82e98","name":"","server":"8ac442b3.df77c","entityid":"input_boolean.number1","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"id":"4s89bt4i0fu","targetType":"entity_id","targetValue":"input_boolean.number1","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"},{"id":"65kh788ro6b","targetType":"entity_id","targetValue":"input_boolean.number2","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"},{"id":"9vdl0xgvfab","targetType":"entity_id","targetValue":"input_boolean.number3","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"}],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":1250,"y":2400,"wires":[["3cc93480.c01e8c"],["d16a5e2.903e4a"]]},{"id":"3cc93480.c01e8c","type":"api-call-service","z":"5f64b721.e82e98","name":"","server":"8ac442b3.df77c","version":1,"debugenabled":false,"service_domain":"input_boolean","service":"turn_on","entityId":"input_boolean.output","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1680,"y":2320,"wires":[[]]},{"id":"d16a5e2.903e4a","type":"api-call-service","z":"5f64b721.e82e98","name":"","server":"8ac442b3.df77c","version":1,"debugenabled":false,"service_domain":"input_boolean","service":"turn_off","entityId":"input_boolean.output","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1680,"y":2380,"wires":[[]]},{"id":"8ac442b3.df77c","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

I think Kermits response is spot on , which makes sense :smile:, I didn’t realise you could have multiple entities in one trigger:state node, so thanks for that!