I am wondering what is the most “elegant” way of triggering an automation at sunset, but latest at 20:00 (whichever comes first).
Currently the only idea is to have two separate automation, one with a tigger at sunset, and another with a trigger at 20:00 and both would set an input boolean helper what would be used as a condition to the other. This means however, that there needs to be two almost identical automation (both needs to be maintained on every changes) + a helper input boolean + another automation what resets the input boolean later.
I suppose there should be a more elegant way achieving the same?
This will not work, because in this case the automation will be kicked off both at sunset AND at 20:00. I know under some circumstances it may be OK, but I really only want it to run just once.
I don’t immediately see how to to min() on an event, tbh.
I’d suggest to use @pedolsky solution with an additional check on when the automation was last triggered, i.e.
condition:
- condition: template # only notify once every day at most
value_template: "{{ ( as_timestamp(now()) - as_timestamp(state_attr('automation.my_automation', 'last_triggered')) |int(0) ) > 28800 }}"
I assume 8h max between sunset and 20:00, should be ok unless you live at a pole
A full day (86400) won’t work because the sunset time varies every day.
Thanks for looking at it, but the first part of the post is the other way around (logic is different at sunrise) and as I said before, for the sunset actions I only want the automation to run once. In the second part of the linked post there is a sunset automation, but there they don’t handle/ care that the automation run twice after each other.
One thought here: Use an input_boolean as you thought. (You’ll also need to turn it off at another time (say sunrise?).
Write a script which does the bulk of the automation work, but only after checking that the input_boolean is off.
Write automations for sunset and time which simply call the script.
If you believe an automation with some sort of ‘all in one’ trigger, without a condition, will “run” less, you’re mistaken.
An automation’s last_triggered attribute is updated if its trigger and condition evaluate to true. In other words, an automation “runs” when it executes its action. Therefore the suggestion to use two triggers (Sunset and Time) plus a condition is perfectly suited for this application.
The statement you quoted from me was a reply to pedolsky (maybe you missed that?) who linked a solution in another thread triggering sunset and time without condition - resulting the automation to “run” twice after each other.
The solution to your first post has already been provided by the combination of pedolsky’s suggestion (Sunset and Time Triggers) and koying’s suggestion (Template Condition). The resulting automation will execute its action (i.e. “run”) once (per day).
I replied to pedolsky, declaring his solution (in its own) won’t work. Then he referenced another thread, where the same approach was suggested to set sunset and time as trigger without condition, hence I said again this is not ok for my use case.
In the meantime koying proposed his smart solution for which I replied I will test it.
You were jumping on a post where you haven’t checked to whom it was addressed to.
Actually, I indicated you potentially have a mistaken impression of what constitutes a “run” for an automation (regardless of which thread is the reference).
FWIW, if there is a pressing need to combine everything into a single Template Trigger then that would be feasible as well.