Skip to content

Instantly share code, notes, and snippets.

@greg-randall
Last active November 1, 2023 19:14
Show Gist options
  • Save greg-randall/0a2c754fe443ec27035d72920424a0bb to your computer and use it in GitHub Desktop.
Save greg-randall/0a2c754fe443ec27035d72920424a0bb to your computer and use it in GitHub Desktop.
Checks if the temperatures will be freezing in your area over the next 24hrs and texts you if there are freezing temps. I have mine set to run at 7:00am on a cron job.
#########SETUP#########
#textbelt_key: you get one free text per day with the key 'textbelt' or if you think you need more get an api key from https://textbelt.com/
#phone_numbers: a single phone number as a list, just the digits, or if you want to send to multiple people, add addtional numbers in the list
#lat_lon: your latitude and longitude in the format "xxx.xxxx,-xxx.xxxx". https://mapper.acme.com/ is helpful here, though note to change the formatting (ie "N 45.52300 W 122.67600" to "45.52300,-122.67600")
#freezing_notification_temp: default is that you will be notified for temperatures at or below 35, but change that if you want
textbelt_key = "textbelt"
phone_numbers = ["5555555555","5555554444"]
lat_lon = "45.52300,-122.67600"
freezing_notification_temp = 35 #temperature or lower in fahrenheit that we'll send a notification for
import urllib.request
import json
from datetime import datetime
import requests
def pretty_timestamp_short(timestamp):
dt = datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S%z")
return dt.strftime("%-I%p")
def send_text(phone,message,api_key):
requests.post('https://textbelt.com/text', {'phone': phone,'message': message,'key': api_key,})
#the weather.gov api is only accepts lat/lon with four decimals, so we'll round it
lat, lon = lat_lon.split(',')
lat_lon = f"{round(float(lat), 4)},{round(float(lon), 4)}"
with urllib.request.urlopen(f"https://api.weather.gov/points/{lat_lon}") as url: #get the forecast url for our location
data = json.load(url)
hourly_forecast = data['properties']['forecastHourly']
location_name = f"{data['properties']['relativeLocation']['properties']['city']}, {data['properties']['relativeLocation']['properties']['state']}"
with urllib.request.urlopen(hourly_forecast) as url: #get the forecast data
data = json.load(url)
message = f"{location_name} temperature predicted to be at or below {freezing_notification_temp}F over the next 24 hours:\n"
freezing_temps_found = False
for period in data['properties']['periods']: #loop through the forecast data
if period['temperature']<=freezing_notification_temp: #if the temperature is close to freezing we'll text
message += f"{period['temperature']}F at {pretty_timestamp_short(period['startTime'])}, "
freezing_temps_found = True
if period['number']>24: #break after looking at 24hrs of data
break
if freezing_temps_found:
message = f"{message.rstrip(', ')}." #remove trailing comma
print(message)
for phone_number in phone_numbers:
send_text(phone_number,message,textbelt_key)
else:
print(f"No temperatures at or below {freezing_notification_temp}F over the next 24 hours.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment