Template Light


The template platform creates lights that combine integrations and provides the ability to run scripts or invoke services for each of the on, off, and brightness commands of a light.

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

# Example configuration.yaml entry
light:
  - platform: template
    lights:
      theater_lights:
        friendly_name: "Theater Lights"
        level_template: "{{ state_attr('sensor.theater_brightness', 'lux')|int }}"
        value_template: "{{ state_attr('sensor.theater_brightness', 'lux')|int > 0 }}"
        temperature_template: "{{states('input_number.temperature_input') | int}}"
        color_template: "({{states('input_number.h_input') | int}}, {{states('input_number.s_input') | int}})"
        turn_on:
          service: script.theater_lights_on
        turn_off:
          service: script.theater_lights_off
        set_level:
          service: script.theater_lights_level
          data:
            brightness: "{{ brightness }}"
        set_temperature:
          service: input_number.set_value
          data:
            value: "{{ color_temp }}"
            entity_id: input_number.temperature_input
        set_white_value:
          service: input_number.set_value
          data:
            value: "{{ white_value }}"
            entity_id: input_number.white_value_input
        set_color:
          - service: input_number.set_value
            data:
              value: "{{ h }}"
              entity_id: input_number.h_input
          - service: input_number.set_value
            data:
              value: "{{ s }}"
              entity_id: input_number.s_input

Configuration Variables

lights mapRequired

List of your lights.

friendly_name string(Optional)

Name to use in the frontend.

unique_id string(Optional)

An ID that uniquely identifies this light. Set this to an unique value to allow customisation trough the UI.

value_template template(Optional, default: optimistic)

Defines a template to get the state of the light.

level_template template(Optional, default: optimistic)

Defines a template to get the brightness of the light.

temperature_template template(Optional, default: optimistic)

Defines a template to get the color temperature of the light.

white_value_template template(Optional, default: optimistic)

Defines a template to get the white value of the light.

color_template template(Optional, default: optimistic)

Defines a template to get the color of the light. Must render a tuple (hue, saturation)

icon_template template(Optional)

Defines a template for an icon or picture, e.g., showing a different icon for different states.

availability_template template(Optional, default: true)

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.

turn_on actionRequired

Defines an action to run when the light is turned on.

turn_off actionRequired

Defines an action to run when the light is turned off.

set_level action(Optional)

Defines an action to run when the light is given a brightness command.

set_temperature action(Optional)

Defines an action to run when the light is given a color temperature command.

set_white_value action(Optional)

Defines an action to run when the light is given a white value command.

set_color action(Optional)

Defines an action to run when the light is given a color command.

Considerations

If you are using the state of a platform that takes extra time to load, the Template Light 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') }}

Working without entities

If you use a template that depends on the current time or some other non-deterministic result not sourced from entities, the template won’t repeatedly update but will only update when the state of a referenced entity updates. For ways to deal with this issue, see Working without entities in the Template Binary Sensor integration.

Examples

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

Theater Volume Control

This example shows a light that is actually a home theater’s volume. This component gives you the flexibility to provide whatever you’d like to send as the payload to the consumer including any scale conversions you may need to make; the Media Player component needs a floating point percentage value from 0.0 to 1.0.

light:
  - platform: template
    lights:
      theater_volume:
        friendly_name: "Receiver Volume"
        value_template: >-
          {% if is_state('media_player.receiver', 'on') %}
            {% if state_attr('media_player.receiver', 'is_volume_muted') %}
              off
            {% else %}
              on
            {% endif %}
          {% else %}
            off
          {% endif %}
        turn_on:
          service: media_player.volume_mute
          data:
            entity_id: media_player.receiver
            is_volume_muted: false
        turn_off:
          service: media_player.volume_mute
          data:
            entity_id: media_player.receiver
            is_volume_muted: true
        set_level:
          service: media_player.volume_set
          data:
            entity_id: media_player.receiver
            volume_level: "{{ (brightness / 255 * 100)|int / 100 }}"
        level_template: >-
          {% if is_state('media_player.receiver', 'on') %}
            {{ (state_attr('media_player.receiver', 'volume_level')|float * 255)|int }}
          {% else %}
            0
          {% endif %}

Change The Icon

This example shows how to change the icon based on the light state.

light:
  - platform: template
    lights:
      theater_volume:
        friendly_name: "Receiver Volume"
        value_template: >-
          {% if is_state('media_player.receiver', 'on') %}
            {% if state_attr('media_player.receiver', 'is_volume_muted') %}
              off
            {% else %}
              on
            {% endif %}
          {% else %}
            off
          {% endif %}
        icon_template: >-
          {% if is_state('media_player.receiver', 'on') %}
            {% if state_attr('media_player.receiver', 'is_volume_muted') %}
              mdi:lightbulb-off
            {% else %}
              mdi:lightbulb-on
            {% endif %}
          {% else %}
            mdi:lightbulb-off
          {% endif %}
        turn_on:
          service: media_player.volume_mute
          data:
            entity_id: media_player.receiver
            is_volume_muted: false
        turn_off:
          service: media_player.volume_mute
          data:
            entity_id: media_player.receiver
            is_volume_muted: true

Change The Entity Picture

This example shows how to change the entity picture based on the light state.

light:
  - platform: template
    lights:
      theater_volume:
        friendly_name: "Receiver Volume"
        value_template: >-
          {% if is_state('media_player.receiver', 'on') %}
            {% if state_attr('media_player.receiver', 'is_volume_muted') %}
              off
            {% else %}
              on
            {% endif %}
          {% else %}
            off
          {% endif %}
        icon_template: >-
          {% if is_state('media_player.receiver', 'on') %}
            {% if state_attr('media_player.receiver', 'is_volume_muted') %}
              /local/lightbulb-off.png
            {% else %}
              /local/lightbulb-on.png
            {% endif %}
          {% else %}
            /local/lightbulb-off.png
          {% endif %}
        turn_on:
          service: media_player.volume_mute
          data:
            entity_id: media_player.receiver
            is_volume_muted: false
        turn_off:
          service: media_player.volume_mute
          data:
            entity_id: media_player.receiver
            is_volume_muted: true