Trigger using sunset not working

I have been trying for several days to get a working automation rule for turning on a switch 1.5 hours before sunset. I copied the example for using the sun platform and have checked it over many times and cannot find the error in my code. I found one example that used the single quote around the time offset and another example that used the double quote around the time offset. I have used both and neither worked. I have even tried removing the offset altogether and simply use sunset as the trigger and still nothing.

I checked the homeassistant log file and it has no errors. I have one other rule that turns the same switch off at midnight using the time platform. That one successfully fires every day. The only difference I can see between the two is the use of the sun platform. Sun tracking is working on my platform as far as I can tell. I see sunrise and sunset showing in my log and when I click sun on the home assistant screen it shows me next sunrise and sunset. Below are the sections in their current state from my configuration file:

My code for the sunset automation (not working):

automation:
   alias: 'Turn on sunroom light 1:30 before sun sets'
   trigger:
      platform: sun
      event: sunset
      offset: '-01:30:00'
   action:
      service: homeassistant.turn_on
      entity_id: switch.ge_45609_onoff_relay_switch_switch_5

My code for the midnight automation (working:

automation:
   alias: 'Turn off sunroom switch at midnight'
   trigger:
      platform: time
      after: '00:00:00'
   action:
      service: homeassistant.turn_off
      entity_id: switch.ge_45609_onoff_relay_switch_switch_5

If anyone has any ideas or sees where my error lies I would greatly appreciate the help!

Could it be that you forgot to set latitude: and longitude: at the top of your configuration.yaml file?

Thanks for the reply!

sun, latitude, and longitude are included (and set) at the top of my config file. If I look at the logbook I see entries such as “Sun has risen” and “Sun has set” so the sun tracking is working to some degree. Just not working as a trigger for an automation.

Try formating:

[code]automation:

  • alias: ‘Turn on sunroom light 1:30 before sun sets’
    trigger:
    platform: sun
    event: sunset
    offset: ‘-01:30:00’
    action:
    service: homeassistant.turn_on
    entity_id: switch.ge_45609_onoff_relay_switch_switch_5

  • alias: ‘Turn off sunroom switch at midnight’
    trigger:
    platform: time
    after: ‘00:00:00’
    action:
    service: homeassistant.turn_off
    entity_id: switch.ge_45609_onoff_relay_switch_switch_5[/code]

Thanks Mihai. I figured out that it was my YAML formatting screwing up the automation and rules are working perfectly now.