Anyone had luck with nmap component and modern phones?

kinda depends on the scan interval of the bluetooth component. I’m not using it in “production” right-now due to some problems with my zwave setup. My RPI is right at the entrance of my (small) studio, so I’m willing to bet it will detect my tag even before I have unlocked the door.

Its about 2x2 cm and 5 mm tick. Not the largest item on my keyring. Battery is a single cr3032.

I think the key with presence detection is to use multiple methods at ones, and to assign a separate device in you known_devices.yaml for each method. I have great results using nmap, gps logger, and owntracks on android 7.
(Owntracks is used only because I am not sure if HA can calculate proximity values from anything else.)
I have three separate devices in my known_devices.yaml for my phone and only trigger automations to off when all report ‘not home’, but trigger on when any of the three devices reports ‘home’. (Actually setting a binary sensor to off and on)

No need for any fancy extra hardware.

1 Like

Is there a tutorial somewhere for setting this up? Anything I’ve tried on my iPhone that uses location services absolutely destroys my battery,

Here is how to configure the tracker. Note: for Iphone, I had to go into the bluetooth settings and leave that open while restarting home assistant. Iphones seem to only broadcast while in the bluetooth settings and for me, home assistant seemed to only add Iphones on startup.

If after configuring the device tracker you get an error similar to …

INFO:homeassistant.util.package:Attempting install of pybluez==0.22
Command "/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 -u -c "import setuptools, tokenize;__file__='/private/var/folders/f1/x1cbc0q53sjc8w8sbvyzw4n40000gn/T/pip-build-p5f2np6x/pybluez/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /var/folders/f1/x1cbc0q53sjc8w8sbvyzw4n40000gn/T/pip-fqesz8r4-record/install-record.txt --single-version-externally-managed --compile --home=/var/folders/f1/x1cbc0q53sjc8w8sbvyzw4n40000gn/T/tmpzg2q0qx6" failed with error code 1 in /private/var/folders/f1/x1cbc0q53sjc8w8sbvyzw4n40000gn/T/pip-build-p5f2np6x/pybluez/
ERROR:homeassistant.bootstrap:Not initializing device_tracker.bluetooth_tracker because could not install dependency pybluez==0.22

Run these commands to install the missing dependency.

sudo apt-get update
sudo apt-get install python-pip python-dev ipython
sudo apt-get install bluetooth libbluetooth-dev
sudo pip install pybluez
2 Likes

Thanks man, works great!

Hi,

I know this could be an “old” post since nobody writes on it since 1 month ago, but I’m running some tests on my RPi3 to detect all my diferent devices (ios, android 5 &6 and win-pc systems) trough “arp” instead of nmap and right know the success rate is good enough for me as every time, i perform an arp request (around 5 times a day) my andorid 6 is detected as well as my win pc and Ipad.

In the past, I wrote about the possiblity to use arp instead of nmap for presence detection (link below), but someone point out that it could be possible that arp deosn’t detect some devices, as in order to work, the device running the arp command (in my case the RPi3) should have a connection with all the devices to detect, which yes, that’s the theory but in my case even if a new device is registered in my LAN, inmeaiatley can be found through the arp command and I’m not running Pi-hole or something that forces the communication between new devices connected to my network and the Pi.

So do any other have had good results with arp?

Using ARP instead of Nmap

Hi all,

i’m having the best results with arp too.

Before i used HA, i had a script that worked very good with iOS devices.
Since last weekend i activated it again to use with HA as “command_line device tracker”

The script runs as root on a ubuntu 14.04 server every minute and publishes the states to mqtt.

In HA i use mqtt device tracker

- platform: mqtt
  devices:
    person1: home/people/person1
    person2: home/people/person2
    person3: home/people/person3
    person4: home/people/person4

and here’s the script.

#!/bin/bash

# Activate iTunes Sync over WLan for this to work!
# Use fixed IP`s or reservations in DHCP

## Devices to scan
###################
DEVS="
192.168.1.27|xx:xx:xx:xx:xx:xx|person1
192.168.1.78|xx:xx:xx:xx:xx:xx|person2
192.168.1.40|xx:xx:xx:xx:xx:xx|person3
192.168.1.36|xx:xx:xx:xx:xx:xx|person4
"

## Minutes to show device Offline
##################################
MINUTES=10

## iterate over devices
for DEV in $DEVS; do
    case "$DEV" in
    \#*)
        continue
        ;;
    *)
        IP=$(echo $DEV | cut -d '|' -f1)
        MAC=$(echo $DEV | cut -d '|' -f2)
        USER=$(echo $DEV | cut -d '|' -f3)

        # tickle iOS Device
        /usr/sbin/hping3 -2 -c 10 -p 5353 --fast $IP -q >/dev/null 2>/dev/null

        DEVICES=`/usr/sbin/arp -an | awk '{print $4}'`
        if [[ $DEVICES == *$MAC* ]]; then
            echo -n '' > $USER.txt
            mosquitto_pub -h 127.0.0.1 -t home/people/$USER -m "home" -r
        else
            echo 'OFF' >> $USER.txt
            MINOFF=$(wc -l $USER.txt | awk '{print $1}')
            if [ $MINOFF -gt $MINUTES ]; then
                echo "$(tail -n $((MINUTES+1)) $USER.txt)" > $USER.txt # keep not more than MINUTES+1 lines
                mosquitto_pub -h 127.0.0.1 -t home/people/$USER -m "not_home" -r
            fi
        fi
        ;;
    esac
    sleep 1
done

Sometimes (i do it every night via cron) i have to flush the arp cache
ip -s neigh flush all >/dev/null 2>/dev/null

This works perfect for me.

Greetings
Rainer

1 Like

sorry for this old topic @VDRainer

I’m trying to use your script, how do you setup this as ‘command line’ and track it as a device tracker ?

First install hping3 and mosquitto-clients.
apt-get install hping3 mosquitto-clients

For example, the script is saved as nscan.sh in folder /root/scripts/nscan/.
You have to edit the DEVS and the MQTT IP (127.0.0.1 if mqtt runs on the same server) to your needs.

Now there is a cronjob that calls it every minute.
* * * * * cd /root/scripts/nscan; ./nscan.sh

The script publishes the state of the devices to mqtt.

In HA setup the mqtt device tracker with the topics from the script.

device_tracker:
  - platform: mqtt
    devices:
      person1: home/people/person1
      person2: home/people/person2
      person3: home/people/person3
      person4: home/people/person4

BTW, there’s also a custom_component from @return01 for this.

2 Likes

thank you, this will help for sure/

1 Like

This is an old post, but the problem with nmap and sleeping phones remains the same. I have tried to figure out a solution, and I think I might have found one. It requires some changes both in the HA app and in the nmap component, so I have posted it as a feature request. Please have a look at: https://community.home-assistant.io/t/improvement-to-nmap-device-tracking-using-raise-to-wake-to-avoid-false-away/

Could this be a possible approach?