Sunset Walks

Sunset Walks

A simple script to send a text message and email to my wife about our evening walks.

Each day, the sun sets at a different time, and in order to avoid our walk being too hot, too cold, too bright, or too dark, we try to leave approximately 75 minutes before sunset, as our walk takes about 1 hour.

  • Mutt, a command line interface to access my gmail account from the terminal.
  • Python’s suntime library to compute the sunset based on our coordinates and local time zone.
  • crontab, a file that allows for scheduling of scripts
  1. Getting Mutt up and operational was by far the most challenging element of this project. After following the correct syntax in the .muttrc file, I had to add some parameters to allow for proper login, including changing my gmail account to a two factor authentification and generating a custom key for the script.
  2. The suntime library was not the first library I tried, but the first two failed because I had my coordinates as a positive instead of a negative value, because I missed the “W” vs. “E” label when I looked up my coordinates on google. I spent ~30 minutes troubleshooting and messing with date times only to find that I was consistently off by the same amount, which was the clue I needed to check my coordinates.
  3. I had to link up the bash script, and since I don’t know how to pass a python script output directly to a bash script, I used “daily_message.log” as an intermediary to store the message.
  4. Finally, I set up the cron job with the correct syntax.

The following bash script runs every morning according to the follwing files:

crontab

0 9 * * * cd /path/to/file && ./sunset_walk.sh



sunset_walk.sh

python sunset_walks.py > daily_message.log 		# writes the message to the daily log for easy access
DOW=$(date +%A)
cat daily_log.log | mutt -s "${DOW} Sunset Walk Reminder" wife_email@gmail.com
cat daily_log.log | mutt -s "${DOW} Sunset Walk Reminder" wife_phone_number@vtext.com



sunset_walks.py

from datetime import date, timedelta
from suntime import Sun
import personal_info as pi

def suffix(d):
    return 'th' if 11<=d<=13 else {1:'st',2:'nd',3:'rd'}.get(d%10, 'th')

def custom_strftime(format, t):
    return t.strftime(format).replace('{S}', str(t.day) + suffix(t.day))

latitude = pi.home_latitude
longitude = pi.home_longitude

sun = Sun(latitude, longitude)

# Local time
today = date.today()
sunset_time = sun.get_local_sunset_time(today)
walk_departure_time = sunset_time - timedelta(hours=1, minutes=15)
print("Hi Sweetie! \n\n Today, {}, the sun sets at {}, so we should leave to go on our walk at {}. \n\n - JakeBot :)".
      format(custom_strftime('%B {S}, %Y', today), sunset_time.strftime('%I:%M %p'), walk_departure_time.strftime('%I:%M %p')))