AutoLock.yaml

I spent a lot of time working on this, so I thought I would share.

I have two entrances to my place, a garage door (connected via MQ and appears as a cover) and my front door, which uses a Schlage Z-wave device. When the front door has been opened, I would like it to automatically lock after a few minutes. However there are times of the week when I’m doing work outside, or have workers at the house where this quickly became a pain in the neck. So I wrote this “AutoLock” function that is initially in the “on” state meaning it will automatically lock or notify me when a specified device has been left open. When I turn AutoLock “off” usually by voice or google assistant, locking and notification stops for 8 hours.

The original code I wrote had another slight annoyance. I liked to have the front door lock after 15 minutes, but my wife wanted it to lock after 5 minutes. So we came up with a compromise. During daylight hours the front door locks after 15 minutes, and in periods of dark, it locks after 5 minutes.

I’ll break down the code via the files I created.

timers.yaml

Create this file and add the following to your configuration.yaml via

timer: !include timers.yaml

This defines three timers (and default values) for how long the garage remains opened before sending a notification. Note, the front_door timer doesn’t have a default value for the timer, as that is selected in a script. The auto_lock_disable defines how long before AutoLocking resumes.

garage_door:
  duration: '00:15:00'
front_door:
auto_lock_disable:
  duration: '08:00:00'

Add the following to your scripts.yaml. You can modify the day/nighttime door lock values by changing the 00:15:00 and 00:05:00 accordingly. This script simply starts the front door timer, but uses dusk and dawn values to determine how long the timer should last.

start_front_door_custom_timer:
  sequence:
    - service: timer.cancel
      entity_id:  timer.front_door
    - service: timer.start
      data_template:    # if next_dusk happens sooner than next_dawn, then (next_dusk - next_dawn) < 0
         entity_id: timer.front_door
         duration: >
          {% if ((((as_timestamp(states.sun.sun.attributes.next_dusk)) - (as_timestamp(states.sun.sun.attributes.next_dawn)))) < 0) %}
            00:15:00
          {% else %}
            00:05:00
          {% endif %}

AutoLock.yaml

Note: You will need to create an input_boolean named autolock which you can connect to the personal assistant of your choice to enable/disable autolocking

- alias: Front Door Unlocked
  trigger:
    entity_id: lock.schlage_be469_touchscreen_deadbolt_locked_2
    platform: state
    to: unlocked
  condition:
    condition: state
    entity_id: input_boolean.autolock
    state: 'on'
  action:
    - service: script.turn_on
      entity_id: script.start_front_door_custom_timer
    
- alias: Front Door Locked
  trigger:
    entity_id: lock.schlage_be469_touchscreen_deadbolt_locked_2
    platform: state
    to: locked
  action:
    - service: timer.cancel
      entity_id: timer.front_door

- alias: Lock Timer Finished
  trigger:
    platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.front_door
  condition:
     condition: state
     entity_id: input_boolean.autolock
     state: 'on'
  action:
    service: lock.lock
    entity_id: lock.schlage_be469_touchscreen_deadbolt_locked_2
    
- alias: autolock finished
  trigger:
    platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.auto_lock_disable
  action:
    service: input_boolean.on
    entity_id: input_boolean.autolock
      
- alias: Disable Auto Lock
  trigger:
    entity_id: input_boolean.autolock
    platform: state
    to: 'off'
  action:
    - service: timer.cancel
      entity_id:
        - timer.garage_door
        - timer.front_door
    - service: timer.start
      entity_id: timer.auto_lock_disable
     
- alias: Enable Auto Lock
  trigger:
    entity_id: input_boolean.autolock
    platform: state
    to: 'on'
  action:
    - service: timer.cancel
      entity_id:
        - timer.garage_door
        - timer.front_door
        - timer.auto_lock_disable
    - service: lock.lock
      entity_id: lock.schlage_be469_touchscreen_deadbolt_locked_2
    - service: cover.close_cover
      entity_id: cover.garage_door_opener
      
- alias: Toggle Front Door
  trigger:
    platform: webhook
    webhook_id: !secret frontdoor_token
  action:
    service_template: >
      {%- if is_state('lock.schlage_be469_touchscreen_deadbolt_locked_2', 'locked') -%}
         lock.unlock
      {%- else -%}
         lock.lock
      {%- endif -%}
    entity_id: lock.schlage_be469_touchscreen_deadbolt_locked_2

GarageTimer.yaml

I don’t want the garage closing automatically, so instead I send a message to IFTTT which will then trigger a notification to my phone. I could have done this with other HA notification components, but I already had this working for my ISY hub, so why reinvent the wheel?

- alias: Garage Opened
  hide_entity: True
  trigger:
    - platform: state
      entity_id: cover.garage_door_opener
      to: 'open'
  condition:
    - condition: state
      entity_id: input_boolean.autolock
      state: 'on'
  action:
    - service: timer.start
      entity_id: timer.garage_door
      
- alias: Garage Closed
  hide_entity: True
  trigger:
    - platform: state
      entity_id: cover.garage_door_opener
      to: 'closed'
  action:
    - service: timer.cancel
      entity_id:  timer.garage_door
      
- alias: Garage Opened Timer Finished
  hide_entity: True
  trigger:
    - platform: event
      event_type: timer.finished
      event_data:
        entity_id: timer.garage_door
  condition:
    - condition: state
      entity_id: cover.garage_door_opener
      state: 'open'
    - condition: state
      entity_id: input_boolean.autolock
      state: 'on'
  action:
    - service: ifttt.trigger
      data: {"event": "GarageLeftOpened"}
    - service: timer.start
      entity_id: timer.garage_door
1 Like