Automation running until sun.sun is above_horizon

Hi guys

I want to run a automation until sun.sun is above_horizon

I want to automate some lights then sun.sun is below_horizon and i am away from home

I have a script that turn the lights off and on and some delays, so the only thing i need is keep running the script/automation until sun.sun become above_horizon.

How can i do that?

write an automation that turns on and off the other automation

1 Like

So an automation that start the automation and stops then i am home again?

Yeah, just use ‘homeassistant.turn_on’ or ‘homeassistant.turn_off’ and call out the automation you want to turn on and off using it’s entity id.

1 Like

Thanks dude, sure thats the way :slight_smile:

1 Like

I still need help

I need to run my script continuesly until sun.sun is above_horizon.

How should the automation look like?

how often do you want it to run? Once every minute?

The script is turning lights on and off with some delays.

Lets say 30minutes is the script running, and after that, it should run again, but only if the sun.sun is below_horizon.

As soon as the sun.sun is above_horizon the script can stop then its done turning the lights on and off.

Its for my alarm system, anti thief with the lights :slight_smile:

oh then you don’t even need to turn it on or turn it off. Just use a time_pattern trigger with a condition

    platform: time_pattern
    minutes: /30
    condition: state 
    entity_id: sun.sun
    state: 'above_horizon'
    service: script.xxxxx

So the scripts should only start then i am away from home only.

You can add that as a condition.

So the automation is running, lets say each minutes and run the script if the conditions are true?

My script is still not triggered.

automations will only trigger if the conditions are true. So it will attempt to run every x minutes if conditions are met.

Okay, thank you :slight_smile:

I have something not too dissimilar. I’d suggest something slightly different. @petro’s suggestion is fine, but this might work a little better for you.

First, make the script run long enough to cover the whole time between sunset and sunrise, even if you include random delays.

Next, create an automation that starts the script when the sun is below the horizon AND you’re not home. (I assume you want it to start if you leave after sunset, yes?) This can be done with a template trigger.

Lastly, create another automation that stops the script when the sun goes above the horizon OR you come home.

If you like this idea, let me know if you need any concrete examples.

But what if he leaves before sunset ?

The expression becomes true when both conditions are met, and that’s when the automation triggers. If he leaves before sunset the expression will not be true yet, so it won’t trigger. But when the sun sets, if he’s still gone, the expression will become true and the automation will trigger.

EDIT: So then you ask, what happens if he comes back home, and then leaves again, all while the sun is still set? It will trigger again when he leaves, because once a template trigger fires it won’t fire again until the expression becomes False, and then True again.

Sorry, I thought I found a gap but you had it covered.

Thank you guys.

I need to have the script running only then i am not_home and the sun.sun is below_horizon.

The issue i got now with @petro solution, is that the script is keep running, how can i stop it, lets say the scripts are half way done, and i come home, i want the script to stop, and not continue.

will script.turn_off work?

yep it will