Snippets
Created by
Brian Medley
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | #!/usr/local/bin/bash
#user editable settings
start_checktime=0800 # 8AM
stop_checktime=2200 #10 PM
zipcode="Highlands/Ranch,US"
email=""
desired_temp=75
check_frequency=900 #seconds
api_key=$1
#do not modify below
state_filepath="/usr/local/scripts/run/weatherfan_statefile"
temps_filepath="/usr/local/scripts/run/weatherfan_tempsfile"
pid_filepath="/usr/local/scripts/run/weatherfan.pid"
pid=""
current_time=`date +"%H%M"`
current_statedate=`stat -f %Sm -t "%m%d%Y" $state_filepath`
current_date=`date +"%m%d%Y"`
max_temp_reached=false
current_temp=0
max_temp_time=0
max_temp=0
#check if the current day has already finished successfully
if [ ! -e "$state_filepath" ] ; then
touch -t `date -v-1d +"%Y%m%d%H%M"` "$state_filepath"
echo "State file does not exist. Creating..."
else
if [ "$current_statedate" -eq "$current_date" ] ; then
echo "Successfully completed for current day"
exit
fi
fi
#check if already running
if [ ! -e $pid_filepath ] ; then
echo $$ > $pid_filepath
echo "pid file does not exist... creating"
else
pid=$(<"$pid_filepath")
if `ps -p $pid > /dev/null` ; then
echo "Script already running... existing"
exit
else
echo "Script not running... Continuing"
fi
fi
#verify the start and stop times are within range
if [ ! "$current_time" -ge "$start_checktime" ] || [ ! "$current_time" -le "$stop_checktime" ]; then
echo "Not within start times, exiting..."
exit
fi
while [ max_temp_reached=false ] ; do
TEMPS=$(/opt/perl -Mojo -E 'use experimental qw(postderef);' \
-E '$j=g("http://api.wunderground.com/api/'$api_key'/conditions/hourly/q/CO/Highlands_Ranch.json")->json;' \
-E '%temp=map({ $_->{FCTTIME}{epoch} => $_->{temp}{english} } @{ $j->{hourly_forecast} });' \
-E '$max=0;' \
-E '$time=time;' \
-E 'say($j->{current_observation}{temp_f});' \
-E 'foreach my $t (sort keys %temp) { next unless (localtime($time))[7] == (localtime($t))[7]; $temp{$max} < $temp{$t} ? $max = $t : $max; $today{$t} = $temp{$t} };' \
-E 'foreach my $t (sort keys %today) { say(sprintf("%02d%02d %s %s", (localtime($t))[2,1], $today{$t}, $t)) }')
IFS="
"
for i in $TEMPS; do
IFS=" "
set -- $i
if [ -z $2 ]; then
current_temp=$1
IFS="
"
continue
fi
echo "$1 $2 $3" >> "$temps_filepath"
IFS="
"
done
/opt/perl -n -i \
-E 'BEGIN { $time = time };' \
-E 'if (/(\d+)\s+(\d+)\s+(\d+)/) {' \
-E ' ($h, $t, $epoch) = ($1, $2, $3);' \
-E ' next if (localtime($epoch))[7] != (localtime($time))[7];' \
-E ' $cache{$epoch}++;' \
-E ' next if 1 < $cache{$epoch};' \
-E ' print;' \
-E '}' $temps_filepath
exit
set -- $TEMPS
current_temp=$1
max_temp_time=$2
max_temp=$3
echo "Max Temp: $max_temp"
echo "Max Temp Time: $max_temp_time"
echo "Current Temp: $current_temp"
if [ $current_time -ge $max_temp_time ] && [ $max_temp_reached=true ] ; then
max_temp_reached=true
echo "Max temp has been reached"
if (( $(/usr/bin/bc <<<''$current_temp' < '$desired_temp'') )); then
echo -e "" | mail -s "Turn on whole house fan" $email
touch $state_filepath
echo "Temperature validation success. Sending notification. Terminating script..."
exit
fi
fi
echo "max not reached.. sleeping for $check_frequency seconds"
sleep $check_frequency
done
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.