#!/ram/sh # this script figures out what the weather was today and it causes # the sprinklers to adjust to hot weather (run every day) or rain # (skip a rainy day). It defaults to watering M W F, or M,F depending # on the time of year then uses the weather to decide whether to # water on Sat and Sun # search for "78" to see where the temperature forecast controls are # the watering controller is an Orbit Model 57161-WT2X 6 Station # It remembers the program even if the 110v power is off, as it is # when the bottlerocket commands turn the X10 outlet off DEBUG=Y DEBUG=N TMP_FILE="/ram/tmp/todays_weather" LOG="/tmp/water_log" # set the next two lines to your house code HOUSE_CODE="M" # M seems to be taken HOUSE_CODE="N" WATER_CONTROL="${HOUSE_CODE}12" WEATHER_LOCATION_CODE="SJOC1:" # changed May 15 2008 # SJO and : will be checked as two values separated by a single space # because of the set -- # even though they are reported with multiple spaces. WEATHER_LOCATION_CODE="SJC :" trap "[ -s $TMP_FILE ] && > $TMP_FILE # this runs every time but it makes sure the outlet is OFF sleep 5 /usr/local/bin/br -c "$HOUSE_CODE" $WATER_CONTROL OFF --repeat=2 exit" 0 1 2 15 WATER(){ case "$DEBUG" in Y)echo "Would Water" return ;; esac echo "$THIS_DAY High temp: $HIGH did water" >> "$LOG" # turn it on /usr/local/bin/br -c "$HOUSE_CODE" "$WATER_CONTROL" ON --repeat=2 # give it 40 minutes to finish its business, 5 stations # currently 8 8 8 5 5 sleep 2400 # turn it off /usr/local/bin/br -c "$HOUSE_CODE" "$WATER_CONTROL" OFF --repeat=2 } # end of functions # turn manually off if necessary - uncomment the next two lines and run # /usr/local/bin/br -c "$HOUSE_CODE" "$WATER_CONTROL" OFF --repeat=2 # exit # go get the latest weather data if [ ! -s "$TMP_FILE" ] then # debug - Note - the Weather Services stopped updating this location in July of 2004 URL="http://weather.noaa.gov/pub/data/summaries/max_min_temp_and_precip/ca.txt" | more # note - this was obsoleted by the Weather Service sin October of 2004 URL="http://www.wrh.noaa.gov/afos/SFO/RTP/SFORTPMTR" # note - beginning October 2004 - the pattern seems to be that the Weather Services is "unpredictable" URL="http://newweb.wrh.noaa.gov/mtr/gettext.php?pil=rtp&sid=mtr" URL="http://newweb.wrh.noaa.gov/mtr/getzfpzone.php?sid=mtr&zone=caz513" URL="http://www.nws.noaa.gov/data/MTR/RTPMTR" # debug # lynx -dump "$URL" | tee /ram/tmp/new # cp /ram/tmp/new /ram/tmp/todays_weather # exit # latest source until lynx -dump "$URL" > "$TMP_FILE" 2>/dev/null do # wait a bit sleep 15 COUNT="${COUNT}." # 2 minutes of retrys, and then we give up case "$COUNT" in ........)TOO_MANY_RETRIES=" due to too many retries" break ;; esac continue done case "$DEBUG" in Y)ls -l "$TMP_FILE" ;; esac fi # save stdin # exec 3<&0 # a line of data looks like this, minus the leading comment # symbol (#) and space: # SJOC1: SAN JOSE 67 : 75 / 62 / 0.00 / # May 15 2008 changed again without warning # SJC : SAN JOSE ARPT 51 : 90 / 57 / 0.00 / # start reading from the file instead exec 0< "$TMP_FILE" while read VAR1 VAR2 VAR3 VAR4 VAR5 VAR6 VAR7 VAR8 VAR9 VAR10 VAR11 VAR12 VAR13 JUNK do case "$VAR1 $VAR2" in # capture todays date # :FOR THE 24 HOUR PERIOD ENDING AT 4AM PST SUN MAR 26 2006 # changed July 27 2007 # :FOR THE 24 HOUR PERIOD ENDING AT 5 AM PDT # so now I am lookng for a different string # :530 AM PDT MON AUG 06 2007 # ":FOR THE"*) # TIME="$VAR8" THISTZ="$VAR9" DAY_WEEK="$VAR10" MONTH="$VAR11" DATE_NUMBER="$VAR12" YEAR4="$VAR13" # ;; # new way ":"[0-9][0-9][0-9]*[AP]M*) TIME="$VAR1" AMPM="$VAR2" THISTZ="$VAR3" DAY_WEEK="$VAR4" MONTH="$VAR5" DATE_NUMBER="$VAR6" YEAR4="$VAR7" ;; # needed to update this once again May 2008, as they # changed the code again for San Jose "$WEATHER_LOCATION_CODE"*) CODE="$VAR1" COLON="$VAR1" NAME1="$VAR3" NAME2="$VAR4" NAME3="$VAR5" ELEVATION="$VAR6" COLON="$VAR7" HIGH="$VAR8" SLASH="$VAR9" LOW="$VAR10" SLASH="$VAR11" case "$VAR12" in [0-9]*)PRECIPITATION="$VAR12" ;; *)PRECIPITATION="0" ;; esac break ;; esac done case "$DEBUG" in Y)echo "TIME: $TIME $AMPM $THISTZ $DAY_WEEK $MONTH $DATE_NUMBER $YEAR4" echo "$NAME1 $NAME2 High: $HIGH Low: $LOW Rain: $PRECIPITATION" >&2 ;; esac # back to stdin # exec 0<&3 # get today's date THIS_TZ="PST8PDT" # see if there is a saved date, otherwise execute the date command if [ -s /ram/today."$THIS_TZ" ] then read MM DD YY SLASHED DAY MON NUMDAY TIME ZONE THIS_YEAR4 LAST_YEAR4 NEXT_YEAR4 < /ram/today."$THIS_TZ" TODAYS_DATE="${MM}/${DD}/${YY}" else set -- `date "+%Y %b %d %m %y"` TODAYS_DATE="${4}/${3}/${5}" fi # vars all set, check that we read the date yesterday as a failsafe... case "$DAY" in Sun)THIS_DAY="SUN";YESTERDAY="SAT";; Mon)THIS_DAY="MON";YESTERDAY="SUN";; Tue)THIS_DAY="TUE";YESTERDAY="MON";; Wed)THIS_DAY="WED";YESTERDAY="TUE";; Thu)THIS_DAY="THU";YESTERDAY="WED";; Fri)THIS_DAY="FRI";YESTERDAY="THU";; Sat)THIS_DAY="SAT";YESTERDAY="FRI";; esac case "$DEBUG" in Y)echo "$DAY_WEEK" ;; esac # ok, see if it rained or was hot. No water if it rained # extra water if it was hot case "$DAY_WEEK" in "$THIS_DAY"|"$YESTERDAY") case "$PRECIPITATION" in # no rain, or very little rain, we will water # note, "M" now seems to be a valid value and means zero M|0|0.0|0.0[0-9]) # check the time of year case "$MONTH" in # during winter water once a week by default OCT|NOV|DEC|JAN|FEB|MAR|APR) case "$THIS_DAY" in MON) WATER ;; # on the other days, see if it was over 82 degrees # since the days are much shorter, watering is not needed as often *) case "$HIGH" in # 82 or higher # if they screw up the # display so we cant # get the high, as a failsafe, # water "")echo "$0: Could not find the high temperature. Watering to be safe." | mail -s "$0 error " davidbu WATER ;; 8[2-9]|9[0-9]|???) WATER ;; # not hot enough and not correct day *)echo "$THIS_DAY High temp: $HIGH did not water" >> "$LOG" ;; esac ;; esac ;; # during summer water every other day by default MAY|JUN|JUL|AUG|SEP) case "$THIS_DAY" in MON|WED|FRI) WATER ;; # on the other days, see if it was over 78 degrees *) case "$HIGH" in # 78 or higher # if they screw up the # display so we cant # get the high, as a failsafe, # water "")echo "$0: Could not find the high temperature. Watering to be safe." | mail -s "$0 error" davidbu WATER ;; # 78 or higher 7[89]|[89][0-9]|???) WATER ;; # not hot enough and not M W F *)echo "$THIS_DAY High temp: $HIGH precip: $PRECIPITATION did not water" >> "$LOG" ;; esac ;; esac ;; esac ;; *)echo "$THIS_DAY High temp: $HIGH precip: $PRECIPITATION did not water" >> "$LOG" ;; esac ;; *)echo "$0: Warning - the lookup of local temperatures and precipitation failed$TOO_MANY_RETRIES $URL" | mail -s "$0 Warning" davidbu # failsafe - water at least twice a week case "$DAY" in Sun|Wed)WATER ;; esac ;; esac # faster than a remove... > "$TMP_FILE"