Presence script for OpenWRT

Hi,
here is a (very small and efficient) script for openwrt router/AP that checks “iw event” (meaning, monitoring all interfaces, in case you have multiple SSIDs in your house)
then it updates openhab items via REST api.
The advantage over ping is that there is no overhead, and some phones do not reply to pings when sleeping.
“iw event” only registers connection/disconnection events.

on the router, install nohup and curl packages.
edit /etc/rc.conf:

#!/bin/sh -e
# Put your custom commands here that should be executed once
# the system init finished. By default this file does nothing.
/usr/bin/nohup iw event | /root/presence > /dev/null 2>&1 &
exit 0

script in /root/presence:

while read input
do
        ACTION="OFF"
        USER=""
        case $input in
                *"del station"*)
                        ACTION="OFF";;
                *"new station"*)
                        ACTION="ON";;
                *)
                        continue;;
        esac

        MAC=${input//*station /}
        case $MAC in

         "aa:bb:11:00:11:00")   ## your phone mac goes here
         USER="John";;          ## This is the name of your openhab switch that gets updated

         "aa:bb:11:00:11:0a")  ## your phone mac goes here
         USER="Doe";;          ## This is the name of your openhab switch that gets updated

         "aa:bb:11:00:11:0b")  ## your phone mac goes here
         USER="Test";;          ## This is the name of your openhab switch that gets updated

         "aa:bb:11:00:11:0e")  ## your phone mac goes here
         USER="Test2";;        ## This is the name of your openhab switch that gets updated

                *)
                continue;;
        esac

## In case you have authentication mandatory on your openhab use this
curl -X POST --header "Content-Type: text/plain" --header "Accept: application/json" -d "$ACTION" "user:pass@your_openhab_ip:8080/rest/items/$USER"

## In case you allow implicit user role, or have white listed your home subnet in openhab api security settings, use this
curl -X POST --header "Content-Type: text/plain" --header "Accept: application/json" -d "$ACTION" "http://your_openhab_ip:8080/rest/items/$USER"

done

run the line from rc.local manually to test if its running (check with ps | grep presence), or reboot the router to have it activated
also you can run curl commands manually to check if they are working