Control your electrical loads using Google Cloud Platform (GCP), Shelly relay and hourly electricity price (Finland)
This is a cloud based load control solution for Finnish residents (with a spot-price contract) who would like to reduce their electricity bill
This simple Python project is an evolution from electricity-saver-I project. In this project, we are not using any local computers to run scripts thus deploying our code directly in Google Cloud Platform (GCP). Our Shelly relay is controlled via (Google) cloud - (Shelly) cloud connection and we are requesting electricity prices via this new API (prices only for Finland, sorry..). This makes script simpler because we do not need to work with raw data or with ENTSO-E API key as in "electricity-saver-I" project.
This project requires beginner level knowledge of programming & Linux environments and is meant for people who are intrested smart homes and maybe using already Shelly relays in their home.
In this demo, we are using Shelly pro 4PM and its first relay output (ID = 0). You can select your own prefered Shelly relay model and expand the script for different or multiply outputs of course. Note that this kind of electrical installation must be carried out by a professional (= electrician) in Finland so ask your local electrician to do your Shelly installation/wiring.
- connect Shelly to your home network and make basic comissioning with Shelly Cloud App
- open the app (web or mobile) and copy your relay "DEVICE ID" (Click the device --> Settings --> Device Information --> DEVICE ID).
- activate the cloud key (User Settings --> Authorization Cloud Key)
Check this tutorial from Algovibes how to run Python in GCP environment. As soon as your Linux Debian virtual machine is installed with Miniconda environment:
- open "nano" text editor by typing command line:
nano my_spot_script.py - copy the python script from the repository and paste it to nano editor
- modify following parameters in the script based on your device: DEVICE_ID, CLOUD_KEY, SERVER
- close the file by pressing ctrl + x
- press y (= saving yes) and press enter to save and exit
Test that your script is working by typing command line: python my_spot_script.py
You should see something like this: 29-10-2022,08:54,*your_device_id*,True,0.0161,0.2,True which is a log file we are going to save for the future use. It contains following information: date, UTC time, device ID, Shelly is online (true/false), hour price in kWh with taxes, my limit price in kWh, relay is ON (true/false)?
Great. If you see the output above we are almost there 👍
Crontab is a file that allows script automatization in Linux environment. This is a great feature in projects like this because we must run the script at least once per hour and check if current electricity price is lower than our limit price. In addition, it also allows to save logs to a text file. Look this tutorial from Algovibes how to automate your script using "Crontab".
If you want to run the script on the hour (15:00, 16:00, 17:00 ... and so on) use following syntax:
0 * * * * home/username/miniconda3/bin/python my_spot_script.py >> history.txt
First five digits/stars * * * * * represents time syntax and more interval examples you'll find from here.
Then comes a path to your Python installation. This you will find by typing: which python. Last but not least, the extension >> history.txt creates a a file where all outputs: 29-10-2022,08:54,*your_device_id*,True,0.0161,0.2,True are saved. You can open the file later by typing nano history.txt Note that this file is created after first Crontab run and not before.
This project was deployed using GCP. A cloud service comes with a cost but selecting smallest virtual machine available costs should stay under 10 € / month. Google is giving three months free tier (300 $) so testing the system should not generate any costs. Second. Controlling your electrical loads via (two) clouds is exposing your system for cyber attacks comparing local machine running in the local network. If you want to go more secure go for electricity-saver-I. Evaluate possible risks in your case and make the call.
That's it. Happy scripting!

