Template does not trigger the condition

I have a problem with a template. In the events it works correctly and gives me true and false. In the automation, however, the condition always remains true. Do you have any tips?

{% set scenes = [
        'scene.wohnzimmer_naturliches_licht',
        'scene.wohnzimmer_herzliche_umarmung',
        'scene.wohnzimmer_glanz_und_glamour',
        'scene.wohnzimmer_disturbia',
        'scene.wohnzimmer_suzuka',
        'scene.wohnzimmer_lesen',
        'scene.wohnzimmer_nachtlicht',
        'scene.wohnzimmer_memento',
        'scene.wohnzimmer_witching_hour',
        'scene.wohnzimmer_ruhephase',
        'scene.wohnzimmer_konzentrieren',
        'scene.wohnzimmer_energie_tanken',
        'scene.wohnzimmer_entspannen',
        'scene.sonos_dashboard'
      ] %}
      {% set scene_aktiv = false %}
      {% for scene in scenes %}
        {% if (as_timestamp(now()) - as_timestamp(states[scene].last_changed)) < 600 %}
          {% set scene_aktiv = true %}
        {% endif %}
      {% endfor %}
      {{ scene_aktiv == false }}  # Gibt True zurĂźck, wenn keine Szene aktiv ist

That template cannot possibly give the results you are expecting due to how Jinja variable scope works. Whatever variables you change inside the loop will not be visible to the outside. The way around this is to use a namespace(), see “Scoping Behavior” in the Jinja documentation.

https://jinja.palletsprojects.com/en/stable/templates/#assignments

{% set scenes = [
        'scene.wohnzimmer_naturliches_licht',
        'scene.wohnzimmer_herzliche_umarmung',
        'scene.wohnzimmer_glanz_und_glamour',
        'scene.wohnzimmer_disturbia',
        'scene.wohnzimmer_suzuka',
        'scene.wohnzimmer_lesen',
        'scene.wohnzimmer_nachtlicht',
        'scene.wohnzimmer_memento',
        'scene.wohnzimmer_witching_hour',
        'scene.wohnzimmer_ruhephase',
        'scene.wohnzimmer_konzentrieren',
        'scene.wohnzimmer_energie_tanken',
        'scene.wohnzimmer_entspannen',
        'scene.sonos_dashboard' ] %}
{{ scenes | expand
| selectattr('last_changed', 'gt', (now() - timedelta(minutes=10)))
| list | count > 0}}

EDIT: Included OP’s scenes set statement. I’m sorry my shorthand caused confusion.

1 Like

I entered the template in the events. It only gives me false. But the condition is never fulfilled in the automation.

Ergebnis

UndefinedError: 'scenes' is undefined
false
Ergebnistyp: string
Dieses Template abonniert die folgenden Ereignisse zur Zustandsänderung:
Entität: light.hue_play_rl
Entität: scene.wohnzimmer_naturliches_licht

I have this when I activate a scene. Unfortunately, it doesn’t trigger the natural light scene because Hue then controls individual light scenes.

You need to combine the first part of your original template, that’s the part that defines the scenes variable containing a list of scenes, with the template Didgeridrew suggested.

Without your initial definition of the scenes variable, the result of Didgeridrew’s template will be the error message you received.

Post the complete automation showing where you have added the template.

alias: Wohnzimmer Lichtsteuerung
triggers:
  - entity_id: binary_sensor.wohnzimmer_sensor_motion
    to: "on"
    trigger: state
  - entity_id: light.wohnzimmer
    from: "on"
    to: "off"
    id: light_off_by_user
    trigger: state
    enabled: false
conditions:
  - condition: numeric_state
    entity_id: sensor.wohnzimmer_sensor_illuminance
    below: 35
  - condition: template
    value_template: |
      {{ scenes | expand
      | selectattr('last_changed', 'gt', (now() - timedelta(minutes=10)))
      | list | count > 0}}
    enabled: true
actions:
  - choose:
      - conditions:
          - condition: time
            after: "05:00:00"
            before: "10:00:00"
        sequence:
          - target:
              entity_id: light.wohnzimmer
            data:
              brightness: 80
              color_temp: 350
            action: light.turn_on
      - conditions:
          - condition: time
            after: "10:00:00"
            before: "18:00:00"
          - condition: numeric_state
            entity_id: sensor.wohnzimmer_sensor_illuminance
            below: 50
        sequence:
          - target:
              entity_id: light.wohnzimmer
            data:
              brightness: 60
              color_temp: 400
            action: light.turn_on
      - conditions:
          - condition: time
            after: "18:00:00"
            before: "23:00:00"
        sequence:
          - target:
              entity_id: light.wohnzimmer
            data:
              brightness: 50
              color_temp: 300
            action: light.turn_on
      - conditions:
          - condition: time
            after: "23:00:00"
            before: "05:00:00"
        sequence:
          - target:
              entity_id: light.wohnzimmer
            data:
              brightness: 20
              color_temp: 450
            action: light.turn_on
  - delay: "00:15:00"
  - target:
      entity_id: light.wohnzimmer
    action: light.turn_off
    data: {}
mode: restart

I created the automation using chat gpt. I had now entered the template from here

thank you

The Template Condition in your automation contains the mistake I described in my previous post.

You need to combine the first part of your original template, the part that defines the scenes variable, with the template Didgeridrew gave you.

    - condition: template
      value_template: | 
        {% set scenes = [
          'scene.wohnzimmer_naturliches_licht',
          'scene.wohnzimmer_herzliche_umarmung',
          'scene.wohnzimmer_glanz_und_glamour',
          'scene.wohnzimmer_disturbia',
          'scene.wohnzimmer_suzuka',
          'scene.wohnzimmer_lesen',
          'scene.wohnzimmer_nachtlicht',
          'scene.wohnzimmer_memento',
          'scene.wohnzimmer_witching_hour',
          'scene.wohnzimmer_ruhephase',
          'scene.wohnzimmer_konzentrieren',
          'scene.wohnzimmer_energie_tanken',
          'scene.wohnzimmer_entspannen',
          'scene.sonos_dashboard'
          ] %}
        {{ scenes | expand | selectattr('last_changed', 'gt', (now() - timedelta(minutes=10))) | list | count > 0 }}
alias: Wohnzimmer Lichtsteuerung
triggers:
  - entity_id: binary_sensor.wohnzimmer_sensor_motion
    to: "on"
    trigger: state
  - entity_id: light.wohnzimmer
    from: "on"
    to: "off"
    id: light_off_by_user
    trigger: state
    enabled: false
conditions:
  - condition: numeric_state
    entity_id: sensor.wohnzimmer_sensor_illuminance
    below: 35
  - condition: template
    value_template: |-
      {% set scenes = [
              'scene.wohnzimmer_naturliches_licht',
              'scene.wohnzimmer_herzliche_umarmung',
              'scene.wohnzimmer_glanz_und_glamour',
              'scene.wohnzimmer_disturbia',
              'scene.wohnzimmer_suzuka',
              'scene.wohnzimmer_lesen',
              'scene.wohnzimmer_nachtlicht',
              'scene.wohnzimmer_memento',
              'scene.wohnzimmer_witching_hour',
              'scene.wohnzimmer_ruhephase',
              'scene.wohnzimmer_konzentrieren',
              'scene.wohnzimmer_energie_tanken',
              'scene.wohnzimmer_entspannen',
              'scene.sonos_dashboard' ] %}
      {{ scenes | expand
      | selectattr('last_changed', 'gt', (now() - timedelta(minutes=10)))
      | list | count > 0}}
    enabled: true
actions:
  - choose:
      - conditions:
          - condition: time
            after: "05:00:00"
            before: "10:00:00"
        sequence:
          - target:
              entity_id: light.wohnzimmer
            data:
              brightness: 80
              color_temp: 350
            action: light.turn_on
      - conditions:
          - condition: time
            after: "10:00:00"
            before: "18:00:00"
          - condition: numeric_state
            entity_id: sensor.wohnzimmer_sensor_illuminance
            below: 50
        sequence:
          - target:
              entity_id: light.wohnzimmer
            data:
              brightness: 60
              color_temp: 400
            action: light.turn_on
      - conditions:
          - condition: time
            after: "18:00:00"
            before: "23:00:00"
        sequence:
          - target:
              entity_id: light.wohnzimmer
            data:
              brightness: 50
              color_temp: 300
            action: light.turn_on
      - conditions:
          - condition: time
            after: "23:00:00"
            before: "05:00:00"
        sequence:
          - target:
              entity_id: light.wohnzimmer
            data:
              brightness: 20
              color_temp: 450
            action: light.turn_on
  - delay: "00:15:00"
  - target:
      entity_id: light.wohnzimmer
    action: light.turn_off
    data: {}
mode: restart

I have now inserted the whole thing. Everything works in the events, but in the automation the condition always remains fulfilled, even though it goes to false.where is my mistake in thinking?

thank you

I don’t understand that statement.

What do you mean the condition “remains fulfilled” but reports “false”?

When you test that template in the Template Editor, does it report True or False?

So when I test the template in the template editor, the scenes are set to false, as it should be. But if I use the whole thing as a condition in the AM, it always shows me that the condition is met, even though a scene is active. The motion detector should not change the light if I activate a scene manually. The motion sensor should only be active again when the lights are turned off by the motion sensor, app or switch.

What exactly is showing the condition is met?

If you mean you’re in the Automation Editor in Visual mode and using the Test button to test the Template Condition, then I suggest you ignore what it reports. It merely checks if the Template Condition is functional.

I check this in editor mode directly in the AM under the conditions. There it always shows me that the condition is met. If I now activate a scene, the motion sensor immediately switches the light back to the standard values.