Using NMAP for Device Tracking & Presence Detection in Home Assistant

Cyan Automation
4 min readJul 27, 2020
Photo by henry perks on Unsplash

If done right, presence detection can be great. When I get home, Home Assistant greets me over the speakers, turn on the lights when it’s dark and also turns on the dehumidifier, if the humidity is too high. When my house is empty, this trigger can turn off any lights left on (see Using Groups To Turn Lights On/Off). This really is a path to the nirvana of home automation.

Side note: It’s advisable not to control your critical systems — being your Home Alarm, Smoke and CO systems — solely via a Home Automation Hub.

Getting Started

Here are some common ways to carry out presence detection:

  • The Home Assistant app (or alternative Home Automation Hub app)
  • A specific presence detection app, like Life360
  • A combination of PIR, motion and/or radar sensors
  • Router connected devices (clients) tracking
  • BLE beacons or tags

Each of these has a drawback, and I often hear users using a combination of 2–3 of these to balance the accuracy with the battery life on mobile devices. I thought long and hard about this, and I didn’t want to have another app running in the background just for presence detection – it just wasn’t elegant and right for me.

Instead, I took another look at my requirements:

  • Detect whether I am at home or not at home: my particular location outside the home is not important for Home Automation
  • Be accurate: no false positives
  • Be responsive: no waiting 30min for the state to change
  • Be kind to my mobile phone battery

This led me to NMAP Tracker (link here) which is an alternative to router-based device tracking, where the Add-On directly scans your home network for connected devices. In short, if there is a named device connected to your home network, NMAP would class this device as home, and once its confident the device has dropped off the network, it would mark it as away.

There’s three key things to know about NMAP:

  • It doesn’t negatively impact your phone’s battery life, as it’s only seeing if you phone is connected to your home network, which it would already be doing if it was within range
  • You have to explicitly tell it what devices to track, which sounds like a chore but actually allows it to be focused on device tracking. You start with assigning the device a static IP, then capture the MAC address in Home Assistant to allow for tracking.
  • There are sensitivity thresholds that you need to make use of to ensure that you don’t get false positives

Note: It can only track when your device is connected to your home network, if your phone is on airplane mode, or randomly dropping off your network (I’m looking at you iPhone 6S), then it won’t work.

Update! September 2020

If you are an iOS user, iOS 14 has implemented a feature called Private Wi-Fi Addresses, which in their own words: “use[s] a different MAC address for each Wi-Fi network” that your device connects to. It’s a fantastic feature for when you are out and about, but this is not something that NMAP likes. As your Home Network should be private and secure, it’s something that can be safely disabled for just that network. In Settings, head to Wi-Fi, select your Home Network using the (i) and then under the setting Private Address, set this to false/off.

Making It Work

Now it’s time to define your devices in NMAP, along with the MAC addresses of the devices you want to track. Once this is set-up, you will find that it creates a variable called device_tracker.your_phone (or similar). Remember to set the sensitivity by tweaking the variables home_interval and consider_home, as suggested below.

This is what it looks like in YAML:

Now that you can track your phone (and presence!), you can start creating those very useful Arrival & Departure automations.

--

--

Cyan Automation

Marc’s tips on building your Smart Home with Home Assistant, Shelly and ESPHome.