Similar to many users, I wasn’t happy with the smartthings mobile app presence detection. I just moved back from a veralite which I had setup my router to run a script to toggle a virtual presence switch. That was a little easier to setup than with smartthings as there was already a good discussion thread to work off of. See:
Micasaverde Forums
To get this to work, you need:
- DD-WRT (possibly Tomato) based router to run the devices tracking script.
- Virtual Presence Toggle device for the router to interact with (with endpoint access)
First, the virtual presence device is similar to others that have been posted. I set mine up with a virtual switch and a virtual presence tile/capabilities. This allows you to manually override or set the presence. See the following for the basic device code.
/**
* Presence Toggle
*
* Author: impliciter
*
* Date: 2015-04-01
*/
metadata {
// Automatically generated. Make future change here.
definition (name: "Presence Toggle", namespace: "impliciter", author: "impliciter", oauth: true) {
capability "Actuator"
capability "Switch"
capability "Sensor"
capability "Presence Sensor"
}
// simulator metadata
simulator {
}
// UI tile definitions
tiles {
standardTile("button", "device.switch", width: 2, height: 2, canChangeIcon: true) {
state "off", label: 'Away', action: "switch.on", icon: "st.switches.switch.off", backgroundColor: "#ffffff", nextState: "on"
state "on", label: 'Home', action: "switch.off", icon: "st.switches.switch.on", backgroundColor: "#79b821", nextState: "off"
}
standardTile("presence", "device.presence", width: 2, height: 2, canChangeBackground: true) {
state "not present", labelIcon:"st.presence.tile.not-present", backgroundColor:"#ffffff"
state "present", labelIcon:"st.presence.tile.present", backgroundColor:"#53a7c0"
}
main(["button","presence"])
details(["button","presence"])
}
}
def parse(String description) {
}
def on() {
sendEvent(name: "switch", value: "on")
sendEvent(name: 'presence', value: 'present')
}
def off() {
sendEvent(name: "switch", value: "off")
sendEvent(name: 'presence', value: 'not present')
}
Next, you need to setup OAUTH endpoint access. That is covered elsewhere.
The router script is taken from the afforementioned micasaverde discussion 1. A few changes were made. The wget
command was changed to a curl
command as the wget command didn’t seem to work with HTTPS
requests. Add the following script as a custom script in DD-WRT.
#!/bin/sh
WATCHDOG_SLEEP_SEC=2
MAC_ADDRESS_1="MAC" # Phone 1 MAC
c1_last_state="0"
MAC_ADDRESS_2="MAC" # Phone 2 MAC
c2_last_state="0"
PHONE1ON="https://graph.api.smartthings.com/api/..."
PHONE1OFF="https://graph.api.smartthings.com/api/..."
PHONE2ON="https://graph.api.smartthings.com/api/..."
PHONE2OFF="https://graph.api.smartthings.com/api/..."
x=0
y=0
offcount1=0
offcount2=0
while sleep $WATCHDOG_SLEEP_SEC
do
if [ "$x" == 180 ]; then
# Every 30 minutes or so we do them all again, just in case Vera missed something
x=0
c1_last_state="0"
c2_last_state="0"
h1_last_state="x"
h2_last_state="x"
h3_last_state="x"
h4_last_state="x"
h10_last_state="x"
fi
x=$(( $x + 1 ))
c1_new_state=`wl assoclist | grep $MAC_ADDRESS_1`
if [ "$c1_new_state" == "$c1_last_state" ] ; then
sleep 0
else
if [ "$c1_new_state" == "assoclist $MAC_ADDRESS_1" ]; then
c1_last_state="$c1_new_state"
curl "${PHONE1ON}" -k
offcount1=0
else
offcount1=$(( $offcount1 + 1 ))
fi
fi
c2_new_state=`wl assoclist | grep $MAC_ADDRESS_2`
if [ "$c2_new_state" == "$c2_last_state" ] ; then
sleep 0
else
if [ "$c2_new_state" == "assoclist $MAC_ADDRESS_2" ]; then
c2_last_state="$c2_new_state"
curl "${PHONE2ON}" -k
offcount2=0
else
offcount2=$(( $offcount2 + 1 ))
fi
fi
if [ $offcount1 -lt 2 ]; then
# give not responding devices 1 minute to respond
sleep 0
else
c1_last_state="$c1_new_state"
curl "${PHONE1OFF}" -k
offcount1=0
fi
if [ $offcount2 -lt 2 ]; then
#give not responding devices 1 minute to respond
sleep 0
else
c2_last_state="$c2_new_state"
curl "${PHONE2OFF}" -k
offcount2=0
fi
done
Fill out the necessary MAC address info and endpoint addresses you setup previously. Also, add the following startup script in DD-WRT:
while [ ! -e "/tmp/custom.sh" ]; do
sleep 1 # wait till /tmp/custom.sh gets generated
done
/tmp/custom.sh &
You can add more devices to track pretty easily. I have it setup to track 2 phones. It’s functionality is easily extendable as you could have it track any devices connect to your router. For example, you could trigger an action whenever a laptop, smart tv, etc connect. Additionally, because the virtual presence is driven by a virtual switch, you could manually override/set presence with a physical switch or IFTTT.
One extension I’ve thought of is triggering a “Watching TV” mode or scene whenever the TV connects to the network. Seems like this could be quite useful.
Hopefully this was helpful. Its a little light on details but most of the discussion from the micasaverde forum1 applies.