Help with Yeelight automation

Hello,

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

Thanks for the help!

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 are totally right, the above code is meant to run only once when clicking the Aqara switch. I will edit my post to make it more clear.

Thanks!

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.

  trigger:
    - platform: event
      event_type: xiaomi_aqara.click
      event_data:
        entity_id: binary_sensor.switch_xxxxxxxxxx
        click_type: single
  action:
   - service: scene.turn_on
      data_template:
        entity_id: >
          {% if is_state('group.yeelight_lights', 'off') %} 
            scene.yeelight_on
          {% else %}
            scene.yeelight_off
          {% endif %}

Scenes:

- 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

I’m not familiar with the Xiaomi Aqara switch you mentioned but I think I understand what you want.

  • When the switch is set to off, it turns off all four Yeelights (more precisely, turns off only the ones that are on)
  • When the switch is set to on, it turns on just two of the four Yeelights (1 and 3).

Is that how you want it to work?


EDIT
I think vladosam ninja’d me … and with example code! :slight_smile:

1 Like

@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

Whoops! I take back my so-called enhancement! It works opposite to what you want!

No. Why? That was good. Bring it back. :smiley:

Also data_template indentation is wrong.

   - service: scene.turn_on
     data_template:
       entity_id: >

@vladosam can it also be done without a scene? Check the state of each yeelight individually with an OR statement?

Probably. But why if this works?

Also as an educational activity, to familiarize with templates

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.

You can try but that can look ugly. if, and, or and you get lost. :smiley:

He can switch scene names. :smiley: And voila. You really cut that code like a true ninja.

1 Like

Clever! I like it! You’re a true ninja as well. :slight_smile:

I’ll post it here again for your use:

  action:
   - service: scene.turn_on
      data_template:
        entity_id: "scene.yeelight_{{states('group.yeelight_lights')}}"

@henry_kay

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.

1 Like

Here is a single automation (no scenes required) that will achieve the desired goal.

  • It will turn on Yeelight 1 and 3.
  • It will turn off members of group.yeelight_lights but only the ones that are on.

I tested a slightly different version (using my home’s lights) and it works.

- alias: 'Yeelight Control'
  trigger:
    - platform: event
      event_type: xiaomi_aqara.click
      event_data:
        entity_id: binary_sensor.switch_xxxxxxxxxx
        click_type: single
  action:
   - service_template: "{{ 'light.turn_on' if is_state('group.yeelight_lights', 'off') else 'light.turn_off' }}"
     data_template:
       entity_id: >-
         {% if is_state('group.yeelight_lights', 'off') %}
          light.yeelight1, light.yeelight3
         {% else %}
          {{ states | selectattr('entity_id','in', state_attr('group.yeelight_lights', 'entity_id') ) | selectattr('state','eq','on') | map(attribute='entity_id') | join(', ') }}
         {% endif %}

Here’s how the action section works:

  • 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.
1 Like