Template Lock


The template platform creates locks that combines components.

For example, if you have a garage door with a toggle switch that operates the motor and a sensor that allows you know whether the door is open or closed, you can combine these into a lock that knows whether the garage door is open or closed.

This can simplify the GUI and make it easier to write automations.

In optimistic mode, the lock will immediately change state after every command. Otherwise, the lock will wait for state confirmation from the template. Try to enable it, if experiencing incorrect lock operation.

Configuration

To enable Template Locks in your installation, add the following to your configuration.yaml file:

# Example configuration.yaml entry
lock:
  - platform: template
    name: Garage door
    value_template: "{{ is_state('sensor.door', 'on') }}"
    lock:
      service: switch.turn_on
      target:
        entity_id: switch.door
    unlock:
      service: switch.turn_off
      target:
        entity_id: switch.door

Configuration Variables

name string (Optional, default: Template Lock)

Name to use in the frontend.

unique_id string (Optional)

An ID that uniquely identifies this lock. Set this to a unique value to allow customization through the UI.

value_template template Required

Defines a template to set the state of the lock.

availability_template template (Optional, default: true)

Defines a template to get the available state of the entity. If the template either fails to render or returns True, "1", "true", "yes", "on", "enable", or a non-zero number, the entity will be available. If the template returns any other value, the entity will be unavailable. If not configured, the entity will always be available. Note that the string comparison not case sensitive; "TrUe" and "yEs" are allowed.

Template and action variables

State-based template entities have the special template variable this available in their templates and actions. The this variable aids self-referencing of an entity’s state and attribute in templates and actions.

Considerations

If you are using the state of a platform that takes extra time to load, the Template Lock may get an unknown state during startup. This results in error messages in your log file until that platform has completed loading. If you use is_state() function in your template, you can avoid this situation. For example, you would replace {{ state('switch.source') == 'on') }} with this equivalent that returns true/false and never gives an unknown result: {{ is_state('switch.source', 'on') }}

Examples

In this section, you find some real-life examples of how to use this lock.

Lock from Switch

This example shows a lock that copies data from a switch.

lock:
  - platform: template
    name: Garage Door
    value_template: "{{ is_state('switch.source', 'on') }}"
    lock:
      service: switch.turn_on
      target:
        entity_id: switch.source
    unlock:
      service: switch.turn_off
      target:
        entity_id: switch.source

Optimistic mode

This example shows a lock in optimistic mode. This lock will immediately change state after command and will not wait for state update from the sensor.

lock:
  - platform: template
    name: Garage Door
    value_template: "{{ is_state('sensor.skylight.state', 'on') }}"
    optimistic: true
    lock:
      service: switch.turn_on
      target:
        entity_id: switch.source
    unlock:
      service: switch.turn_off
      target:
        entity_id: switch.source

Sensor and Two Switches

This example shows a lock that takes its state from a sensor, and uses two momentary switches to control a device.

lock:
  - platform: template
    name: Garage Door
    value_template: "{{ is_state('sensor.skylight.state', 'on') }}"
    lock:
      service: switch.turn_on
      target:
        entity_id: switch.skylight_open
    unlock:
      service: switch.turn_on
      target:
        entity_id: switch.skylight_close