home-assistant.io/source/_integrations/switch.template.markdown
Franck Nijhof 23b0b57c08
Rename frontmatter ha_qa_scale to ha_quality_scale to match codebase (#11672)
* Rename frontmatter ha_qa_scale to ha_quality_scale to match codebase

* Update all integrations quality scales (done by sync)
2020-01-08 13:10:43 +01:00

6.3 KiB

title, description, ha_category, ha_release, ha_iot_class, logo, ha_quality_scale
title description ha_category ha_release ha_iot_class logo ha_quality_scale
Template Switch Instructions on how to integrate Template Switches into Home Assistant.
Switch
0.13 Local Push home-assistant.png internal

The template platform creates switches 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 switch that knows whether the garage door is open or closed.

This can simplify the GUI and make it easier to write automations. You can mark the integrations you have combined as hidden so they don't appear themselves.

Configuration

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

{% raw %}

# Example configuration.yaml entry
switch:
  - platform: template
    switches:
      skylight:
        value_template: "{{ is_state('sensor.skylight', 'on') }}"
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.skylight_open
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.skylight_close

{% endraw %}

{% configuration %} switches: description: List of your switches. required: true type: map keys: friendly_name: description: Name to use in the frontend. required: false type: string entity_id: description: A list of entity IDs so the switch only reacts to state changes of these entities. This can be used if the automatic analysis fails to find all relevant entities. required: false type: [string, list] value_template: description: Defines a template to set the state of the switch. required: true type: template availability_template: description: Defines a template to get the available state of the component. If the template returns true, the device is available. If the template returns any other value, the device will be unavailable. If availability_template is not configured, the component will always be available. required: false type: template default: true turn_on: description: Defines an action to run when the switch is turned on. required: true type: action turn_off: description: Defines an action to run when the switch is turned off. required: true type: action icon_template: description: Defines a template for the icon of the switch. required: false type: template entity_picture_template: description: Defines a template for the picture of the switch. required: false type: template {% endconfiguration %}

Considerations

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

Examples

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

Copy Switch

This example shows a switch that copies another switch.

{% raw %}

switch:
  - platform: template
    switches:
      copy:
        value_template: "{{ is_state('switch.source', 'on') }}"
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.target
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.target

{% endraw %}

Toggle Switch

This example shows a switch that takes its state from a sensor and toggles a switch.

{% raw %}

switch:
  - platform: template
    switches:
      blind:
        friendly_name: "Blind"
        value_template: "{{ is_state_attr('switch.blind_toggle', 'sensor_state', 'on') }}"
        turn_on:
          service: switch.toggle
          data:
            entity_id: switch.blind_toggle
        turn_off:
          service: switch.toggle
          data:
            entity_id: switch.blind_toggle

{% endraw %}

Sensor and Two Switches

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

{% raw %}

switch:
  - platform: template
    switches:
      skylight:
        friendly_name: "Skylight"
        value_template: "{{ is_state('sensor.skylight', 'on') }}"
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.skylight_open
        turn_off:
          service: switch.turn_on
          data:
            entity_id: switch.skylight_close

{% endraw %}

Change The Icon

This example shows how to change the icon based on the day/night cycle.

{% raw %}

switch:
  - platform: template
    switches:
      garage:
        value_template: "{{ is_state('cover.garage_door', 'on') }}"
        turn_on:
          service: cover.open_cover
          data:
            entity_id: cover.garage_door
        turn_off:
          service: cover.close_cover
          data:
            entity_id: cover.garage_door
        icon_template: >-
          {% if is_state('cover.garage_door', 'open') %}
            mdi:garage-open
          {% else %}
            mdi:garage
          {% endif %}

{% endraw %}

Change The Entity Picture

This example shows how to change the entity picture based on the day/night cycle.

{% raw %}

switch:
  - platform: template
    switches:
      garage:
        value_template: "{{ is_state('cover.garage_door', 'on') }}"
        turn_on:
          service: cover.open_cover
          data:
            entity_id: cover.garage_door
        turn_off:
          service: cover.close_cover
          data:
            entity_id: cover.garage_door
        entity_picture_template: >-
          {% if is_state('cover.garage_door', 'open') %}
            /local/garage-open.png
          {% else %}
            /local/garage-closed.png
          {% endif %}

{% endraw %}