One automation via GUI for on/off?

I just started over my Home Assistant installation and I’m working on setting up my automations again. I’d like to do it as clean as possible though.

Here is what my automation is for:
My laundry room has a door sensor on it. I want it so that when the door is opened, the lights turn on. When the door is closed, the lights turn off.

Before, I used to have 1 automation for turning the lights on, and 1 automation for turning the lights off.

I like to avoid working in yaml files if possible. Is there a way I can do this via the automation GUI?

If not, is there an easy way to do it via yaml?

EDIT:
I ended up figuring it out. In the “Actions” drop down box, there is a “Choose” method. I was able to set both on/off with a single automation using this “Choose” option.

Here is the code in yaml if anyone wants guidance:

alias: Laundry Room - turn on/off lights when door is opened/closed
description: ''
trigger:
  - type: opened
    platform: device
    device_id: 5e1ee5b4640232b8042b9f057564af9a
    entity_id: binary_sensor.door_sensor_laundry_room
    domain: binary_sensor
  - type: not_opened
    platform: device
    device_id: 5e1ee5b4640232b8042b9f057564af9a
    entity_id: binary_sensor.door_sensor_laundry_room
    domain: binary_sensor
condition: []
action:
  - choose:
      - conditions:
          - type: is_open
            condition: device
            device_id: 5e1ee5b4640232b8042b9f057564af9a
            entity_id: binary_sensor.door_sensor_laundry_room
            domain: binary_sensor
        sequence:
          - type: turn_on
            device_id: b78941393030d23c00e20566f531e625
            entity_id: light.laundry_room
            domain: light
      - conditions:
          - type: is_not_open
            condition: device
            device_id: 5e1ee5b4640232b8042b9f057564af9a
            entity_id: binary_sensor.door_sensor_laundry_room
            domain: binary_sensor
        sequence:
          - type: turn_off
            device_id: b78941393030d23c00e20566f531e625
            entity_id: light.laundry_room
            domain: light
    default: []
mode: single

Using the Automation Editor:

  • Start a new empty automation
  • Click the overflow menu in the upper right hand corner (three vertical dots) and select “Edit in YAML mode”
  • Copy the YAML shown below and paste it over what is already shown in the editor
  • Replace the names of the sample entities (for the door binary_sensor and light) with your own
  • Click the Save button
  • When it displays an “Enable/disable automation” toggle, that means the YAML successfully passed the configuration check and the automation is ready and active (you can exit the Automation Editor).
alias: Light controlled by door
trigger:
  - platform: state
    entity_id: binary_sensor.your_door
    to:
      - 'on'
      - 'off'
condition: []
action:
  - service: 'light.turn_{{ trigger.to_state.state }}'
    target:
      entity_id: light.your_light

NOTE

If you switch the Automation Editor back to UI mode, it will indicate that parts of the automation cannot be displayed in UI mode. That’s because the Automation Editor has limitations and can’t display templates and a few other things “visually”.

Take a look at the choose action. I also recommend using two triggers, one for the door opening, and one for the door closing. Set the close to require the door being closed for 10-30 seconds - that means if you close the door and immediately open it again because you forgot something, the light will still be on.

1 Like

Given your original, modest requirements, that just produces a needlessly longer automation.

True, but as someone who wants to setup automations via the GUI and would like to avoid templates (as I find them confusing), what other option is there?

Home Assistant is definitely a lot more user friendly than it was even a year ago. However, if Home Assistant wants to catch a lot of the less technical people out there, they’ll need to figure out how to do this type of stuff easily and quickly via UI.

1 Like

I think you meant to say “as someone who wants to avoid using templates” because there’s no UI for composing templates (and unlikely to ever be because of the breadth of what Jinja2 templates can do).

If your automation goals remain modest, you’re likely to be fine with the limitations of the Automation Editor exclusively in UI mode and avoiding templates. Good luck!