Hi everyone, just came to share the method i’m using to determine if i’m home.
I use an OpenWRT router, so I can determine what MAC addresses are connected. I like MQTT a lot, so messy SSH connections and scripts on the openhab machine weren’t going to cut it for me.
basically the openwrt router is sending to an mqtt topic by the name of “presence/wifi/MAC” the value ‘ON’ every 5 seconds if the device is connected and that is all it does, openhab gets this, and sets a timer for 7 seconds later turning the state back OFF again. if it receives another ON via MQTT it resets the timer, if not it assumes disconnect. This works very well for me, even if I haven’t touched my Android phone for hours it remains associated with the access point and openhab knows i’m here.
also this will work for wifi routers with multiple wireless interfaces like mine with 2.4ghz and 5ghz.
Install the mosquitto-client package with opkg, then save this shell script as /etc/config/mqtt_connected_wifi.sh and chmod +x
#!/bin/sh
# sends connected clients to mqtt server
while true
do
for interface in `iw dev | grep Interface | cut -f 2 -s -d" "`
do
# for each interface, get mac addresses of connected stations/clients
maclist=`iw dev $interface station dump | grep Station | cut -f 2 -s -d" "`
# for each mac address in that list...
for mac in $maclist
do
mosquitto_pub -h 10.1.0.254 -i horus -t "presence/wifi/${mac//:/-}" -m "ON"
done
done
sleep 5
in /etc/rc.local add
/etc/config/mqtt_connected_wifi.sh &
before
exit 0
then in your items,
something like this:
Switch LocationJupiter_PhoneWIFI "J Phone on WiFi [%s]" { mqtt="<[belic:presence/wifi/d0-22-be-b5-8c-07:state]" }
the rules file looks like this
import org.openhab.model.script.actions.Timer
var Timer jTimer = null
rule "jupiter wifi"
when
Item LocationJupiter_PhoneWIFI received update
then
if (LocationJupiter_PhoneWIFI.state == ON) {
JupiterIsHome.setState(ON)
if (jTimer != null) {
jTimer.cancel()
jTimer = null
}
jTimer = createTimer(now.plusSeconds(7), [|
jTimer = null
LocationJupiter_PhoneWIFI.setState(OFF)
JupiterIsHome.setState(OFF)
])
}
end