Sharing something that I have created to enable presence detection using a Draytek Vigor 2927 router; I expect that this will work for most other DrayOS based routers that have the ability to check the ARP table via the telnet admin interface.
Problem:‘to my knowledge’ Draytek routers are not a supported platform for HA presence detection with no out of the box integrations.
Solution: Using a second RPI that executes two scripts to check the ARP table of the router to see of certain mac addresses are present; the output then updates MQTT location topics set in HA.
Pre reqs: install the following packages on the RPI - telnet (i know dont judge), expect and mosquitto-clients
Script #1 (check_arp) - Login to the router via telnet and check the arp table
Script #2 (presence_check) - Grep the output for mac values and set the relevant MQTT status
#!/bin/bash
if /home/pi/path_to_script/check_arp|grep -q mac_address1;
then mosquitto_pub -h 192.168.x.x -t location/name1 -m 'home' -u username -P password
else mosquitto_pub -h 192.168.x.x -t location/name1 -m 'not_home' -u username -P password
fi
if /home/pi/path_to_script/check_arp|grep -q mac_address2;
then mosquitto_pub -h 192.168.x.x -t location/name2 -m 'home' -u username -P password
else mosquitto_pub -h 192.168.x.x -t location/name2 -m 'not_home' -u username -P password
fi
if /home/pi/path_to_script/check_arp|grep -q mac_address3;
then mosquitto_pub -h 192.168.x.x -t location/name3 -m 'home' -u username -P password
else mosquitto_pub -h 192.168.x.x -t location/name3 -m 'not_home' --u username -P password
fi
if /home/pi/path_to_script/check_arp|grep -q mac_address4;
then mosquitto_pub -h 192.168.x.x -t location/name4 -m 'home' -u username -P password
else mosquitto_pub -h 192.168.x.x -t location/name4 -m 'not_home' -u username -P password
fi
Save the above two scripts onto your RPI box (not home assistant) , modify the path_to_script so it matches your script locations and set the scripts to execute (sudo chmod +x filename).
Update the mac_addresses, username, passwords to match your environment - and lastly setup your crontab to run the presence_check script as often as you like.
I am sure that this could be done easier and likley direct on the HA RPI, but given my limited knowledge/ time this is the best I could figure out - hope it helps someone
I can see how it works for detection of new connection (device joining network), as new entry is added to ARP table. But what about leaving network? I think Draytek routers are caching ARP table, so disconnection won’t be quickly/reliably discovered… or am I wrong?
On the router it takes about a minute for the arp table to clear disconnected devices unless you have set it to cache for a longer period for some reason.
I think I know where the problem is… I have 90% of my devices configured with IP reservations on router (Bind IP to MAC). These are always showing in ARP table. Then the difference is in the last 2 columns of the ‘ip arp status \r’ command - VLAN status and Port: it is showing dash for offline devices and VLAN assignment and connection port for devices that are active… but only refreshes after router restart or clearing of the cache manually. I have cache life set to 10 seconds and despite this once static IP device comes to on-line status it is never cleared… or I did not wait long enough (~10 minutes).
I have a Draytek Router but have always used the NMAP Integration for tracking devices that do not support the Companion App, another option is a Ping Sensor.
Could someone enlighten me as to why you would want to use this instead of NMAP?
Mirek - the grep statement in my script is really basic if you know what the exact output is in the arp table, you suggested a dash?, when the device is on/ offline then you could modify and expand the query to match the exact line?
Jonah, I used to use nmap and ping sensors myself but when you have apple phones, the WiFi sleeps after a small period of time to save battery and does not respond to ping or other layer 3 probes this results in the sensors reporting you being away when your not if your tracking people via there apple device. That said they work really well for other devices that have an active always on network connection, just not apple.
All the devices in this log are configured with static IP on myrouter (not real static IP configured on the device, but IP address reservation in router DHCP server). I’m not grep specialist, but I think in your script you are looking for occurence of MAC address. This will work for dynnamic IPs that will be cleared from the output as soon as cache is cleared. But preconfigured IPs are already known to router and it is showing them even if off-line. The distinction for these is dat ain last 2 columns (VLAN and PORT), that shows data if devices are online or dashes if offline. So if grep can somehow capture combination of these two (MAC address presence and status of last columns) this would really give reliable presence detection! Possible conditions are for single line would be:
I tried it on my setup with the arp table output copied straight into the script - seems to work ok. The only thing I added where the ’ ’ to ensure that grep searches for the exact string. The logic of the script is still the same match the string your home anything else your not