I recently changed my main dhcp server over from a linux server to openwrt and wanted to carry the presence detection that I’d setup on my old server to the new one. It’s based on someone else’s work and I’ll try and find the link to that later.
The openwrt is in a vm and not directly connected to the wifi AP’s which is why I didn’t use the usual addon.
in Homeassistant I set up the presence detection like this
mqtt:
device_tracker:
- name: "phone"
state_topic: "location/ff:cf:bf:ff:ff:ff"
payload_home: "home"
payload_not_home: "not_home"
In openwrt I installed the mosquitto-client
package (I don’t have mqtt ssl set up so I didn’t install that one).
openwrt doesn’t let you set your own dhcp-script=
configuration so you have to put the script into /etc/hotplug.d/dhcp
I created the file 98-mqtt-leases
. Make sure the perms are set to rw-r--r--
The file is sourced into another script so it doesn’t need to be made executable.
File contents
#!/bin/sh
mac="${MACADDR}"
topic="location/${mac}"
presence="not_home"
if [ "${ACTION}" = "update" ]; then
presence="home"
elif [ "${ACTION}" = "add" ]; then
presence="home"
fi
mosquitto_pub -h <mosquitto-hostname> -u <username> -P <password> -t "${topic}" -m "${presence}" -r
Finally, change the dhcp lease time in the openwrt interface configuration to something short. The minimum time is 2m for 2 minutes. The default is 12 hours which probably means unless you go out for the day it might not ever set you as away. Dnsmasq does allow you to set different lease times for different groups of mac addresses, but as it’s not exposed in the luci interface I haven’t bothered.