Shellscript: Sunset or Sunrise times

I was looking for a script which shows the sunrise or sunset times for my location. I want to use these times to turn on or off the lights in my house. I couldn’t find a script that satisfy my needs so i need to make it by my self, i’m not a professional coder by the script below satisfy my needs…

#!/bin/bash
#
# This scripts downloads today's sunrise or sunset times from weather.yahoo.com
# You'll need to obtain a location ID from weather.yahoo.com for your location
# Go to http://weather.yahoo.com and enter your city or zip code a new URL opens
# EXAMPLE: http://weather.yahoo.com/nederland/noord-brabant/tilburg-733881/ 
# In the example 733881 is the location ID for my location...

# Example parameters:
# thisscript.sh sunset # shows today's sunset
# thisscript.sh sunrise # shows today's sunrise
# thisscript.sh sunset \"30 minutes\" # shows today's sunset +30 minutes
# thisscript.sh sunset \"2 hours\" # shows today's sunset +2 hours
# thisscript.sh sunset \"30 minutes ago\" # shows today's sunset -30 minutes
# thisscript.sh sunset \"2 hours ago\" # shows today's sunset -2 hours
# thisscript.sh sunrise \"30 minutes\" # shows today's sunrise +30 minutes
# thisscript.sh sunrise \"2 hours\" # shows today's sunrise +2 hours
# thisscript.sh sunrise \"30 minutes ago\" # shows today's sunrise -30 minutes
# thisscript.sh sunrise \"2 hours ago\" # shows today's sunrise -2 hours

# Enter your location ID obtained from weather.yahoo.com URL 
LOCATION=733881

if [ ! $1 ]; then
echo "Parameter(s) missing!!!"
echo ""
echo "Example parameters:"
echo "thisscript.sh sunset # shows today's sunset"
echo "thisscript.sh sunrise # shows today's sunrise"
echo "thisscript.sh sunset \"30 minutes\" # shows today's sunset +30 minutes"
echo "thisscript.sh sunset \"2 hours\" # shows today's sunset +2 hours"
echo "thisscript.sh sunset \"30 minutes ago\" # shows today's sunset -30 minutes"
echo "thisscript.sh sunset \"2 hours ago\" # shows today's sunset -2 hours"
echo "thisscript.sh sunrise \"30 minutes\" # shows today's sunrise +30 minutes"
echo "thisscript.sh sunrise \"2 hours\" # shows today's sunrise +2 hours"
echo "thisscript.sh sunrise \"30 minutes ago\" # shows today's sunrise -30 minutes"
echo "thisscript.sh sunrise \"2 hours ago\" # shows today's sunrise -2 hours"
fi

case $1 in

sunset) 
SUNSET12H=`curl -s http://weather.yahooapis.com/forecastrss?w=${LOCATION}|grep astronomy|awk -F\" '{print $4}'`
SUNSET24H=`date --date="${SUNSET12H}" +%T`
date --date "${SUNSET24H} $2" +%R 
;;

sunrise)
SUNRISE12H=`curl -s http://weather.yahooapis.com/forecastrss?w=${LOCATION}|grep astronomy|awk -F\" '{print $2}'`
SUNRISE24H=`date --date="${SUNRISE12H}" +%T`
date --date "${SUNRISE24H} $2" +%R 
;;
esac

6 thoughts to “Shellscript: Sunset or Sunrise times”

  1. Perfect script, Just what i needed. big time saver 🙂
    Now i need to script the sunset/sunrise time for selective script execution 😛

    great effort thanks

  2. It looks like this no longer works as Yahoo have dropped public access to the API and replaced it with OAuth.

    Do you have a working example using OAuth?

  3. The yahoo RSS has just stopped working, It now asks for some authentication!
    I just changed my script temporarily to use
    htm=`curl -s time.unitarium.com/sunrise/42332|grep “”`
    sunrise=`echo $htm | awk -Ftd\> ‘{print $2;}’ | awk -F\ ‘{print $4;}’ | awk -F\</ '{print $1;}'`
    log "Sunrise today: $sunrise, sunset $sunset"

    if [ `date +%s` -gt `date –date="$sunrise" +%s` ] && [ `date +%s` -lt `date –date="$sunset +00:30" +%s` ]; then

Leave a Reply

Your email address will not be published. Required fields are marked *