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"}]