Snippets

Brian Medley Store only today's temperatures in a file

Created by Brian Medley
#!/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)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.