Running scripts on Sunrise/Sunset

I have my front yard light connected to a outletbox controlled via a LPT port. And it's hooked up the the rest of the home automation system.

I was just having cron turn on and off the light a specified times. But a little script and a bit of help from a government website, the light turns off at sunrise and back on at sunset.

Here's the little script that does it:

#!/bin/sh
#
# Gets sunset / sunrise tables from aa.usno.navy.mil
# http://ubuntu.online02.com/node/36
# Run with cron after midnight but before sunrise (4am) to get the times for the day
#

YEAR=$(date +%Y)
MONTH=$(date +%m)
DAY=$(date +%d)
STATE="NY"
CITY="omaha"

wget http://aa.usno.navy.mil/cgi-bin/aa_pap.pl --post-data="xxy=$YEAR&xxm=$MONTH&xxd=$DAY&st=$STATE&place=$CITY" -O /tmp/suntable.htm

SUNRISE=$(cat /tmp/suntable.htm | grep Sunrise | sed 's/Sunrise //' | sed 's/\.//g' | sed 's/ //g')
SUNSET=$(cat /tmp/suntable.htm | grep Sunset | sed 's/Sunset //' | sed 's/\.//g' | sed 's/ //g')

echo Sunrise: $SUNRISE
echo Sunset: $SUNSET

# Run commands at sunrise or sunset
at -f /path/to/script.sh $SUNRISE
at -f /path/to/script.sh $SUNSET

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options