Variable and if then else in a script does not work

Hi, i have a simple script that turns on entities based on a variable, device_aan_1.
This variable is filled based on a condition of a state of dagtype and fills it with variable devices1 or variable devices2.
This does not work.
When i use devices_aan_1: “{{ devices1 }}” it works well !
Why cant i use an if then else to fill this variable ?

this is a part of a script, when is problem is solved, i will add the second part and problem…

alias: avondtest
description: ""
icon: mdi:home
mode: single
variables:
  devices1:
    - light.lamp_tv_rechts
    - light.lantaarn
    - light.haard
  devices2:
    - light.dressoir
  dagtype: "{{ dagtype if dagtype is defined else states('input_select.dagtypen') }}"
  devices_aan_1: >-
    {% if dagtype in ('normaal','party','alarm') %} "{{ devices1 }}" {% else %}
    "{{ devices2 }}" {% endif %}
sequence:
  - action: light.turn_{{keuze}}
    target:
      entity_id: "{{ devices_aan_1 }}"
    metadata: {}
    data: {}
    enabled: true

Couldn’t you just use iif (immediate if) for this:

What does the trace show?

Remove the double quotes in the definition of devices_aan_1, you are already using a multi-line quote indicator >-… Otherwise what is returned by the template is not your list of entity IDs, but the string “{{ devices1 }}” or “{{ devices2 }}”.

  devices_aan_1: >-
    {% if dagtype in ('normaal','party','alarm') %} {{ devices1 }} {% else %}
    {{ devices2 }} {% endif %}
2 Likes