Automation with sun condition doesn't works

I would like to make an automation when my alarm is triggered it switch on all garden lights after 5 seconds.

I made a wifi switch that detect when my alarm goes on. and tried to configure an automation in the graphical interface of home assistant

device_id: 2833ca72c042415786ed04a5ff230e96
domain: binary_sensor
entity_id: binary_sensor.luci_allarme_switch1
platform: device
type: turned_on
condition: sun
after: sunset
device_id: 7cf4d1691d754cbb8a85ef6a538f083f
domain: switch
entity_id: switch.shelly_shsw_1_688f36
type: turn_on

the switch works correctly if I don’t use the sunset condition, I tried the device at midnight and it doesn’t work. What I am missing
Thx
Andrea

well, that GUI editor’s automations do look horrible… and coupled with the device approach (which I’m not familiar with) it’s :frowning:
Try this

- alias: my_alarm_automation
  trigger:
    platform: state
    entity_id: binary_sensor.luci_allarme_switch1
    from: 'off'
    to: 'on'
    for:
      seconds: 5
  condition:
    condition: sun
    after: sunset
  action:
    service: switch.turn_on
    data:
      entity_id: switch.shelly_shsw_1_688f36

Use a sun state condition of below_horizon instead. A sun condition with after_sunset will only be from sunset to midnight. https://www.home-assistant.io/docs/scripts/conditions/#sun-state-condition

2 Likes

Yeah, what he said :+1: :rofl:

It doesn’t say it explicitly does it?
And by the way, will condition: sun work if there is no sun: in configuration.yaml?

It doesn’t say it in text, but there’s a chart on that page a little below what I linked that explains it. Can’t imagine a sun condition would work without the sun.sun entity (which you get by having sun: or default_config: in your config).

Okay, but if you extend your logic 01:00 would be valid (and maybe elevation is still dropping) but how about 02:00, 03:00, 08:00, 10:00, 12:00, 13:00 ???

Where exactly would you draw the line ?

That’s why these things are generally on a day basis.

You could say: after sunset OR before sunrise
But it’s just easier to say ‘below_horizon’

the sun will do that. next sunrise :wink:

exactly what I thought after that below_horizon topic…
Also, I’m not 100% sure but it looks that for the condition to work using sunset/sunrise we need to use both as otherwise it’s open for discussion (actually, it’s not looking at the visual timeline provided in the docs) :wink:

yeah, in a way but I feel that’s not enough…

looking at the code I can see that they use astral library.

As Mutt said, a condition of (before sunrise or after sunset) would work, or just sun state condition of below_horizon