Time and sunset automation

Hi. I’m looking for a bit of help on an automation I’d like to create. It seems simple but I just cannot figure it out!

I’ve got a light that I’d like to switch on at 5.30 if the sun has set. If the sun has not set at this time the light should switch on when it does.

This is my (not working) code so far:

- alias: Lamp - on in the evening
  trigger:
  - platform: time
    after: '17:30'
  condition:
    condition: and
    conditions:
    - condition: time
      before: '23:59'
    - condition: sun
      after: sunset
  action:
  - service: light.turn_on
    data:
      entity_id: light.lamp

This will turn the light on at 5.30 if the sun has set but it never turns on if the sun sets after 5.30.

Thanks for any help :slight_smile:

1 Like

Put a trigger for sunset in also. That way it will trigger at 5:30 and sunset. If the sun has not set at 5:30 the condition you already have will prevent the light from turning on, however, once the sun sets it will turn on.

Alternatively, switch the logic so that the trigger is sunset, and the condition is after 17:30. You don’t need to check that it’s before midnight, since after sunset infers that (after sunset covers until midnight, before sunrise after midnight).

- alias: Lamp - on in the evening
  trigger:
  - platform: sun
    event: sunset
  condition:
    condition: time
    after: '17:30:00'
  action:
  - service: light.turn_on
    data:
      entity_id: light.lamp

I read his request as on at 5:30 if the sun is already down, if the sun is not down wait until it sets and then turn on the light.

With only the sunset trigger it will only trigger at sunset. Not at 5:30, sunset can be after 9:00 in the summer.

So, light should go on at sunset but not before 5:30?

Thanks for the replies! I just couldn’t get my head around this one but I’m pretty sure having the time and sunset as triggers with a condition to check if the sun has set will work.

Here’s what I have:

- alias: Lamp - on in the evening
  trigger:
  - platform: time
    after: '17:30'
  - platform: sun
    event: sunset
  condition:
  - condition: sun
    after: sunset
  action:
  - service: light.turn_on
    data:
      entity_id: light.lamp

I’ll let you know how it goes.

Thanks again.

I’m having a simular situation with the following automation:

- alias: 'Lights on when home after sunset'
  trigger:
    platform: state
    entity_id: device_tracker.xxxxx
    state: 'home'
  condition:
    condition: sun
    after: sunset
    after_offset: "-0:20:00"
  action:
    - service: light.turn_on
      data:
        entity_id: light.hall_way
        brightness: 125
    - service: light.turn_on
      data:
        entity_id: light.diner_table
        brightness: 125
    - service: light.turn_on
      data:
        entity_id: light.living_room
        brightness: 140
    - service: light.turn_on
      data:
        entity_id: light.PC
        brightness: 240
    - service: switch.turn_on
      entity_id: switch.wake_on_lan    
    - service: tts.google_say
      data_template:
        entity_id: media_player.home_group
        message: 'Welcome Home!'
        cache: false

The automation triggers fine if I arrive home after sunset. What I want is that the automation also triggers when the sun sets if I’m allready home before that time (which it doesn’t at the moment).

Do I have to setup a second automation for this with a sun trigger?

I don’t think that will work. Logically read it says

if (17:30 OR sunset) AND (after sunset) then

So, it will work on sunset (event if it is before 17:30), but not if 17:30h is before sunset. Again, please specifiy what you want to achive.

  1. lights on at sunset, but not before 17:30 OR
  2. lights on at sunset, but latest at 17:30

Regards

Will this work?

- alias: 'Lights on when home after sunset'
  trigger:
    - platform: state
      entity_id: device_tracker.xxxxxx
      state: 'home'
    - platform: sun
      event: sunset
  condition:  
    condition: and
    conditions:
      - condition: sun
        after: sunset
        after_offset: "-0:20:00"
      - condition: state
        entity_id: device_tracker.xxxxxx
        state: 'home'
  action:
    - service: light.turn_on
      data:
        entity_id: light.hall_way
        brightness: 125
    - service: light.turn_on
      data:
        entity_id: light.diner_table
        brightness: 125
    - service: light.turn_on
      data:
        entity_id: light.living_room
        brightness: 140
    - service: light.turn_on
      data:
        entity_id: light.PC
        brightness: 240
    - service: switch.turn_on
      entity_id: switch.wake_on_lan    

What I’m trying to achieve is;

  • When entering home after sunset, run automation.
  • When sun sets if I’m allready home, run automation.

Can’t get the “after_offset” to work so ignore that line (it will never be triggered).I will also not block my automation because 20 min’s before sunset the condition will turn to “true” (right?)

I think that sunset condition won’t work. It won’t be 20 min before sunset when the sun sets and that’s a very specific time to meet when you come home. Otherwise looks good.

@silvrr Thanks, I’ll delete that line and wait at home untill the sun sets to see if it works :wink:

Update:

This should work according to the docs:

  trigger:
    - platform: state
      entity_id: device_tracker.xxxxxx
      state: 'home'
    - platform: sun
      event: sunset
      offset: "-0:20:00"
  condition:  
    condition: and
    conditions:
      - condition: sun
        after: sunset
        after_offset: "-0:20:00"
      - condition: state
        entity_id: device_tracker.xxxxxx
        state: 'home'

https://home-assistant.io/cookbook/automation_sun/

Allright. What am I doing wrong (and what can be more efficient). Since I’ve changed some of the lines (brightness and combining switches and ligts) my HA won’t start anymore

## Ben ik thuis zet dan de lampen op 150% 1,5 uur voor zonsondergang
- action:
  data:
      brightness: 150
      rgb_color: [255,205,115]
      entity_id: light.tradfri_group_4
    service: homeassistant.turn_on
  alias: Thuis na zonsondergang (fase 1)
  condition:
  - condition: state
    entity_id: device_tracker.xxxxxxxxxx
    state: home
  id: '1513284266523'
  trigger:
  - event: sunset
    offset: -01:30:00
    platform: sun
## Ben ik thuis zet dan de lampen op 200% 45 min voor zonsondergang
- action:
  data:
      brightness: 200
      rgb_color: [255,205,115]
      entity_id: light.tradfri_group_4
    service: homeassistant.turn_on
  alias: Thuis na zonsondergang (fase 2)
  condition:
  - condition: state
    entity_id: device_tracker.xxxxxxxxxx
    state: home
  id: '1513284266523'
  trigger:
  - event: sunset
    offset: -00:45:00
    platform: sun
## Ben ik thuis zet dan de lampen op 250%  tijdens voor zonsondergang en zet de ikea lampen aan
- action:
  data:
      brightness: 250
      rgb_color: [255,205,115]
      entity_id:
        - light.tradfri_group_4
        - switch.ikea_lamp_woonkamer
        - switch.ikea_lamp_keuken
    service: homeassistant.turn_on
  alias: Thuis na zonsondergang (fase 3)
  condition:
  - condition: state
    entity_id: device_tracker.xxxxxxxxxx
    state: home
  id: '1513284266523'
  trigger:
  - event: sunset
    platform: sun
## schakel alle lapen uit als ik weg ben
- action:
  - data:
      entity_id: 
        - group.all_lights
        - switch.ikea_lamp_woonkamer
        - switch.ikea_lamp_keuken
    service: homeassistant.turn_off
  alias: Lampen uit als ik weg ben
  condition: []
  id: '1513431122964'
  trigger:
  - entity_id: device_tracker.xxxxxxxxxx
    from: home
    platform: state
    to: not_home
## zet ik de TV aan, zet dan de sonos1 speakers uit
- action:
  - data:
      entity_id: media_player.huiskamer
    service: homeassistant.turn_off
  alias: TV aan = Sonos uit
  condition: []
  id: '1513431292312'
  trigger:
  - entity_id: media_player.tv_woonkamer
    from: 'off'
    platform: state
    to: playing
## Kom ik thuis 1,5uur voor of na zonsondergang, doe dan de lampen op 150%
- action:
  - data:
      data:
      brightness: 150
      rgb_color: [255,205,115]
      entity_id: light.tradfri_group_4
    service: homeassistant.turn_on
  alias: Thuiskomen na zonsondergang
  condition:
  - after: sunset
    after_offset: -01:30:00
    before_offset: -00:45:00
    condition: sun
  id: '1514285419023'
  trigger:
  - entity_id: device_tracker.xxxxxxxxxx
    from: not_home
    platform: state
    to: home
## Kom ik thuis tijdens of na zonsondergang, doe dan de lampen meteen op 250% en de ikea lampen aan
- action:
  data:
      brightness: 250
      rgb_color: [255,205,115]
      entity_id:
        - light.tradfri_group_4
        - switch.ikea_lamp_woonkamer
        - switch.ikea_lamp_keuken
    service: homeassistant.turn_on
  alias: Ikea lamp na 18:00 als ik thuis kom
  condition:
    condition: and
    conditions:
      - condition: sun
        after: sunset
      - condition: time
        before: '23:59'
  id: '1515248500003'
  trigger:
  - entity_id: device_tracker.xxxxxxxxxx
    from: not_home
    platform: state
    to: home

At first glance your indentation doesn’t look right. Yaml is VERY strict on indentation. You may want to review this first. You’ll need trigger, condition and action on the same level to start with…

The indentation is based on the automation that Hass.io made in the first place. I’ve just added some lines to create a flow in brightness based on the sunset.

The problem was - afterwards - the ID’s. They weren’t unique and that caused some problems. It’s fixed now =) Thanks though

Thanks. When I replied I was checking your code on my phone
Guess I won’t do that anymore :wink: