Automations - Should they trigger when HA reboots if conditions are met?

Can’t figure out why my lights aren’t turning off when I reboot HA.

PHYSCAL SETUP
Rpi3 + Hassio installation + Aeotec Zwave USB Stick gen5
GE Zwave On/Off Switch
GE Zwave Outlet

CONFIG
Both switch and outlet have been successfully included to the network. I can turn them on and off through my Hassio dashboard but I can’t tell if my automations are working. The goal is very simple: turn lights on at sunset and off at sunrise. So why do the lights stay on when I reboot HA? Shouldn’t HA see that the sun is up and turn off my living room outlet and my backyard switch? My Lat/Long are correct and sun is correctly reporting as up. The following is in my automations.yaml file.

- id: lights_on_at_night
  alias: lights on
  initial_state: 'on'
  trigger:
    platform: sun
    event: sunset
  action:
    service: switch.turn_on
    entity_id:
    - switch.living_room_outlet_switch
    - switch.backyard_lights_switch
        
        
- id: lights_off_at_day
  alias: lights off
  initial_state: 'on'
  trigger:
    platform: sun
    event: sunrise
  action:
    service: switch.turn_off
    entity_id: 
    - switch.living_room_outlet_switch
    - switch.backyard_lights_switch

DEVELOPER TOOLS STATES INFO

switch.backyard_lights_switch
node_id: 4
value_index: 0
value_instance: 1
value_id: 72057594109837312
old_entity_id: switch.backyard_lights_switch_4_0
new_entity_id: switch.backyard_lights_switch
friendly_name: Backyard lights Switch

switch.living_room_outlet_switch
node_id: 2
value_index: 0
value_instance: 1
value_id: 72057594076282880
old_entity_id: switch.living_room_outlet_switch_2_0
new_entity_id: switch.living_room_outlet_switch
friendly_name: Living room outlet Switch

Think of sunrise and sunset as a specific time not as a status. What I mean by that is where I am, sunrise occurred at 7:10 am. That means the automation will only trigger at 7:10 am. It doesn’t switch to sunrise and keep that state until sunset.

1 Like

when you (re)boot that itself is a separate trigger. your automatons are a completely different trigger (time based upon sun). If you want to turn the lights on or off at (re)boot add a separate automation that does a condition check for sunrise or sunset.

- alias: On Startup
  trigger:
    platform: homeassistant
    event: start

To go along with what @forsquirel said, you could do:

- alias: Lights on after sunset (if HA restarts)
  trigger:
    - platform: homeassistant
      event: start
  condition:
    - condition: state
      entity_id: sun.sun
      state: "below_horizon"
  action:
    - service: switch.turn_on
      entity_id:
      - switch.living_room_outlet_switch
      - switch.backyard_light_switch

Think of sunrise and sunset as a specific time not as a status

Ah! That is a great explanation and makes perfect sense. Thank you.

I think I’m starting to understand… using the state condition of sun.sun and “below_horizon” would solve the problem that shire210 pointed out correct? Now instead of a trigger based on a specific time I have a state that can be checked by a certain trigger.

marthocoo I should ADD your code to my automations if I want to ensure that any time HA restarts the lights will be on if the sun is down and vice versa? In addition to that I can use my existing triggers to ensure that at the specific time when the sun sets my lights will be turned on and vice versa.

Outside of those specific times (sun rise&set) I can manually turn on/off the lights with the physical switch but whenever HA restarts or sunrise/set occurs the lights will reset to their correct states (on when dark and off when light).

Am I getting it?

Totally correct. Here’s the opposite automation to the one I posted earlier, add both to your automations.yaml to take care of what happens when HA restarts during the day and at night, in addition to your sunrise and sunset automations:

- alias: Lights off after sunrise (if HA restarts)
  trigger:
    - platform: homeassistant
      event: start
  condition:
    - condition: state
      entity_id: sun.sun
      state: "above_horizon"
  action:
    - service: switch.turn_off
      entity_id:
      - switch.living_room_outlet_switch
      - switch.backyard_light_switch
1 Like

:worried: darn, added them both in and doesn’t seem to work. How do I troubleshoot?

I’m wondering if it’s something to do with the Zwave network or my automations or something I haven’t even considered. Where do I even start?

Here is my automations.yaml

- alias: Lights off at restart
  trigger:
    - platform: homeassistant
      event: start
  condition:
    - condition: state
      entity_id: sun.sun
      state: "above_horizon"
  action:
    - service: switch.turn_off
      entity_id:
      - switch.living_room_outlet_switch
      - switch.backyard_light_switch
      
- alias: Lights on at restart
  trigger:
    - platform: homeassistant
      event: start
  condition:
    - condition: state
      entity_id: sun.sun
      state: "below_horizon"
  action:
    - service: switch.turn_on
      entity_id:
      - switch.living_room_outlet_switch
      - switch.backyard_light_switch

- alias: lights on
  initial_state: 'on'
  trigger:
    platform: sun
    event: sunset
  action:
    - service: switch.turn_on
      entity_id:
      - switch.living_room_outlet_switch
      - switch.backyard_lights_switch
        
- alias: lights off
  initial_state: 'on'
  trigger:
    platform: sun
    event: sunrise
  action:
    - service: switch.turn_off
      entity_id: 
      - switch.living_room_outlet_switch
      - switch.backyard_lights_switch

you could just try making 1 action at boot time that is runs an if statement. Not 100% sure of the syntax of the top of my head but essentially it would follow as

boot -> sun up? Turn lights on ELSE turn light off

no need to check a second condition with a second automation because your lights are either ON or OFF

1 Like

I like that idea, feels more succinct. Also have no idea about the syntax. Is there a section of the HA website that just talks more about yaml syntax basics? Or maybe another website entirely? I still get confused about the indenting and use of of the dash.

Why do you want it to specifically do anything when you reboot it? Just get the automation right and it will work correctly regardless of whether you reboot or just leave it alone.

Your entity_id’s need to be indented by 2 spaces…

entity_id:
  - switch.living_room_outlet_switch
  - switch.backyard_light_switch

You shouldn’t be setting an initial state.

I was also thinking this might be why it’s not doing what you want.
Also, what does rebooting HA have to do with anything? Are the lights turning on when you reboot in the middle of the day?

Excellent!

How would I write the trigger/condition if I wanted the lights to go on if it was 1:50 before sunset and HA restarts - so the original above is great but I’d like the lights to come back on when HA restarts if it’s 1:50 before sunset.

The initial state only pertains to the automation either being on or off at startup, it it is off then that automation won’t run until either the user turns it back on in the frontend or another automation that is on turns them back on. Or am I missing something here?

you are correct. Initial state only refers to the automation being active or not. It has nothing to do with the output(s) from that automation (eg: I have a light which is affected by multiple automation’s, depending on the case at hand but only one automation for that particular light is active during the daytime)

I always set initial_state: on as if you restart HA a few times in a row, HA will decide to turn off all automations and if you don’t notice then none of your automations will run till you notice and turn them back on in the frontend, which can make for a lot of hair pulling and shouting at the computer :stuck_out_tongue:

I’m using this one (modified) but would like my lights to come on on restart of Home Assistant if sunset is within 1:50 or already occurred. I can already set the lights to come on at 1:50 before sunset but would be great if on a restart of Home Assistant they would also come on.

Is your HA server being stopped/restarted enough that it’s frequently not running the moment sunset occurs? I’m just trying to understand what your need for this automation is. It seems like since you’re using the below_horizon state it should still work if it gets started and it’s now below_horizon where it wasn’t before it stopped