Template Cover


The template platform can create covers that combine components and provides the ability to run scripts or invoke services for each of the open, close, stop, position, and tilt commands of a cover.

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

# Example configuration.yaml entry
cover:
  - platform: template
    covers:
      garage_door:
        friendly_name: "Garage Door"
        value_template: "{{ states('sensor.garage_door')|float > 0 }}"
        open_cover:
          service: script.open_garage_door
        close_cover:
          service: script.close_garage_door
        stop_cover:
          service: script.stop_garage_door

Configuration Variables

covers

(map)(Required)List of your covers.

friendly_name

(string)(Optional)Name to use in the frontend.

entity_id

(string | list)(Optional)Add a list of entity IDs so the switch only reacts to state changes of these entities. This will reduce the number of times the cover will try to update its state.

value_template

(template)(Exclusive)Defines a template to get the state of the cover. Valid values are open/true or closed/false. value_template and position_template cannot be specified concurrently.

position_template

(template)(Exclusive)Defines a template to get the state of the cover. Legal values are numbers between 0 (closed) and 100 (open). value_template and position_template cannot be specified concurrently.

icon_template

(template)(Optional)Defines a template to specify which icon to use.

open_cover

(action)(Inclusive)Defines an action to run when the cover is opened. If open_cover is specified, close_cover must also be specified. At least one of open_cover and set_cover_position must be specified.

close_cover

(action)(Inclusive)Defines an action to run when the cover is closed.

stop_cover

(action)(Optional)Defines an action to run when the cover is stopped.

set_cover_position

(action)(Optional)Defines an action to run when the cover is set to a specific value (between 0 and 100).

set_cover_tilt_position

(action)(Optional)Defines an action to run when the cover tilt is set to a specific value (between 0 and 100).

optimistic

(bool)(Optional)Force cover position to use optimistic mode.

Default value: false

tilt_optimistic

(bool)(Optional)Force cover tilt position to use optimistic mode.

Default value: false

tilt_template

(template)(Optional)Defines a template to get the tilt state of the cover. Legal values are numbers between 0 (closed) and 100 (open).

Considerations

If you are using the state of a platform that takes extra time to load, the Template Cover 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 {{ states.switch.source.state == 'on' }} with this equivalent that returns true/false and never gives an unknown result: {{ is_state('switch.source', 'on') }}

Optimistic Mode

In optimistic mode, the cover position state is maintained internally. This mode is automatically enabled if neither value_template or position_template are specified. Note that this is unlikely to be very reliable without some feedback mechanism, since there is otherwise no way to know if the cover is moving properly. The cover can be forced into optimistic mode by using the optimistic attribute. There is an equivalent mode for tilt_position that is enabled when tilt_template is not specified or when the tilt_optimistic attribute is used.

Examples

In this section you will find some real life examples of how to use this cover.

Garage Door

This example converts a garage door with a controllable switch and position sensor into a cover.

cover:
  - platform: template
    covers:
      garage_door:
        friendly_name: "Garage Door"
        position_template: "{{ states('sensor.garage_door') }}"
        open_cover:
          service: switch.turn_on
          data:
            entity_id: switch.garage_door
        close_cover:
          service: switch.turn_off
          data:
            entity_id: switch.garage_door
        stop_cover:
          service: switch.turn_on
          data:
            entity_id: switch.garage_door
        icon_template: >-
          {% if states('sensor.garage_door')|float > 0 %}
            mdi:garage-open
          {% else %}
            mdi:garage
          {% endif %}

Multiple Covers

This example allows you to control two or more covers at once.

homeassistant:
  customize:
    all_covers:
      assume_state: true

cover:
  - platform: template
    covers:
      all_covers:
        friendly_name: "All Covers"
        open_cover:
          service: script.cover_all_open
        close_cover:
          service: script.cover_all_close
        stop_cover:
          service: script.cover_all_stop
        set_cover_position:
          service: script.cover_all_set_position
          data_template:
            position: "{{ position }}"
        value_template: "{{ is_state('sensor.all_covers', 'open') }}"
        icon_template: >-
          {% if is_state('sensor.all_covers', 'open') %}
            mdi:window-open
          {% else %}
            mdi:window-closed
          {% endif %}
        entity_id:
          - cover.bedroom
          - cover.livingroom

sensor:
  - platform: template
    sensors:
      all_covers:
        value_template: >-
          {% if is_state('cover.bedroom', 'open') %}
            open
          {% elif is_state('cover.livingroom', 'open') %}
            open
          {% else %}
            closed
          {% endif %}
        entity_id:
          - cover.bedroom
          - cover.livingroom

script:
  cover_all_open:
    sequence:
      - service: cover.open_cover
        data:
          entity_id:
            - cover.bedroom
            - cover.livingroom
  cover_all_stop:
    sequence:
      - service: cover.stop_cover
        data:
          entity_id:
            - cover.bedroom
            - cover.livingroom
  cover_all_close:
    sequence:
      - service: cover.close_cover
        data:
          entity_id:
            - cover.bedroom
            - cover.livingroom
  cover_all_set_position:
    sequence:
      - service: cover.set_cover_position
        data_template:
          entity_id:
            - cover.bedroom
            - cover.livingroom
          position: "{{ position }}"

automation:
  - alias: "Close covers at night"
    trigger:
      - platform: sun
        event: sunset
        offset: '+00:30:00'
    action:
      - service: cover.set_cover_position
        data:
          entity_id: cover.all_covers
          position: 25