How do I have macros stop running when I'm not at home?

I would like my macros to stop running when I leave the house. I don't have any motion detection hardware at home, but I do have an iPhone that I take with me when I leave. I also have an Apple Watch that I wear all the time. Is there a way, even if it requires a lot of special code, to detect whether my phone or watch are near my house. I think I can achieve it, but my method is a bit kludgy. Can anyone think of a simple, proper way to achieve this?

My tentative method (it seems to work in my preliminary testing) is to have two iCloud accounts. A device at home will be registered to one account, while the devices I carry with me are registered to a second account. It is possible for a KM macro to open a browser window, visit the Apple website to "Find my Friends", wait for the distance to appear for my first account, and it will look something like this:


I can use pattern matching or OCR to see that the above distance is under, say, one mile. That would tell me I'm away from home. This is not too difficult for me to code, but it doesn't feel like the "right" way. It does have one big advantage however: it uses the iCloud services to handle all of the dynamic tracking. So for example I don't need to install an app on my phone to check the GPS and send an email when the location changes. If there's no other solution, I'll use this one.

But maybe there's a simpler way, such as using the WIFI on the iPhone to somehow connect with my Mac. As I write this I notice something called Handoff. When I'm using certain apps on the phone, my Mac's Dock shows a special icon indicating that my phone is nearby. Perhaps I can use KM to monitor the Dock. (Preliminary testing shows that the icon disappears 10 seconds after the iPhone's goes to sleep, so that doesn't help.) I presume Apple's OS detects when the device is "nearby", and I simply want access to that tiny bit of information.

It's worth noting that the iPhone cannot be connected to my computer when I'm at home because I need to be able to walk around with my iPhone around the house during the day, in part to allow KM macros to buzz me when my computer needs attention. (Although now that I think of it, maybe my Watch could handle that part. That may work after all.)

Another possible solution is to make my iPhone into an AirPlay Receiver. Not a sender, but a receiver. That way it would show up in the list of available Airplay devices on my Mac (when it's within WIFI range). And perhaps KM should support AirPlay but giving us an action that lets us use AirPlay from our Mac (I think Peter may like that idea.) But if not I can make it happen using mouse clicks.

Upon further testing, a good solution would be the Hotspot feature built into my iPhone. My Mac can monitor whether the iPhone shows up in the list of available Hotspots. The only problem with this is that it’s not easy (but it is possible) to get that list. Unless KM is updated to provide an action that lets us examine (and/or pick) the WIFI locations available, it will be tricky (but possible) to get this information.

KM has so many wonderful hardware related actions this idea would be in keeping with its philosophy. After all, it does have “Set Network Location”. WIFI is a network interface. And if so, then a new AirPlay action would also be interesting and similar.

Assuming that the idle trigger won’t do the trick, what about using IFTTT and its iOS app, which can trigger events based on your location, in tandem with the public web trigger or the new remote trigger?

I use the IDLE() function a lot, (but have never used the trigger) but I have never seen or hard of IFFFT before. I’ll look into that. Still I hope Peter considers adding an action to support WIFI and AirPlay.

I’ve looked into IFTTT. It’s got a lot of potential. Of course much of what it can do overlaps with KM. But it does apparently do the geolocation thing pretty well. Still, I found an equivalent solution with iOS Personal Hotspots that doesn’t depend on this additional software, so I’m inclined to use my own solution. But thanks. Very good tip, thanks.

1 Like

Here's a solution I got working today. You may find it novel and interesting.

(settings are for the above action)

Some people may need to modify it to work for them. For example I hardcoded some "areas" which you may possibly need to modify. If you have more than one hotspot in your area you may have to add an additional condition clause to the third action in this list.

The value of the variable AtHome tells my actions whether I'm at home or not (i.e., whether my iPhone is visible with WIFI, so my phone has to remain on, which is always the case anyway.) I will use this a lot.

Depending on your needs, you can also attach a periodic trigger to this macro. But in that case be aware it grabs the mouse and could conflict with other running macros.

I was just reading another thread which triggered this thought: Just put your Mac to sleep, and no macros will run.

I never leave my house without putting my Mac to sleep, or shutting it down. To leave it open would be a huge security issue.

hehe. Unfortunately I want this done for certain macros, not all.

I thought I should add here that I just learned that even if I turn off the Personal Hotspot, but leave Bluetooth on, it still works. So it’s triggering more on your phone being in Bluetooth range rather than a Hotspot being available. I don’t fully understand the difference but it works fine and I’m using it each day.

I found a macOS command that you can use in a KM action to fetch all the WIFI available connections:

/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport /usr/sbin/airport -s

You can also see the details of your current WIFI connection using this command:

/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport /usr/sbin/airport -I

You can also get a list of all the Bluetooth devices (that have been registered on?) your computer with:

system_profiler SPBluetoothDataType

However none of these tell me what I want, which is whether my iPhone is visible to the Mac via its Bluetooth. It doesn’t show up in the first command’s airport list (probably because it’s bluetooth, not WIFI, even though it shows up under the WIFI icon on the Menu Bar) and it always shows up in the third command’s Bluetooth list whether it’s turned on or not.

Remember, this is all in aid of having me solve the problem where a KM macro runs only when I’m at home, so it is a very relevant issue for how I use KM. If I can get one of these commands to work, I won’t have to use pattern matching on the macOS GUI.