Freeze Alert ❄️

Designing a Python script to send an email when it's going to freeze

Weather map of North Carolina counties showing where a freeze alert is in effect

Background

Winter weather in North Carolina can be unpredictable. Extended hard-freezes are uncommon, but can result in burst water pipes when precautions are not followed. As water turns into ice, it undergoes expansion, leading to the solid ice occupying a larger volume than its previous liquid state in the pipes. This expansion of ice generates pressure which can ultimately lead to a rupture.

Constraints

My goal for this project was to send an email alert when the three-day forecast called for a freeze. In addition:

Core Functionality

The project is written in Python. The main function checks the NOAA weather API for a given location's 72-hour weather forecast. If any temperatures are forecasted to fall below 32 degrees Fahrenheit, it adds the timestamps to an email and sends it using SendGrid.

Infrastructure & Automation

I decided to use SendGrid's Email API because it was well-documented and supports sending up to 100 emails per day for free.

The function is hosted on Google Cloud Functions because I already had a Google Cloud account and was familiar with the platform. Google supports 2 million invocations per month for free - far more than I would ever need.

To check the 72-hour forecast each day at 7pm ET, I set up a job with gcloud scheduler like this:

gcloud scheduler jobs create pubsub run-my-function-name-job \
--schedule "0 19 * * *" \
--topic run-my-function-name \
--message-body "{}" \
--project my-project-name \
--time-zone "America/New_York" \
--location us-east1

Privacy

This project presents two areas of privacy concern:

To avoid exposing location or emails, I wrote the application to pull these values from environment variables which are not committed to git. This is a good practice even if the repository were private.

recipients = os.environ.get('RECIPIENTS')
sender = os.environ.get('SENDER')
points = os.environ.get('POINTS')

Conclusions

After one month, the project is working as expected. An email is sent when the 72-hour forecast calls for a freeze, which increases awareness in my community about precautions to take. A sample email:

A freeze is forecasted for the following date(s):

- November 02, 06:00 AM
- November 02, 07:00 AM
- November 02, 08:00 AM

Please take the following precautions:

- Keep heat set to 60 degrees Fahrenheit or higher
- Cover front and rear spigots

In case of a burst pipe:

- Turn off water at the main shut-off valve
- If the sprinkler system has activated, call 911

https://github.com/nickFalcone/freeze-alert

Enjoy what you read? Feel free to share this post!

Published