Does sunset event only work At the moment of sunset or beyond?

I am trying to get my first zwave switch to turn on at sunset

I just made this script by using examples on the docs. Config checker says good but it didn’t work.
My ? is the sunset event valid AT and beyond sunset or only at sunset? It’s well after sunset so not sure if that is the issue or it doesn’t like my code?

automation:
  trigger:
   platform: sun
   event: sunset
   
  action:
   service: light.turn_on
   entity_id: switch.leviton_dzs151lz_switch_switch

it will only trigger AT sunset.

Also some of the documentation seems conflicting.
Should this code go into the main Configuration.yaml file or the automation.yaml file?

I tried it in Configuration.yaml (and it did not work), then I tried it in automation.yaml .
But In automation.yaml it says the automation: header is invalid,.

Thanks- is there any way to trick HA that it is Sunset just for testing purposes till I get it working?

you only use automation: as a headed if you have your automations IN the config.yaml file. If you have split the config and used the automations.yaml file, the automation: header is not to be used

Ah so that is what was so confusing. It can go in ether one. Which is best practice to use? I would think automations to keep things more tidy.

You can trick it by using an offset…

automations.yaml is usual and you would have this in config:

automation: !include automations.yaml

Ok thanks that is a big help. It looks like that include has already been done. Let me figure out what sunset was today and try the offset. Thanks!

1 Like

I actually use Packages now which I prefer over all of the file methods, but the next best (in my opinion) is to have a folder called “automations” and keep a separate yaml file for each automation you have (eg: kitchen_light_auto.yaml etc.) and then put automation old: !include_dir_list automations in your configuration.yaml

You could temporarily change the trigger to a specific time just for testing purposes (then change it back to use sunset).

If you feel you must test the automation at sunset, change the computer’s clock time so it’s just before sunset in your location. You may need to restart Home Assistant so that it’ll be in sync with the new time. NOTE: if your computer is periodically syncing its clock with an NTP server, this trick might not work (or only work for a short period of time).

This just won’t work.

Maybe later but this is just confusing for him right now. I use packages and have an automations file with 90% or my automations in it. I personally see no benefit is having different files for each automation but that’s just me… I have less than 20 in total so maybe if I had more it might make more sense.

Hmm. Why wouldn’t it?

because NTP will keep syncing his computer with UTC if he is connected to the internet.

My issue is I am new at this and need to make sure it works before I remove all my switches from Wink.

Since I got zwave working yesterday, the system takes even longer to reboot I set offset to 8pm and missed the window by 3 min. So let me try again …I will try just an exact time 815pm and see what happens…thanks.

OK so if you use the automation editor, you should not need to restart. If you have used a YAML file then maybe edit that file and then use the reload automations option instead of restarting home assistant.

I am not sure the automation editor is actually working. It says nothing is there when it is.
I have been using the hass.io editor. The same one I use for the main config. Once I get things working I will see whats wrong with the automation editor.

Rebooted and waiting for 817pm …thanks

If you created the file in a text editor and saved a YAML file then the automation editor won’t work as it needs an ID in the file. Here is a sample of mine:

- id: '9205977024304'
  alias: Turn Lounge Light On Elevation < 25° & after 4PM
  trigger:
  - platform: numeric_state
    below: 25
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
  - platform: time
    at: '16:00:00'
  condition:
  - condition: time
    after: '16:00:00'
    before: '18:30:00'    
  - condition: template
    value_template: '{{states.sun.sun.attributes.elevation < 25}}'
  action:
  - service: light.turn_on
    data:
      brightness_pct: 75
      entity_id: light.lounge
      kelvin: 3100

The ID is any unique identifier.

Even if you manually edit the yaml you still should be able to reload automations instead or restarting.

I didn’t create it but it was already there. How do I get that unique ID?

It didnt work so let me try adding a minute or 2 and this time i will try to reload as that will save a bunch of time.

just make it up - it’s only important that it exists and is unique.