Turn all lights on when sun set and first to arrive

It seems like a common automation and thus solved by quite a few people in this forum already - but apparently none of the solutions corresponds to what I try to achieve:

Trigger:

  • someone comes home

Condition:

  • sun is set
  • no one is home yet

Action:

  • turn all lights on

Seems fairly straight forward and I came up with the following - except it doesnt work:

- alias: Turn all lights on when sun set and arriving
  id: turn_on_lights_arriving
  trigger:
  - entity_id: device_tracker.PersonA
    from: away
    platform: state
    to: home
  - entity_id: device_tracker.PersonB
    from: away
    platform: state
    to: home
  condition:
  - condition: sun
    after: sunset
    after_offset: "0:30:00"
  - condition: or
    conditions:
    - condition: state
      entity_id: device_tracker.PersonA
      state: away
    - condition: state
      entity_id: device_tracker.PersonB
      state: away
  action:
  - data:
      entity_id: group.all_lights
    entity_id: group.all_lights
    service: homeassistant.turn_on

Any hints on what I did wrong or how I could debug this (I tried by setting the device_tracker’s and the sun’s state manually but without any effect)?

For detecting whether ‘anyone’ is home we use groups. Homeassistant automatically creates group.all_devices for the device tracker group. This group will show home if one or more people are home and away if all devices are away.

So using the state of that group alone is all you need to establish if the person coming home is arriving to an empty house.

(this presumes you’re not using a device tracker platform to ping something that is always at home, if that’s the case you create a group with the ones you want to use for presence detection and use that instead).

As for the rest of your code, the offset needed to know which way to go with a + / -, and you were calling the entity_id twice in the service of the action, which will probably error.

Cleaning it up looks like this…

- alias: Turn all lights on when sun set and arriving
  id: turn_on_lights_arriving
  trigger:
    platform: state
    entity_id: group.all_devices
    to: 'home'
  condition:
    condition: sun
    after: sunset
    after_offset: "-00:30:00"
  action:
    service: homeassistant.turn_on
    entity_id: group.all_lights
3 Likes

Thanks a lot. I was aware of the group feature but since I use the device tracker not only for “humans” (and their phones) put also to see if certain devices (sensors) are still online I didn’t use it. But it just crossed my mind that I can create and use a group group.all_phones instead.

Will test it tonight as I figured just out that the sun cannot be overwritten via the interface.

Thanks again!

1 Like

You can change the after_offset and test it any time you want

2 Likes