Testing
Once you have the code in your editor, and you’ve modified it with your own account_sid
, auth_token
, from
number (All three of which can be found in your Twilio Console), num_to_text
(typically your cell number, or anyone who would need to know when electricity is expensive), and your desired threshold
, it’s time to test.
Execute the script. If successful, you should receive a text message like the below screenshot:
If you received a text message, congratulations, it worked! A troubleshooting note may be beneficial at this point: If you ran the script and did not receive a message, it could be because your threshold currently exceeds the real time price of electricity. Change your threshold
variable to 0, and rerun your script.
Trigger
You’ve just received a text message, so you know everything is working. But you don’t want to run it just once. You want a vigilant watcher of real time electricity prices that runs on a regular basis, so you need a mechanism to trigger it. Because I use a Windows based laptop, and I was going for the path of least resistance, I set the Windows Task Scheduler to run every five minutes.
If you’re not familiar with the Windows Task Scheduler, here is a helpful link to get you started. The Task Scheduler is basically just an XML macro, that tells your computer what to do and when to do it. Fear not, there is no need to write any XML code to use the Task Scheduler.
(As an aside, if you don’t have a Windows machine, or you just want a more sophisticated trigger, check out this great blog post on how to use the AWS Lambda Function.)
To get started with the Task Scheduler, the easiest way to do it is to Press the Windows + R keys on your keyboard to open Run.
When the Run prompt appears, type taskschd.msc in the Open field. Click or tap on OK.
When the Task Scheduler opens up, look on the right side for a section called Actions. Double click on Create Task.
When you double click, it will open up the Task window. On the top of the screenshot below, there are six tabs (General, Triggers, Actions, Conditions, Settings, History (disabled)). You only need to worry about the first three (General, Triggers, Actions).
On the General tab, you need to give your Task a name. I called it the “ComEd High Price Alert.” It will be helpful, but not necessary, to give your task a description.
Click on the Trigger tab. Then click on “New” to add a new trigger. Under Settings, add a One time trigger and add a Start time. I used 8/5/18 at 1:00pm. Your time will vary, and that’s ok.
Under Advanced Settings, check the box that that says Repeat task every, and change the dropdown to 5 minutes. Make sure the duration dropdown is set to Indefinitely. Now make sure to check the Enabled box, and click OK.
I promise that we are almost done! The last thing to do is click on the Actions tab. This is a slightly tricky step, so there is no shame in rereading this part. Click on “New” to add a new action.
Make sure the Action is set to “Start a program.” The Program/script you want to start is your Python interpreter, so make sure your pathway is set appropriately. Mine is “C:\ProgramData\Anaconda3\pythonw.exe”, but yours will likely be different.
Then you need to fill in the Add arguments (optional) piece. Even though it says “optional”, for our purposes it is required. This is the location of your .py script. This was the part that initially gave me the most trouble, so if you get stuck, here is a nice step-by-step aid.
(Troubleshooting tip: If the Action tab is causing you trouble, it may help to remind you that all Python scripts can be started from the Command Line Interface. The Windows Task Scheduler simply automates that.)
Finally, click OK.
There you have it. In less than 100 lines of code, you’ve built yourself a helpful little script that that texts you an alert when the real time price of electricity rises above the threshold you set. The combination of Twilio, Python, and ComEd have helped me shrink my utility bill, hopefully, it will shrink yours as well!
If you want more information on real time electricity, the ComEd Hourly Pricing site is excellent, or check out this short YouTube video for a great explanation.
If you want to see the live prices at the source, check out the Live Prices page. The ComEd API feeds are pretty fun to play around with as well, and are very customizable if you are so inclined to modify the code to suit your purposes.
If you have any questions, you can find me on Twitter, or LinkedIn. Have fun!