Trigger on sunrise with limits

Hi !

I would like to have a trigger based on sun sunrise/sunset datetime but with some restrictions (like never before 7am) with a single automation (I don’t think I can do that with ‘conditions’).
Something like:

 trigger:
   platform: sun
   event: max(sunrise, "07:00")

The template trigger looks interesting for this but I’m totally lost with the ninja2 syntax.

Can someone help ? :slight_smile:

Yes, you can.

- alias: example
  trigger:
    platform: sun
    event: 'sunrise'
  condition: "{{ now().hour < 7 }}"
  action:
    # etc ...

Yes but that will just not trigger the action, never. In my case I would like to postpone it.

How do you want to handle sunrises that occur after 7:00?

- alias: example
  trigger:
    platform: sun
    event: 'sunrise'
  condition: "{{ now().hour >= 7 }}"
  action:
    # etc ...
1 Like

If you want it to happen at sunrise, or 07:00, whichever happens first then:

  1. You need two triggers - sunrise, and 07:00
  2. You need a template condition - to check that the automation hasn’t triggered in the last 12 hours
condition:
  - condition: template
    value_template: "{{ state_attr('automation.YOUR_AUTOMATION','last_triggered') == None or (now() - state_attr('automation.YOUR_AUTOMATION','last_triggered')).seconds > 43200 }}"

Then the first trigger that day will cause the automation to run, whether that’s because the sun rose, or because it’s 07:00.

Thanks Tinkerer, that would work !
In the meantime I was wondering if I could have a template trigger with something like ([‘tomorrow 7h’, ‘sun.sun.next_dawn’]|min), but I don’t know jinja2 enough.
I may have a look at the sun trigger code directly to add optional ‘never_before’/‘never_after’ settings.