Blueprint Condition Template Works in Developer Tools but Not in Blueprint

Objective:

We aim to create a blueprint where a single switch controls and manages lights in the following manner:

  1. Switch ON:

    • When toggled ON, the automation dynamically checks the state of the lights.
      • If no lights (primary or secondary) are ON, it turns ON the primary lights.
      • If any primary or secondary lights are already ON, it turns OFF all lights (both primary and secondary).
  2. Auto Reset:

    • After completing the action (turning lights ON or OFF), the switch automatically resets to OFF. (if not there is a relay powered on that I don’t want to)

This approach ensures that the switch always returns to the OFF position, making it easier to manage the next state change. The blueprint should be reusable, flexible, and should not rely on hardcoding specific light entities into the YAML, since I want to do this with multiple rooms.

This blueprint must:

  • Detect the current state of both primary and secondary lights accurately.
  • Use variables or inputs to allow the selection of lights dynamically.
  • Ensure conditions and triggers operate as expected within the automation.

The provided examples highlight what works and what fails. Your guidance on solving this issue is greatly appreciated!

Code Examples

Code That Works in Templates Tool but Not in Automation

{% set primary_lights_entity = expand('light.despacho') %}
{% set secondary_lights_entity = expand('light.pasillo', 'light.flexo_despacho') %}
{% set all_lights = expand(primary_lights_entity | list + secondary_lights_entity | list) %}

# Checking if any light is ON
{{ all_lights | selectattr('state', 'eq', 'on') | list | count > 0 }}

Failing Blueprint Example

blueprint:
  name: Switch-Controlled Lights (Primary and Secondary)
  description: Manage primary and secondary lights using a single switch.
  domain: automation
  input:
    switch:
      name: Switch
      description: Switch to control the lights.
      selector:
        entity:
          domain: switch
    primary_lights:
      name: Primary Lights
      description: Lights to turn ON with the switch.
      selector:
        target:
          entity:
            domain: light
    secondary_lights:
      name: Secondary Lights
      description: Lights to turn OFF with the switch.
      selector:
        target:
          entity:
            domain: light

trigger:
  - trigger: state
    entity_id: !input switch
    from: 'off'
    to: 'on'

variables:
  target_primary_lights: !input primary_lights
  target_secondary_lights: !input secondary_lights
  all_lights: >
    {{ expand(target_primary_lights.entity_id | list + target_secondary_lights.entity_id | list) }}

action:
  - choose:
      - conditions:
          - condition: template
            value_template: >
              {{ all_lights | selectattr('state', 'eq', 'on') | list | count > 0 }}
        sequence:
          - action: light.turn_off
            target:
              entity_id: >
                {{ (expand(target_primary_lights.entity_id) | map(attribute='entity_id') | list) +
                   (expand(target_secondary_lights.entity_id) | map(attribute='entity_id') | list) }}
          - action: switch.turn_off
            target:
              entity_id: !input switch
      - conditions:
          - condition: template
            value_template: >
              {{ all_lights | selectattr('state', 'eq', 'on') | list | count == 0 }}
        sequence:
          - action: light.turn_on
            target: !input primary_lights
          - action: switch.turn_off
            target:
              entity_id: !input switch

Please help us resolve the issue where the automation does not detect the current state of the lights correctly. Any advice or suggestions would be greatly appreciated!

:frowning: :frowning: :frowning: :frowning: :frowning: :frowning: :frowning: :frowning: :frowning: :frowning: :frowning: :frowning: :frowning: :frowning:

Jinja templates can only ever output strings, and HASS can only interpret basic variable types (bool, number, string, list, mapping, etc.). You are attempting to return a list of state objects (the result of the expand) which will never work.

The blueprint can be significantly simplified by changing the target selectors to entity selectors. If using target selectors, the entity id list will only contain the explicitly selected light entities – not any of the lights included in selected labels, areas och devices. While they can be extracted there is no one-command solution to do so, it requires a whole bunch of manual templating…

blueprint:
  name: Switch-Controlled Lights (Primary and Secondary)
  description: Manage primary and secondary lights using a single switch.
  domain: automation
  input:
    switch:
      name: Switch
      description: Switch to control the lights.
      selector:
        entity:
          filter:
            domain: switch
    primary_lights:
      name: Primary Lights
      description: Lights to turn ON with the switch.
      selector:
        entity:
          filter:
            domain: light
        multiple: true
    secondary_lights:
      name: Secondary Lights
      description: Lights to turn OFF with the switch.
      selector:
        entity:
          filter:
            domain: light
        multiple: true
trigger:
  - trigger: state
    entity_id: !input switch
    to: 'on'
variables:
  primary_lights: !input primary_lights
  secondary_lights: !input secondary_lights
action:
  - if: "{{ (primary_lights + secondary_lights) | map('is_state', 'on') | list | count > 0 }}"
    then:
    - action: light.turn_off
      target:
        entity_id: "{{ primary_lights + secondary_lights }}"
    else:
    - action: light.turn_on
      target:
        entity_id: "{{ primary_lights }}"
  - action: switch.turn_off
    target:
      entity_id: !input switch