Node-RED How does my unlock flow look?

Tags: #<Tag:0x00007f7397a9e568> #<Tag:0x00007f7397a9e338>

I got tired of dealing with August’s Auto-Unlock that doesn’t work half the time, so I figured I’d write my own unlock automation in Node-RED. This is my first attempt at using Node-RED, so I’d like some feedback on if this looks like the most efficient way that I could have scripted this. Are there any things I could do to simplify things, particularly the change node where I check the topic (“person.xxxxx”) and replace it with either mine or my wife’s name? I think it’d be better if it was generic to any person being sent to the node, but can’t figure out a way to extract the name and capitalize whatever the first letter is.

The input nodes on the left are strictly for testing purposes.
 
 

[{"id":"b630031d.75b78","type":"tab","label":"Unlock Front Door","disabled":false,"info":""},{"id":"e2cca79d.d6ebc8","type":"debug","z":"b630031d.75b78","name":"Is not Home","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","x":910,"y":540,"wires":[]},{"id":"256c801c.f4b64","type":"debug","z":"b630031d.75b78","name":"Is Home","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":900,"y":280,"wires":[]},{"id":"5cc40f37.6bd2c","type":"trigger-state","z":"b630031d.75b78","name":"Is Hannah Home?","server":"b2b4933d.45d52","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"person.hannah","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"id":"w22ikbt8a7g","targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"home"}],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":610,"y":360,"wires":[["256c801c.f4b64","72b38f66.42c5d"],["e2cca79d.d6ebc8"]],"outputLabels":["Home","Not Home"]},{"id":"4ccad26b.77f82c","type":"inject","z":"b630031d.75b78","name":"Arrive Home","topic":"","payload":"{\"entity_id\":\"person.hannah\",\"old_state\":{\"state\":\"not_home\"},\"new_state\":{\"state\":\"home\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":330,"y":280,"wires":[["5cc40f37.6bd2c"]]},{"id":"eeb677fd.828ee8","type":"inject","z":"b630031d.75b78","name":"Leave Home","topic":"","payload":"{\"entity_id\":\"person.hannah\",\"old_state\":{\"state\":\"home\"},\"new_state\":{\"state\":\"not_home\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":330,"y":360,"wires":[["5cc40f37.6bd2c"]]},{"id":"43fd5dc3.bf5544","type":"inject","z":"b630031d.75b78","name":"Stayed Home","topic":"","payload":"{\"entity_id\":\"person.hannah\",\"old_state\":{\"state\":\"home\"},\"new_state\":{\"state\":\"home\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":330,"y":320,"wires":[["5cc40f37.6bd2c"]]},{"id":"a0a07e1c.3564a","type":"api-current-state","z":"b630031d.75b78","name":"Is the door locked?","server":"b2b4933d.45d52","version":1,"outputs":2,"halt_if":"locked","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"lock.front_door","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":1150,"y":400,"wires":[["14a05b39.187355","784e1ca.abe23e4"],[]],"outputLabels":["locked","unlocked"]},{"id":"784e1ca.abe23e4","type":"api-call-service","z":"b630031d.75b78","name":"Unlock Door","server":"b2b4933d.45d52","version":1,"debugenabled":false,"service_domain":"lock","service":"unlock","entityId":"lock.front_door","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1150,"y":480,"wires":[["2480b108.2479ce","6846da8f.cd1a94"]]},{"id":"2480b108.2479ce","type":"debug","z":"b630031d.75b78","name":"Unlocking Door","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1440,"y":320,"wires":[]},{"id":"4649c8cb.54c948","type":"template","z":"b630031d.75b78","name":"format Notify","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{\n  \"data\":{\n    \"title\": \"Unlocking Front Door\",\n    \"message\": \"Attempting to unlock the Front Door for {{person}}\"\n  }\n}","output":"json","x":1390,"y":580,"wires":[["ca4fab51.437828","cf2c31cf.e331b","e0f04c2e.21c3f"]]},{"id":"ca4fab51.437828","type":"debug","z":"b630031d.75b78","name":"Template Output","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1440,"y":480,"wires":[]},{"id":"9da45023.a746c","type":"trigger-state","z":"b630031d.75b78","name":"Is William Home?","server":"b2b4933d.45d52","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"person.william","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"id":"w22ikbt8a7g","targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"home"}],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":610,"y":460,"wires":[["256c801c.f4b64","529ccfb1.4919b"],["e2cca79d.d6ebc8"]],"outputLabels":["Home","Not Home"]},{"id":"2ceb24f6.d4fd7c","type":"inject","z":"b630031d.75b78","name":"Arrive Home","topic":"","payload":"{\"entity_id\":\"person.william\",\"old_state\":{\"state\":\"not_home\"},\"new_state\":{\"state\":\"home\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":330,"y":420,"wires":[["9da45023.a746c"]]},{"id":"10285743.bece19","type":"inject","z":"b630031d.75b78","name":"Stayed Home","topic":"","payload":"{\"entity_id\":\"person.william\",\"old_state\":{\"state\":\"home\"},\"new_state\":{\"state\":\"home\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":330,"y":460,"wires":[["9da45023.a746c"]]},{"id":"38dd5c09.038654","type":"inject","z":"b630031d.75b78","name":"Leave Home","topic":"","payload":"{\"entity_id\":\"person.william\",\"old_state\":{\"state\":\"home\"},\"new_state\":{\"state\":\"not_home\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":330,"y":500,"wires":[["9da45023.a746c"]]},{"id":"cf2c31cf.e331b","type":"api-call-service","z":"b630031d.75b78","name":"Notify William","server":"b2b4933d.45d52","version":1,"debugenabled":false,"service_domain":"notify","service":"mobile_app_william_s_s10","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1660,"y":600,"wires":[[]]},{"id":"e0f04c2e.21c3f","type":"api-call-service","z":"b630031d.75b78","name":"Notify Hannah","server":"b2b4933d.45d52","version":1,"debugenabled":false,"service_domain":"notify","service":"mobile_app_hannah_s_s10","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1660,"y":540,"wires":[[]]},{"id":"6846da8f.cd1a94","type":"change","z":"b630031d.75b78","name":"Add Person","rules":[{"t":"set","p":"person","pt":"msg","to":"topic","tot":"msg"},{"t":"change","p":"person","pt":"msg","from":"^person.william$","fromt":"re","to":"William","tot":"str"},{"t":"change","p":"person","pt":"msg","from":"^person.hannah$","fromt":"re","to":"Hannah","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1170,"y":580,"wires":[["b2547d9d.b3303","4649c8cb.54c948"]]},{"id":"14a05b39.187355","type":"debug","z":"b630031d.75b78","name":"Locked Check","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1300,"y":280,"wires":[]},{"id":"b2547d9d.b3303","type":"debug","z":"b630031d.75b78","name":"Adding Name","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1320,"y":660,"wires":[]},{"id":"72b38f66.42c5d","type":"switch","z":"b630031d.75b78","name":"Spam Blocker","property":"payload","propertyType":"msg","rules":[{"t":"neq","v":"data.event.old_state.state","vt":"msg"}],"checkall":"true","repair":false,"outputs":1,"x":920,"y":360,"wires":[["a0a07e1c.3564a"]]},{"id":"529ccfb1.4919b","type":"switch","z":"b630031d.75b78","name":"Spam Blocker","property":"payload","propertyType":"msg","rules":[{"t":"neq","v":"data.event.old_state.state","vt":"msg"}],"checkall":"true","repair":false,"outputs":1,"x":920,"y":440,"wires":[["a0a07e1c.3564a"]]},{"id":"b2b4933d.45d52","type":"server","z":"","name":"Home Assistant"}]

Can you describe roughly what you’re trying to do with the flow. I assume simply mimic the auto unlock behaviour when either you or your wife arrives home ?

You could use groups in HA to combine people, then simply use that within your flow.

  • Assuming you have friendly_name set for each person entity because this is used to set the name in the notifications.
  • When using substring as the entity id filter the entity_id field can take a comma-delimited list of entity ids.

[{"id":"fe2ba1.1f68f46","type":"debug","z":"33126da.4908292","name":"Is not Home","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","x":678,"y":208,"wires":[]},{"id":"ebef389e.950938","type":"debug","z":"33126da.4908292","name":"Is Home","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":668,"y":112,"wires":[]},{"id":"6d2a6d0d.08ec94","type":"trigger-state","z":"33126da.4908292","name":"Is Hannah/William Home?","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"person.hannah,person.william","entityidfiltertype":"substring","debugenabled":false,"constraints":[{"id":"w22ikbt8a7g","targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"home"},{"id":"18zt3p8f8y1","targetType":"this_entity","targetValue":"","propertyType":"previous_state","propertyValue":"old_state.state","comparatorType":"is_not","comparatorValueDatatype":"str","comparatorValue":"home"},{"id":"b0d38gzqwc7","targetType":"entity_id","targetValue":"lock.front_door","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is_not","comparatorValueDatatype":"str","comparatorValue":"locked"}],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":462,"y":160,"wires":[["ebef389e.950938","75104e9d.c7b97"],["fe2ba1.1f68f46"]],"outputLabels":["Home","Not Home"]},{"id":"9b48563b.7f3948","type":"inject","z":"33126da.4908292","name":"Hannah Arrive Home","topic":"","payload":"{\"entity_id\":\"person.hannah\",\"old_state\":{\"state\":\"not_home\"},\"new_state\":{\"state\":\"home\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":154,"y":64,"wires":[["6d2a6d0d.08ec94"]]},{"id":"3e170991.ac7c56","type":"inject","z":"33126da.4908292","name":"Hannah Leave Home","topic":"","payload":"{\"entity_id\":\"person.hannah\",\"old_state\":{\"state\":\"home\"},\"new_state\":{\"state\":\"not_home\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":164,"y":128,"wires":[["6d2a6d0d.08ec94"]]},{"id":"77ee67a5.eae6e8","type":"inject","z":"33126da.4908292","name":"Hannah Stayed Home","topic":"","payload":"{\"entity_id\":\"person.hannah\",\"old_state\":{\"state\":\"home\"},\"new_state\":{\"state\":\"home\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":164,"y":96,"wires":[["6d2a6d0d.08ec94"]]},{"id":"75104e9d.c7b97","type":"api-call-service","z":"33126da.4908292","name":"Unlock Door","version":1,"debugenabled":false,"service_domain":"lock","service":"unlock","entityId":"lock.front_door","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":678,"y":160,"wires":[["bc3a390a.d83c38","48f5a25.a45bf5c","290be8f8.63c988"]]},{"id":"bc3a390a.d83c38","type":"debug","z":"33126da.4908292","name":"Unlocking Door","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":896,"y":112,"wires":[]},{"id":"fc506ad7.2cfb88","type":"inject","z":"33126da.4908292","name":"William Arrive Home","topic":"","payload":"{\"entity_id\":\"person.william\",\"old_state\":{\"state\":\"not_home\"},\"new_state\":{\"state\":\"home\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":154,"y":192,"wires":[["6d2a6d0d.08ec94"]]},{"id":"ab514b2a.4832d8","type":"inject","z":"33126da.4908292","name":"William Stayed Home","topic":"","payload":"{\"entity_id\":\"person.william\",\"old_state\":{\"state\":\"home\"},\"new_state\":{\"state\":\"home\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":164,"y":224,"wires":[["6d2a6d0d.08ec94"]]},{"id":"ab4911f8.e4ee4","type":"inject","z":"33126da.4908292","name":"William Leave Home","topic":"","payload":"{\"entity_id\":\"person.william\",\"old_state\":{\"state\":\"home\"},\"new_state\":{\"state\":\"not_home\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":154,"y":256,"wires":[["6d2a6d0d.08ec94"]]},{"id":"290be8f8.63c988","type":"api-call-service","z":"33126da.4908292","name":"Notify William","version":1,"debugenabled":false,"service_domain":"notify","service":"mobile_app_william_s_s10","entityId":"","data":"{\"title\":\"Unlocking Front Door\",\"message\":\"Attempting to unlock the Front Door for {{data.event.new_state.attributes.friendly_name}}\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":896,"y":208,"wires":[[]]},{"id":"48f5a25.a45bf5c","type":"api-call-service","z":"33126da.4908292","name":"Notify Hannah","version":1,"debugenabled":false,"service_domain":"notify","service":"mobile_app_hannah_s_s10","entityId":"","data":"{\"title\":\"Unlocking Front Door\",\"message\":\"Attempting to unlock the Front Door for {{data.event.new_state.attributes.friendly_name}}\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":896,"y":160,"wires":[[]]}]

Goal, when me or wife arrives home, the door unlocks regardless of whether the other is home. Then it sends a notification to both of us saying that Xxxxx unlocked the door.

I had misread the latest release notes for HA and thought it was saying that groups were being deprecated so wanted to avoid using them. Didn’t realize at first it was just a certain bit of groups and not groups in general that were on the way out.

1 Like

I forgot about the friendly name for people being capitalized and accessible. Ended up creating a subflow for extracting the friendly name for easy future use.

I’m gonna keep the lock check and “spam” check separate from the entity trigger because I had an issue last night where my phone’s GPS fudged up and said I was outside my home, then corrected itself and ended up unlocking the front door. Trying to think of a way to add a timer now, where you have to be away from home for say 2 minutes before coming back before it will unlock the door. For that, need the false output from the first node to mean just “not home”.