Automation 20min before sunrise or 7:15 if sunrise is after 7:30

HI,
I have an automation, that runs a script 18 Minutes before sunrise. I did this with the grafical user interface at config → automations. Works like a charm (shades open at this time). But now, as winter is coming the opening is too late and in addition, I want the shades to open in a different height, with another script. This script also works fine. But I cannot find a way that one script is running if 20 minutes before sunrise, and the other script is running at a specific time (say 7:15), if the sunrise is after 7:10 (or whatever other time).
Each day only script-A or script-B should run in the morning, never both on the same day.

I found Automation trigger on sunrise but only past 8 but have absolutely no clu how to change my grafical automation script to this YAML code (and still have the other automations that are grafical running.)

Here the automation config that i use. I have two de-activated triggers, which are on purpose de-activated.

Many thanks for any help
Juergen

So if I get your question correctly, only 1 of the 2 automations should run each day.
If that were me, I would create an input boolean (toggle helper) that gets set to true when either of the automations runs.

Each automation should also have a condition to NOT run if the toggle helper is true.

You then just need a way to reset the helper for the following day, which you could do with another automation that runs at say midnight each day.

(I have a ‘daily reset’ automation that checks that things like helpers are reset)

If you need one, there is a slightly dated, but still informative video primer on translating between YAML and UI editor available on the ResinChem Tech Youtube channel .

You don’t need to change anything to YAML permanently. You can switch back and forth between the graphical editor and YAML editor.

Automation1: You need a boolean like “wintertime”. (As is it german only if) Aber wenn “Wintertime is true”…
Automation2: Aber nur wenn “Wintertime is false”…

But may I recommend you the Sun Integration.
There are 5 entities, one is sensor.sun_next_sunrise and also sensor.sun_next_dawn. Maybe Dawn is “wintertime safe” and be and alternative for sunrising. In this case you’ll only need one automation.
At my location, dawn was today at 6:38, that’s close to your calculation.

Hi,
I think I got it
a.) I need a variable to keep track, if the script did already run today. This is reset via another automation at “around” midnight.
b.) Triggers are both 7:13 and 18 minutes before sunrise
c.) Condition used: continue only if not already run (from variable above)
d.) in any programming language I would now say
if (sunrise-18min) < 7:13 than
do script-a
else
do script-b

But how to achive this with

I cannot find a bigger/ealier/whatever in the action condition at the end.
Any idea?

thanks
Juergen

HI,
I found something. If it works tomorrow, than I will post it. Feels somehow complicated, but should work. But maybe this is as I am not used to it.

anyway: Thanks a lot to all that gave ideas. Brought me on the (hopefully) right track.

Is there any way to have comments in the graphical automation scripts? I doubt, I will understand the automation in a month from now :slight_smile:

Juergen

Currently, the main option you can use for comments is to use the alias field. In the visual editor you can edit it by choosing “Rename” in the 3-dot expansion menu for the trigger, condition, or action you want to comment on:

In the image above, the alias is currently “Confirm the time is after 7:30 AM and before 8:30 AM and the day is Thursday”

If you are using templates, you have the option of adding comments within your template. In the Jinja template language comments must use specific delimiters {# #}.

Hi @Didgeridrew,
Awesome. Many thanks!
going to comment my stuff.

thanks
Juergen

I believe the following example meets your stated requirements:

alias: example
trigger:
  - platform: time
    at: '07:13:00'
  - platform: sun
    event: sunrise
    offset: '-00:18:00'
condition:
  - condition: template
    value_template: "{{ this.attributes.last_triggered | default(as_datetime(0), true) < today_at() }}"
action:
  - action: script.turn_on
    target:
      entity_id: "script.script-{{ 'a' if trigger.platform == 'sun' else 'b' }}"

How it works

  • The automation is triggered at 07:13:00 and at 18 minutes before sunrise.

  • The Template Condition confirms that the automation has not executed its action today. In other words, only one of the two triggers (whichever occurs first) will be allowed to execute the action today.

  • The action turns on one of two scripts, depending on which of the two triggers occurred first today.

2 Likes

Hi @123 ,
I just wanted to post my solution (after it seems to work since two days), but I am embarassed to what I have done compared to your elegant solution. Wow!
Thanks a bunch. I’ll definitely try it (have to rename my scripts for sure, as I named them different, than I asked for :slight_smile: )

So if anyone wants to see a newbie solution, I would post it, but I doubt it will help further readers.

thanks @123 again.

Juergen

1 Like

Much nicer way of doing it, rather than relying on my input boolean approach. I’ll have to remember that for next time I’m doing something like that.