Template light: Error when using variables

I get the following error message when trying to use variables in a light template:

Invalid config for [light.template]: expected dictionary for dictionary value @ data[‘lights’][‘sofa_temp’]. Got '“{% set light_entity = “light.lampe_sofa” %}” “{% set brightness_entity = “input_number.brightness_lampe_sofa” %}” “{% set brightness = states(brightness_entity)|int %}” friendly_name: “Lampe ved sofaen template” value_template: “{{ states(light_entity) }}” level_template: “{{ state_attr(light_entity,'brightness')|int }}” effect_list_template: “{{ state_attr(light_entity, 'effect_list') }}” availability_template: “{{ states(light_entity) in ['on', 'off']}}” turn_on:\n service: light… (See ?, line ?).

Here’s my template code:

light:
  - platform: template
    lights:
      sofa_temp: >-
        {% set light_entity = "light.lampe_sofa" %}
        {% set brightness_entity = "input_number.brightness_lampe_sofa" %}
        {% set brightness = states(brightness_entity)|int %}
        friendly_name: "Lampe ved sofaen template"
        value_template: "{{ states(light_entity) }}"
        level_template: "{{ state_attr(light_entity,'brightness')|int }}"
        availability_template: "{{ states(light_entity) in ['on', 'off']}}"
        turn_on:
          service: light.turn_on
          data:
            brightness: "{{ brightness }}"
        turn_off:
          service: light.turn_off

The same template without variables doesn’t give an error message:

light:
  - platform: template
    lights:
      sofa_temp:
        friendly_name: "Lampe ved sofaen template"
        value_template: "{{ states('light.lampe_sofa') }}"
        level_template: "{{ state_attr('light.lampe_sofa','brightness')|int }}"
        availability_template: "{{ states('light.lampe_sofa') in ['on', 'off']}}"
        turn_on:
          service: light.turn_on
          data:
            brightness: "{{ states('input_number.brightness_lampe_sofa')|int }}"
        turn_off:
          service: light.turn_off

Using variables in a template light configuration like that is not a thing… which is why it’s not shown in the docs.

1 Like