Script variables to entity_id in conditions

Hi, I thought this would be straightforward, but I’m struggling. I’ve read the docs, but I’m clearly missing something… I want to pass a variable into this script, to template the entity_id in the condition:

alias: Light Toggle
sequence:
  - choose:
      - conditions:
          - condition: state
            entity_id: light.{{room}}
            state: "on"
            alias: If light is on
            enabled: true
        sequence:
          - service: input_select.select_option
            data:
              option: "Off"
            target:
              entity_id: input_select.scene_{{room}}
    default: []
mode: restart
icon: mdi:lightbulb-multiple-outline

But I recieve an error:

Message malformed: Entity light.{{room}} is neither a valid entity ID nor a valid UUID for dictionary value @ data['sequence'][0]['choose'][0]['conditions'][0]['entity_id']

Is it that entity_id's can’t be templated? I ask that as I’m passing variables to build entity_id's in the actions of other scripts without an error:

alias: Lights On Using Default Scene
sequence:
  - service: input_select.select_option
    target:
      entity_id: input_select.scene_{{room}}
    data:
      option: "{{ states('input_select.house_default_lighting')}}"
    alias: Set "input_select.scene_room" to "input_select.house_default_lighting"
mode: restart
icon: mdi:lightbulb-auto

If that is the case (no templating entity_id's in conditions), is there another way to achieve this? Currently, I have 3 scripts per lighting zone (aka: room), and a total of 14 lighting zones. I want to reduce the overhead of static files.

You can only template key values under variables:, data:. target: or service: or in value_template: keys. You can’t just put templates anywhere. e.g.

date:
  message: "{{ room }}"

Or

target:
  entity_id: "{{ 'light.'~room }}"

For your condition use a template condition instead.

- condition: template
  value_template: "{{ is_state('light.'~room,'on') }}"

Oh well, I missed the template condition, and wasted hours of my life :smiley:

This works (for anyone else that’s interested):

alias: Light Toggle
sequence:
  - choose:
      - conditions:
          - condition: template
            value_template: |-
              {% set entity = "light."+room %}
              {{ states(entity) == "on" }}
            alias: If light is on
        sequence:
          - service: input_select.select_option
            data:
              option: "Off"
            target:
              entity_id: input_select.scene_{{room}}
      - conditions:
          - condition: template
            value_template: |-
              {% set entity = "light."+room %}
              {{ states(entity) == "off" }}
        sequence:
          - service: input_select.select_option
            data:
              option: "Off"
            target:
              entity_id: input_select.scene_{{room}}
    default: []
    enabled: true
mode: restart
icon: mdi:lightbulb-multiple-outline

Use ~ instead of + for concatenation. It will save you hours of trouble when it is misinterpreted as addition.

1 Like

Thanks for the tip, and nicely reducing it to a one liner.

1 Like