Sunset and sunrise condition should be easy but

Hello everyone,
I have an automation where I only want it to run at night, after sunset, and before the sunrise. I set it up with an OR and it still says the condition was not valid.
Does this look correct to everyone or did I do something wrong?
Here is my automation that shuts turns on my shelly for 10 mins with an MQTT event.

- id: '1649213236173'
  alias: Back Porch Light Person Auto
  description: ''
  trigger:
  - platform: mqtt
    topic: frigate/back_alley/person
  condition:
  - condition: or
    conditions:
    - condition: sun
      before: sunrise
      before_offset: 00:30:00
      after: sunset
      after_offset: 01:00:00
  action:
  - type: turn_on
    device_id: 7e44d09d6956eae7188f98c77e3130c5
    entity_id: switch.shelly1_98f4abf2a6b6
    domain: switch
  - delay:
      hours: 0
      minutes: 10
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: 7e44d09d6956eae7188f98c77e3130c5
    entity_id: switch.shelly1_98f4abf2a6b6
    domain: switch
  mode: single

You need to split the Sun condition for the Or to have any effect:

condition:
  - condition: or
    conditions:
    - condition: sun
      before: sunrise
      before_offset: 00:30:00
    - condition: sun
      after: sunset
      after_offset: 01:00:00

Apparently there is a bug with this condition and it was intended that this should be true at night (it is currently not):

condition:
  - condition: sun
    after: sunset
    before: sunrise
1 Like

2 other ways to tackle it.

  condition:
    condition: state
    entity_id: sun.sun
    state: "below_horizon"

or this, which you can tweak more with the sun angle in the sky. Works similar to using offset.

  condition:  
    condition: template
    value_template: '{{ state_attr("sun.sun", "elevation") < -5 }}'
1 Like

There is another way.
I use an addon called sun2 (GitHub - pnbruckner/ha-sun2: Home Assistant Sun2 Sensor) which provide one sensor for sunrises and one sensor for sunsets.
It is so much easier to handle and it have every thinkable value available in relation to the sun.

I have just chosen a few values to monitor and here are an example of my sensors.

[sensor.copenhagen_sunrise]
Copenhagen Sunrise 2022-05-05T05:18:37.575909+02:00
yesterday: 2022-05-04T05:20:46.012114+02:00
today: 2022-05-05T05:18:37.575909+02:00
tomorrow: 2022-05-06T05:16:30.697320+02:00
device_class: timestamp
icon: mdi:weather-sunset-up
friendly_name: Copenhagen Sunrise

[sensor.copenhagen_sunset]
Copenhagen Sunset 2022-05-05T20:55:39.939221+02:00
yesterday: 2022-05-04T20:53:42.159402+02:00
today: 2022-05-05T20:55:39.939221+02:00
tomorrow: 2022-05-06T20:57:37.237827+02:00
device_class: timestamp
icon: mdi:weather-sunset-down
friendly_name: Copenhagen Sunset

Thanks everyone I will look at your examples and put something together and test it this weekend.
@ kanga_who does “elevation” come default with the sun property? How would I know what elevation is for after sunset and for before sunrise?

1 Like

Elevation is an attribute of the sun.sun entity you don’t need to do anything.

The sun moves at 15° per hour and is at 0° elevation at sunrise and sunset.

e.g. -15 is one hour after sunset, and 7.5 is 30 minutes after sunrise.

1 Like

Thank you! thats exactly what i was looking for.