Hello everyone,
I am working on a Home Assistant automation project that aims to control living room lights using a Hue button. The automation is designed to increase and decrease brightness, as well as toggle the lights’ on/off state. However, I am encountering a synchronization issue with the current state of the lights, leading to unintended behaviors.
Context
The setup includes an automation that responds to short presses on different areas of a Hue button. The intended actions are to increase brightness, decrease it, or toggle the state of the lights (on/off). The problem arises when toggling between on and off states.
Problem
The lights in my setup do not always maintain synchronization in their current states, particularly when I attempt to control them collectively using a single command. While it is expected and normal for some lights to be on and others to be off during certain scenes or settings, the issue arises when I use the Hue button to simultaneously turn all lights in the group on or off. Instead of uniformly responding to the command, some lights that were previously off may turn on, and vice versa, leading to inconsistent states across the group. This desynchronization affects the intended uniformity of the lighting environment when a single action—either turning all lights on or all off—is expected.
Here’s an example of what I tried to do to:
action:
- choose:
- conditions:
- condition: trigger
id: button_on_off
sequence:
- choose:
- conditions:
- condition: template
value_template: >-
{{expand(state_attr('input.light_entity', 'entity_id')) | selectattr('state','eq','on') | map(attribute='entity_id') | list | count == 0}}
sequence:
- service: light.turn_on
target: !input light_entity
data:
kelvin: !input color_temperature
brightness: 255
transition: 1
- conditions:
- condition: template
value_template: >-
{{expand(state_attr('input.light_entity', 'entity_id')) | selectattr('state','eq','on') | map(attribute='entity_id') | list | count > 0}}
sequence:
- service: light.turn_off
target: !input light_entity
Here are the inputs :
input:
hue_button_device:
name: Hue remote
description: Three button Hue remote
selector:
device:
integration: hue
entity:
domain: sensor
light_entity:
name: Light
description: Lights to control
selector:
target:
entity:
domain: light
Specific Issue
The problem with the current template is that during execution, it always selects the first branch ({{expand(state_attr('input.light_entity', 'entity_id')) | selectattr('state','eq','on') | map(attribute='entity_id') | list | count == 0}}
), even if all the lights are already on. Seems like the condition is always met because of a empty list.
What I’m Looking For
I am seeking assistance with developing a Jinja2 template that can accurately and dynamically count the number of lights in a specified ‘on’ state from a group of lights provided in the blueprint input !input light_entity
. The goal is to enable effective synchronization of the lights’ states in response to interactions with the Hue button. This template should efficiently assess the active states to ensure that the automation reacts correctly, turning all lights on or off based on their collective state.
I am particularly interested in solutions or suggestions on how to effectively use Jinja2 templates to filter and transform these data to manage the light states reliably.
Any help or guidance would be greatly appreciated to overcome this challenge.
Thank you in advance for your support and suggestions.