DD-WRT Presence Detection with ARP for openHAB

Hi all.
I’d just like to revive this good ol’ thread since it’s still useful. I’ve just set up presence detection on another router using the same concept and it took me an hour, so I’ll add some more useful info that I hopefully will find here next time I change router :sunglasses:

One thing worth mentioning is that arp isn’t really a good idea using for this. A device being connected and a device being in the router’s arp list is not the same thing. Also wl_atheros which has been mentioned in the thread only exists on routers running Atheros (while mine is Broadcom). And only “wl assoclist” only seems to return the devices connected to eth1.

So I ended up fetching the connected devices in my script using the following:

devices=“$(wl -i eth1 assoclist) $(wl -i eth2 assoclist)”

Works like a charm :slight_smile:

1 Like

Daniel,
the old link on the top of this thread is gone. And this thread is very old in deed. I don’t understand what exactly is the best of all mentioned options in this thread, Does this still work in Openhab 2.5 and will it work in OH3 which is gleeming on the horizon?
And could you list the steps you took to get it working as a tutorial? That would help me a lot!
I am trying to signal my son’s lights whenever he is online for an hour. So he has a reminder to do something else for some time.

I’m doing the exact same thing in OH3 as I did in OH2, no difference there. I think most of the stuff you need is hidden somewhere in this thread, but I’ll post how I did for completeness.

Things worth noting about how I did:

  1. My OH isn’t password protected for access via LAN and port 8080. If yours is, you need to add credentials to the end of the URL when calling the API. I think there are examples of that above.
  2. You need to change MAC address and Item name in the script. If you only want to monitor one device you can ignore the information for the other devices in the script. And of course you need to change the IP of the OH installation.
  3. It seems the commands for getting the active devices out of the router is different depending on hardware and dd-wrt version. For example see my last post above. It might be the fact that you need to telnet into the router and play around to find out the exact correct command for you.

Now go to Administration->Commands in the dd-wrt interface, paste the script in the Commands text area and click “Save Startup”. Now this script will be run the next time you reboot your router and (since it contains an eternal loop) then it will check the connected devices every 10 seconds and report them to OH.

Pasting my entire script below (after removing some personal names). Have fun! :sunglasses:

#!/bin/sh
# Proximity detection

# MAC address of each device to watch. Don't leave blank.
macdevice1="66:52:D1:73:C7:EE"    #Comment describing device 1
macdevice2="62:94:22:61:C6:B3"    #Comment describing device 2
macdevice3="D8:0B:9A:BD:17:86"    #Comment describing device 3
macdevice4="00:00:00:00:00:00"    #Comment describing device 4

#OpenHAB IP Address and port
IPAddr="192.168.168.9"
port="8080"

# OpenHAB switch items to be updated for each tracked MAC
item1="ItemName1"
item2="ItemName2"
item3="ItemName3"
item4="ItemName4"

# Occupied and unoccupied delay in seconds to check status
delay_occupied=20
delay_unoccupied=10

# initial testing loop count - uncomment the counter near the bottom of the script for testing only. 
limit=120

sleep 10

# status of each MAC. 0=disconnected. 1=connected.  -1 initially forces openHAB update first loop
macconnected1=-1
macconnected2=-1
macconnected3=-1
macconnected4=-1
# total number of currently conencted devices.
counter=0

# Initial testing loop.  Will run continually after testing is complete
while [ $counter -lt $limit ]; do

#reset current status. Two variables are used for each device.  The past known status and the current
# status.  Only a change is reported to openHAB.  Otherwise, it would constantly be updating openHAB with
# the current status creating unnecessary traffic for both the router and openHAB
maccurrent1=0;
maccurrent2=0;
maccurrent3=0;
maccurrent4=0;

# compare each device that is currently connected to the MAC devices we want to watch.
# changed the following to check for each MAC
devices="$(wl -i eth1 assoclist) $(wl -i eth2 assoclist)"

maccurrent1=$(echo $devices | grep -c $macdevice1)
if [ $maccurrent1 -gt 0 ]; then
	maccurrent1=1
fi

maccurrent2=$(echo $devices | grep -c $macdevice2)
if [ $maccurrent2 -gt 0 ]; then
	maccurrent2=1
fi

maccurrent3=$(echo $devices | grep -c $macdevice3)
if [ $maccurrent3 -gt 0 ]; then
	maccurrent3=1
fi

maccurrent4=$(echo $devices | grep -c $macdevice4)
if [ $maccurrent4 -gt 0 ]; then
	maccurrent4=1
fi

# Look for a change in status from the old known to the current status.
# If it changed, update openHAB. Otherwise it leaves it as is. 
if [ $macconnected1 -ne $maccurrent1 ]; then
	if [ $maccurrent1 -eq 1 ]; then
		macstatus1="ON";
	else
		macstatus1="OFF";
	fi
	curl -X POST -d $macstatus1 -H "Content-Type: text/plain" -i http://$IPAddr:$port/rest/items/$item1
	macconnected1=$maccurrent1;
fi

if [ $macconnected2 -ne $maccurrent2 ]; then
	if [ $maccurrent2 -eq 1 ]; then
		macstatus2="ON";
	else
		macstatus2="OFF";
	fi
	curl -X POST -d $macstatus2 -H "Content-Type: text/plain" -i http://$IPAddr:$port/rest/items/$item2
	macconnected2=$maccurrent2;
fi

if [ $macconnected3 -ne $maccurrent3 ]; then
	if [ $maccurrent3 -eq 1 ]; then
		macstatus3="ON";
	else
		macstatus3="OFF";
	fi
	curl -X POST -d $macstatus3 -H "Content-Type: text/plain" -i http://$IPAddr:$port/rest/items/$item3
	macconnected3=$maccurrent3;
fi

if [ $macconnected4 -ne $maccurrent4 ]; then
	if [ $maccurrent4 -eq 1 ]; then
		macstatus4="ON";
	else
		macstatus4="OFF";
	fi
	curl -X POST -d $macstatus4 -H "Content-Type: text/plain" -i http://$IPAddr:$port/rest/items/$item4
	macconnected4=$maccurrent4;
fi

# Total up the number of devices connected.
let connected=$macconnected1+$macconnected2+$macconnected3+$macconnected4

# Delay (sleep) depending on the connection status.
# No devices connected could delay less. Once a device is connected, it could delay longer.
if [ $connected -gt 0 ]; then
    sleep $delay_occupied
    else
    sleep $delay_unoccupied
fi

#for testing only - uncomment to have the looping stop at X loops defined in variable: limit.
#let counter=$counter+1
done

I have not been on the forum for a very very long time.
But I still want to thank you for your elaborate answer Cheers!

1 Like