Turn on lights on sundown if someone is home

Hi
I am pretty new to HA. I am trying to get some of my lights to turn on an hour before the sun sets, if someone is home, or gets home after 16.00. I am using Nmap to track persons, and have put the three phones in a group to separate them from all the other devices connected to the network. The lights should also turn off when nobody is home.
I have created a scene in configuration.yaml for the lights.

For some reason the ligths do not turn on in the afternoon even if someone is home, or when I get home,
The lights turn off when we all leave. It also worked once (that I have tried) when we all came home together at night.
Can someone see what is wrong in my code in automation.yaml; It is the part “Lys ettermiddag”

- alias: Media player paused/stopped
  trigger:
  - platform: state
    entity_id: media_player.tv
    from: playing
    to: paused
  condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  action:
    service: scene.turn_on
    entity_id: scene.livingroom_normal
  id: 98e11ba249e541e2a1f75193f54d1b2b
- alias: Media player playing
  trigger:
  - platform: state
    entity_id: media_player.tv
    to: playing
    from: paused
  condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  action:
    service: scene.turn_on
    entity_id: scene.livingroom_dim
  id: 6237de3eb11c41bba188223c70f92e15
- id: '1546584080845'
  alias: Lys ettermiddag
  trigger:
  - event: sunset
    offset: -01:00:00
    platform: sun
  - entity_id: group.personer
    platform: state
    to: home
  condition:
  - condition: state
    entity_id: group.personer
    state: home
  - after: '16:00:00'
    before: '23:30:00'
    condition: time
  action:
  - data:
      entity_id: scene.lys_ettermiddag
    service: scene.turn_on
- id: '1546626365367'
  alias: 'Alle borte '
  trigger:
  - entity_id: group.personer
    platform: state
    to: not_home
  condition: []
  action:
  - data:
      entity_id: light.alt_lys
    service: light.turn_off
- id: '1546767237569'
  alias: Lys av morgen
  trigger:
  - at: 08:00:00
    platform: time
  condition:
  - condition: state
    entity_id: device_tracker.huawei_mate_10_pro519e61lan
    state: not_home
  - condition: state
    entity_id: device_tracker.lenesiniphonelan
    state: not_home
  action:
  - data:
      entity_id: light.alt_lys
    service: light.turn_off

Can you post your config preserving the formatting? See the blue box at the top of every post.

Better like that? changed it in my original post, As I said I am pretty new to this.

I can add that the result I am after is that the light turn on one hour before sunset if any one of the three devices are home. If everybody are away the lights should not turn on before one of us gets home.

When is sunset in your part of the world now? If it’s before 17:00:00 then the time condition will prevent the action from running (because an hour before sunset would be before 16:00:00.)

I would suggest changing the time condition from:

  - after: '16:00:00'
    before: '23:30:00'
    condition: time

to:

  - condition: sun
    after: sunset
    after_offset: -01:00:00
  - before: '23:30:00'
    condition: time

Ok. Sunset is around 15.45 now. I copied this code from somewhere and thought it would turn on the lights 14.45 or so if someone is home. If not the light would go on if we came home after 16.00
In your suggestion the lights will go on an hour before sunset if one or more of us are home, otherwise when we get home after sunset?

Remember, the trigger part says when the actions should run, and the condition part says if the actions will run (at the time of the trigger.) Your problem was the sunset trigger occurred around 14.45 (an hour before 15.45), but at that time the condition was false (i.e., not after 16.00), which prevented the sunset trigger from causing the actions to run.

The change I suggested will allow the lights to be turned on at an hour before sunset if at that time someone is home, or when someone comes home between an hour before sunset and 23.30.

Thank you. I have made the changes as suggested, and will see if it works this afternoon.
I suspected it could have something to do with using a group as condition, and that all devices had to be home for it to work. But I guess the group will get the “state: home” if only one is home, and therefore fulfill the condition

Yes, that is the default behavior, but a recent change allowed groups to only be on (or home, or…) if all the entities are. FYI, see the all parameter (although, in your case, you should not use it, since you want it to be home when any entity is home.)

Success?

I have a similar automation, works ok but late at night is hit or miss. I can share my code if you are still struggling…

It did work as intended changing the code as suggested by pnbruckner :-). Thank you

1 Like