Skip to content

Instantly share code, notes, and snippets.

@mrworf
Created January 5, 2016 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrworf/ea17d55988a742b135ac to your computer and use it in GitHub Desktop.
Save mrworf/ea17d55988a742b135ac to your computer and use it in GitHub Desktop.
WiFi Presence for ISY994 - Let ISY know when you're in the house
#!/bin/bash
#
# WiFi Presence for ISY994
#
# Keeps track of your presence by the virtue of the WiFi in your
# cellphone. This script assumes that the server that runs it
# is also your Firewall/Router so it will see the device in
# question.
#
# You CANNOT run this on a machine which doesn't see all traffic
# to/from the cellphone. It doesn't work, period.
#
# Most linux machines keep the ARP table entries for 1-5 minutes,
# meaning that the device will be marked as gone after that time
# if it's nolonger using wifi.
#
# Check /proc/sys/net/ipv4/neigh/default/gc_stale_time for the
# exact time to live for ARP entries without traffic.
#
# I run this script from crontab on a 5min interval, which is more
# than enough to catch the changes in a timely manner.
#
# NOTE!
# Currently, you need to find the ID of the variable you want
# to toggle. Also, this script assumes you want to use state
# variables.
#
# Get the list of state variables and their ids using the following
#
# http://<isy994 address>/rest/vars/definitions/2
#
# wifipresence is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# wifipresence is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with wifipresence. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
# List of devices, space separated. Can be DNS names or MAC addresses
# Android: "Settings > About phone > Status" for WiFi MAC
# iOS: ???
#
DEVICES="phone-husband phone-wife"
# Matching variable IDs, use parenthesis and space separate them.
#
VARID=(7 8)
# Login information and ISY994 location (with leading http:// )
# (make SURE only root is allow to read this file)
#
USER=someuser
PASS=somepassword
ADDRESS=http://insteon.my.local.network.com
# Do not edit anything below this point
##############################################################################
#
I=0
for DEV in $DEVICES; do
# Look up the status of our device
STATE=0
arp -a | grep -v incomp | grep ${DEV} >/dev/null 2>/dev/null
if [ $? -eq 0 ]; then
# It's on the network
STATE=1
fi
# Get the current value
CURRENT=$(curl -s -u "${USER}:${PASS}" ${ADDRESS}/rest/vars/get/2/${VARID[$I]} | egrep -o 'val>([01])</val' | egrep -o '[01]' )
if [ $? -ne 0 ]; then
echo "ERROR: Unable to read variable state, did you configure it correctly?"
exit 1
fi
# Only update the server if the value is changed, this way, last-modified date is useful
if [ "${CURRENT}" != "${STATE}" ]; then
curl -s -u "${USER}:${PASS}" ${ADDRESS}/rest/vars/set/2/${VARID[$I]}/${STATE} | grep '>200<' >/dev/null 2>/dev/null
if [ $? -ne 0 ]; then
echo "ERROR: Unable to change variable state, did you configure it correctly?"
exit 1
fi
fi
# Next!
I=$(( $I + 1 ))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment