How Do I Get My Mac to Wake From Sleep for a Macro Triggered at a Specific Time

On my old Mac, I was using KM to run a macro at 01:30. Sleep mode never worked reliably on that Mac, so there wasn't a problem.

I've got a new Mac now, and sleep mode works reliably again. I had expected KM to wake the Mac to run the macro, then sleep it again afterwards, but it doesn't seem to — the macro doesn't get run at all.

I don't want the macro to run on wake, as it does stuff on servers that needs to be done in the middle of the night when nobody is using them.

Is there a way to get KM to wake the Mac for scheduled macros, and sleep it again afterwards (preferably without having to end each macro with Put Computer to Sleep, as I'd only want it to do that if it was asleep when the macro was triggered…)

I think the simplest solution is to go into Mac System Preferences/Energy Saver and schedule the Mac to wake up a minute before the Macro's scheduled run time. This image is from macOS 12.3.1 so it will look different on a different version of macOS but works the same way.

I don't think you'll need to schedule a "go back to sleep" as that will just happen anyway when the computer is idle.

1 Like

Thanks — the problem with that is that there is only one time you can set, and I have various jobs that need to run at various times.

I was hoping that KM would support Power Nap so it could register a time to wake without my having to do it myself — but it appears there isn't even an option for enabling Power Nap in the Energy Saver preferences on the Mac Studio, so that wouldn't help.

I may have to just disable sleep altogether and just sleep the displays. I might stand a better chance of having my network mounts still connected that way, too…

That’s a shame. But maybe there’s a third-party App that can do that. Or like you say, sleep the screen but not the system.

You could use KM to change the Mac's wake and sleep times using something like the shell script command: pmset.

For example, if you want the Mac to wake at 3pm, then sleep at 3:10pm then wake at 4pm then sleep at 5pm, you could do something like this:

KM: shell script pmset set wake time to 3pm

KM: at 3:09pm, shell script pmset set wake time to 4pm, then sleep

KM: at 5pm, sleep

etc.

1 Like

Hey @Unikitty,

Please remember to search the forum for answers. No one expects you to spend an hour searching, but at least give it a go before posting a question.

Search results for 'wake from sleep' - Keyboard Maestro Discourse

Third hit:

Wake from Sleep as an Action

Take Care,
Chris

(Keyboard Maestro Moderator)

Thanks, but the article you've posted isn't a solution to my original question, which was "how to get KM to automatically wake for a scheduled macro, without having to configure it myself?" (Admittedly, it could have been phrased slightly more clearly :slight_smile: )

The Wiki documents that, if the Mac is asleep when a timed macro is triggered, the macro will not run, but I believe that's important enough that it should appear on screen in the app when a timed trigger is selected. It's a huge limitation that makes KM very expensive to run, as I can no longer allow my Mac to sleep.

Theoretically you can set multiple wake or sleep times with the command line utility pmset.

For example:


Open Terminal.app and run:

sudo pmset repeat wake MTWRFSU 07:55:00

This will wake your system at 7:55am every day.


pmset - Wikipedia

I will not guarantee this, as I have not played with pmset for years – but I reckon it's worth a try.

@peternlewis?

1 Like

Noted, but I disagree that it is a huge limitation worthy of inclusion in the display.

With the screen slept, the Mac not sleeping is unlikely to use a substantial amount of energy while doing not much. Modern Macs are pretty energy efficient. Personally, my Mac has had sleep disabled for decades to allow it to backup or perform tasks at night.

But in any event, all triggers have various limitations and they cannot all be covered directly in the UI without adding a lot of clutter. Keyboard Maestro is not running when the Mac is asleep, so no triggers will operate. This applies to all triggers, not just time based ones. Network changes, Idle triggers, periodic triggers, cron triggers, USB Device Key triggers, none of them will work if the Mac is asleep, or if the Mac is turned off. And even if it is not asleep, most UI actions cannot work while the Mac is screen locked, screen saving or screen sleeping.

There are no current plans to have Keyboard Maestro automatically wake from sleep to perform macros, because that could easily be more of an issue than not doing that. If the Mac was running on battery then waking from sleep to perform an actions periodically could easily drain the battery.

2 Likes

Fair enough on the energy efficiency — I've just moved from a 2010 Mac Pro with twin Xeons (definitely not energy efficient!) to a Mac Studio, so I guess I still need to get used to that.

It would be nice if there could be an off-by-default option to allow automatic wake from sleep, though — there are still some of us who use desktop systems and who only need to care about batteries when the UPS kicks in :grin:

But for now, I think I'll just sleep the screens instead.

Thanks!

2 Likes

Hi @Unikitty. I have an old headless Mac mini that I wake at 2:00, 3:00, and 4:00 a.m. to do some routine tasks using Keyboard Maestro. I stripped out the tasks and created this template that you might want to try.


Cron Trigger—TEMPLATE

PURPOSE

The macro template includes actions that will run when:

  • This cron entry matches the current time , OR
  • any other configured trigger is detected (e.g., ⌃⌥F2).

In the former case, the Mac will be put the Mac to sleep unless another instance of this macro is started before this macro completes.

SCHEDULING A MAC TO WAKE

System Preferences > Energy Saver (or Battery) > Schedule > Start up or wake can be used to schedule only one startup time per day. If more times are needed, the command pmset can be used in combination with cron. Since pmset must be run as root to modify settings, the crontab command should be used as follows: sudo crontab

For example, if this template is copied to create macros that trigger 2:00, 3:00, and 4:00 a.m., the root contab file should be set as follows:

@reboot pmset repeat wakeorpoweron MTWRFSU 01:59:00
0 2 * * * pmset repeat wakeorpoweron MTWRFSU 02:59:00
0 3 * * * pmset repeat wakeorpoweron MTWRFSU 03:59:00
0 4 * * * pmset repeat wakeorpoweron MTWRFSU 01:59:00

TESTED WITH

• Keyboard Maestro 10.0.2
• Monterey, v12.3.1/MacBookPro16,1
• Mojave 10.14.16/Macmini6,2
• High Sierra 10.13.6/iMac11,1445

VERSION HISTORY

1.0 - Initial version


DOWNLOAD Macro File:
Cron Trigger—TEMPLATE.kmmacros (24 KB)
Note: This macro was uploaded in a DISABLED state. It must be ENABLED before it can be run. If it does not trigger, the macro group might also need to be ENABLED.

Macro-image

2 Likes