Pause for a long time alternative

I wake up at a different time every day and i trigger a keyboard shortcut to stop the alarm .. I want this macro to notify me when the time comes, that I have to sleep knowing that I need to sleep 8 hours.

I found that a notification that triggers 15 hours after the alarm would be good .. But the problem is that when I tell km to pause for 15 hours, "Its Engine will be wasting CPU doing nothing while the macro runs" .. So I was wondering if there is an alternative solution that would achieve the same result ? thank you

I'm sure others will come up with a better solution, but here's my initial thought.

Create a macro that triggers every, say 15 minutes (whatever duration you want - even a minute would be fine). Have it check a variable that says when the alarm should sound. If it's past time, sound the alarm.

Then you can have another macro that turns off the alarm, and sets the variable to whatever time you want the alarm to sound again.

Let me know if that makes sense.

In my view this problem is best solved using a set of seven cron triggers. Each trigger is set to the preferred time of day when the alarm must sound. This is so simple, I must be misunderstanding the problem.

"I wake up at a different time every day" because the alarm change automatically based on sunrise .. I need the 15 hours to change automatically with the alarm that's why I use the keyboard shortcut to :

  1. stop the alarm
  2. and add the 15 hours to match the next alarm

It can't be a static time..I would have done it otherwise. Thank you for your concern thought and sorry for not explaining my problem properly.

Okay I am reading your code, thanks for uploading it.

1 Like

alarm macro.kmmacros (4.4 KB)

Okay, I think your code is fine. It does exactly what you want.

The Pause statement where you pause for 15 hours is not going to burn any CPU. So I think you are fine. I don't see any problem with it. Is that what you are concerned about? Why do you think it's using a lot of CPU?

So I think your macro is perfect, but I wouldn't have written it that way because any time the KM Engine stops and restarts, your code will fail to give you the final notification. There are ways to fix that, but that's not what you are concerned about.

1 Like

ah okay, well i was concerned when i read this https://wiki.keyboardmaestro.com/action/Pause tha'ts why

Here's how I'd modify your macro to do what you want without pausing for 15 hours (new/modified actions in yellow):

alarm macro 1.1.kmmacros (3.7 KB)
image

The trick with this is that it uses a companion macro that uses a periodic trigger a la @DanThomas's suggestion above that starts checking the SleepTime variable in the evening and notifies you once it's passed 15 hours from the point that you set it:

Sleep Time.kmmacros (2.6 KB)
image

EDIT: Revised macros to enable and disable the Sleep Time macro so that it doesn't keep spamming notifications after the initial one. Of course, if you want to be reminded periodically until you go to sleep, you can always disable those actions and/or disable the Sleep Time macro manually.

I see your point. I'm not sure that the page you are referencing is clearly or correctly worded. It almost sounds like the Engine would be burning lots of CPU the way it reads. I think it's written wrong.

GG's alternative would avoid the CPU if that's an issue, but to be honest, I don't think the CPU load is an issue at all. I could be wrong, as I'm wrong 20% of the time, but I think this will be in the 80% of my being right.

1 Like

Hahah, you keep stats of that?

For the record, I'm pretty sure I've discussed this subject with @peternlewis before, and Pause is implemented in such a way as to not impact CPU performance. In other words, and I'm probably using the wrong terms, but the thread just blocks until the time is up.

If you are using the KM icon that is animated when a macro is running, I suppose it's possible that might impact the CPU ever so slightly, but if I were you and I was going to have something pause like this, I certainly wouldn't use the animated icon, because it would be going all the time!

No - any macro that is running will cause Keyboard Maestro to use some non-trivial amount of CPU.

When no macro is running, CPU usage is generally around 1% or less.

When any macro is running, even just doing a pause, CPU usage is generally quite significant, 10-20%.

When a macro is doing serious work, especially things like searching the screen, OCR, etc, CPU usage is basically 100+%.

1 Like

Thanks for the clarification, Peter. I don't know where I got my ideas from, since they obviously didn't come from you! :unamused:

Yes, I have told that line to many people in my life, even on this website, and it's always meant to be half-serious. It's also intended to get people to think about probability. E.g., if a pair of magic dice could predict the winner of a horse race 80% of the time, would you be willing to use them to bet on the horses?

In any case I was partly wrong. Peter himself wrote above that there is a small drain on the CPU by the KM Engine when using the Pause statement. But I don't feel too bad, because Dan, who is one of the elite wizards here, also predicted what I did, and he spoke without qualifying his likelihood of being correct, so there's a little more egg on his face than mine right now.

Peter's statement was a bit of a revelation to me. I may have to reconsider my use of the Pause statement. In some cases I have been able to replace the Pause statement by using periodic triggers on a macro. (E.g., instead of pausing for a minute within an infinite loop, just have the macro trigger once per minute.) I didn't realize that that practice could have beneficial effects on my CPU usage.

He he he. It's not the first time I've been wrong, and won't be the last. Usually I'm more careful about wording it like "I think" but I could have sworn I had this discussion with Peter many years go. No matter, though, I have no problem admitting when I'm wrong.

To the subject at hand:

I have a macro that runs Time Machine for me every 2 hours during "working hours". It mounts the Time Machine drive if needed, and starts the backup, assuming it isn't still running for some odd reason.

And it enables a second macro that is normally disabled. This second macro runs once every minute, when it's enabled. It checks to see if Time Machine has finished, and if it has, it unmounts the drive (if necessary), and disables itself.

I think this is the perfect way to handle something like this.

Dan

I may start another thread someday asking for advice on KM with Time Machine. Thanks.

Just do some searches. There's lots of threads on this already.

This made me curious, as I often use a Pause Until action.
So I ran a little test.

My Mac

Keyboard Maestro 9.0.2 on macOS 10.14.5 (Mojave)
2019 iMac-27 512GB SSD, 3.6Ghz i9 CPU, 40GB RAM

Idle -- No Macros Running

image

Running Pause Until Keystroke

image

image

Conclusion

For simple Pause Until, the CPU load is quite light, adding only ~4%.

Of course, YMMV.