Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Ryan Kauffman
Published © CC BY-NC-ND

High Priced Electricity SMS Alerts with Python and Twilio

This project sends you a text message the price of electricity rises above a threshold that you determine.

BeginnerFull instructions provided1 hour623
High Priced Electricity SMS Alerts with Python and Twilio

Things used in this project

Software apps and online services

SMS Messaging API
Twilio SMS Messaging API

Story

Read more

Code

Code snippet #9

Plain text
def how_many_fives():
    y = 0
    for x in range(1,get_minutes()):
        if x%5 == 0:
            y = y + 1
    return y

Code snippet #10

Plain text
for x in range(0,how_many_fives()):
    agg_price = five_min_api_response.json()[x]['price']
    agg_price = float(agg_price)
    my_price = my_price + agg_price
    x = x + 1

Code snippet #19

Plain text
def main():
    get_time()
    get_minutes()
    how_many_fives()
    
main()

Code snippet #20

Plain text
import requests
import time
from twilio.rest import Client

account_sid = "your account SID"
auth_token = "your Auth Token"
client = Client(account_sid, auth_token)

five_min_api = "https://hourlypricing.comed.com/api?type=5minutefeed"

five_min_api_response = requests.get(five_min_api)

counter = 0
my_price = 0

# Gets the current time in hh:mm format
def get_time():
    cur_time = (time.strftime('%I:%M'))
    return cur_time

# Gets the minute right now.
def get_minutes():
    minutes = (time.strftime('%M'))
    minutes = int(minutes)
    return minutes

# Determines how many increments of 5 have passed this hour.
def how_many_fives():
    y = 0
    for x in range(1,get_minutes()):
        if x%5 == 0:
            y = y + 1
    return y

for x in range(0,how_many_fives()):
    agg_price = five_min_api_response.json()[x]['price']
    agg_price = float(agg_price)
    my_price = my_price + agg_price
    x = x + 1   

my_price = my_price/x
my_price = round(my_price,1)
time_now = get_time()

my_msg = "ALERT: You may want to turn off some appliances, "
hourly_avg = ("the current hourly avg at " +  str(time_now) + " is: " \
              + str(my_price) + " cents/kWh. ")

num_to_text = "415-500-1234"
threshold = 7.2

# Task schedule the script to run every 5 minutes, if the price exceeds the threshold, 
# send the text.
if my_price > threshold:
    client.messages.create(num_to_text, 
    body=(my_msg + str(hourly_avg)),
    from_="+14155001234")

def main():
    get_time()
    get_minutes()
    how_many_fives()

main()

Github

https://github.com/Rkauff/Expensive-Electricity-Alert

Credits

Ryan Kauffman
3 projects • 9 followers
Contact

Comments

Please log in or sign up to comment.