Script to run everyday at sunset Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
silentrings
Posts: 7
Joined: 13 Jan 2022, 06:03

Script to run everyday at sunset

Post by silentrings » 13 Jan 2022, 06:20

Hi all,
I need to run some script an hour or so after sunset, everyday.

Changing Taskmanager to keep up with this slowly changing time is a hassle and AHK supports the basic math functions to calculate the daily sunset during a year.
So those times can be calculated within AHK -which is so cool BTW- to decide when to start the scripts.

But I don't know how to actually have AHK check the calculated times and start.
I researched and found several possibilities, some comparing the time, some setting up an internal timer etc.

What do you recommend please?

Should use as little resources as possible, needs not to be precise, within 15-20 minutes or so of the calculated time is ok.
AHK could be Taskmanager-started at a (fixed) time of say earliest sunset and run from then on.

So impressed how AHK expanded, was using it over 10 years ago, so long that even my login here had expired meanwhile.

Thanks!

amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: Script to run everyday at sunset

Post by amateur+ » 13 Jan 2022, 07:20

Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.


silentrings
Posts: 7
Joined: 13 Jan 2022, 06:03

Re: Script to run everyday at sunset

Post by silentrings » 13 Jan 2022, 22:22

regarding
viewtopic.php?p=427702#p427702
So this loop checks every minute for a certain substring and when it matches it sends that ^y?

The APIs are really nice and cool, but as mentioned the math to calculate the sunset (+1h) is easy, and AHK provides the functions to calculate that.
I will gladly use the API for now, but since the internet is not very reliable where this all is, I will eventually have to calculate the sunset locally.
We're doing this already with an Arduino.

My concern is the triggering the main AHK script at sunset (+ 1h).
I just don't want AHK to lock up the PC (if that's the case) waiting for the right time to start the script.
People might need to use the PC (note to self: lock out users during script execution).
I looked at several approaches, don't know which is the least resource hungry and "backgroundish".

As mentioned I plan to start this AHK script every day at the year's earliest afternoon which is 4:40ish here in LA (plus the 1 hour), via task scheduler.
And close it all when AHK is finished.

So this easy loop or interrupt (if AHK can do that) is the question

Thanks all!!

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Script to run everyday at sunset

Post by boiler » 14 Jan 2022, 00:35

silentrings wrote: I just don't want AHK to lock up the PC (if that's the case) waiting for the right time to start the script.
That's not the case. Did you run it and see any significant difference in CPU usage? Use Task Manager to check the difference in CPU usage between when it is running and when it is not.

amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: Script to run everyday at sunset

Post by amateur+ » 14 Jan 2022, 04:47

silentrings wrote:
13 Jan 2022, 22:22
So this loop checks every minute for a certain substring and when it matches it sends that ^y?
This loop checks every minute current time and when it matches yestertday's sunset, it sends ^+y (Ctrl + Shift + Y), not ^y as you said.
Why does it send +^y? Because that guy asked for a script to send ^+y every sunset.

What is your math you have realised in arduino? Post it here.

And I think, in taskschd.msc OS just checks every second where some event is to be executed. Here you can check only once a minute, once in 5 minutes, once in 10 minutes regard to the precision you need. What will "eat" more resources: checking every second or every minute of five?
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Script to run everyday at sunset

Post by mikeyww » 14 Jan 2022, 07:09

As already noted in the script, the time of sunset depends on the latitude and longitude. I can think of only three overall ways to get those coordinates: hard-code, ask the user, or access the Internet. Perhaps there are some additional ways, or the user's location is somehow available at your end, though I don't think that the computer itself could reliably determine its own location without accessing an external network, using a satellite system, etc.

The script identifies the sunset time, checks every minute, and executes the desired action upon the sunset. It then computes the value of the next sunset, and starts over, since sunset changes each day. This sort of script would consume minimal resources.

Incidentally, one can readily use the Windows Task Scheduler to start any script at a specific time. From my perspective, that is the easiest way, since the Task Scheduler is already running and checking the time. Without doing that, the script whose link is posted above shows how to check the time every minute (or other interval). There are some other posted scripts that show how to use AHK to create a Task Scheduler item, if that is needed.

Code: Select all

MsgBox, 64, Current time, % time := A_Hour A_Min

garry
Posts: 3736
Joined: 22 Dec 2013, 12:50

Re: Script to run everyday at sunset

Post by garry » 14 Jan 2022, 07:28

also many informations here > example Australia/Brisbane : https://www.timeanddate.com/sun/australia/brisbane

Code: Select all

https://www.timeanddate.com/sun/australia/brisbane
Daylight 05:05 – 18:47   Duration  13 hours, 42 minutes

Current Time :	14. Jan 2022, 22:24:34
Sun Direction:	207,02° SSW↑
Sun Altitude :	-36,40°
Sun Distance :	147,140 million km
Next Equinox :	21. Mär 2022 01:33 (Autumnal)
Sunrise Today:	05:05↑ 115° Southeast
Sunset Today :	18:47↑ 245° West

silentrings
Posts: 7
Joined: 13 Jan 2022, 06:03

Re: Script to run everyday at sunset

Post by silentrings » 14 Jan 2022, 08:04

Interesting thoughts, however the countdown/comparison has to happen somewhere, whether via taskmanager or via AHK or any other "loop" or check. Whether it checks every 1 or 5 or 10 minutes, with AHK the CPU still counts down that time somewhere.

Yes, I'm now also thinking to use the actual task manager.
To calculate the sunset and to update itself with the new time and trigger AHK at that time.
If that's possible at all.
Taskmanager has a high priority and happens anyhow.
Have to look into that.
My hesitance was/is the complexity (ie user accounts/rights etc) and difficulty (turn off the scheduled task if not needed for a while) vs going manually via a loop/AHK.

Yes, as mentioned getting the actual sunset time is not the issue here, it will be hardcoded and calculated at some point.
I maybe should have left it out of my question to this great board, it seems to distract from the actual issue, of how to start a task at a certain (but moving) time.

Thank you for your ideas/input!

RussF
Posts: 1237
Joined: 05 Aug 2021, 06:36

Re: Script to run everyday at sunset

Post by RussF » 14 Jan 2022, 08:23

I have a script that displays an on-screen clock to the nearest minute. The timer function within the script fires every second to compare the current system time to the last (displayed) time. When the minute changes, it updates the on-screen display. Having that script comparing the time every second causes no noticeable increase in system resource usage. If your script starts before the earliest possible sunset, calculates the current day's sunset time and checks every minute until the system time equals the calculated time, you won't notice it at all.

Now as for what the script actually fires off when sunset occurs, I can't speak to.

Russ

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Script to run everyday at sunset

Post by mikeyww » 14 Jan 2022, 08:31

Code: Select all

#Persistent
sunset = 1801
SetTimer, Sunset, 60000
Sunset:
If (A_Hour A_Min = sunset)
 MsgBox, 64, Instructions, Time for bed!
Return

amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: Script to run everyday at sunset

Post by amateur+ » 14 Jan 2022, 09:10

RussF wrote: The timer function within the script fires every second to compare the current system time to the last (displayed) time.
Russ, you can calculate A_now, and set up a sleep that amount of seconds so current time after the sleep would be hh:mm:01 (or :02). And at this moment you set the 1-minute timer for checking (optionally) and changing the clock. So you won't have to check every second and still have 1-2 seconds precision.

@silentrings, so can you post the math for arduino here? I guess there can appear a spherical model of Earth or some other sorts of mistakes.

@garry, with those tables one can hard-code all sunsets of the year of chosen location and forget about internet access.
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.

RussF
Posts: 1237
Joined: 05 Aug 2021, 06:36

Re: Script to run everyday at sunset

Post by RussF » 14 Jan 2022, 09:19

amateur+ wrote: Russ, you can calculate A_now, and set up a sleep that amount of seconds
If that were the only thing the script was doing, that would be fine, however, a sleep effectively pauses script execution until the sleep expires, which means any other code I have in the script would only execute once every minute (or whatever interval I set the sleep to). The timer lets me continue with other processing and only fires when I want it to.

Russ

amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: Script to run everyday at sunset

Post by amateur+ » 14 Jan 2022, 09:25

Russ, you should do that sleep delay just once in auto-execute section. And of course, you should use Settimer, StartTheClock, % -Mod(61 - A_Sec, 60) *1000 instead of sleep in your case.
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.

RussF
Posts: 1237
Joined: 05 Aug 2021, 06:36

Re: Script to run everyday at sunset

Post by RussF » 14 Jan 2022, 10:01

@amateur+ - so this is getting somewhat off topic, but my application is not a stopwatch or countdown timer. It displays a real-time clock display (along with lots of other information) on a status board system in a large enough font on a 65" display so that people in our shipping department can see the time from 30 feet away.

Your example uses a negative timer value, which means the timer would fire only once until reset. I set the timer once in the setup code to run a subroutine that simply compares the current minute value with the one displayed. If it's the same, it does nothing. If it's different, it updates the display. Simple. In the mean time, I can do any other file checking, processing, or whatever to update the rest of the display and not have to worry about calling the code to update the clock - it does it on its own.

I honestly don't see the benefit in your example. We are a furniture manufacturer. For any given piece of furniture, there will be as many different ways to build it as there are craftsmen on the floor, but as long as the result is the same, what does it matter? If it works, don't fix it. :D No disrespect intended. :D

Russ

silentrings
Posts: 7
Joined: 13 Jan 2022, 06:03

Re: Script to run everyday at sunset

Post by silentrings » 14 Jan 2022, 10:51

@amateur+
the math itself is well known and simple, but needs trigonometric functions.
We use one of the many sunrise/sunset implementations for our weather station, don't know exactly which, there are so many.
https://www.google.com/search?q=sunset+arduino
I think we use a simplified version, which doesn't need floats.
It's precise within 15 minutes or so which is totally fine for us and runs on an Arduino Mega.
Since we didn't need super exact timings, we were also looking into tables, but the math solution was simpler and more flexibvle, can be used at other locations with just lat and long coordinate changes (instead of generating/saving a table).

I'm now looking into
https://risacher.org/sunwait/
which roughly does what we need.

It should do what I mentioned originally
AHK could be Taskmanager-started at a (fixed) time of say earliest sunset and run from then on.
Except that AHK won't need to do the wait/sunset math, sunwait does.

Still open for your kind suggestions on specifics to how "Script to run everyday at sunset"

Thanks!

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: Script to run everyday at sunset

Post by mikeyww » 14 Jan 2022, 11:35

Could try my last script.

RussF
Posts: 1237
Joined: 05 Aug 2021, 06:36

Re: Script to run everyday at sunset

Post by RussF » 14 Jan 2022, 11:47

@mikeyww , that's what I was going to suggest.
It should do what I mentioned originally
AHK could be Taskmanager-started at a (fixed) time of say earliest sunset and run from then on.
Except that AHK won't need to do the wait/sunset math, sunwait does.

Still open for your kind suggestions on specifics to how "Script to run everyday at sunset"
It looks like @silentrings has everything needed except to put the pieces together.

Russ

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: Script to run everyday at sunset

Post by flyingDman » 14 Jan 2022, 12:42

I had hard coded it like this. Not pretty but usable (times are for Southern California)

Code: Select all

msgbox % snst(1,14)

snst(mm,dd){
var=
(
16:54	17:23	17:50	19:14	19:37	20:00	20:08	19:53	19:18	18:36	16:59	16:43
16:55	17:24	17:51	19:15	19:38	20:00	20:08	19:52	19:17	18:35	16:58	16:43
16:56	17:25	17:51	19:16	19:39	20:01	20:08	19:51	19:15	18:34	16:58	16:43
16:57	17:26	17:52	19:16	19:40	20:01	20:08	19:51	19:14	18:32	16:57	16:43
16:58	17:27	17:53	19:17	19:40	20:02	20:08	19:50	19:12	18:31	16:56	16:43
16:58	17:28	17:54	19:18	19:41	20:02	20:08	19:49	19:11	18:30	16:55	16:43
16:59	17:29	17:55	19:19	19:42	20:03	20:07	19:48	19:10	18:28	16:54	16:44
17:00	17:30	18:56	19:20	19:43	20:03	20:07	19:47	19:08	18:27	16:53	16:44
17:01	17:31	18:56	19:20	19:44	20:04	20:07	19:46	19:07	18:26	16:53	16:44
17:02	17:32	18:57	19:21	19:44	20:04	20:07	19:45	19:06	18:24	16:52	16:44
17:03	17:33	18:58	19:22	19:45	20:05	20:06	19:43	19:04	18:23	16:51	16:44
17:04	17:34	18:59	19:23	19:46	20:05	20:06	19:42	19:03	18:22	16:51	16:44
17:05	17:35	19:00	19:23	19:47	20:05	20:05	19:41	19:01	18:20	16:50	16:45
17:05	17:36	19:00	19:24	19:47	20:06	20:05	19:40	19:00	18:19	16:49	16:45
17:06	17:37	19:01	19:25	19:48	20:06	20:05	19:39	18:59	18:18	16:49	16:45
17:07	17:37	19:02	19:26	19:49	20:06	20:04	19:38	18:57	18:17	16:48	16:46
17:08	17:38	19:03	19:26	19:50	20:07	20:04	19:37	18:56	18:16	16:48	16:46
17:09	17:39	19:03	19:27	19:50	20:07	20:03	19:36	18:54	18:14	16:47	16:47
17:10	17:40	19:04	19:28	19:51	20:07	20:03	19:34	18:53	18:13	16:47	16:47
17:11	17:41	19:05	19:29	19:52	20:07	20:02	19:33	18:52	18:12	16:46	16:47
17:12	17:42	19:06	19:29	19:52	20:08	20:01	19:32	18:50	18:11	16:46	16:48
17:13	17:43	19:07	19:30	19:53	20:08	20:01	19:31	18:49	18:10	16:46	16:48
17:14	17:44	19:07	19:31	19:54	20:08	20:00	19:30	18:47	18:09	16:45	16:49
17:15	17:45	19:08	19:32	19:55	20:08	19:59	19:28	18:46	18:07	16:45	16:50
17:16	17:46	19:09	19:33	19:55	20:08	19:59	19:27	18:45	18:06	16:45	16:50
17:17	17:46	19:10	19:33	19:56	20:08	19:58	19:26	18:43	18:05	16:44	16:51
17:18	17:47	19:10	19:34	19:57	20:08	19:57	19:24	18:42	18:04	16:44	16:51
17:19	17:48	19:11	19:35	19:57	20:08	19:57	19:23	18:40	18:03	16:44	16:52
17:20	17:49	19:12	19:36	19:58	20:08	19:56	19:22	18:39	18:02	16:44	16:53
17:21	00:00	19:13	19:37	19:58	20:08	19:55	19:21	18:38	18:01	16:44	16:54
17:22	00:00	19:13	00:00   19:59	00:00	19:54	19:19	00:00	18:00	00:00	16:54
)
for x,y in strsplit(var,"`n","`r")
	for a,b in strsplit(y,"`t")
		if (x=dd and a=mm)
			res := b
return res
}
14.3 & 1.3.7

silentrings
Posts: 7
Joined: 13 Jan 2022, 06:03

Re: Script to run everyday at sunset  Topic is solved

Post by silentrings » 14 Jan 2022, 13:29

Hi all,
just an update, sunwait.exe works very well.

However the "sunwait.exe" program linked in authors' page
https://risacher.org/sunwait/
uses different paramters/syntax as mentioned there.

sunwait -h
shows what's supported, plus an example:
sunwait sun up -0:15:10 38.794433N 77.069450W

I used that example with our coordinates and it worked.
Used "down" since I need it after the "sun" goes "down".

sunwait sun down 01:00:00 38.794433N 77.069450W

Instead of Task Manager I plan to autostart a batch with sunwait and the AHK script.
That way it works even if we had a power failure meanwhile (very remote location, happens often)

Will update if it all works as planned.
Thanks all!

Post Reply

Return to “Ask for Help (v1)”