Objective:
We aim to create a blueprint where a single switch controls and manages lights in the following manner:
-
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).
- When toggled ON, the automation dynamically checks the state of the lights.
-
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!