Or condition for device trackers in automation

I’m struggling to get my automation to work. The idea is to turn on a room’s lights just before sunset if either of three devices are home. I don’t get any errors in the logs, but the automation just doesn’t trigger. Can anyone spot anything wrong:

- alias: Turn living room lights on at sunset if anyone is home
  trigger:
    platform: sun
    event: sunset
    offset: "-00:15:00"
  condition:
    condition: or
    conditions:
      - condition: state
        entity_id: device_tracker.adams_phone_wifi
        state: 'home'
      - condition: state
        entity_id: device_tracker.adams_phone_bt
        state: 'home'
      - condition: state
        entity_id: device_tracker.catherines_phone_wifi
        state: 'home'

just create a group with the 3 devices, and use one condition (if group state is on).
A the state of a group is already an or condition. It will be cleaner on your automations and you can use this group’s state for every other presence requirement

I notice your offset is defined with double quotes and the example offset is defined with single quotes. I wouldn’t expect that to matter, but when something doesn’t work I always go back to the example for reasons why.

If that doesn’t solve the problem, the next thing to do is remove the conditions. That will tell you if it is a trigger problem or a condition problem.

could you post the full automation (with action part).
Also could you check in dev-state if the status of the automation itself is on ? (you could force it using initial_state)
What happen when you try to trigger it manually using dev-services ?

I have a similair setup: I want my lights to turn on when I get home after sunset, but also if I’m allready home when the sun sets.

- alias: 'Lights on when home after sunset'
  trigger:
    - platform: state
      entity_id: group.all_devices
      from: 'not_home'
      to: 'home'
    - platform: sun
      event: sunset
  condition:  
    condition: and
    conditions:
      - condition: sun
        after: sunset
      - condition: state
        entity_id: group.all_devices
        state: 'home'
  action:
    - service: scene.turn_on
      entity_id: scene.evening

Ah, I wasn’t sure if a group would need all 3 devices to be “home” or not. If I use a group does it act as an “or” on each device?

1 Like

My automation triggers if anyone comes home (anyone who is tracked that is). If you want to only trigger on the mentioned devices, just use multiple entries in your trigger like this:

- alias: 'Lights on when home after sunset'
  trigger:
    - platform: state
      entity_id: 
        - device_tracker.adams_phone_wifi
        - device_tracker.adams_phone_bt
        - device_tracker.catherines_phone_wifi 
      from: 'not_home'
      to: 'home'
    - platform: sun
      event: sunset
  condition:  
    condition: and
    conditions:
      - condition: sun
        after: sunset
      - condition: state
        entity_id: group.all_devices
        state: 'home'
  action:
    - service: scene.turn_on
      entity_id: scene.evening
1 Like

P.S You do need to add an OR statement in your condition to finish it off.

a group works like an OR, if any of your device is home, the group is home.
I track multiple devices, but only 3 are used to determine presence so I have the following group

presenceDetection_devices:
  control: hidden
  name: Presencedetection_devices
  entities:
  - device_tracker.onePlus_thomas
  - device_tracker.J5maud_perso
  - device_tracker.samsungtv

and I simply check the state of this group in conditions.

1 Like

Can you still use an offset with sun condition “after: sunset”? I like having the lights come on before sunset as it gets a little dark in my flat before the “real” sunset

Not sure you can get this done in one automation because you have triggers and conditions switched for both cases.

Case one - as you have it above:

  • trigger: somebody comes home
  • condition: it’s after sunset

Your second case sound like:

  • trigger: the sun sets
  • condition: somebody is home

I’m happy to be corrected here, but creating 2 automations would be my (simple-minded) approach.

Regarding the delay, this is what I use:

- alias: Turn on living room light 13min before sunset if somebody is home
  trigger:
    platform: sun
    event: sunset
    offset: "-00:13:00"
  condition: 
    condition: state
    entity_id: group.people
    state: 'home'
  action:
    service: homeassistant.turn_on
    entity_id: switch.hook_b3

No you can do it in one automation with 2 triggers and two conditions. I have the following that has been working fine for at least two years now

# Living lights management
- alias: 'Turn on living in evening'
  initial_state: True
  trigger:
    - platform: sun
      event: sunset
      offset: "-00:45:00"
    - platform: state
      entity_id: group.presencedetection_devices
      to: 'on'
  condition:
    - condition: sun
      after: sunset
      after_offset: "-02:00:00"
    - condition: state
      entity_id: group.presencedetection_devices
      state: 'home'
    - condition: state
      entity_id: group.smartBulbs
      state: 'off'
  action:
    service: scene.turn_on
    entity_id: scene.regular
1 Like

Hmm I just tried this, at it looks like the automation ran as I can see it was in the dev-state, however the lights never actually came on. Can you see anything wrong with it:

- alias: Turn office lights on at sunset if Adam is home
  trigger:
    - platform: sun
      event: sunset
      offset: "-00:15:00"
    - platform: state
      entity_id: group.adams_devices
      to: 'home'
  condition:  
    condition: and
    conditions:
      - condition: sun
        after: sunset
        after_offset: "-00:15:00"
      - condition: state
        entity_id: group.adams_devices
        state: 'home'
  action:
    - service: light.turn_on
      entity_id: light.desk_lightstrip
    - service: notify.pushbullet
      data:
        message: "Office lights turned on"

If you trigger it manually, what happens?

The lights turn on…

That’s good, it means that the conditions and action are behaving as expected.

So, were you already home 15 minutes before sunset, or did you come home after that? It might help us narrow down which trigger isn’t working.

I was already home. It seemed the automation triggered at the correct point too as it was last run ~15 minutes before sunset. Hmm, I’ve just checked my logs there’s a fair few “cannot reach the bridge” messages for my philips hue bridge so I suspect it’s that? I have been having some weird wifi issues with my pi which could be the problem. For some reason connectivity is very spotty (things like I can ssh in but it’ll drop out randomly or the typing becomes slow, and my samba share on Windows can’t connect to the pi) on the pi without having changed anything (when I first got it a few weeks ago the wifi connection was brilliant), is there something Home Assistant could be doing to cause slow downs?

WiFi isn’t the most reliable way of networking anything, and the antenna for the Pi is pretty small.

If you can, I’d highly advise adding a network cable to a switch. It should help significantly.

Yeah, i tried using an ethernet cable earlier to help but I could never get it to connect.

What’s annoying is that the wifi connection has been super stable up until maybe a couple of days ago, so all my automations have been working really well.

I’m tempted to try nuking the pi and starting fresh to see if that helps, because I haven’t moved the pi and the wifi has suddenly gone, might also be able to get ethernet working then!

Sudden WiFi issues usually point to interference (could be because you’re started using ZigBee, could be a neighbour).

Getting wired networking going is usually a lot easier than WiFi. Generally it’ll just be a case of connecting a cable and rebooting. You’ll then need to disable the WiFi to avoid issues.