#Trigger an automation 14 hours after the last sunrise time

1 messages ยท Page 1 of 1 (latest)

wicked beacon
#

I am brand new to HA, automations, and YAML. Trying to figure my first one out!

I am trying to create an automation that automatically triggers 14 hours after the last sunset time. I have figured out that the built-in Sun integration only gives the next sunrise. I found the custom Sun2 integration, which creates sensors for Dusk, Dawn, Sunrise, Sunset, and more. I can also see that the sensors have multiple attributes available. For instance, the 'Home Sun Rising' sensor has three attributes: yesterday, today, and tomorrow.

Anyway, I see that I can use the 'Home Sun Rising' sensor directly in the 'At Time' field. This would trigger the automation right at Sunrise. How would I go about modifying this so that it triggers 14 hours after that value?

fierce barn
#

lol you're starting out with a difficult one. What you can do is create a template sensor helper with the following template: {{ as_datetime(state_attr("sun.sun", "next_setting")) + timedelta(hours=14) }}. That will reset every day and will give you the next_rising time + 14 hours (in your local timezone). Then, you can use that sensor as a time trigger.

I think that'll work as is, but it might need to be tweaked out a bit.

#

Actually, double-checking it, the template sensor you create would need to have a device class of timestamp. So, set the device class to timestamp with that template and you should be fine with it.

#

This is what the helper would look like.

#

And this is the trigger.

fresh cape
#

next_setting + any amount of time will never trigger; it will reset to a new time in the future before it has a chance to trigger

#

I would do a trigger-based template sensor, use a state trigger based on the next_setting attribute. Then set the state to {{ trigger.from_state.attributes.next_setting | as_datetime + timedelta(hours=14) }}

wicked beacon
#

Could I do next_setting - some amount of time? For instance, could I set it to turn off 10 hours before the next sunrise? It would be off by a few minutes but would be close enough...

#

(and yes, that's classic me: start out with hard mode lol)

fierce barn
#

What are you actually trying to do? The sun events with offsets should be enough for most cases. Can you give a description of what you are trying to do?

wicked beacon
#

Sure! It's a light inside a chicken coop. Egg laying decreases with daylight hours, but if you provide supplementary light, it can keep production going throughout the winter. The goal is 13-14 hours of light per day, but I only need it to be on at the end of the day, after it's gotten dark.

fierce barn
#

Ah, so you could do that with a positive offset on the sunset. So, something like this.

wicked beacon
#

Understood, but it's not quite right, because Sunrise is also getting later each day

fierce barn
#

And then you can have a second trigger with a positive offset on the sunrise.

#

Yup, that gets calc'd automatically.

wicked beacon
#

Hmm... could you explain a bit more?

fierce barn
#

Plus, you can also do some neat things with seasons as well.

#

Sure... I think the best way to approach this is to use the sunset (with or without an offset) and a timer.

#

(Much easier than a template (imho).

wicked beacon
#

That does sound easier lol

fierce barn
#

So, when the sun goes down, the light comes on and the timer for 13-14 hours starts. When the timer finishes, the lights go off.

#

You could even do it where you turn on the light before the sun goes down (negative offset).

wicked beacon
#

OK, I see what you mean, but that would mean it's light 24 hours a day.

fierce barn
#

Nope. The timer would take care of that.

wicked beacon
#

Yea, I've got the on part working already with a -1:00:00 offset to sunset

fierce barn
#

If you want 13 hours of light, you set the timer with a 13 hour duration. When the timer finishes (using a trigger), the light goes off.

wicked beacon
#

OK, maybe I should clarify. I mean 13 hours of combined sunlight + artificial light

fierce barn
#

Ahhhhh... ok. Hmmmm, let me think on that for a second.

wicked beacon
#

haha, like I said... hard mode

fierce barn
#

So, you need the amount of time that the sun will be shining for the day + how much time will be needed for a total of 13-14 hours of "light" a day?

wicked beacon
#

Sunrise today was at 7:38, and Sunset was 6:55. So I'd want the light to turn on at 5:55 (I think I've got that working) and turn off at 7:38 + 14h = 9:38p

fierce barn
#

TBF, I did the same when I started with HA too.

#

Yup. I got it. I think a simple template might actually solve that.

wicked beacon
#

As an aside, I've got some non-professional software dev experience, but I just got HA last week. I'm totally addicted, it's like programming IRL

#

So of course I could just set an estimated Off time and reset it every few weeks, but where's the fun in that?!

#

And I do have Sun2 available with sensors representing today's sunrise. I just don't know how to put everything together

fierce barn
#

Yeah, the "easy" solution would be to have a timer set to 3-4 hours.

wicked beacon
#

right

#

Which is what most people do. But I figured this would be a good chance for me to figure out how this stuff works

fierce barn
#

In Developer Tools > Templates, can you show me what {{ states("sensor.daylight") }} returns? (The sensor might be named something else, I don't have the integration installed here).

#

You might need to enable that sensor.

wicked beacon
#

So I'm trying to figure out just how to use that page. Is that like a live test for templating code?

fierce barn
#

Correct. ๐Ÿ™‚

wicked beacon
#

this is friggin cool

fierce barn
#

As you start to learn templates, it'll become a VERY used tool. lol

#

๐Ÿ™‚

#

I'm installing the integration now. I think the key to this is that daylight sensor. It gives you the number of hours from sunrise to sunset. That's going to be your offset.

wicked beacon
#

I just enabled daylight hours

#

the sensor, I mean

#

It just says Unknown

fierce barn
#

Yup. 1 sec.

#

Ok, go to the settings for that entity and verify what the name is:

#

The way you get to that page is to click on the entity and then the โš™๏ธ icon (top right of the screen)

#

And this is what it returns:

wicked beacon
#

Oh, you won't believe this, I think I already had it working lol...

fierce barn
#

I'd believe it. ๐Ÿ™‚

wicked beacon
#

My test automation just ran

#

9:37 pm lol

fierce barn
#

lol really?! That's awesome!

#

lol you're in the same TZ as me.

wicked beacon
fierce barn
#

Nice! Self-solved solution. I like it.

wicked beacon
fierce barn
#

BOOM... simple. Nice. Well done.

wicked beacon
#

awesome... haha thanks. I still want to figure out that templating though

#

Where on the east coast are you at?

fierce barn
#

I'm in the Charlotte, NC area.

wicked beacon
#

Greenville, SC area here. Neighbors ๐Ÿ™‚

fierce barn
#

lol It's better on top ๐Ÿ˜‰

wicked beacon
#

lol nice

fierce barn
#

But, been down to Greenville a lot over the years.

#

Eh, couldn't resist the dig... I'm actually about 30 minutes from the SC border.

wicked beacon
#

haha, I love it. We know it's the best here anyway ๐Ÿ˜‰ mountains and lakes and open country, hard to beat.

#

Thanks for your help, I really appreciate it, and I'll definitely start poking around the templates channel/forums areas

fierce barn
#

DAMN SKIPPY!!! lol (Although, my favorite mountain places were trashed by Helene).

#

And absolutely! Be sure to ask LOTS of questions as you're getting your feet wet.

wicked beacon
#

We really are neighbors... you're talking about Jocassee Gorges?

fierce barn
#

Nah... Little Switzerland... Linville Falls plus the Dragon (Deal's Gap, etc). That whole area got hit hard.

#

So, as you hang around the server, you'll find out everyone calls me Bill (my name lol) and I'm a Mini Cooper fanatic. All the fun roads up there (The Diamondback, the Dragon, etc)... all trashed. ๐Ÿ˜ฆ

wicked beacon
#

Nice to meet you Bill, I'm Jon. I'm sure you'll be seeing more of me here and there. And yea, it's real sad how much got destroyed by that storm. Everyone is working hard to get things back though so there is hope

#

Actually, I haven't been able to watch TV or youtube or anything since, because of so much crazy going on. So I decided to order HA Green and dive into this instead :). True story

fierce barn
#

Likewise! I have a LOT of friends up there that are just now getting back to some semblance of "normal". We got grazed a bit here, but literally 30-45 minutes west of me got hammered.

#

lol fair enough and same... I've kinda tuned out of YT for the same reason.

#

And I spend way, way WAY too much time on home automation. lol

wicked beacon
#

lol, well there are worse things to spend way too much time on

fierce barn
#

Anyhow, gotta hit the sack... work starts early. meh... Look forward to seeing you around!