Please help xiaomi wireless button to control two lights config (already resolved)

Hello everyone what i want to do with xiaomi wireless button is


one click : Light_A > ON, another one click, Light_A > off ( already ok in the code)
double click : Light_B > ON, another one click, Light_B > off ( already ok in the code)
long click :

  1. if Light_A or Light _B is on , then the button will turn off Light_A or Light _B
  2. if both Light_A and Light _B is on, then will turn off both Light_A and Light _B
  3. if both Light_A and Light_B is off, then will turn on both Light_A and Light _B

One click and double click already can success in the code , I want to add one more “if “ after long click. Can someone help me to edit the code (shout be the code formation issue) or provide some advice?

My automation :

  • alias: Toggle kitchen light on single press
    trigger:
    • platform: event
    • event_type: xiaomi_aqara.click
    • event_data:
    • entity_id: binary_sensor.switch_158d00085022537

Create a light group with both those lights using this:

This will create a separate light entity which should be on when one or both lights are on, and off when both lights are off.

Then create an automation that toggles that new light entity when the long click is triggered.

Something like this:

#put in config.yaml
light:
  - platform: group
    name: Both Lights
    entities:
      - light.light_a
      - light.light_b

#put in automations.yaml
- alias: Both lights
  trigger:
    platform: event
    event_type: xiaomi_aqara.click
    event_data:
      entity_id: binary_sensor.switch_158d00085022537
      click_type: long_click_press
action:
    service: light.toggle
    entity_id: light.both_lights

Omg…you are so clever@@,

But when i go check the webside one min ago.
————
It said :

SERVICE LIGHT.TOGGLE

Toggles the state of one or multiple lights using groups. Takes the same arguments as turn_on service.

Note : If light.toggle is used for a group of lights, it will toggle the individual state of each light.
————
I also will try first when i back home . And I also want to know , homeassistant automation only allow one ‘if’? Can not input the second if under the frist ‘if’ just like c++?

Omg…you are so clever@@,

But when i go check the webside one min ago.
————
It said :

SERVICE LIGHT.TOGGLE

Toggles the state of one or multiple lights using groups. Takes the same arguments as turn_on service.

Note : If light.toggle is used for a group of lights, it will toggle the individual state of each light.
————
I also will try first when i back home . And I also want to know , homeassistant automation only allow one ‘if’? Can not input the second if under the frist ‘if’ just like c++?

!!! success…

thanks a lot.!! the code already bothered me for a few days…

and i wanna ask , your way is to create a light.group to finish it, let homeassistant think this group is a light to make it happen? so we can definition different to different things? if it is a switch we can also change it to a light? can i delete the old object (F_switch) in the data once i changed (F_switch) to a light( F_light)?

and also how can i change the entity id for example : binary_sensor.motion_sensor_158d00097ka309 to binary_sensor.motion_sensor_a?
it will more easy to write the code.

many thanks again.!!