Ping + Arp custom_component device tracker

This is my first contribution to home-assistan comunity with a custom component device tracker.
I am reading lots of complaining about how accurate or not is nmap or ping for device tracking.

I am been testing by myself both of them to track my iOS devices (iPhone/iPad) and these are my findings:

When a iOS device is turned on, connected to a wifi ap but not active (sleep), the device remain connected but after a few seconds it will start dropping icmp packets, not all of them but most of them.
Depending of your active services (push notification, etc) the device need to get back awake. During those short period, the device can answer icmp packets, but it will start droping them as soon as it get back to sleep. Thats why ICMP solutions (Ping, Nmap, etc) for tracking is very erratic.

The good thing is that even if the device is in a sleep state, it will still keep sending arp packets continuously (check it by your own using wireshark)

In this scenario, the arp table on the HA Server contain all the pairs ip-mac of the discovered devices, even if the iPhone is in a sleep state, his arp registry is still active on the HA Server arp table because, as I said, the iOS device is still sending arp querys to the network.

If you turn off the wifi on the iOS device, the arp registry will remain active on you HA Server during some time untill it gets deleted but you can speed up by just pinging the iOS device ip. Doing this, just keep ping-ing the IOS device ip from the HA Server, as soon as you turn off the wifi on the iOS device, the HA Server will update his arp table deleting the iOS arp registry within seconds.

Keeping all this in mind, I have create a custom tracking component that implement both ideas (ping a device + query HA Server arp table)

To use, put device_tracker.py in your /custom_components/ping_arp/ directory

Then just create a device_tracker entry on your configuration.yaml with something like this:-

device_tracker:
   - platform: ping_arp
     count: 5
     consider_home: 600   #10 Minutes
     hosts:
        myIphone: 192.168.10.40

and define the level of logging to

logger:
   default: info

Reboot your server and tail your home-assistant.log from a ssh session and check the ping and arp status messages for your defined host. You will see how, even if the ping is not responding, the arp record is still detected untill you turn off the wifi on the iOS device.

With this you can finally set up a presence detection based on the new entity created (device_tracker.myiphone) by the device_tracker.

Hope you find this info usefull and I apologize for my poor english (IĀ“m spanish)

Best regards.

3 Likes

nice work! Iā€™ll check if this works as expected.
Btw since HA 88 this is incorrect;

It should be;
To use, put device_tracker.py in your /custom_components/ping_arp/ directory
and the config indentation is wrong, it should be;

device_tracker:
   - platform: ping_arp
     count: 5
     hosts:
       myIphone: 192.168.10.40
       myIphone2: 192.168.10.41

Many thanks for your feedback !!

Thanks, this looks very useful!!

Noob question though: Where is the /custom_components directory?

In the config folder. If you donā€™t have the folder, you will have to create it.

1 Like

Is ā€œcountā€ in seconds?

Hi, count is the number of packets sent by the ping command. 5 is a common number to avoid false positives.

Does Android phones behave the same way?

Easy to test.
Just connect the phone to your wifi, and from other computer just ping the android phone and confirm it respond.
Now run arp -a to display the arp table and find the android phone ip / mac address
Turn sleep your android and wait until it does not respond to ping.
Run again arp -a and you should still see the ip / mac address.

Finally disconect the android phone from the wifi and wait a few seconds and run again arp -a.
The entry ip / mac address should be gone.

Did something similar a while ago using a simple bash script.

I got false positive sometimes, so this is not reliable :frowning:
This night for exemple I was home and my iPhone switched 2 times from ā€œhomeā€ to ā€œnot_homeā€ during 1 minute.

I will try to remove ā€˜consider_home: 60ā€™ to see itā€™s the problem.

I think this is normal with the iPhone. No one knows what they do at night. :blush:
Also seeing this from time to time.

Hi @jean675 Is the other way arroud. to avoid false positives when you set a too low value for ā€œconsider_homeā€, consider to increase it to for instance to two minutes: consider_home: 120

It still doesnā€™t work with consider_home : 120.

14

the first line shows me Iā€™m absent from 05:43 to 05:43
the second line shows me Iā€™m absent from 06:38 to 06:40
the third line shows me Iā€™m absent from 08:03 to 08:04
the fourth line shows me Iā€™m absent from 09:50 to 09:50
the fifth line shows me Iā€™m absent from 10:59 to 11:00
the sixth line shows me Iā€™m absent from 11:30 to 11:31

I have an iPhone X with iOS 12.2

Here is my component :

device_tracker:
  - platform: ping_arp
count: 5
consider_home: 120
hosts: 
  iPhone_de_jean: 192.168.1.30
new_device_defaults:
  track_new_devices : false

Donā€™t know if my settings or if the component is not reliable with iPhones.

works fine for me. indents maybe?

device_tracker:
  - platform: ping_arp
    count: 5
    consider_home: 120
    hosts:
      host_1: 192.168.1.30

And you have checked so this is not an internet problem?

not a syntax problem
not an internet issue, and in any case itā€™s LAN only

hi @jean675, can I ask you which hardware is running home assistance? raspberry? wifi?

Hi [JBelinchon]
I has add code
device_tracker:
- platform: bluetooth_tracker
track_new_devices: false
- platform: ping_arp
count: 5
consider_home: 120 #This setting speed up the transition from home to not_home
hosts:
myIphone: 192.168.1.15
- platform: ping
hosts:
hostone: 192.168.1.15
Anh copy file device_tracker.py to ping_arp folder
In known_devices.yaml show

myiphone:
  hide_if_away: false
  icon:
  mac: 04:D1:3A:D9:EF:80
  name: Kien ʔi
  picture:
  track: false
hostone:
  hide_if_away: false
  icon:
  mac:
  name: hostone
  picture:
  track: false

but in home assistant UI , do not have device_tracker.myiphone
or device_tracker.hostone

Please help me

Just turn true the device you want to be tracked.

myiphone:
  hide_if_away: false
  icon:
  mac: 04:D1:3A:D9:EF:80
  name: Kien ʔi
  picture:
  track: true