Start an automation 30 minutes before sunset IF ambient light levels are low

I have a light sensor that measures light in w/m2. I want to automate switching on outdoor lighting at sunset “OR” during a period 30 minutes BEFORE and UP TO sunset if the light level drops below 4 m/m2. I have no problem running an automation which is triggered by sunset but cannot figure out how to include the light condition and the time period from 30 minutes before and up to sunset. This is my suggestion:

alias: Switch on outside lighting at sunset
description: Switch on outside lighting at sunset or 30 before if light levels are low

  • platform: sun
    event: sunset
    condition: and
    • condition: sun
      before: sunset
    • condition: sun
      before: sunset
      before_offset: ”"
    • condition: or
    • condition: sun
      before: sunset
    • condition: sun
      before: sunset
      before_offset: ”-00:30:00"
      • condition: state
    • condition: numeric_state
      entity_id: sensor.lallerod_562_orust_solar_rad
      below: ‘4’
      attribute: friendly_name

# Switch on
- type: turn_on
device_id: 67bb2c974e90d08547c8b90d5301cce5
entity_id: switch.shelly_shsw_1_8caab5055beb
domain: switch
mode: single

Could you please edit your post and format your pasted configuration correctly?

See point 11 here: How to help us help you - or How to ask a good question

It might be easier (at least to start) to break this into 2 automation - one for each use case.
You can have multiple triggers - one based on a change to the light level, and another based on time/sunset. You can then have multiple conditions using OR statements - in the first condition then have an AND condition to check if the time is before sunset AND the light level is below 4. In the 2nd condition, just check the sunset time. Make sense?

I assume you just like the light to switch on before sunset when it’s already dark. That should not be to hard. Because your YAML is mangled I do it in pseudo code:


  • Sunset
  • Sunset - 30 minutes
  • Light < 4 m/m2


  • OR
    • After sunset
    • AND
      • After sunset -30 minutes
      • Light < 4


  • Turn on light
1 Like