Trigger macro based on Wifi or Bluetooth connection or location

What I'm trying to accomplish is to trigger a macro based on whether my phone is connected to, or leaves the connection of, my home WiFi network (Eero, if that matters). Alternatively I could use the Mac's Bluetooth connection although the connection area is a bit too small as compared to the Wifi network.

I've tried using IFTTT and other similar location-based services but they are unreliable. Any assistance would be greatly appreciated.

Sorry I am slightly confused
I assume you have macbook
When you are at home, does your macbook connect to your home wifi network (Eero) via wifi?
Then do you want to detect when your macbook leaves your home network
I feel the phone is a redherring
If you are only looking at the macbook then the answer is easy....

Thanks very much for your reply. If anyone should be apologizing it's me, not you. I will try to be more clear. The phone is most definitely NOT a red herring. It is the central part of the equation. Specifically:

I have a desktop Mac (iMac of recent vintage running latest Catalina) that is connected to my home Wifi. I have a macro set up that I trigger with a hotkey which launches my screensaver, locks the screen and turns off my desk lamp (thanks to the "TypeToSiri" macro supplied by another forum member). What I want to do is trigger that same macro whenever my iPhone (11 Pro if it matters) leaves the Wifi network.

This could also be triggered by my Apple Watch but I think it's easier to use the phone. I'd also be open to triggering it by the Bluetooth connection between the iPhone and iMac being broken instead of the phone leaving the Wifi network.

Does that clarify?

The only thing I can think of is that
if you mount a "disk" that is ON your iphone, say call it idisk then you can trigger the action if the disk becomes "unmounted" by virtue of you walking away from the house.

You could also setup a CRON job to check every 15 minutes to see if iDisk was available, and if not then trigger your action.

Others may have better suggestions.....

Here's how I would do it.

  1. Create a folder in your iCloud Folder for Shortcuts (go to "iCloud Drive" on your Mac and create a folder named something like "MyiPhoneHasLeftMyHomeWiFiNetwork"

  2. Create shortcut on the iPhone which will create a file in the iCloud folder "/Shortcuts/MyiPhoneHasLeftMyHomeWiFiNetwork/" - the actual name of the file and its contents are unimportant. Just creating the file is the important part.

  3. Normally I would use Hazel for the next part (it's also possible with launchd), but since this is the Keyboard Maestro forum, you can use a "This Folder" trigger and point it at iCloud Drive + "/Shortcuts/MyiPhoneHasLeftMyHomeWiFiNetwork/" on your Mac.

  4. Set the first dropdown to "adds an item"

  5. Set the second dropdown to "trigger all changes immediately"

  6. Have it run the macro to lock your screen, turn off your lights, etc.

  7. Delete the file from the "/Shortcuts/MyiPhoneHasLeftMyHomeWiFiNetwork/" folder so you can use the trigger next time.

Just an FYI - I sometimes get a notification on my iPhone that I have joined my home Wi-Fi network even when I was just on my home Wi-Fi network, which runs my "Welcome Home" macro.

I assume the same might happen with your "Leaving Home Wi-Fi" macro, so you might find it triggering at unexpected times.

Because computers.

That's an option and I appreciate the input. It does sound a bit roundabout and requires some additional maintenance (deleting the file constantly) so I'd prefer a more straightforward approach but if nothing else comes up I'll try this.

Thanks very much for taking the time to reply.

Well, of course, you don't have to delete the file, but then you'd have to make sure the filename was different so that it would trigger "adds an item" (I'm assuming by the name that "an item is changed" would not trigger it). Presumably Keyboard Maestro can delete the file as part of its macro, so that shouldn't be too onerous.

Here's the problem that you're going to run into if you try a more 'straight forward' approach.

Once you have left your home Wi-Fi network, you and your computer are no longer on the same network, Yes, that seems obvious, but bear with me.

So, if you want to interact 'directly' with your computer, how are you going to do it? SSH? Some sort of a web-trigger?

How are you going to connect from outside of your home Wi-Fi network to a computer inside it? Open a port in your router/firewall? Does your Mac have a static IP address?

You can do that with your eero system. I've done it myself, and it works fairly well, IMO. But it's another thing to manage. And sometimes it doesn't work. Because reasons. Is your Shortcut going to be able to handle repeating the request a few times if necessary, but stopping once it actually gets through?

The nice thing about creating a file in a service like iCloud or Dropbox is that it's quick, easy, and reliable. Apple is very much motivated to make sure that files work well with cloud storage, especially iCloud. That's a core feature that they really want to have working well.

Once the file is added to cloud storage, your Mac wants to download it, even if you have 'optimize storage on' I believe this should work if Keyboard Maestro is just looking to see if the folder has had a new file added to it. I assume an iCloud placeholder file will work if you don't choose 'Ignore Partial Files'.

So iOS is going to work hard to make sure that the file gets uploaded, and macOS is going to work hard to make sure that the file is available. Both of those things increase the odds that your 'roundabout' method of triggering this action is much more likely to actually happen.

Otherwise, here's how I look at what you're relying on: a SSH connection through your firewall to run a shell script to run an AppleScript to execute a macro.

Having done a bit of automation in my time, I know which one I'd choose. :smiley:

p.s. - I would not recommend a Bluetooth trigger. Bluetooth is… unreliable, at best. I've tried Bluetooth-based triggers before, and have seen these two things happen:

  1. I've been sitting at my desk with my iPhone next to my MacBook and had my Mac say "Oh, your iPhone has left the area, I'm locking myself."

  2. I leave my office, cross a large room, go into a bathroom, and my Mac still thinks my iPhone is nearby.

Logical? No. Actual? Yes.

1 Like

Thanks. I understand your points and respect your position. The problem is that while I'm very adept at creating macros I'm lousy at creating Shortcuts on iOS devices (personally I think the interface sucks but that's a different issue). I honestly can't figure out how to detect when I'm out of the home wifi range and then create a file.

If I knew how to create a Shortcut that would detect when I was leaving my home wifi network there are many other approaches that I could take, such as sending an email that would trigger an AppleScript that would perform the actions I want. (Mail offers that in the Rules section of Preferences, a little-known option that I have used in the past.)

Huh. So I just looked, and there's an automation that you can run when you join a specific Wi-Fi network, but I can't see one for "if I leave a Wi-Fi network".

If you do a location-based trigger, you'll have to confirm it manually, even in iOS 14.

You put an NFC tag on the door frame and tap your phone against it when you leave. (I have one in my car exactly for that purpose.)

Thank you again. Putting up NFC tags or taking other actions along those lines is a bit extreme for what I am trying to do. I’ve got this set up to trigger with one hot key stroke so I think it’s easier to train myself to remember to hit it when I leave my desk. It hides all my apps, switches to the Finder, launches the screensaver, locks the screen and turns off the desk lamp. If it could be easily triggered by being out of range of the WiFi that would be wonderful but in this case I’ll either try to remember to press the button or set up an AppleScript that’s triggered when I send an email with a specific subject line.

I appreciate your input.

Sure. Wish we could have found a way to make it work, I might have been able to use it myself.

The iOS Shortcuts app has an Automation section, and one of the options there is to activate a shortcut when leaving a location. That's akin to leaving your home network, and your shortcut could then send the email to trigger an AppleScript.

If Shortcuts doesn't allow you to create a setup completely fulfilling your goal, you may want to look at Puchcut, an app that extends Shortcuts in some ways.

The app: Pushcut
Brief Review: MacStories

Duhhh. Thank you for pointing out what was right in front of my face all along. I already had an automation programmed in Shortcuts (via HomeKit - the same list appears in both places) that turned off my desk lamp when I left home.

I didn’t realize that I could set up an identical automation that would send an email. I just set the whole thing up, wrote a quick AppleScript that gets launched when Mail (which I always leave active) receives an email to a specific address with a specific subject line.

Tested it and it works perfectly. And although it has nothing to do with KM, the help I’ve received in this forum, and in particular from you and Tj Luoma, has been invaluable. Thank you both for the generous contribution of your time!


Hello @Scott721 @tjluoma, another possibility would be the shortcuts of Adam Tow (developer of the iOS App Launchcuts).
Here his Wifi Shortcut article: Wi-Fi Triggers | wifi-triggers

Another alternative (also not related to KM) which has worked for me is to use arping. It requires that your phone(s) have fixed IP address(es). Periodically call arping to see if it can ping your phone - I do this in a systemd service on a Linux system, but it could as easily be done via cron or a launchd service. If your phone can't be pinged for a couple of minutes, you're not at home.

Thanks very much for taking the time to reply, Larry. This one’s over my head - I can barely spell “arping” without thinking of AARP - so I’m going to keep looking. I do appreciate the thought, though.