Template Switch

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.

配置

要将 Template Switch helper 添加到您的 Home Assistant 实例中,请使用此 My 按钮:

手动配置步骤

如果上述 My 按钮不起作用,您也可以手动执行以下步骤:

  • 浏览到您的 Home Assistant 实例。

  • 转到 设置 > 设备与服务

  • 在屏幕顶部,选择标签:Helpers

  • 在右下角,选择 创建助手 按钮。

  • 从列表中选择 Template Switch

  • 按照屏幕上的说明完成设置。

Important

To be able to add Helpers via the user interface, you should have default_config: in your configuration.yamlconfiguration.yaml 文件是 Home Assistant 的主要配置文件。它列出了要加载的集成及其特定配置。在某些情况下,需要直接在 configuration.yaml 文件中手动编辑配置。大多数集成可以在 UI 中配置。 [Learn more]. It should already be there by default unless you removed it.

Note

Configuration using our user interface provides a more limited subset of options, making this integration more accessible while covering most use cases.

If you need more specific features for your use case, the manual YAML-configuration section of this integration might provide them.

YAML Configuration

To enable Template Switches in your installation, add the following to your configuration.yamlconfiguration.yaml 文件是 Home Assistant 的主要配置文件。它列出了要加载的集成及其特定配置。在某些情况下,需要直接在 configuration.yaml 文件中手动编辑配置。大多数集成可以在 UI 中配置。 [Learn more] file:

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

Configuration Variables

switches map Required

List of your switches.

friendly_name string (Optional)

Name to use in the frontend.

unique_id string (Optional)

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

value_template template (Optional, default: optimistic)

Defines a template to set the state of the switch. If not defined, the switch will optimistically assume all commands are successful.

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.

turn_on action Required

Defines an action or list of actions to run when the switch is turned on.

turn_off action Required

Defines an action or list of actions to run when the switch is turned off.

icon_template template (Optional)

Defines a template for the icon of the switch.

entity_picture_template template (Optional)

Defines a template for the picture of the switch.

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 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 {{ states.switch.source.state == '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 switch.

Invert a Switch

This example shows a switch that is the inverse of another switch.

switch:
  - platform: template
    switches:
      invert:
        value_template: "{{ not is_state('switch.target', 'on') }}"
        availability_template: "{{ has_value('switch.target') }}"
        turn_on:
          action: switch.turn_off
          target:
            entity_id: switch.target
        turn_off:
          action: switch.turn_on
          target:
            entity_id: switch.target

Toggle Switch

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

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

Multiple actions for turn_on or turn_off

This example shows multiple actions for turn_on and turn_off.

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

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.

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

Change The Icon

This example shows how to change the icon based on the state of the garage door.

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

Change The Entity Picture

This example shows how to change the entity picture based on the state of the garage door.

switch:
  - platform: template
    switches:
      garage:
        value_template: "{{ is_state('cover.garage_door', 'open') }}"
        turn_on:
          action: cover.open_cover
          target:
            entity_id: cover.garage_door
        turn_off:
          action: cover.close_cover
          target:
            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 %}