Last active
May 11, 2025 19:25
-
-
Save Ltek/f6e517ac49d3e9ecd58714c2b9afe7d3 to your computer and use it in GitHub Desktop.
Home Assistant Blueprint: Auto-Lock by LTek
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
blueprint: | |
name: "Auto-Lock by LTek" | |
description: ' | |
Monitors for doors that just unlocked and Automatically Locks them after a period of time, when the door is closed. | |
Features: | |
* Door Sensor state monitoring to ensure door is closed before attempting to lock | |
* Configurable retry logic (in case lock jams) | |
* Mobile and UI notifications | |
* Customizable Delays and Timeouts for Actions | |
Author: LTek | |
Version: 2025.05.10.3 | |
source_url: https://gist.github.com/Ltek/f6e517ac49d3e9ecd58714c2b9afe7d3 | |
' | |
domain: automation | |
input: | |
door_lock: | |
name: "Door Lock" | |
description: "Lock entity to control" | |
selector: | |
entity: | |
domain: lock | |
door_sensor: | |
name: "Door Sensor" | |
description: "Binary sensor that shows door open/closed state" | |
selector: | |
entity: | |
domain: binary_sensor | |
counter_variable: | |
name: "Retry Counter" | |
description: "Counter helper to track attempts - you must created this manually" | |
default: "counter.door_lock_retries" | |
selector: | |
entity: | |
domain: counter | |
bypass_entity: | |
name: "Bypass Entity" | |
description: "Entity that disables automation when on" | |
default: "" | |
selector: | |
entity: | |
multiple: false | |
max_retries: | |
name: "Maximum Retries" | |
description: "Number of lock attempts after door is detected to be closed. Helps in case of jams or alignment issues." | |
default: 3 | |
selector: | |
number: | |
min: 1 | |
max: 10 | |
step: 1 | |
door_close_timeout: | |
name: "Wait for Door to Close (minutes)" | |
description: "Automation Timesout (stops running) when this time passes, if door is not closed. Uses Door Sensor set above." | |
default: 1 | |
selector: | |
number: | |
min: 1 | |
max: 15 | |
step: 1 | |
verification_timeout: | |
name: "Delay after Closed (minutes)" | |
description: "Wait to verify door remains closed. Helpful in case you quickly reopen and close the door again after the initial close." | |
default: 1 | |
selector: | |
number: | |
min: 1 | |
max: 15 | |
step: 1 | |
enable_mobile_notifications: | |
name: "Enable Mobile Notifications" | |
description: "Send notifications to mobile devices" | |
default: true | |
selector: | |
boolean: {} | |
notification_device: | |
name: "Send Notification" | |
description: "Notification service (e.g., notify.mobile_app_yourphone)" | |
default: "notify.notify" | |
selector: | |
text: | |
enable_persistent_notifications: | |
name: "Enable UI Notifications" | |
description: "Show notifications in Home Assistant" | |
default: true | |
selector: | |
boolean: {} | |
notify_monitoring_start: | |
name: "Notify on Monitoring Start" | |
description: "Send notification when monitoring begins" | |
default: true | |
selector: | |
boolean: {} | |
notify_timeout: | |
name: "Notify on Lock Timeout" | |
description: "Send notification when door fails to close" | |
default: true | |
selector: | |
boolean: {} | |
notify_max_retries: | |
name: "Notify on Failure to Lock" | |
description: "Send notification when max attempts reached and door did not lock" | |
default: true | |
selector: | |
boolean: {} | |
notify_lock_success: | |
name: "Notify on Lock Success" | |
description: "Send notification when door successfully locks" | |
default: true | |
selector: | |
boolean: {} | |
variables: | |
door_lock: !input door_lock | |
door_sensor: !input door_sensor | |
counter_variable: !input counter_variable | |
max_retries: !input max_retries | |
door_close_timeout: !input door_close_timeout | |
verification_timeout: !input verification_timeout | |
notify_monitoring_start: !input notify_monitoring_start | |
notify_timeout: !input notify_timeout | |
notify_max_retries: !input notify_max_retries | |
notify_lock_success: !input notify_lock_success | |
enable_mobile_notifications: !input enable_mobile_notifications | |
enable_persistent_notifications: !input enable_persistent_notifications | |
notification_device: !input notification_device | |
bypass_entity: !input bypass_entity | |
current_retry_count: > | |
{% set count = states(counter_variable) | int(0) %} | |
{{ 0 if count < 0 else count }} | |
valid_notification_service: true | |
safe_notification_service: "{{ notification_device }}" | |
trigger: | |
- platform: state | |
entity_id: !input door_lock | |
to: "unlocked" | |
- platform: state | |
entity_id: !input door_sensor | |
to: "on" | |
condition: | |
- condition: template | |
value_template: >- | |
{% if bypass_entity %} | |
{{ not is_state(bypass_entity, 'on') }} | |
{% else %} | |
true | |
{% endif %} | |
action: | |
# Monitoring Start Notification | |
- if: | |
- condition: template | |
value_template: "{{ current_retry_count == 0 and notify_monitoring_start }}" | |
then: | |
- if: | |
- condition: template | |
value_template: "{{ enable_mobile_notifications }}" | |
then: | |
- service: "{{ safe_notification_service }}" | |
data: | |
message: "Auto-Lock monitoring {{ state_attr(door_lock, 'friendly_name') }}" | |
- if: | |
- condition: template | |
value_template: "{{ enable_persistent_notifications }}" | |
then: | |
- service: persistent_notification.create | |
data: | |
title: "Auto-Lock monitoring {{ state_attr(door_lock, 'friendly_name') }}" | |
message: "" | |
notification_id: "lock_monitoring_{{ door_lock }}" | |
# Max Retries Notification | |
- if: | |
- condition: template | |
value_template: "{{ current_retry_count >= max_retries and notify_max_retries }}" | |
then: | |
- if: | |
- condition: template | |
value_template: "{{ enable_mobile_notifications }}" | |
then: | |
- service: "{{ safe_notification_service }}" | |
data: | |
message: "Auto-Lock Failed to lock {{ state_attr(door_lock, 'friendly_name') }} after {{ max_retries }} attempts" | |
- if: | |
- condition: template | |
value_template: "{{ enable_persistent_notifications }}" | |
then: | |
- service: persistent_notification.create | |
data: | |
title: "Auto-Lock Failed to lock {{ state_attr(door_lock, 'friendly_name') }}" | |
message: "after {{ max_retries }} attempts" | |
notification_id: "lock_failed_{{ door_lock }}" | |
- service: counter.reset | |
target: | |
entity_id: "{{ counter_variable }}" | |
# Increment counter | |
- service: counter.increment | |
target: | |
entity_id: "{{ counter_variable }}" | |
# Wait for door to close | |
- wait_template: "{{ is_state(door_sensor, 'off') }}" | |
timeout: | |
minutes: "{{ door_close_timeout }}" | |
continue_on_timeout: true | |
# Timeout Notification | |
- if: | |
- condition: state | |
entity_id: !input door_sensor | |
state: "on" | |
- condition: template | |
value_template: "{{ notify_timeout }}" | |
then: | |
- if: | |
- condition: template | |
value_template: "{{ enable_mobile_notifications }}" | |
then: | |
- service: "{{ safe_notification_service }}" | |
data: | |
message: "Auto-Lock Timeout waiting for {{ state_attr(door_sensor, 'friendly_name') }} to close" | |
- if: | |
- condition: template | |
value_template: "{{ enable_persistent_notifications }}" | |
then: | |
- service: persistent_notification.create | |
data: | |
title: "Auto-Lock Timeout, {{ state_attr(door_sensor, 'friendly_name') }} didn't close in time" | |
message: "" | |
notification_id: "door_timeout_{{ door_sensor }}" | |
- service: automation.trigger | |
target: | |
entity_id: "{{ this.entity_id }}" | |
# Verification period | |
- wait_for_trigger: | |
- platform: state | |
entity_id: !input door_sensor | |
to: "off" | |
timeout: | |
minutes: "{{ verification_timeout }}" | |
continue_on_timeout: true | |
# Successful Lock | |
- if: | |
- condition: state | |
entity_id: !input door_sensor | |
state: "off" | |
then: | |
- service: lock.lock | |
target: | |
entity_id: !input door_lock | |
- service: counter.reset | |
target: | |
entity_id: "{{ counter_variable }}" | |
- if: | |
- condition: template | |
value_template: "{{ notify_lock_success and enable_mobile_notifications }}" | |
then: | |
- service: "{{ safe_notification_service }}" | |
data: | |
message: "Auto-Lock locked {{ state_attr(door_lock, 'friendly_name') }} successfully" | |
- if: | |
- condition: template | |
value_template: "{{ notify_lock_success and enable_persistent_notifications }}" | |
then: | |
- service: persistent_notification.create | |
data: | |
title: "Auto-Lock locked {{ state_attr(door_lock, 'friendly_name') }}" | |
message: "" | |
notification_id: "lock_success_{{ door_lock }}" | |
mode: restart |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment