Automation If Then Else

I’m wondering if someone can help me with a simple Automation based off a button press (both physical and one on the home screen).

I have a Aqara Hub with button. I have a working script to turn everything on and a second working script to turn everything off. I’d like to press the button and if everything is off, turn it on. If everything is on, turn it off. How can I combine both of my scripts to accomplish this?

Here’s the Off script, the On is the same formatting.

alias: Office All Off
sequence:
  - type: turn_off
    device_id: afb0318bf05da14778a86d604b7cd3a9
    entity_id: switch.console_speakers
    domain: switch
  - type: turn_off
    device_id: 3360bdb8f1fb83e8c0a34946904d0ad2
    entity_id: switch.office_strip_lights
    domain: switch
  - type: turn_off
    device_id: 8b83580613ce478a7e3c321a54598e00
    entity_id: switch.office_console_screen_1
    domain: switch
  - type: turn_off
    device_id: b97f066c33a66a42140f81a8976b687c
    entity_id: switch.office_console_monitor_desk
    domain: switch
  - type: turn_off
    device_id: dde4c7f861de6b17bc1e61f0146ccf54
    entity_id: switch.radar_plug
    domain: switch
mode: single

Thank you very much.

You don’t need two scripts, or two automations.

Create a group with all the devices in it.

Call the homeassistant.toggle service when the button is pressed and toggle the group. https://www.home-assistant.io/docs/scripts/service-calls/#homeassistant-services

Awesome. I got that working on the home screen. Under automation actions, what do I specify to toggle my group.office?

I’m trying to do something like this:

alias: Office On/Off Button
description: ''
trigger:
  - platform: device
    device_id: 2edaedee606aa33da0e10acdf8989884
    domain: homekit_controller
    type: button1
    subtype: single_press
condition: []
action:
  action: toggle
  - entity_id: 'group.office'
mode: single
action:
  - servcie: homeassistant.toggle
    entity_id: group.office

Awesome. I’ll try the button when I get home. Thank you very much!

1 Like