Turning Off a Noisy External Hard Drive Automatically

The Background

Since buying my Mac mini several months ago, I’ve gone through a seemingly endless array of disk configurations to find the perfect solution for booting multiple operating systems, preserving enough free space that I don’t have to worry about it, optimizing speed, minimizing cost, and keeping the whole setup as quiet as possible.

The result is a bizarre combination of the Mac’s internal hard drive, an SSD from an old laptop in an external USB 3.0 enclosure, an external 6 TB hard drive attached via Thunderbolt, and a multi-bay drive enclosure with two 2 TB hard drives also attached via USB 3.0.

Let’s not worry about why I think I need this setup or how I ended up with it. (Maybe I’ll talk about it in a future post.) Let’s just say for now that it allowed me to repurpose some existing equipment and works for my current needs.

With one exception.

The Problem

The multi-bay drive enclosure has a really annoying fan. In the grand scheme of computing fans, it’s actually not that bad. But as I said earlier, one of my goals with this computer setup has been to keep the system really quiet. (I’m very sensitive to background noise.)

I settled on using the multi-bay enclosure primarily for backups and storage of infrequently needed stuff. This way, I can turn it on when I need it and off when I don’t.

This is a perfectly reasonable solution; however, turning off the device requires unmounting multiple drives/partitions and then reaching below the desk to hold down a power button for several seconds. Wouldn’t it be cool if this could happen automatically as soon as I’m done using it?

The Objective

The most frequent thing this hard drive enclosure is being used for is to do a weekly system backup via Carbon Copy Cloner. My goal is to have the enclosure shut off completely as soon as the backup is complete.

The Solution

The unit’s fan still runs even after unmounting all the volumes, so I can’t use CCC’s built-in unmounting option to get the job done.

Instead, I’ve cobbled together a solution using a Wemo smart plug, IFTTT (If This Then That), and a shell script.

The Smart Plug

Since the drive enclosure does not seem to be able to shut itself down on its own, I need something to do the job for me. The smart plug, which originally turned our Christmas tree on and off each evening, does just this. When the plug is deactivated, the power to the drive enclosure is cut off. This seems safe to me because the same thing would happen if I hold down the power button.

The smart plug is designed to work with Apple’s HomeKit or Wemo’s own app. HomeKit gives you the ability to combine the plug with other devices in various scenes and automations. Unfortunately, the only way to activate any of this is via Apple’s Home app. Although inclusion of the Home app was one of the big new features of macOS Catalina, Apple has not created a way to integrate it with other applications. You either have to speak to it via Siri or open the app and push the buttons yourself. So that won’t work.

The Wemo app lets you set up automations as well. For instance, you can have the plug turn on and off at certain times. (This is how we set up the Christmas tree.) But this still doesn’t answer the question of how I can get another app (Carbon Copy Cloner) to talk to it.

If This Then That

IFTTT is one of the cooler services on the Internet. I’ve known about it for a while, but I’ve never found an excuse to use it. For those who might be unfamiliar, what IFTTT does is allow you to cause things to happen in various apps and services when triggered by events in other apps and services. (In other words, if this thing happens, then that thing should happen.)

Thankfully, Wemo advertises IFTTT integration as one of the features of their smart home devices. I was able to connect the smart plug to IFTTT right in the Wemo app on my phone.

Then, I created an IFTTT event that could send a “Turn Off” request to the smart plug.

The trigger for this event was an IFTTT “web hook.” This is one of IFTTT’s more powerful features. Web hooks allow you to trigger IFTTT events by sending some data to a special URL.

The Shell Script

The final piece of the puzzle is a shell script to get the ball rolling. Carbon Copy Cloner has a feature that allows you to call a shell script at the conclusion of the backup task. So I’ve created a script that unmounts each of the drives one by one.

As long as each drive unmounts successfully, the script sends the web hook command to IFTTT. In response, IFTTT sends a request to my smart plug to have it turn off.

A moment later, the script sends an additional command to IFTTT that causes the plug to turn back on. (I discovered that the drive enclosure will not turn on when power is restored; I have to press the button. So this last step gets it ready for that.)

The Outcome

With this script in place, a few moments after the backup task completes, power to the drive enclosure cuts out and the fan stops!

I’ve also set up CCC to begin a cloning task as soon as the backup drive shows up to the system. All I’ve got to do to start a backup is lean down and push the power button. When the backup is complete, the enclosure will turn back off and essentially wait to be activated again.

The Takeaway

Want to try something like this yourself?

The shell script looks like this:

#!/bin/bash

diskutil unmount {DRIVE_NAME} && diskutil unmount {OTHER_DRIVE_NAME} &&
curl -X POST https://maker.ifttt.com/trigger/unmount/with/key/{YOUR_KEY} && 
sleep 5 &&
curl -X POST https://maker.ifttt.com/trigger/mount/with/key/{YOUR_KEY}

You need to replace {YOUR_KEY} with the appropriate key from IFTTT. You can get it by logging in to IFTTT, visiting the web hook page, and clicking “Documentation.”

You’ll also need to replace all the drive names with your own Volumes. Just don’t leave out the ampersands in between; they tell the script to NOT continue if one of the drives fails to unmount.

Finally, you’ll need to create IFTTT events that are triggered by the “mount” and “unmount” commands. The trick is to use Webhook as the triggering service and Wemo as the resulting service. The rest isn’t too hard to figure out via their website. (Don’t forget to link the device to IFTTT in the Wemo app first!)

Good luck, and stay tuned for more crazy tech concoctions!