Sunset/Sunrise Trigger

Hi,

Is it possible to Trigger a Macro by Sunset/Sunrise? If yes, can you please tell me how? I have tried & can not figure it out, nor could I find an answer on this forum.

Thanks!
-Doren

1 Like

Interesting. KM has a new action called Get Location which possibly is needed to solve this problem.

Do you own any light sensors in your house that measure lumens? I have a few of them myself, which means that's probably how I would handle it. They transmit their data to my HomeKit hub, which is my Apple TV.

It's also possible that we could program KM to calculate the brightness by knowing your location. I'm pondering that idea.

1 Like

Off the top of my head, I think the most workable option might be using the Shortcuts app time of day trigger, which can be set to sunrise and sunset.

1 Like

True, and that's exactly what I was pondering. But I'm having trouble finding that trigger in macOS. Apparently it's only in iOS, and that means we need to find a way to transmit from one device to the other. I think the simplest solution would be to calculate daylight based on Get Location, which gives a precise Latitude/Longitude.

There's a simple calculator online here that converts Lat/Long/Time to Solar declination. I like this idea.

https://gml.noaa.gov/grad/solcalc/azel.html

1 Like

The Mac version of the Shortcuts app doesn't support automation, but this free app picks up the slack, apparently. I haven't used it but worth a look.

If it's no good, you can always run the automation from your phone and trigger your macros via the Remote URL Trigger.

1 Like

Fortunately, the iOS version (and apparently Shortery too) has explicit Sunrise and Sunset triggers. All you need to do is set up an automation for each time of day, each running a shortcut. The shortcuts will call the Remote URL Trigger, thereby triggering the macro(s).

Edit: I just downloaded Shortery and it turns out the Time trigger is a pro feature. So, unless you want to pay a yearly subscription, I'd go with the iOS option.

2 Likes

Sure! That sounds simple to you, but converting a Lat/Long to a solar declination sounds simpler to me. The only problem that I'm having is that the new KM v11 Get Location action is still not fixed, I get timeouts when I use it. So for now, your method is the best.

2 Likes

Yeah, that sounds way simpler than this. :joy:

1 Like

You said you were going to call a URL trigger. You didn't show that part. That's the hard part. That's about 150 characters that have to be carefully entered, including 100 random alphanumeric characters. Or maybe a few less if you get the user to configure the Web Server feature of KM.

Besides, we don't even know if the original poster owns an iPhone, or an iPhone data plan. My method doesn't have those hardware requirements. We know the user has a Mac.

1 Like

It's a lot easier than you might think.

  1. Copy the web trigger:

Keyboard Maestro

  1. Paste it into a text block and add a GET URL action.

Shortcuts

1 Like

Yes, you are right, it is a little easier than I thought. But my method also is simpler than you think:

image

All I need to do is fill in the correct formula above, which is based on Location.x, Location.y and NOW(). The resulting value, called Brightness, would be a number representing how high the Sun is above the horizon, or a negative number indicating how far below the horizon the Sun is. My method doesn't require an iPhone or a data plan. I was hoping to get the formula from that website.

1 Like

I'm impressed with your mathematical prowess. This still begs the question, how is your macro triggered?

You could probably set up an intermittent chron trigger that aborts if the current time doesn't satisfy the calculation conditions.

This is unarguably more convoluted and inaccurate than using a shortcut, if the user has an iPhone that is. I'd wager it's likely they do, given that this forum tends to attract people who are interested in mac automation and integration.

1 Like

I hadn't thought of that. Probably polling by the minute. But maybe users prefer your approach, so I'll leave it with you now.

1 Like

Wow! Thanks guys! I'm slightly lost after reading everything though LOL I do have an iPhone, iPad, Apple TV & Mac Studio. I also have a Pro version of Shortey. I'm going to re-read everything & see what I can figure out. If I still can't figure things out I will post again, but I will post regardless. Hope I can figure this out LOL Thanks again!

There are three components:

The trigger: I suggest Shortery in your case
The shortcut: explained above
The macro: up to you!

1 Like

Ok, still slightly confused LOL sorry. Run the KM web server? Or use Shortery to run what? You can't make a Personal Automation on the Mac, only on iOS devices. So what do I do? Sorry for not understanding. This is way outside of my realm, but I really need this, so I'm learning as fast as I can. Thank you!

I think I did it!!! I manually ran the Shortery Trigger and it worked! I'll have to wait till Sunset to see if it is truly triggered by Sunset though. I will post back here regardless of what happens. Screenshots attached. I blanked the long codes because I have no idea if they're sensitive or not. Thank you!



1 Like

Wait, how do I repeat this every day? Or does Shortery run it every day at Sunset? Thanks!

I don't know about Shortery but it does look like that's a daily trigger.

Not that it matters now, but I've started to work on my solution, and it's going to look something like this:

image

This isn't working yet. The formula isn't taking everything into account yet. The goal is to calculate the vertical position of the Sun based on your Latitude, Longitude, and the time. What this formula does is calculate the height of the Sun at noon for a given day X, counting from the spring equinox. I think I have to do three more things:
(a) add something inside the SIN() function to account for the time of day, and
(b) add another thing inside the SIN() function to account for the longitude, then
(c) add a value outside the SIN() function to account for the latitude.

2 Likes

Thank you for everything! How's the progress coming on your solution?