Switch on/off automation on sunset/sunrise and HASS restart

I am struggling with setting up and automation. I want lights to turn on on sunset and off on sunrise. Basically I did that, but the problem starts when I have to restart Home Assistant for a reason. If the lights are off (for example after power outage) and then HA restart on power being back, they don’t turn on (even if it’s after sunset). I added another trigger:

  - platform: homeassistant
    event: start

Now, they are back on HA restart, but… even if it’s before sunset.
What these automations should look like to turn lights on and off on sunset and sunrise including power outages that may occur during these times?

Just duplicate the triggers in the condition.

If I add condition, then it doesn’t start.

- id: '1607988449357'
  alias: Włącz przedłużacz ogrodowy
  description: ''
  trigger:
  - platform: sun
    event: sunset
  - platform: homeassistant
    event: start
  condition:
  - condition: sun
    before: sunrise
    after: sunset
  action:
  - type: turn_on
    device_id: c7de8b3e082fece272aa29649d0d8ece
    entity_id: switch.przedluzacz_ogrodowy
    domain: switch
  mode: single

That’s because you have after sunset and before sunrise. It can’t be both at the same time. Completely remove this line

before: sunrise

I was almost happy, when I switched this config to:

- id: '1608412031414'
  alias: Włącz przedłużacz ogrodowy
  description: ''
  trigger:
  - platform: sun
    event: sunrise
  - platform: homeassistant
    event: start
  condition:
  - condition: sun
    after: sunrise
  action:
  - type: turn_on
    device_id: c7de8b3e082fece272aa29649d0d8ece
    entity_id: switch.przedluzacz_ogrodowy
    domain: switch
  mode: single

As I am after sunset it shouldn’t turn on but it does. Is there something wrong or I don’t understand this process completly?

You’ve put after sunrise instead of after sunset, same in the trigger.

Because I wanted to test if it turns on even it’s not sunrise yet. And it does. Anyway, my present automation looks like below. I am just wondering if it turns off after sunrise, and if I need to add anything like homeassistant start to the off automation.

- id: '1608412031414'
  alias: Włącz przedłużacz ogrodowy
  description: ''
  trigger:
  - platform: sun
    event: sunset
  - platform: homeassistant
    event: start
  condition:
  - condition: sun
    after: sunset
  action:
  - type: turn_on
    device_id: c7de8b3e082fece272aa29649d0d8ece
    entity_id: switch.przedluzacz_ogrodowy
    domain: switch
  mode: single
- id: '1608412541143'
  alias: Wyłącz przedłużacz ogrodowy
  description: ''
  trigger:
  - platform: sun
    event: sunrise
  condition: []
  action:
  - type: turn_off
    device_id: c7de8b3e082fece272aa29649d0d8ece
    entity_id: switch.przedluzacz_ogrodowy
    domain: switch
  mode: single

I don’t really understand why you need the homeassistant start event for either, the likelihood of restarting homeassistant at the exact second the sun crosses the horizon is pretty small, and you’ll notice within about 5 minutes and turn the light on yourself anyway.

I will definitely not do it on purpose, but I live in a place, where power outages, especially during summer, are quite often. But like you said - I will notice that as soon as I wake up, I guess.
Thank you for your time and patience.

1 Like

Similar problem. I also have Hue bridge with motion sensor. I copied a bit the last automation, but it doesn’t work. The idea is to turn lights on when it’s dark (between sunset and sunrise) and a move is detected.Lights are turning on only if there is no condition about sunset and sunrise. Even if I add condition regarding light level it’s ok, but whenever I do the same as did with previous automation (adding sunset and sunrise) it doesn’t.

Yeah, if you’ve enabled it in the same way as you did there, it’s never going to work. It is impossible for it to be both before sunrise and after sunset at the same time.

See the documentation for the sun condition, it explains how to use an OR condition.

Or just use a state condition that the sun.sun entity is below_horizon.

1 Like