Help with an automation with and/or conditions

I’m wanting to add an automation but am struggling to get my head around it properly.

What I want to do it turn on the lounge lights at sunset, but only if me or my wife are at home (any of us, not both), and only if the Input Boolean switch is turned on.

Here’s the code I have at the moment

automation:
  - alias: "Lounge Lights on at Sunset"
    trigger:
      - platform: sun
        event: sunset
    condition:
      condition: and
      conditions:
        - condition: state
          entity_id: input_boolean.lounge_lights_sunset
          state: 'on'
        - condition: or
          conditions:
          - condition: state
            entity_id: 'device_tracker.jonos_iphone'
            state: 'home'
          - condition: state
            entity_id: 'device_tracker.wifes_iphone'
            state: 'home'
    action:
      - service: scene.turn_on
        entity_id: scene.lounge_lights_on

Does this look OK, or does it need some tweaking?

I’ve seen some examples in the docs’ that use ‘group.all_devices’ like this

  • condition: state
    entity_id: group.all_devices
    state: ‘home’

If I were to do this would I have to create a group and add any devices I wanted to it, and if so would that only work when both/all people in the group were home?

The way you have it should work.

You could also create a group like in the example, in which case it would show ‘home’ if any of the devices in it were home.

By default, group.all_devices automatically contains any device_tracker devices.

The group’s state is “home” when one or more devices are home and “away” when all devices are away, so you can use it in any of your automations.

Your automation can work just fine above, but it can also work like below:

  - alias: "Lounge Lights on at Sunset"
    trigger:
      - platform: sun
        event: sunset
    condition:
      condition: and
      conditions:
        - condition: state
          entity_id: input_boolean.lounge_lights_sunset
          state: 'on'
        - condition: state
          entity_id: group.all_devices
          state: 'home'
    action:
      - service: scene.turn_on
        entity_id: scene.lounge_lights_on

Thanks :thumbsup:

Thanks, for clarifying that. This code looks cleaner :slight_smile: (and easier to get my head around)