5.
Some simple devices
5.1.
Being home
For some scripts it can be important whether I am home. For example: certain
lights will go on only when I am there, otherwisw they stat out.
My pressence at home is determined by the fact that my phone is there.
My DHCP server hands out a fixed address to the phone, based on the MAC-ID.
On th phone, I set the sleep policy to "never":
Settingss
-> Wireless & Network Setting
-> Wifi Settings
-> Press Menu button
-> Advanced
-> WiFi Sleep Policy
That was enough for my Android phone to respond to pings.
I made a virtual hardware device type "Dummy (Does nothing, use for virual switches only)".
For that hardware, I made a virtual sensor, type switch. The name of that sensor
is assigned to the variable
DOMO_DEV
in the script below.
The script polls the phone and sets the value of the virtual switch.
#!/bin/bash
#REMOTE@ domoticz.home /usr/local/bin/ljthuis
##########################################################################
# Set or change the hostnames below to match your environment
##########################################################################
DOMOTICZ='domoticz.home:8888'
TARGET=192.168.178.219
DOMO_DEV='laurent-jan_thuis'
##########################################################################
helpme(){
cat <<EOF
NAME: Am i at home?
EOF
}
if [ "$1" = "-h" ] ; then
helpme
exit 0
fi
verbose=0
if [ "$1" = "-v" ] ; then
verbose=1
fi
debug(){
if [ "$verbose" = 1 ] ; then
echo $*
fi
}
tmp=$(mktemp)
now=$(date)
# Get list of devices
curl --silent "http://$DOMOTICZ/json.htm?type=command¶m=devices_list" |
sed 's/"//g;s/,//' |
while read nv col val ; do
if [ "$nv" = 'name' ] ; then
name="$val"
elif [ "$nv" = 'value' ] ; then
echo "$val $name"
fi
done > $tmp
val=0
if ping -c5 $TARGET > /tmp/last_ljm.out 2> /tmp/last_ljm.err ; then
val=1
fi
debug "Value: $val"
idx=$(sed -n "s/ $DOMO_DEV//p" $tmp)
debug "idx=$idx"
if [ "idx" = "" ] ; then
true
else
debug curl --silent "http://$DOMOTICZ/json.htm?type=command¶m=udevice&idx=$idx&nvalue=$val"
curl --silent "http://$DOMOTICZ/json.htm?type=command¶m=udevice&idx=$idx&nvalue=$val" > /dev/null
fi
rm -f $tmp
The script runs every 5 minutes from
cron
#USER root@domoticz.home #--ljthuis.cron:root
#INSTALLEDFROM verlaine:src/domoticz #--ljthuis.cron:root
# m h D M dow cmd #--ljthuis.cron:root
1,16,31,46 * * * * /bin/bash /usr/local/bin/ljthuis > /tmp/last_ljthuis.out 2>/tmp/last_ljthuis.err #--ljthuis.cron:root
6,21,36,51 * * * * /bin/bash /usr/local/bin/ljthuis > /tmp/last_ljthuis.out 2>/tmp/last_ljthuis.err #--ljthuis.cron:root
11,26,41,56 * * * * /bin/bash /usr/local/bin/ljthuis > /tmp/last_ljthuis.out 2>/tmp/last_ljthuis.err #--ljthuis.cron:root
5.2.
Internet connection
I had the impression that my Internet connection was unreliable. To get some data about how unreliable the connection
was, I created a custom sensor, with the sensor unit "msec".
The value of the sensor is the round-trip time for a ping to 8.8.8.8. Again, an external script updates the value
of the sensor.
#!/bin/bash
#REMOTE@ domoticz.home /usr/local/bin/internetup
##########################################################################
# Set or change the hostnames below to match your environment
##########################################################################
DOMOTICZ='domoticz.home:8888'
TARGET=8.8.8.8
##########################################################################
helpme(){
cat <<EOF
NAME:
internetup - test if internet is up
SYNOPSIS:
p1_report
DESCRIPTION:
Internetup pings $TARGET to see if the Internet is reachable.
The default URL for Domoticz is '$DOMOTICZ' and the default
target for ping is $TARGET
Internetup is typicaly started by cron to ensure a regular data feed to
Domoticz.
EOF
}
if [ "$1" = "-h" ] ; then
helpme
exit 0
fi
verbose=0
if [ "$1" = "-v" ] ; then
verbose=1
fi
debug(){
if [ "$verbose" = 1 ] ; then
echo $*
fi
}
tmp=$(mktemp)
pingout=$(mktemp)
now=$(date)
val=500
if ! ping -c1 8.8.8.8 > $pingout ; then
sleep 1
ping -c1 8.8.8.8 > $pingout
fi
if grep -q 'time=' $pingout ; then
val=$(sed -n 's/ ms//; s/.*time=//p' $pingout)
fi
debug "Value: $val"
# Get list of devices
curl --silent "http://$DOMOTICZ/json.htm?type=command¶m=devices_list" |
sed 's/"//g;s/,//' |
while read nv col val ; do
if [ "$nv" = 'name' ] ; then
name="$val"
elif [ "$nv" = 'value' ] ; then
echo "$val $name"
fi
done > $tmp
inet_idx=$(sed -n 's/ internet_up//p' $tmp)
debug "inet_idx=$inet_idx"
if [ "inet_idx" = "" ] ; then
echo "$now No inet dev">> /tmp/last_inet
else
debug curl --silent "http://$DOMOTICZ/json.htm?type=command¶m=udevice&idx=$inet_idx&nvalue=1&svalue=$val"
curl --silent "http://$DOMOTICZ/json.htm?type=command¶m=udevice&idx=$inet_idx&nvalue=1&svalue=$val" > /dev/null
echo "$now Internet: $val" >> /tmp/last_inet
fi
tail -1024 /tmp/last_inet > $tmp
mv $tmp /tmp/last_inet
rm -f $tmp $pingout
And again, launched from cron:
#USER root@domoticz.home #--internet_up.cron:root
#INSTALLEDFROM verlaine:src/domoticz #--internet_up.cron:root
# m h D M dow cmd #--internet_up.cron:root
0,15,30,45 * * * * /bin/bash /usr/local/bin/internetup > /tmp/last_internetup.out 2>/tmp/last_internetup.err #--internet_up.cron:root
5,20,35,50 * * * * /bin/bash /usr/local/bin/internetup > /tmp/last_internetup.out 2>/tmp/last_internetup.err #--internet_up.cron:root
10,25,40,55 * * * * /bin/bash /usr/local/bin/internetup > /tmp/last_internetup.out 2>/tmp/last_internetup.err #--internet_up.cron:root
The results for my Ziggo line where, that I had a down time of more than 15 minutes about every two days. Also, the tound-trip time
varied wildly, between 8msec and 100msec. That jitter was a serious problem in video calls.