Fixing macOS' defective sleep mode with a Hammer (...Spoon)

This is kind of a followup post to Using a Mac Mini as a bedroom PC for Kodi, Moonlight and YouTube.

It's been nearly a year since I set up a Mac Mini as my one-stop-shop for all multimedia related tasks in the bedroom. It's generally worked well, and both streaming media, games, general web browsing and PC usage has been smooth. You're rolling around in bed, playing video games, watching YouTube, or maybe the freshest episode of Smiling Friends. But then you fall asleep, and the fun abruptly stops.

"I'm gonna wreck it!" - Apple, probably

Sometimes, the computer will still be on and fully active in the morning after falling asleep using it, soaking up all those precious jiggawatts, directly out of your wallet.

Why, I hear you ask? Maybe there is a mouse connected that causes jiggle when you shuffle around in your sleep?

No, only a USB remote and a keyboard + touchpad combo device (the Logitech K400 Plus, which is absolute fucking garbage[1]) are connected.

How can this be, when the system is set to sleep after 1 hour of inactivity? Surely, Thou Be Trippin'?

Well, as many things Apple, it is a defect by design: Apple lets software overrule this user setting, without notifying the user, and without letting the user change this behaviour. This means that since EmulationStation - the launcher I'm using - is running in the background, and is considered a game, it inhibits system sleep. I've looked all over, and there is no way in current-day macOS to let me, the user, owner, administrator and fucking Dom Top of this machine, to ignore what the system thinks, and just Go The Fuck To Sleep after 1 hour, no matter what. That's a huge defect, but dealing with huge defects is the bread and butter of a technical person trying to make an Apple product cooperate.

"I'm gonna fix it!" - Me

I'm not gonna spin a yarn and complain any more about Apple today, even if I could fill pages with that kind of content. I'm a Solutions™ kinda guy, not an Apple Sux™ kinda guy.

Here's how to fix it: Using HammerSpoon, an application for automating macOS, along with a simple Lua script. I have never used HammerSpoon before this, but I've written a ton of Lua (for video games), so getting started was easy enough.

Install HammerSpoon, start it, give it the appropriate permissions, set it to run at boot, and edit your ~/.hammerspoon/init.lua to contain the following:

local function handlePowerEvent(event)
    if event == powerWatcher.systemDidWake or event == powerWatcher.screensDidWake then
      print("System woke up, restarting idle monitoring")
    elseif event == powerWatcher.systemWillSleep or event == powerWatcher.screensDidSleep then
      print("System going to sleep, suspending monitoring.")

local function checkSleep()
    local idleTime =
    if idleTime < sleepThreshold then
        print("Idle for " .. idleTime .. " secs")
        print("Idle time exceeded threshold, going to sleep")

sleepThreshold = 3600 --seconds
powerWatcher = hs.caffeinate.watcher
idleTimer =, checkSleep)

Save the file, click the HammerSpoon icon on your menu bar, then "Reload Config", and boom, you're in business.

The script is extremely simple. It sets a timer that monitors how long the system has been idle for, which runs every 60 seconds. If the timer sees that the idle time is over the set threshold (set to 3600 seconds here - 1 hour), it tells the computer to go to sleep immediately — come hell or high water. The script additionally monitors the sleep and wake up events, in order to stop and start the timer, so it doesn't run while the computer is asleep (yeah, that can happen). It also prints some log messages to the HammerSpoon console, which you can see by clicking the HammerSpoon icon and clicking "Console".

And that's all there is to it. Apple has a long way to go when it comes to user friendliness, and definitely needs to provide an option for "going to sleep no matter what the system or any piece of software has to say about the present state of things". I'm not going to be in charge of wording the toggle, though.

[1] The Logitech K400 Plus, also known as The Frustrationator 400, is e-waste that they charge money for. The touch pad comes with an infuriating acceleration that can't be turned off, which makes navigating the user interface of the computer an exercise that all but ensures that you go to bed angry. They told me I shouldn't do that, but here we are. It also looks dumb, feels cheap, and makes me nostalgic for the simpler times when I didn't own the Logitech K400 Plus.


  1. Markdown is allowed. HTML tags allowed: <strong>, <em>, <blockquote>, <code>, <pre>, <a>.