“Alert

Overview

DarkSky is - in my opinion - the best weather App for iOS out there. They provide an API that developers can use in their Apps or Scripts to play around with weather predictions and patterns.

This post explains how to use this API to get notifications to your phone of rain or snow in a predefined location, 24 hrs in advance (although the time interval can easily be changed). This can be useful to plan your activities for the weekend, determine if you’ll need to bring your umbrella the next day to the office, or simply show your friends what big of a geek you are.

I have been using this script for some months now, with no problems at all. It has been very accurate thanks to DarkSky’s predictive magic. I recently saw a post in Pedro Lobo’s blog making reference to a similar script in a different blog. I thought some people might be interested in my script and decided to share it.

Note: This post contains affiliated links. If you want to support this site, please buy the software and/or hardware through the links included in this article.

What is Needed?

  • DarkSky iOS App. You can get it by clicking here.
  • PushOver iOS App. Get it here. More information on PushOver here
  • Python Script. You can downloaded here
  • A computer running the Python Script on a regular basis.

How Does it Work?

“DarkSky Alert

The script is run at predefined times in an always-on machine. It requests weather information for the following day through the DarkSky API. If the probability of rain 24 hours from the moment the script is run, is equal or larger than 55%, an instant message is sent to my iPhone by ways of Pushover. I make sure the script runs in the morning at noon and at the end of the afternoon. Pretty neat, right?.

It’s worth mentioning that by the time of this writing, the latest version of DarkSky for iOS can notify you when is about to rain in the location you are currently at (see image to the right). The main advantage of this script is that it’s always looking at the same location (e.g. Home or Work) and alerts you know of rain well in advance instead of just minutes before.

Pushover Service

Pushover allows you to receive instant notifications to your phone. I currently use it on several scripts. The service is free, but the App is less than four bucks. Some automation services such as IFTTT connect directly to pushover. We will be using their API to interact with their servers from the Python script.

Once you download Pushover from the iTune Store, go to the developer page, signup for the service and obtain your User Key and create a Token for your application (in this case, the script). You can also assign an icon to each application so it would show up when getting the alert on your phone.

The Script

Full Disclaimer: I don’t consider myself a programmer. Although I did program in several languages in college, that was some time ago. I’m pretty sure you can find some better and more efficient ways to achieve the same results. If you do, feel free to revise the code.

Python Script

You can access the script by clicking the image to the left. I included comments in the script to make it as self-explanatory as possible in case I needed to revise it in the future. The first part defines the Constants to be used such as the DarkSky API key and the Lalitude and Longitude of the location to be monitored. There are several online tools such as this one that will help you find out the exact coordinates. Google Earth can also be of help.

A function is defined at the beginning of the script that will be later called to send the message to pushover so it is transmitted to the iPhone. You should replace the token and user variables with the ones you got from Pushover. I could have included them in the Constants section, but is wanted to make the function standalone so that I could use it in other scripts.

The Main Program part of the script begins by connecting to DarkSky and getting the weather information for the following day. I’ve included print statements in the code so you can see what’s happening when running the script manually from the console.

In the main If statement we define the Precipitation Probability threshold for the alerts. Thus, if it is larger than or equal to 55% (0.55 in the script), a message is sent to your phone through Pushover. The message will also include comments on the precipitation intensity depending on the data provided by Pushover. This is taken care of in lines 54 to 58.

Running the Script at Predefined Intervals

I have the script running at predefined times, three times a day (morning, noon and evening). There are many ways to achieve this. Since mine is running on a Linux box, I’m using Cron for that, more specifically one of its GUI interface (more info here).

If you have questions or comments, please feel free to leave them in the section below or contact me directly

Tools Used in this Post

Python Script