AND with Nested OR

Not sure why the conditions are not working. I want the lights to come on at sunset (if my wife or I are home) or when one of gets home, given it is after sunset. I know I want the sun to be below the horizon and one of us (my wife or I) to be home. But the action is not firing, which makes me think my AND is always returning a false statement.

  - alias: Turn on Bedroom Lamp/Stairs Lamp if one of us is home and after sunset.
    trigger:
      - platform: sun
        event: sunset
      - platform: state
        entity_id: device_tracker.wilsonama_amandasphone, device_tracker.wilsonbry_bryansphone
        to: 'home'
    condition:
      condition: and
      conditions:
      - condition: state
        entity_id: sun.sun
        state: 'below_horizon'
      - condition: or
        conditions:
        - condition: state
          entity_id: device_tracker.wilsonama_amandasphone
          state: 'home'
        - condition: state
          entity_id: device_tracker.wilsonbry_bryansphone
          state: 'home'
    action:
      service: switch.turn_on
      entity_id: switch.tplink_sp_04, switch.tplink_sp_01

Thanks for all the help!

Create a group for your device_tracker’s then change

- platform: state
        entity_id: device_tracker.wilsonama_amandasphone, device_tracker.wilsonbry_bryansphone

to

- platform: state
        entity_id: group.<name of your group>

What happens If I am home and my wife is not. What will the state of the group be?

The Group is treated as a logical OR, so as long as one of you is home, the state will be home.

1 Like

Would this do the same thing?

  - platform: state
    entity_id: device_tracker.wilsonama_amandasphone
    to: 'home'
  - platform: state
    entity_id: device_tracker.wilsonbry_bryansphone
    to: 'home'

What if my wife and I are not home and at different locations (i.e. my work/her work), what would the state of the group be then?

If both of you are not home, it should show up as not_home.

I’m not really sure, but when I tried to do the same thing about 2 years ago, creating a group made it much cleaner and easier to maintain, since I have four devices.

You can do this without restarting HA, just create the group, modify the automation, then reload groups and automations.

1 Like

I agree with @dap35 that a group will make this easier. (Actually, if there are only these two device_trackers, then there is already a group you can use – group.all_devices.) However, having said that, I see no reason why the automation shouldn’t work. Are you sure the automation is turned on?

BTW, the default for a list of conditions is AND. So you could simplify your automation to:

  - alias: Turn on Bedroom Lamp/Stairs Lamp if one of us is home and after sunset.
    trigger:
      - platform: sun
        event: sunset
      - platform: state
        entity_id: device_tracker.wilsonama_amandasphone, device_tracker.wilsonbry_bryansphone
        to: 'home'
    condition:
      - condition: state
        entity_id: sun.sun
        state: 'below_horizon'
      - condition: or
        conditions:
        - condition: state
          entity_id: device_tracker.wilsonama_amandasphone
          state: 'home'
        - condition: state
          entity_id: device_tracker.wilsonbry_bryansphone
          state: 'home'
    action:
      service: switch.turn_on
      entity_id: switch.tplink_sp_04, switch.tplink_sp_01

So I figured out why it was not working (at least it works now.). My thought is that when the sunset event is triggered the elevation of the sun is 0. I assume that below_horizon means < 0 and the condition is not meet. Below is my remedy:

  - alias: Turn on Bedroom Lamp/Stairs Lamp if one of us is home and after sunset.
    trigger:
      - platform: sun
        event: sunset
        offset: "00:05:00"
      - platform: state
        entity_id: device_tracker.wilsonama_amandasphone
        to: 'home'
      - platform: state
        entity_id: device_tracker.wilsonbry_bryansphone
        to: 'home'
    condition:
      condition: and
      conditions:
      - condition: state
        entity_id: sun.sun
        state: 'below_horizon'
      - condition: or
        conditions:
        - condition: state
          entity_id: device_tracker.wilsonama_amandasphone
          state: 'home'
        - condition: state
          entity_id: device_tracker.wilsonbry_bryansphone
          state: 'home'
    action:
      service: switch.turn_on
      entity_id: switch.tplink_sp_04, switch.tplink_sp_01