Away after sunset

Just wondering if anyone knows the answer to this question. If I want to automate turning on my outside lights after sunset but only if someone is away.

Currently I have 2 automations.

  1. triggers at sunset - condition checks to see if anyone is not_home. If someone is not_home then it turns on the lights. This happens when the wife is at work, she goes in before dark but is out till after sunset.

  2. if someone leaves after sunset the lights turn on.

So the question is do i need the first rule. Will home assistant regularly retrigger the “away” state of a person when their location is updated? Or is having the 2 automations a good way to go about it.

Thanks!

No, an automation will only trigger when the trigger becomes true. If the trigger is already true it won’t trigger again.

However, you can have multiple triggers, so:

trigger:
- platform: state
  entity_id: 
  - person.one
  - person.two
  to: 'not_home'
- platform: state
  entity_id: sun.sun
  to: 'below_horizon'
condition:
- condition: state
  entity_id: sun.sun
  state: 'below_horizon'
- condition: or
  conditions:
  - condition: state
    entity_id: person.one
    state: 'not_home'
  - condition: state
    entity_id: person.two
    state: 'not_home'

You could streamline that by using a group with the all option set to true:

group:
  my_people:
    name: "My people"
    all: true
    entities:
    - person.one
    - person.two

automation:
- alias: "Turn the lights on if somebody is away at sunset"
  trigger:
  - platform: state
    entity_id: group.my_people
    to: 'not_home'
  - platform: state
    entity_id: sun.sun
    to: 'below_horizon'
  condition:
  - condition: state
    entity_id: sun.sun
    state: 'below_horizon'
  - condition: state
    entity_id: group.my_people
    state: 'not_home'
2 Likes

ok cool. so I did have the concept right with that the change has to happen after sunset.

I am going to play with the automation today and make adjustments per your suggestions. I would rather not have more automations the necessary so streamlining will be ideal.

Thanks for the response!

trigger:
- platform: state
  entity_id: 
  - person.one
  - person.two
- platform: state
  entity_id: sun.sun
action:
- service: "light.turn_{{ 'on' if is_state('sun.sun', 'below_horizon') and (states(person.one) or states(person.two)) == 'not_home' else 'off'}}"
  entity_id: light.your_light
1 Like

If i am reading this correct, this triggers any time there is a state change to person1 or person2 or a state change to the sun state.

Then runs some conditional checks in the service.

Is that correct?

that’s exactly it

Be aware that this will also trigger every time any of those three entities have a change of attribute, which means that every few minutes the light will be being turned on, or off

Regarding what @Tinkerer pointer out

trigger:
- platform: state
  entity_id: 
  - person.one
  - person.two
  - sun.sun
condition:
- "{{trigger.from_state.state != trigger.to_state.state}}"
action:
- service: >
    {% if is_state('sun.sun', 'below_horizon') and (states(person.one) or states(person.two)) != 'home' and is_state('light.your_light', 'off')%}
      light.turn_on
    {% elif (is_state('sun.sun', 'above_horizon') or (states(person.one) and states(person.two)) == 'home') and is_state('light.your_light', 'on') %}
      light.turn_off
    {% endif %}
  entity_id: light.your_light

Not sure if the action section could work like this:

action:
- service: >
    {% if is_state('sun.sun', 'below_horizon') and (states(person.one) or states(person.two)) != 'home' and is_state('light.your_light', 'off')%}
      light.turn_on
    {% elif is_state('light.your_light', 'on') %}
      light.turn_off
    {% endif %}
  entity_id: light.your_light

This is what I love about HA, no matter how simple something is you can always make it more complicated :joy:

group:
  my_people:
    name: "My people"
    all: true
    entities:
    - person.one
    - person.two

automation:
- alias: "Turn the lights on if somebody is away at sunset"
  trigger:
  - platform: state
    entity_id: group.my_people
    to: 'not_home'
  - platform: state
    entity_id: sun.sun
    to: 'below_horizon'
  condition:
  - condition: state
    entity_id: sun.sun
    state: 'below_horizon'
  - condition: state
    entity_id: group.my_people
    state: 'not_home'
  - condition: state
    entity_id: light.your_light
    state: 'off'
action:
  - service: light.turn_on
    target:
      entity_id: light.your_light

That will turn on the light if somebody is away at sunset, or leaves after sunset. If the light is already on nothing will happen. It also won’t turn off the light the moment somebody arrives.

Your approach has merits, but in this case they don’t necessarily help here.

1 Like

I thought that was what he wanted ^^. If he doesn’t want that, this one will yield an error when conditions don’t match but just disregard it.

trigger:
- platform: state
  entity_id: 
  - person.one
  - person.two
  - sun.sun
action:
- service: >
    {% if is_state('sun.sun', 'below_horizon') and (states(person.one) or states(person.two)) != 'home' and is_state('light.your_light', 'off')%}
      light.turn_on
    {% endif %}
  entity_id: light.your_light

isn’t this what @brunkj posted in his first post? Did I misunderstand something?

Don’t want it to turn off right when someone gets home. May do something with X minutes later, but for now, just more interested in making sure the light is on when someone is arriving. Where I live, somewhat out in the country, there are no lights, so it’s weirdly dark. Nice to have some lights on.

Now i have to play with all these suggestions, not just to see what works best for me, but to see how the different ideas work :slight_smile:

That one will spit errors when people are home or the sun is above the horizon.

yeah you’re right