Most IP based mobile device scripts check for occupancy either through Tasker/Llama or by pinging the device from vera. I tend to prefer the latter option since it’s universal across devices and it’s easier to manage from a centralized location. However, one downside to pinging is the chance of inaccuracy if a device is on sleep mode or if your wifi reception isn’t very good.
Alternatively, if you have a Broadcom-based router with custom firmware (tomato/dd-wrt/open-wrt), you can use lower level functions to check for wifi clients associated to your network on the physical layer before an IP address is assigned.
Of course, there are pros and cons to consider with this method:
Pros:
Results are more accurate than ping.
Offloads resource utilization from your phones or vera.
Doesn’t require a response from your mobile device, so it drains less power.
Easily tunable to respond as quickly or slowly as you need it to.
Cons:
Open firmware router required with ability to add startup scripts and execute them.
Needs to be rewritten if you have more than one access point (not too hard to overcome though).
Can take more up more resources on your access point.
Requirements:
[ul][li]Custom firmware router with ability to add custom startup[/li]
[li]chixxi’s excellent Virtual Switch plugin (or similar switch to detect occupancy)[/li][/ul]
For those who are interested, here’s a quick and dirty shell script to control Vera occupancy by scanning for mobile devices on your AP. The wl assoclist command included below is specific for Broadcom chipsets, but the script can be altered for Atheros or other routers pretty easily.
To run it, you’ll need to create the following as a startup script for your router. I suggest starting it after everything else has started in order to avoid interrupting other processes on your AP
Original simple code:
#!/bin/sh
# seconds between checks
WATCHDOG_SLEEP_SEC=2
# MAC Address 1
MAC_ADDRESS_1="[XX:XX:XX:XX:XX:XX]"
# MAC Address 2
MAC_ADDRESS_2="[YY:YY:YY:YY:YY:YY]"
# Vera VSwitch 1 Device ID
VSWITCH_1="[99]"
# Vera VSwitch 2 Device ID
VSWITCH_2="[100]"
#This loop will check if a device is registered on the AP and send the on/off command to vera.
while sleep $WATCHDOG_SLEEP_SEC
do
if wl assoclist | grep -Fq $MAC_ADDRESS_1
then
#echo Device 1 user is in
wget -qs "http://[YOUR_VERA_IP_ADDRESS]:3480/data_request?id=lu_action&output_format=xml&DeviceNum=$VSWITCH_1&serviceId=urn:upnp-org:serviceId:VSwitch1&action=SetTarget&newTargetValue=1"
else
#echo Device 1 user is out
wget -qs "http://[YOUR_VERA_IP_ADDRESS]:3480/data_request?id=lu_action&output_format=xml&DeviceNum=$VSWITCH_1&serviceId=urn:upnp-org:serviceId:VSwitch1&action=SetTarget&newTargetValue=0"
fi
if wl assoclist | grep -Fq $MAC_ADDRESS_2
then
wget -qs "http://[YOUR_VERA_IP_ADDRESS]:3480/data_request?id=lu_action&output_format=xml&DeviceNum=$VSWITCH_2&serviceId=urn:upnp-org:serviceId:VSwitch1&action=SetTarget&newTargetValue=1"
else
wget -qs "http://[YOUR_VERA_IP_ADDRESS]:3480/data_request?id=lu_action&output_format=xml&DeviceNum=$VSWITCH_2&serviceId=urn:upnp-org:serviceId:VSwitch1&action=SetTarget&newTargetValue=0"
fi
done
&
[size=18pt]Update 03/26/2015 - New method contributed by Aaron. I have not tested this, but it looks good.[/size]
[ol][li]In vera - install multiswitch plugin[/li]
[li]On your Tomato router - enable JFFS[/li]
[li]Create two scripts using the following code and save with respective file names. Case-sensitive. Pay attention to extensions (remove txt). File names will be services-start and VeraOccupancy. If you’re using windows, make sure to use a linux-compatible text editor such as gedit, notepad++, editplus, etc. Notepad will not work[/li]
[li]Make sure to update VeraOccupancy script to match your environment. In particular, change the values marked XX which include your vera IP, your phones’ MAC addresses, and the device IDs for the vera multiswitch[/li]
[li](Contributed by AskewPiste: For UI7 use http://XX.XX.XX.XX:3480/data_request?id=lu_action&output_format=xml&DeviceNum=XX&serviceId=urn:dcineco-com:serviceId:MSwitch1&action=SetStatus1&newStatus1=0 for the URL)[/li]
[li]Upload both scripts to your router in the /jffs/scripts/ path. You should now have 2 files in your router: /jffs/scripts/services-start and /jffs/scripts/VeraOccupancy[/li]
[li]Set execute permissions for both files: ssh into your router and run chmod 755 /jffs/scripts/services-start and chmod 755 /jffs/scripts/VeraOccupancy[/li]
[li]Reboot your router[/li]
[li]Test by turning wifi on your devices on/off. If everything is working correctly, your multiswitch should react according to how you set it up[/li]
[li]Script your own occupancy scenes based on multiswitch status[/li][/ol]
[ol][li]Bonus: If you have multiple APs, you can add this script to those other APs as well. You should also create more devices on your multiswitch to match[/li]
[li]Bonus: To shorten the script, you can condense the conditional test by adding an OR operator like this example to test for both 2.4ghz and 5ghz at the same time. if (/usr/sbin/wl -i eth1 assoclist | grep -Fq $MAC_ADDRESS1) || (/usr/sbin/wl -i eth2 assoclist | grep -Fq $MAC_ADDRESS2)[/ol]
services-start
#!/bin/sh
# folder location: /jffs/scripts
# file name: services-start
sh /jffs/scripts/VeraOccupancy &
VeraOccupancy
#!/bin/sh
#set -x # activate debugging when launched from cmd
# folder location: /jffs/scripts
# file name: VeraOccupancy
# version: 2.00
# Instructions:
# modifying code - search for and replace all XX
# when using less than 4 MAC addresses: you must remove code for the other MACs
# when adding additional MAC addresses: ensure you understand each section needs to be replicateduplicate
# to be added to future versions... User Constants for all variables; Single code Loop to iterate through all MACs
# Vera Device & Button reference - place your info here to remind yourself
# MAC1 = Device: Occupancy Trigger (246) button 5
# MAC2 = Device: Occupancy Trigger (246) button 6
# MAC3 = Device: Wifi Tracking (278) button 3
# MAC4 = Device: Wifi Tracking (278) button 1
# User Constants
MAC_ADDRESS_1="XX:XX:XX:XX:XX:XX" # HTC
MAC_ADDRESS_2="XX:XX:XX:XX:XX:XX" # Samsung
MAC_ADDRESS_3="XX:XX:XX:XX:XX:XX" # WindowsPC
MAC_ADDRESS_4="XX:XX:XX:XX:XX:XX" # FireTV
VERAURL=""http://XX.XX.XX.XX:3480/data_request?id=variableset&DeviceNum=XXserviceId=urn:dcineco-com:serviceId:MSwitch1&Variable="
VERAURL2="http://XX.XX.XX.XX:3480/data_request?id=variableset&DeviceNum=XX&serviceId=urn:dcineco-com:serviceId:MSwitch1&Variable="
WATCHDOG_SLEEP_SEC=10
a1_2ghz_last="0"
a1_5ghz_last="0"
a2_2ghz_last="0"
a2_5ghz_last="0"
a3_2ghz_last="0"
a3_5ghz_last="0"
a4_2ghz_last="0"
a4_5ghz_last="0"
x=0
y=0
while sleep $WATCHDOG_SLEEP_SEC
do
if [ "$x" == 60 ]; then
# Every 10 minutes or so we do them all again, just in case Vera missed something
x=0
a1_2ghz_last="0"
a1_5ghz_last="0"
a2_2ghz_last="0"
a2_5ghz_last="0"
a3_2ghz_last="0"
a3_5ghz_last="0"
a4_2ghz_last="0"
a4_5ghz_last="0"
fi
x=$(( $x + 1 ))
# Monitor MAC_ADDRESS_1
# 2.4ghz band
a1_2ghz_new=`wl -i eth1 assoclist | grep $MAC_ADDRESS_1`
if [ "$a1_2ghz_new" == "$a1_2ghz_last" ] ; then
sleep 0
else
a1_2ghz_last="$a1_2ghz_new"
if [ "$a1_2ghz_new" == "assoclist $MAC_ADDRESS_1" ]; then
wget -qs "${VERAURL}StatusXX&Value=1"
fi
fi
# 5ghz band
a1_5ghz_new=`wl -i eth2 assoclist | grep $MAC_ADDRESS_1`
if [ "$a1_5ghz_new" == "$a1_5ghz_last" ] ; then
sleep 0
else
a1_5ghz_last="$a1_5ghz_new"
if [ "$a1_5ghz_new" == "assoclist $MAC_ADDRESS_1" ]; then
wget -qs "${VERAURL}StatusXX&Value=1"
fi
fi
if [ -z "$a1_2ghz_new" ] && [ -z "$a1_5ghz_new" ] ; then
wget -qs "${VERAURL}StatusXX&Value=0"
fi
# Monitor MAC_ADDRESS_2
# 2.4ghz band
a2_2ghz_new=`wl -i eth1 assoclist | grep $MAC_ADDRESS_2`
if [ "$a2_2ghz_new" == "$a2_2ghz_last" ] ; then
sleep 0
else
a2_2ghz_last="$a2_2ghz_new"
if [ "$a2_2ghz_new" == "assoclist $MAC_ADDRESS_2" ]; then
wget -qs "${VERAURL}StatusXX&Value=1"
fi
fi
# 5ghz band
a2_5ghz_new=`wl -i eth2 assoclist | grep $MAC_ADDRESS_2`
if [ "$a2_5ghz_new" == "$a2_5ghz_last" ] ; then
sleep 0
else
a2_5ghz_last="$a2_5ghz_new"
if [ "$a2_5ghz_new" == "assoclist $MAC_ADDRESS_2" ]; then
wget -qs "${VERAURL}StatusXX&Value=1"
fi
fi
if [ -z "$a2_2ghz_new" ] && [ -z "$a2_5ghz_new" ] ; then
wget -qs "${VERAURL}StatusXX&Value=0"
fi
# Monitor MAC_ADDRESS_3
# 2.4ghz band
a3_2ghz_new=`wl -i eth1 assoclist | grep $MAC_ADDRESS_3`
if [ "$a3_2ghz_new" == "$a3_2ghz_last" ] ; then
sleep 0
else
a3_2ghz_last="$a3_2ghz_new"
if [ "$a3_2ghz_new" == "assoclist $MAC_ADDRESS_3" ]; then
wget -qs "${VERAURL2}StatusXX&Value=1"
fi
fi
# 5ghz band
a3_5ghz_new=`wl -i eth2 assoclist | grep $MAC_ADDRESS_3`
if [ "$a3_5ghz_new" == "$a3_5ghz_last" ] ; then
sleep 0
else
a3_5ghz_last="$a2_5ghz_new"
if [ "$a3_5ghz_new" == "assoclist $MAC_ADDRESS_3" ]; then
wget -qs "${VERAURL2}StatusXX&Value=1"
fi
fi
if [ -z "$a3_2ghz_new" ] && [ -z "$a3_5ghz_new" ] ; then
wget -qs "${VERAURL2}StatusXX&Value=0"
fi
# Monitor MAC_ADDRESS_4
# 2.4ghz band
a4_2ghz_new=`wl -i eth1 assoclist | grep $MAC_ADDRESS_4`
if [ "$a4_2ghz_new" == "$a4_2ghz_last" ] ; then
sleep 0
else
a4_2ghz_last="$a4_2ghz_new"
if [ "$a4_2ghz_new" == "assoclist $MAC_ADDRESS_4" ]; then
wget -qs "${VERAURL2}StatusXX&Value=1"
fi
fi
# 5ghz band
a4_5ghz_new=`wl -i eth2 assoclist | grep $MAC_ADDRESS_4`
if [ "$a4_5ghz_new" == "$a4_5ghz_last" ] ; then
sleep 0
else
a4_5ghz_last="$a4_5ghz_new"
if [ "$a4_5ghz_new" == "assoclist $MAC_ADDRESS_4" ]; then
wget -qs "${VERAURL2}StatusXX&Value=1"
fi
fi
if [ -z "$a4_2ghz_new" ] && [ -z "$a4_5ghz_new" ] ; then
wget -qs "${VERAURL2}StatusXX&Value=0"
fi
done