New to HA. Turn on at sunset not working. What am I missing?

So I’ve just started playing around with Home Assistant. I tried creating a simple automation from the UI that generated this YAML

alias: On at sunset
description: ""
trigger:
  - platform: sun
    event: sunset
    offset: 0
condition: []
action:
  - type: turn_on
    device_id: b6f2c8e8d3d8eae5e33b3da769295eef
    entity_id: switch.tp_link_smart_plug_1331
    domain: switch
mode: single

Nothing happens at sunset :frowning:

If I replace the sunset trigger with a time instead, it works :slight_smile:

alias: On at 6pm
description: ""
trigger:
  - platform: time
    at: "18:00:00"
condition: []
action:
  - type: turn_on
    device_id: b6f2c8e8d3d8eae5e33b3da769295eef
    entity_id: switch.tp_link_smart_plug_1331
    domain: switch
mode: single

Monitoring the Sun entity shows it’s correctly figuring out sunrise/set times. HA’s time is set correctly.

The YAML was created entirely by the UI so I assume it’s correct. Having said that I can’t find any documentation for the Sun entity events and I’m not sure how to verify that the “sunset” event happens.

Any help/insight greatly appreciated!

Dave W

1 Like

I always had problem with sunrise/sunset triggers and honestly I don’t know why. This is what I done in one bedroom to turn on light when it’s dark till I buy motion sensor with lux capabilities.

alias: Jelena bedroom light on
description: ""
trigger:
  - type: motion
    platform: device
    device_id: cb1eb9d4a9355263a9eb33bcd8f14d09
    entity_id: binary_sensor.jelena_bedroom_pir_occupancy
    domain: binary_sensor
condition:
  - condition: numeric_state
    entity_id: sun.sun
    below: 10
    attribute: elevation
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.jelena_bedroom_light
mode: single

This works for me. If you adjust it to your settings it should work.

Thats strange, this works perfectly for me.

alias: Curtain Close
description: ""
trigger:
  - platform: sun
    event: sunset
    offset: "30"
condition: []
action:
  - device_id: 0fc89744ffb13083ce78d9f08ffc6f83
    domain: cover
    entity_id: cover.curtain
    type: set_position
mode: single

Have you removed “default config:” from your configuration.yaml?
If so then your need to add the intergration manually.

The way YAML is structured always seems too flexible. For whatever reason, when I set up an “after sunset” automation I used a different order for the lines under trigger:

- id: away_guest_lamp_on
  alias: Away - Guest BR Lamp On After Sunset
  initial_state: 'false'
  mode: single
  trigger:
  - event: sunset
    offset: 0:14:00
    platform: sun
  action:
  - service: switch.turn_on
    target:
      entity_id: my_entity_id

With so many “right” ways, I never know which to choose, and it’s harder to know when something is wrong, or just different. At any rate, I know the above works for me.

all of the above seems to be saying that you possibly need to remove the 0 offset:

trigger:
  - platform: sun
    event: sunset

you should be able to check for the sunset event in the dev tools->events page and listen for sun events.

Interesting thought.

I tried removing the offset: 0 and it worked!

I also notice in the documentation for the Sunrise/Set trigger the offset is shown as a string but the UI used a number.

I’ll check whether offset: "0" works. If it does, I guess there is a bug in the UI

Your offset is a string value while mine was a number. I have a hunch that Sun expects a string in the format “HH:MM:SS” or it silently fails.