Dynamic time schedule every day - Set time trigger programmatically

I have Plant lightning for my living room which I try to run certain hours a day, based on the day length.
Here, shortest days (sunrise to sunset) are around 8 hours, longest days are ~ 16 hours.
So my lightning_time is 18 hours - (day_lenght) but with a minimum of 2 hours.
My current solution seems a bit odd to me, it works like this:

  • Automation at sunrise to trigger a Script
  • Script will turn on the lights and goes into a delay, which looks like this:
    delay: >-
    00:00:{{((18 * 60 * 60) -
    (as_timestamp(states.sun.sun.attributes.next_setting) - as_timestamp(now())) |
    round) | max(2 * 60 * 60)}}
    
  • Then, light is turned off again

However, I dont have the impression this is a good thing to use such a long delay.
I guess it woul be better to just calculate at which time the lights should turn off and somehow set this time point as the trigger for another automation. Just, I have no clue how to do this. Is there something like a global variable I can set? Or some function like: Set Trigger to XXX o'clock?

So my idea is:

  • Turn on lights at sunrise and calculate at which time to turn them off again
  • Set calculated time as trigger for another automation which will turn the lights off

I am also welcoming any other idea…

Do you want the lights on when it is daylight?

i.e. Sunlight and artificial light at the same time?

If not, you could turn the lights on at sunset (or a bit before) for (18 - day_length in hours).

Well, yes. At the same time. The plants are in a rather dark place in winter.
However, what you say is just what I have with the difference that it starts at sunset instead of sunrise.
But I don’t know how to set the “shut-down-time” correctly.
As I said, my delay solution seems a bit odd to me. It would be much nicer to just set the “turnoff_time” to a fixed time point at which the automation is triggered.

Ok, then your two automation idea would be best.

Ok, but I dont’t know how to do this.
Can I calculate a time in the first automation and have a second automation running at that time?
This time will change every day.
This is where my problem is. I guess I put it the wrong way.
Right now I just have one automation with a mult-hour-delay.

Perhaps what you are looking for is setting an Input_datetime. Then use that to trigger the second automation.

1 Like

You Sir, are a genius!
It works: First automation get triggered by sunrise, turns on the lights and sets the time helper to the shutdown time. Second automation gets triggered by shutdown time and turns off the lights!

Thank you so much, this was the “global variable” I was looking for!

I came across https://github.com/arthurdent75/SimpleScheduler

pretty niffty and elegant, including sunrise