Skip to content

Instantly share code, notes, and snippets.

@shadministrator
Created March 22, 2021 00:17
Show Gist options
  • Save shadministrator/490746b870fca657e71dc160fd61dd2c to your computer and use it in GitHub Desktop.
Save shadministrator/490746b870fca657e71dc160fd61dd2c to your computer and use it in GitHub Desktop.
Low Temperature Alert
import json
import boto3
import requests
import http.client
def lambda_handler(event, context):
# Instantiate SNS
sns = boto3.client('sns')
# Call OpenWeatherMap API and get low temperature for tomorrow
url = "https://api.openweathermap.org/data/2.5/onecall?lat=41.720380&lon=-70.003700&exclude=current,minutely,alerts&units=imperial&appid=[api_key_here_no_brackets]"
weatherMapCall = requests.get(url).json()
todaysLow = weatherMapCall['daily'][0]['temp']['min']
tomorrowsLow = weatherMapCall['daily'][1]['temp']['min']
# Define low temperature threshold (temperature below which alert should be triggered)
lowThreshold = 26
if todaysLow < lowThreshold:
lowTempAlert = sns.publish(
TopicArn='arn:aws:sns:us-east-1:1234567891011:LowTempAlert',
Subject='Shut Off Water!',
Message='Low temp today is below 26 degrees - shut off water!'
)
elif tomorrowsLow < lowThreshold:
lowTempAlert = sns.publish(
TopicArn='arn:aws:sns:us-east-1:1234567891011:LowTempAlert',
Subject='Shut Off Water!',
Message='Low temp tomorrow is below 26 degrees - shut off water!'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment