Automation doesn't turn on switched after restart?

Just in the beginning process of setting up my system and I’ve come to Automation, when i arrived home today the window lights were lit, so it seems i got that right.

I added a second automation to start the interior lights with no delay.

To test it out i turned off all lights manually and restarted the server, expecting all the lights to come on, but no. Dark as a dungeon in here.

Don’t be fooled by the thread time stamp, it was prior to 22:00:00 so the mood lights should’ve come on.

- id: window_lights_on 
  alias: "Turn on window lights"
  trigger:
    platform: sun
    event: sunset
    offset: '-00:45:00'
  condition:
    condition: time
    before: "21:00:00"
  action:
    service: switch.turn_on
    entity_id:
      - group.fonster
      
- id: punkt_lights_on 
  alias: "Turn on mood light"
  trigger:
    platform: sun
    event: sunset
    offset: '00:00:00'
  condition:
    condition: time
    before: "22:00:00"
  action:
    service: switch.turn_on
    entity_id:
      - group.punktbelysning

I assume that the sun had set already when you restated HA.

This means that the trigger event ‘sunset’ was never invoked because the sun was already down.

You could run a test that makes use the offset you have already in there - look up when the sunset was for today, add the delta from that time to now to the offset in the trigger (plus another 5ish minutes, maybe), restart your server and see what happens within the next 5ish minutes.

I was under the impression that with my statement it would mean:
Sun down AND before 22:00 → turn on.

If the power cuts out before sunset and comes back on after sunset i want my system to go to a fixed state.

I could try your proposal, but it wouldn’t really get me to where i want.

You have to look at it from this point:
A trigger kicks off the action at only one point in time - in your case that is exactly at sunset.

If you reboot the system after sunset there is no trigger, the trigger event has come and gone.
The system will not evaluate what should have happened in the past when the sun set.

For your case - power failure and desired state afterwards - I would recommend an additional automation that’s triggered at the start of homeassistant, checks if it’s after sunset and turns on the lights based on that.

Here’s an example that I use - it has to do with the fact that my cheap etekcity switches do not report their current state, so I inquire if the printer is online when HA starts and set the status of the switch accordingly for my UI:

# Office Printer
- alias: Switch Check for Office Printer
  trigger:
    platform: homeassistant
    event: start
  condition: 
    condition: state
    entity_id: device_tracker.brother_mfc7840w
    state: 'home'
  action:
    service: homeassistant.turn_on
    entity_id: switch.etekcity_0315_5
1 Like

Then it should be possible to add an OR function, for example
IF sunset -45 OR sun_down AND >22 THEN switch.turn_on.

Will have to do some investigation on that. I’m fine with the system trying to turn it on even if it’s on, i have similiar switches as you do.

you can have an or or an and condition or both…

That’s still not going to trigger if the trigger has already gone when it restarts. You could do a trigger that fires every, say, five minutes and then checks the conditions but that will probably make other problems for you if the conditions aren’t very complex. Like if you want to turn them off manually they will turn on again within five minutes when the trigger fires the next time.
The suggestion for checking conditions when hass starts are probably the way to go for you.

Use the above_horizon or below_horizon commands, or sun position. Example from my automation below for reference.

# REBOOT AFTER SUNSET - HOME #
- alias: Reboot After Sunset - Home
  hide_entity: true
  trigger:
    platform: homeassistant
    event: start
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: group.family
        state: 'home'
      - condition: template
        value_template: "{{ states.sun.sun.attributes.elevation  | int < 10 }}"
  action:
    - service: homeassistant.turn_on
      entity_id:
        - script.kitchen_normal
        - switch.driveway
        - light.downstairs
        - light.hall

# SUNSET - STEPS & TABLE #
- alias: Sunset - Steps & Table
  hide_entity: true
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: "{{ state.attributes.elevation }}"
    below: 8
  condition:
    condition: state
    entity_id: group.family
    state: 'not_home'
  action:
    - service: script.turn_on
      entity_id: script.white_lights
1 Like

Then i would need smarter switches with 2-way communication

Is there a way to do a sync when hassio starts, setting everything as it should be just once?

Thanks for the input, I’ve added the following to automations.yaml, seemt to be working fine
I guess that if i get a loss of power between 45 minutes prior to sunset and sunset it wont work. That can be adressed by adding an OR condition to the lights on, if i feel that i need it.

- id: Lights_at_reboot
  alias: "Lights on at reboot"
  trigger:
    platform: homeassistant
    event: start
  condition: 
   condition: and
   conditions: 
    - condition: state
      entity_id: sun.sun
      state: below_horizon
    - condition: time
      before: "22:00:00"
  action:
    service: switch.turn_on
    entity_id:
      - group.fonster
      - group.punktbelysning_vrum