Wasp In A Box Automation
*DISCLAIMER:* Links included in this description might be affiliate links. If you purchase a product or service with the links that I provide I may receive a small commission. There is no additional charge to you! Thank you for supporting me so I can continue to provide you with dope free content!
Technithusiast Wasp Subflow
I recently discussed the Wasp in a box technique on my channel (see video). I tried using the new Aqara P2 Motion and Light sensor, but Apple Homekit left much to be desired. At the time of writing this, the Aqara’s Matter enabled hub is available so I have no way of loading the motion sensor into the Aqara App where I’ve been able to successfully pull off the Wasp in a box automation. But even if i could it still wouldn’t work because Aqara’s app only lets you control Aqara devices and my guest bathroom has uses a Kasa smart light. My only option is to use HomeKit or Home Assistant.
You already know what i’m picking. (Home assistant)
[{"id":"bcf3860d6332a5e4","type":"subflow","name":"Wasp in a box","info":"","category":"","in":[],"out":[{"x":1240,"y":180,"wires":[{"id":"1cc00fbe0b0fb5ba","port":0}]},{"x":1300,"y":580,"wires":[{"id":"0b350fbf1a169ff7","port":0}]}],"env":[{"name":"motion_entity_id","type":"str","value":""},{"name":"light_entity_id","type":"str","value":""},{"name":"door_entity_id","type":"str","value":""},{"name":"motion_delay","type":"num","value":"30"},{"name":"light_poll_seconds","type":"num","value":"120"}],"meta":{},"color":"#686de0","outputLabels":["Light on","Light off"]},{"id":"b27f6c7ea3a955bb","type":"comment","z":"bcf3860d6332a5e4","name":"Turn on light on motion","info":"","x":220,"y":120,"wires":[]},{"id":"09bb0f7abffa2560","type":"server-state-changed","z":"bcf3860d6332a5e4","name":"When door closed for X time","server":"228d3d53.df8e02","version":5,"outputs":2,"exposeAsEntityConfig":"","entityId":"${door_entity_id}","entityIdType":"exact","outputInitially":false,"stateType":"str","ifState":"off","ifStateType":"str","ifStateOperator":"is","outputOnlyOnStateChange":true,"for":"$env('motion_delay') + 2","forType":"jsonata","forUnits":"seconds","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"eventData"}],"x":240,"y":380,"wires":[["ddb42e7a0c26856d"],["e134943e923f34ba"]]},{"id":"ddb42e7a0c26856d","type":"ha-wait-until","z":"bcf3860d6332a5e4","name":"Wait X seconds for motion","server":"228d3d53.df8e02","version":2,"outputs":2,"entityId":"${motion_entity_id}","entityIdFilterType":"exact","property":"state","comparator":"is","value":"on","valueType":"str","timeout":"$env('motion_delay')","timeoutType":"jsonata","timeoutUnits":"seconds","checkCurrentState":true,"blockInputOverrides":true,"outputProperties":[{"property":"entity","propertyType":"msg","value":"","valueType":"entity"}],"entityLocation":"data","entityLocationType":"none","x":580,"y":360,"wires":[["9744b540e787f70e"],[]]},{"id":"d466089281ec74a7","type":"server-state-changed","z":"bcf3860d6332a5e4","name":"When motion detected","server":"228d3d53.df8e02","version":5,"outputs":2,"exposeAsEntityConfig":"","entityId":"${motion_entity_id}","entityIdType":"exact","outputInitially":false,"stateType":"str","ifState":"on","ifStateType":"str","ifStateOperator":"is","outputOnlyOnStateChange":true,"for":"","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":220,"y":180,"wires":[["b2f7cafda9c4cd3a","5336713954198f1f"],[]]},{"id":"7461cb4e2c86856a","type":"function","z":"bcf3860d6332a5e4","name":"Set occupancy","func":"const key = env.get(\"light_entity_id\")\n\nglobal.set(key, true)\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1100,"y":360,"wires":[[]]},{"id":"e134943e923f34ba","type":"function","z":"bcf3860d6332a5e4","name":"Clear occupancy","func":"const key = env.get(\"light_entity_id\")\n\nglobal.set(key, undefined)\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":420,"wires":[[]]},{"id":"1cc00fbe0b0fb5ba","type":"api-call-service","z":"bcf3860d6332a5e4","name":"turn on light","server":"228d3d53.df8e02","version":5,"debugenabled":false,"domain":"switch","service":"turn_on","areaId":[],"deviceId":[],"entityId":["${light_entity_id}"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1050,"y":180,"wires":[[]]},{"id":"af3909aa93527980","type":"delay","z":"bcf3860d6332a5e4","name":"allow 1 trigger every 30 secs","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"30","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"allowrate":false,"outputs":1,"x":800,"y":180,"wires":[["1cc00fbe0b0fb5ba"]]},{"id":"8b14ec76290475c4","type":"poll-state","z":"bcf3860d6332a5e4","name":"Poll light","server":"228d3d53.df8e02","version":3,"exposeAsEntityConfig":"","updateInterval":"${light_poll_seconds}","updateIntervalType":"num","updateIntervalUnits":"seconds","outputInitially":false,"outputOnChanged":false,"entityId":"${light_entity_id}","stateType":"str","ifState":"on","ifStateType":"str","ifStateOperator":"is","outputs":2,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":180,"y":580,"wires":[["edbb93f025a4c3d0"],[]]},{"id":"edbb93f025a4c3d0","type":"function","z":"bcf3860d6332a5e4","name":"is possibly empty","func":"const key = env.get(\"light_entity_id\")\n\nif (!global.get(key)){\n return [msg, null]\n}\n\nreturn [null, msg];","outputs":2,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":390,"y":580,"wires":[["c67bb9df878e4e4b"],[]]},{"id":"0b350fbf1a169ff7","type":"api-call-service","z":"bcf3860d6332a5e4","name":"light off","server":"228d3d53.df8e02","version":5,"debugenabled":false,"domain":"switch","service":"turn_off","areaId":[],"deviceId":[],"entityId":["${light_entity_id}"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1120,"y":580,"wires":[[]]},{"id":"0bb514ca0f2c5a0e","type":"comment","z":"bcf3860d6332a5e4","name":"Set global variable toggling occupancy","info":"","x":270,"y":280,"wires":[]},{"id":"8a1dc4d6c4c67aaf","type":"comment","z":"bcf3860d6332a5e4","name":"Poll every two mins to turn off light","info":"","x":260,"y":520,"wires":[]},{"id":"b2f7cafda9c4cd3a","type":"api-current-state","z":"bcf3860d6332a5e4","name":"And not in sleep mode","server":"228d3d53.df8e02","version":3,"outputs":2,"halt_if":"Sleep","halt_if_type":"str","halt_if_compare":"is_not","entity_id":"input_select.house_mode","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":540,"y":180,"wires":[["af3909aa93527980"],[]]},{"id":"9744b540e787f70e","type":"api-current-state","z":"bcf3860d6332a5e4","name":"If door still closed","server":"228d3d53.df8e02","version":3,"outputs":2,"halt_if":"off","halt_if_type":"str","halt_if_compare":"is","entity_id":"${door_entity_id}","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":850,"y":360,"wires":[["7461cb4e2c86856a"],[]]},{"id":"4afd705badedc121","type":"api-current-state","z":"bcf3860d6332a5e4","name":"Not sleep mode","server":"228d3d53.df8e02","version":3,"outputs":2,"halt_if":"Sleep","halt_if_type":"str","halt_if_compare":"is_not","entity_id":"input_select.house_mode","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":920,"y":580,"wires":[["0b350fbf1a169ff7"],[]]},{"id":"5336713954198f1f","type":"function","z":"bcf3860d6332a5e4","name":"save last motion event","func":"const {state} = msg.data.new_state\nconst key = env.get(\"light_entity_id\")\n\nglobal.set(`${key}-history`, Date.now())\n\n\n\nreturn msg;\n\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":540,"y":120,"wires":[[]]},{"id":"c67bb9df878e4e4b","type":"function","z":"bcf3860d6332a5e4","name":"no motion in the last X seconds","func":"const key = env.get(\"light_entity_id\")\nconst timestamp = global.get(`${key}-history`)\nconst pollIntervalInSeconds = env.get(\"light_poll_seconds\")\nconst now = Date.now()\nconst timeElapsed = (now - timestamp) / 1000\n\nmsg.timeElapsed = timeElapsed\nmsg.lastTriggered = timestamp\nmsg.currentTime = now\n\nif (timeElapsed >= pollIntervalInSeconds){\n \n return [msg, null]\n}\n\n\nreturn [null, msg]\n\n","outputs":2,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":650,"y":580,"wires":[["4afd705badedc121","0b350fbf1a169ff7"],[]]},{"id":"228d3d53.df8e02","type":"server","name":"Home Assistant","addon":false,"rejectUnauthorizedCerts":true,"ha_boolean":"","connectionDelay":false,"cacheJson":false,"heartbeat":false,"heartbeatInterval":"","statusSeparator":"","enableGlobalContextStore":false},{"id":"f8ceac8ac3618ceb","type":"subflow:bcf3860d6332a5e4","z":"859941a3d2dfbbbf","g":"253ff08fd7c92f6f","name":"Wasp in a box (Guest Bathroom)","env":[{"name":"motion_entity_id","value":"binary_sensor.guest_bathroom_motion","type":"str"},{"name":"light_entity_id","value":"switch.guest_bathroom_light","type":"str"},{"name":"door_entity_id","value":"binary_sensor.guest_bathroom_door","type":"str"},{"name":"motion_delay","value":"3","type":"num"},{"name":"helper_entity_id","value":"input_boolean.guest_bathroom_occupancy","type":"str"}],"x":250,"y":220,"wires":[[],[]]}]
AlexanderBabel’s Occupancy Blueprint (Wasp in a box inspired)
There is a blueprint created by AlexanderBabel. This version is different in implementation than my node red version. I admit his version may be more battle-hardened than my own version,
Ben Jones Wasp Subflow
Ben created this subflow to work seamlessly with OXRS System. This subflow will output events that you can use to turn off or on lights based on inputs.
[{"id":"8d8250e01c8c3f77","type":"subflow","name":"wasp","info":"","category":"","in":[{"x":80,"y":160,"wires":[{"id":"c03f39867984fded"}]}],"out":[{"x":1100,"y":140,"wires":[{"id":"a9ed53363ffc3c12","port":0}]}],"env":[{"name":"TIMEOUT_SECS","type":"num","value":"300","ui":{"icon":"font-awesome/fa-hourglass-start","label":{"en-US":"Timeout (s)"},"type":"input","opts":{"types":["num","env"]}}}],"meta":{},"color":"#C0DEED","icon":"font-awesome/fa-sign-in","status":{"x":1100,"y":640,"wires":[{"id":"3c8aa10cb29b8648","port":0}]}},{"id":"e7ebc8bb091611aa","type":"function","z":"8d8250e01c8c3f77","name":"store context","func":"var box_sensors = flow.get(\"box_sensors\") || [];\nvar box_closed = true;\n\n// check if this sensor exists in our flow context \n// and add if not already there, or update if found\nvar sensor_exists = false;\n\nbox_sensors.forEach(function(box_sensor) {\n if (box_sensor.sensor === msg.payload.sensor) {\n box_sensor.event = msg.payload.event;\n sensor_exists = true;\n }\n});\n\nif (!sensor_exists) {\n box_sensors.push(msg.payload);\n}\n\n// check all sensors to determine if the box is closed\nbox_sensors.forEach(function(box_sensor) {\n if (box_sensor.event === \"alarm\") {\n box_closed = false;\n }\n});\n\nmsg.box_sensors= box_sensors\nmsg.box_closed= box_closed\n\nflow.set(\"box_sensors\", box_sensors);\nflow.set(\"box_closed\", box_closed);\n\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":510,"y":80,"wires":[["52998c5977170a4f","7897fa13e4e94370"]]},{"id":"c03f39867984fded","type":"switch","z":"8d8250e01c8c3f77","name":"type","property":"payload.type","propertyType":"msg","rules":[{"t":"eq","v":"security","vt":"str"},{"t":"eq","v":"contact","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":190,"y":160,"wires":[["93be1913aadb78b6"],["30002cd57399831d"]]},{"id":"30002cd57399831d","type":"change","z":"8d8250e01c8c3f77","name":"convert","rules":[{"t":"set","p":"payload.type","pt":"msg","to":"security","tot":"str"},{"t":"set","p":"payload.event","pt":"msg","to":"$.payload.event = \"open\" ? \"alarm\" : \"normal\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":180,"wires":[["93be1913aadb78b6"]]},{"id":"52998c5977170a4f","type":"switch","z":"8d8250e01c8c3f77","name":"event","property":"payload.event","propertyType":"msg","rules":[{"t":"eq","v":"alarm","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":490,"y":140,"wires":[["df63c4872c9285f8","ec9cc39cacbfb25f"]]},{"id":"df63c4872c9285f8","type":"switch","z":"8d8250e01c8c3f77","name":"box closed","property":"box_closed","propertyType":"flow","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":650,"y":280,"wires":[["af6bc361d9287757"],["2274db2b13278b85"]]},{"id":"93be1913aadb78b6","type":"switch","z":"8d8250e01c8c3f77","name":"topic","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"box","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":330,"y":120,"wires":[["e7ebc8bb091611aa"],["52998c5977170a4f"]]},{"id":"551edbbb043f6925","type":"trigger","z":"8d8250e01c8c3f77","name":"switch off","op1":"","op2":"{\"type\":\"switch\",\"event\":\"off\"}","op1type":"nul","op2type":"json","duration":"250","extend":true,"overrideDelay":true,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":1000,"y":260,"wires":[["a9ed53363ffc3c12"]]},{"id":"2274db2b13278b85","type":"change","z":"8d8250e01c8c3f77","name":"delay","rules":[{"t":"set","p":"delay","pt":"msg","to":"$env(\"TIMEOUT_SECS\") * 1000","tot":"jsonata"},{"t":"set","p":"node_timer","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":810,"y":300,"wires":[["551edbbb043f6925","69261272f785c8e8"]]},{"id":"af6bc361d9287757","type":"change","z":"8d8250e01c8c3f77","name":"reset","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"},{"t":"set","p":"node_timer","pt":"flow","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":810,"y":260,"wires":[["551edbbb043f6925","69261272f785c8e8"]]},{"id":"84e9226c1acfe74b","type":"comment","z":"8d8250e01c8c3f77","name":"normalise to security event","info":"","x":390,"y":220,"wires":[]},{"id":"ec9cc39cacbfb25f","type":"change","z":"8d8250e01c8c3f77","name":"switch on","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"type\":\"switch\",\"event\":\"on\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":820,"y":140,"wires":[["a9ed53363ffc3c12"]]},{"id":"a9ed53363ffc3c12","type":"rbe","z":"8d8250e01c8c3f77","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":false,"property":"payload.event","topi":"topic","x":990,"y":140,"wires":[["cba43ddda1c570c8"]]},{"id":"29167ee9f8e27c47","type":"comment","z":"8d8250e01c8c3f77","name":"if box is closed, reset turn-off timer","info":"","x":900,"y":220,"wires":[]},{"id":"48478370d2a317eb","type":"comment","z":"8d8250e01c8c3f77","name":"if box is open, start turn-off timer","info":"","x":890,"y":340,"wires":[]},{"id":"c48719832f0a4321","type":"comment","z":"8d8250e01c8c3f77","name":"turn on with any alarm event","info":"","x":880,"y":100,"wires":[]},{"id":"ddf2877fbed4c28c","type":"inject","z":"8d8250e01c8c3f77","name":"","props":[{"p":"colour","v":"grey","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","x":110,"y":540,"wires":[["ee287d0f0e5df965"]]},{"id":"cba43ddda1c570c8","type":"link out","z":"8d8250e01c8c3f77","name":"link out 138","mode":"link","links":["e19996fc208f8b6b","9cf1eda1a3ea286d"],"x":1095,"y":80,"wires":[]},{"id":"e19996fc208f8b6b","type":"link in","z":"8d8250e01c8c3f77","name":"link in 135","links":["cba43ddda1c570c8"],"x":75,"y":480,"wires":[["040f7acd35d0edc9"]]},{"id":"040f7acd35d0edc9","type":"switch","z":"8d8250e01c8c3f77","name":"event","property":"payload.event","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":190,"y":480,"wires":[["e65497e3c7075eb4"],["ee287d0f0e5df965"]]},{"id":"e65497e3c7075eb4","type":"change","z":"8d8250e01c8c3f77","name":"blue","rules":[{"t":"set","p":"colour","pt":"msg","to":"blue","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":460,"wires":[["d81d9eb9408c614c"]]},{"id":"ee287d0f0e5df965","type":"change","z":"8d8250e01c8c3f77","name":"grey","rules":[{"t":"set","p":"colour","pt":"msg","to":"grey","tot":"str"},{"t":"set","p":"node_timer","pt":"flow","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":500,"wires":[["d81d9eb9408c614c"]]},{"id":"d8573c4a15719388","type":"comment","z":"8d8250e01c8c3f77","name":"node status","info":"","x":650,"y":440,"wires":[]},{"id":"82ed45890ff83203","type":"change","z":"8d8250e01c8c3f77","name":"box open","rules":[{"t":"set","p":"text","pt":"msg","to":"box open","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":820,"y":500,"wires":[["da37c3ba5f6b1033"]]},{"id":"25c170135406ae8b","type":"change","z":"8d8250e01c8c3f77","name":"box closed","rules":[{"t":"set","p":"text","pt":"msg","to":"box closed","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":830,"y":460,"wires":[["da37c3ba5f6b1033"]]},{"id":"94abf623f0f5609a","type":"switch","z":"8d8250e01c8c3f77","name":"box closed","property":"box_closed","propertyType":"flow","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":650,"y":480,"wires":[["25c170135406ae8b"],["82ed45890ff83203"]]},{"id":"3c8aa10cb29b8648","type":"template","z":"8d8250e01c8c3f77","name":"status","field":"payload","fieldType":"msg","format":"json","syntax":"mustache","template":"{\"fill\":\"{{colour}}\",\"shape\":\"dot\",\"text\":\"{{text}}\"}","output":"json","x":990,"y":640,"wires":[[]]},{"id":"d81d9eb9408c614c","type":"change","z":"8d8250e01c8c3f77","name":"persist","rules":[{"t":"set","p":"node_colour","pt":"flow","to":"colour","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":480,"wires":[["94abf623f0f5609a"]]},{"id":"da37c3ba5f6b1033","type":"change","z":"8d8250e01c8c3f77","name":"colour","rules":[{"t":"set","p":"colour","pt":"msg","to":"node_colour","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":630,"y":560,"wires":[["ab3063f8a112beae"]]},{"id":"0926995981afc291","type":"link in","z":"8d8250e01c8c3f77","name":"link in 138","links":["7897fa13e4e94370","69261272f785c8e8","965576bfe0b463fe"],"x":525,"y":420,"wires":[["94abf623f0f5609a"]]},{"id":"7897fa13e4e94370","type":"link out","z":"8d8250e01c8c3f77","name":"link out 139","mode":"link","links":["0926995981afc291"],"x":655,"y":80,"wires":[]},{"id":"97e6791fb703f6f9","type":"comment","z":"8d8250e01c8c3f77","name":"timer state","info":"","x":120,"y":440,"wires":[]},{"id":"ab3063f8a112beae","type":"switch","z":"8d8250e01c8c3f77","name":"timer","property":"node_timer","propertyType":"flow","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":630,"y":640,"wires":[["1967735758a72f15"],["3c8aa10cb29b8648"]]},{"id":"1967735758a72f15","type":"change","z":"8d8250e01c8c3f77","name":"timer running","rules":[{"t":"set","p":"text","pt":"msg","to":"$.text & \" (timer)\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":830,"y":620,"wires":[["3c8aa10cb29b8648"]]},{"id":"69261272f785c8e8","type":"link out","z":"8d8250e01c8c3f77","name":"link out 140","mode":"link","links":["0926995981afc291"],"x":955,"y":300,"wires":[]}]
I had some issues figuring out this flow on my own, so below is an example flow that uses the node.
Doors and windows need to send the property of “topic” with the value of “box” in the message.
Presence sensors doesn’t need to send any topics
The message object needs to have the following properties:
{ type: “security”, event: “alarm” | “normal”, sensor: string}
Use the example flow below for more context
[{"id":"8d8250e01c8c3f77","type":"subflow","name":"wasp","info":"","category":"","in":[{"x":80,"y":160,"wires":[{"id":"c03f39867984fded"}]}],"out":[{"x":1100,"y":140,"wires":[{"id":"a9ed53363ffc3c12","port":0}]}],"env":[{"name":"TIMEOUT_SECS","type":"num","value":"300","ui":{"icon":"font-awesome/fa-hourglass-start","label":{"en-US":"Timeout (s)"},"type":"input","opts":{"types":["num","env"]}}}],"meta":{},"color":"#C0DEED","icon":"font-awesome/fa-sign-in","status":{"x":1100,"y":640,"wires":[{"id":"3c8aa10cb29b8648","port":0}]}},{"id":"e7ebc8bb091611aa","type":"function","z":"8d8250e01c8c3f77","name":"store context","func":"var box_sensors = flow.get(\"box_sensors\") || [];\nvar box_closed = true;\n\n// check if this sensor exists in our flow context \n// and add if not already there, or update if found\nvar sensor_exists = false;\n\nbox_sensors.forEach(function(box_sensor) {\n if (box_sensor.sensor === msg.payload.sensor) {\n box_sensor.event = msg.payload.event;\n sensor_exists = true;\n }\n});\n\nif (!sensor_exists) {\n box_sensors.push(msg.payload);\n}\n\n// check all sensors to determine if the box is closed\nbox_sensors.forEach(function(box_sensor) {\n if (box_sensor.event === \"alarm\") {\n box_closed = false;\n }\n});\n\nmsg.box_sensors= box_sensors\nmsg.box_closed= box_closed\n\nflow.set(\"box_sensors\", box_sensors);\nflow.set(\"box_closed\", box_closed);\n\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":510,"y":80,"wires":[["52998c5977170a4f","7897fa13e4e94370"]]},{"id":"c03f39867984fded","type":"switch","z":"8d8250e01c8c3f77","name":"type","property":"payload.type","propertyType":"msg","rules":[{"t":"eq","v":"security","vt":"str"},{"t":"eq","v":"contact","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":190,"y":160,"wires":[["93be1913aadb78b6"],["30002cd57399831d"]]},{"id":"30002cd57399831d","type":"change","z":"8d8250e01c8c3f77","name":"convert","rules":[{"t":"set","p":"payload.type","pt":"msg","to":"security","tot":"str"},{"t":"set","p":"payload.event","pt":"msg","to":"$.payload.event = \"open\" ? \"alarm\" : \"normal\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":180,"wires":[["93be1913aadb78b6"]]},{"id":"52998c5977170a4f","type":"switch","z":"8d8250e01c8c3f77","name":"event","property":"payload.event","propertyType":"msg","rules":[{"t":"eq","v":"alarm","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":490,"y":140,"wires":[["df63c4872c9285f8","ec9cc39cacbfb25f"]]},{"id":"df63c4872c9285f8","type":"switch","z":"8d8250e01c8c3f77","name":"box closed","property":"box_closed","propertyType":"flow","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":650,"y":280,"wires":[["af6bc361d9287757"],["2274db2b13278b85"]]},{"id":"93be1913aadb78b6","type":"switch","z":"8d8250e01c8c3f77","name":"topic","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"box","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":330,"y":120,"wires":[["e7ebc8bb091611aa"],["52998c5977170a4f"]]},{"id":"551edbbb043f6925","type":"trigger","z":"8d8250e01c8c3f77","name":"switch off","op1":"","op2":"{\"type\":\"switch\",\"event\":\"off\"}","op1type":"nul","op2type":"json","duration":"250","extend":true,"overrideDelay":true,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":1000,"y":260,"wires":[["a9ed53363ffc3c12"]]},{"id":"2274db2b13278b85","type":"change","z":"8d8250e01c8c3f77","name":"delay","rules":[{"t":"set","p":"delay","pt":"msg","to":"$env(\"TIMEOUT_SECS\") * 1000","tot":"jsonata"},{"t":"set","p":"node_timer","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":810,"y":300,"wires":[["551edbbb043f6925","69261272f785c8e8"]]},{"id":"af6bc361d9287757","type":"change","z":"8d8250e01c8c3f77","name":"reset","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"},{"t":"set","p":"node_timer","pt":"flow","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":810,"y":260,"wires":[["551edbbb043f6925","69261272f785c8e8"]]},{"id":"84e9226c1acfe74b","type":"comment","z":"8d8250e01c8c3f77","name":"normalise to security event","info":"","x":390,"y":220,"wires":[]},{"id":"ec9cc39cacbfb25f","type":"change","z":"8d8250e01c8c3f77","name":"switch on","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"type\":\"switch\",\"event\":\"on\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":820,"y":140,"wires":[["a9ed53363ffc3c12"]]},{"id":"a9ed53363ffc3c12","type":"rbe","z":"8d8250e01c8c3f77","name":"","func":"rbe","gap":"","start":"","inout":"out","septopics":false,"property":"payload.event","topi":"topic","x":990,"y":140,"wires":[["cba43ddda1c570c8"]]},{"id":"29167ee9f8e27c47","type":"comment","z":"8d8250e01c8c3f77","name":"if box is closed, reset turn-off timer","info":"","x":900,"y":220,"wires":[]},{"id":"48478370d2a317eb","type":"comment","z":"8d8250e01c8c3f77","name":"if box is open, start turn-off timer","info":"","x":890,"y":340,"wires":[]},{"id":"c48719832f0a4321","type":"comment","z":"8d8250e01c8c3f77","name":"turn on with any alarm event","info":"","x":880,"y":100,"wires":[]},{"id":"ddf2877fbed4c28c","type":"inject","z":"8d8250e01c8c3f77","name":"","props":[{"p":"colour","v":"grey","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","x":110,"y":540,"wires":[["ee287d0f0e5df965"]]},{"id":"cba43ddda1c570c8","type":"link out","z":"8d8250e01c8c3f77","name":"link out 138","mode":"link","links":["e19996fc208f8b6b","9cf1eda1a3ea286d"],"x":1095,"y":80,"wires":[]},{"id":"e19996fc208f8b6b","type":"link in","z":"8d8250e01c8c3f77","name":"link in 135","links":["cba43ddda1c570c8"],"x":75,"y":480,"wires":[["040f7acd35d0edc9"]]},{"id":"040f7acd35d0edc9","type":"switch","z":"8d8250e01c8c3f77","name":"event","property":"payload.event","propertyType":"msg","rules":[{"t":"eq","v":"on","vt":"str"},{"t":"eq","v":"off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":190,"y":480,"wires":[["e65497e3c7075eb4"],["ee287d0f0e5df965"]]},{"id":"e65497e3c7075eb4","type":"change","z":"8d8250e01c8c3f77","name":"blue","rules":[{"t":"set","p":"colour","pt":"msg","to":"blue","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":460,"wires":[["d81d9eb9408c614c"]]},{"id":"ee287d0f0e5df965","type":"change","z":"8d8250e01c8c3f77","name":"grey","rules":[{"t":"set","p":"colour","pt":"msg","to":"grey","tot":"str"},{"t":"set","p":"node_timer","pt":"flow","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":500,"wires":[["d81d9eb9408c614c"]]},{"id":"d8573c4a15719388","type":"comment","z":"8d8250e01c8c3f77","name":"node status","info":"","x":650,"y":440,"wires":[]},{"id":"82ed45890ff83203","type":"change","z":"8d8250e01c8c3f77","name":"box open","rules":[{"t":"set","p":"text","pt":"msg","to":"box open","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":820,"y":500,"wires":[["da37c3ba5f6b1033"]]},{"id":"25c170135406ae8b","type":"change","z":"8d8250e01c8c3f77","name":"box closed","rules":[{"t":"set","p":"text","pt":"msg","to":"box closed","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":830,"y":460,"wires":[["da37c3ba5f6b1033"]]},{"id":"94abf623f0f5609a","type":"switch","z":"8d8250e01c8c3f77","name":"box closed","property":"box_closed","propertyType":"flow","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":650,"y":480,"wires":[["25c170135406ae8b"],["82ed45890ff83203"]]},{"id":"3c8aa10cb29b8648","type":"template","z":"8d8250e01c8c3f77","name":"status","field":"payload","fieldType":"msg","format":"json","syntax":"mustache","template":"{\"fill\":\"{{colour}}\",\"shape\":\"dot\",\"text\":\"{{text}}\"}","output":"json","x":990,"y":640,"wires":[[]]},{"id":"d81d9eb9408c614c","type":"change","z":"8d8250e01c8c3f77","name":"persist","rules":[{"t":"set","p":"node_colour","pt":"flow","to":"colour","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":480,"wires":[["94abf623f0f5609a"]]},{"id":"da37c3ba5f6b1033","type":"change","z":"8d8250e01c8c3f77","name":"colour","rules":[{"t":"set","p":"colour","pt":"msg","to":"node_colour","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":630,"y":560,"wires":[["ab3063f8a112beae"]]},{"id":"0926995981afc291","type":"link in","z":"8d8250e01c8c3f77","name":"link in 138","links":["7897fa13e4e94370","69261272f785c8e8","965576bfe0b463fe"],"x":525,"y":420,"wires":[["94abf623f0f5609a"]]},{"id":"7897fa13e4e94370","type":"link out","z":"8d8250e01c8c3f77","name":"link out 139","mode":"link","links":["0926995981afc291"],"x":655,"y":80,"wires":[]},{"id":"97e6791fb703f6f9","type":"comment","z":"8d8250e01c8c3f77","name":"timer state","info":"","x":120,"y":440,"wires":[]},{"id":"ab3063f8a112beae","type":"switch","z":"8d8250e01c8c3f77","name":"timer","property":"node_timer","propertyType":"flow","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":630,"y":640,"wires":[["1967735758a72f15"],["3c8aa10cb29b8648"]]},{"id":"1967735758a72f15","type":"change","z":"8d8250e01c8c3f77","name":"timer running","rules":[{"t":"set","p":"text","pt":"msg","to":"$.text & \" (timer)\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":830,"y":620,"wires":[["3c8aa10cb29b8648"]]},{"id":"69261272f785c8e8","type":"link out","z":"8d8250e01c8c3f77","name":"link out 140","mode":"link","links":["0926995981afc291"],"x":955,"y":300,"wires":[]},{"id":"e16cc66caf3192a3","type":"server-state-changed","z":"859941a3d2dfbbbf","name":"Guest Bathroom Door","server":"228d3d53.df8e02","version":5,"outputs":1,"exposeAsEntityConfig":"","entityId":"binary_sensor.guest_bathroom_door","entityIdType":"exact","outputInitially":false,"stateType":"str","ifState":"","ifStateType":"str","ifStateOperator":"is","outputOnlyOnStateChange":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":"box","valueType":"str"}],"x":240,"y":560,"wires":[["e97ffe52f8d29686"]]},{"id":"4395e75cae763206","type":"server-state-changed","z":"859941a3d2dfbbbf","name":"Guest Bathroom Motion","server":"228d3d53.df8e02","version":5,"outputs":1,"exposeAsEntityConfig":"","entityId":"binary_sensor.guest_bathroom_motion","entityIdType":"exact","outputInitially":false,"stateType":"str","ifState":"","ifStateType":"str","ifStateOperator":"is","outputOnlyOnStateChange":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":"presence","valueType":"str"}],"x":240,"y":640,"wires":[["e97ffe52f8d29686"]]},{"id":"e97ffe52f8d29686","type":"function","z":"859941a3d2dfbbbf","name":"setup payload","func":"\nconst payload = {\n \"type\" : \"security\",\n \"event\" : msg.payload === \"on\" ? \"alarm\" : \"normal\",\n \"sensor\": msg.data.entity_id \n}\n\nmsg.payload = payload\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":600,"wires":[["72000c89a30347a1"]]},{"id":"72000c89a30347a1","type":"subflow:8d8250e01c8c3f77","z":"859941a3d2dfbbbf","name":"","env":[{"name":"TIMEOUT_SECS","value":"120","type":"num"}],"x":630,"y":600,"wires":[["c80cb19261622823"]]},{"id":"c80cb19261622823","type":"api-call-service","z":"859941a3d2dfbbbf","name":"","server":"228d3d53.df8e02","version":5,"debugenabled":false,"domain":"switch","service":"turn_{{payload.event}}","areaId":[],"deviceId":[],"entityId":["switch.guest_bathroom_light"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":860,"y":600,"wires":[[]]},{"id":"228d3d53.df8e02","type":"server","name":"Home Assistant","addon":false,"rejectUnauthorizedCerts":true,"ha_boolean":"","connectionDelay":false,"cacheJson":false,"heartbeat":false,"heartbeatInterval":"","statusSeparator":"","enableGlobalContextStore":false}]