Auto lock door with conditions

I wanted to write an automation to lock a door 15 minutes after a door is unlocked. If the door is closed lock immediately after the 15 minutes is up (restarting the timer if door is reopened in the 15 minute span), but if the door is still open after the 15 minutes, then wait for the door to close and then lock 5 minutes after it is closed.

I am new to home assistant and have been looking at examples, but just can’t figure this out. I also have some basic questions about what certain symbols mean. like what does > mean or > - and how are they different. For example service_template: > -

Any guidance on how I can accomplish the door lock task would be appreciated.

This should help you get started.

- alias: Automatic Lock
    platform: state
    entity_id: binary_sensor.front_door
    from: 'on'
    to: 'off'
    for: '00:15:00'
    condition: state
    entity_id: lock.front_door
    state: 'unlocked'
    service: lock.lock
    entity_id: lock.front_door
  • When the door sensor changes from on to off it starts an internal 15-minute timer.
  • If 15 minutes pass with the door sensor remaining off, the action locks the front door.
  • If someone re-opens the door during the 15-minute countdown, the internal timer is canceled. The moment the door is closed again, the 15-minute timer restarts its countdown.
  • The purpose of the condition is to simply ensure the action is executed only if the door is currently unlocked (i.e. don’t bother locking it if someone already locked it within the 15-minute period).

What this does not do is meet your requirement where, after a certain sequence of events, the door is locked after 5 minutes. That requirement adds considerable complexity; feel free to figure out how to do it.

The greater-than symbol > is a line-continuation indicator. It means the template doesn’t start on the same line as the option but on the next line.

The hyphen - is used for whitespace control. Practically speaking, it has almost no influence for templates used in automations and Template Triggers, Template Sensors, etc.