I am using Home Assistant for about a year now, with basic automation and it is great but i can’t understand how to do the following automation with a Xiaomi Aqara switch and four Yeelights:
When the switch is clicked ->
If any Yeelight is on, turn off all Yeelights
Else
Turn on Yeelight #1 and Yeelight #3
If you make an automation based on the pseudo-code you posted it will run repeatedly and endlessly.
IF any light Yeelight is ON THEN turn it OFF
ELSE
Turn ON Yeelight 1 and 3
The moment it turns on Yeelight 1, it will satisfy the conditions of the IF statement which will turn it OFF. The moment the light is turned OFF it satisfies the ELSE condition which will turn ON Yeelight 1 and 3 and so on and so on.
You can create group and put all 4 yeelights in it. Also make two scenes and in one turn on yeelight 1 and 3 and in other turn off all yeelights. So then in automation check for yeelight group state. Something like this:
In automations.
- name: yeelight_on
entities:
light.yeelight1: on
light.yeelight3: on
- name: yeelight_off
entities:
light.yeelight1: off
light.yeelight2: off
light.yeelight3: off
light.yeelight4: off
@123 Its a single click switch so this click should do a different action based on the current state of the Yeelights. I think that something similar to what @vladosam has posted should be better suited to my case by i am not really familiar with data templates so i have to try it first to understand how it works
Your template uses the opposite state of group.yeelight_lights. If the group is off it calls scene.yeelight_on.
My template may have been shorter but it overlooked to use the opposite state. If the group is off it calls scene.yeelight_off and that’s not what we want here.
Use vladosam’s code as shown in his post. I wouldn’t concern myself with only turning off the Yeelights that are currently on. There are only 4 lights. If they are already off, the automation will simply send 4 needless ‘turn_off’ commands which, in terms of Wi-fi network traffic, is negligible. If you had 40 lights then we would have a case for not sending unnecessary commands.
On the other hand, if you want to do it that way just to learn how it’s done, then it’ll require a different approach.
To determine which service to use, it checks the group’s state. If the group is off, it uses the light.turn_on service. Otherwise it’ll use light.turn_off.
To determine which entity_id to use, it check’s the group’s state. If it’s off it uses Yeelight 1 and 3. Otherwise it uses a template to create a list of all Yeelights that are currently on.