Developing an Energy Monitor with a Kasa HS300 Power Strip in Python — Part 2

In my previous story, I introduced how you can manually download realtime energy usage data of Kasa HS300 power strip from TP-Link cloud. In this part 2 story, we will automate the download function in python and visualize the time-series energy usage data using timescaleDB and jupyter notebook. To learn timescaleDB briefly, you can visit this story.

The first thing you need to do for storing the energy usage data to timescaleDB is creating a database table.

Assuming that you have installed postgreSQL and timescale, you can login to the postgresql as the default user account, postgres, with the unix command above. (Again, if you are not aware of timescaleDB, I recommend to visit this story before going further.) After logging in to postgreSQL, run the commands below to create a database called ‘energy_data’ and turn it to a timescaleDB:

Then you can create a simple DB table to store the energy usage data downloaded using the TP-Link cloud ‘emeter’ method:

By running the query above, you can see the database table is created successfully:

DB table creation result

After creating the database and a DB table, you need to write a python program which periodically downloads the energy usage data and store it to the created DB table. Since the communication with TP-Link cloud is based on the simple HTTP POST request, you need to write a python program that sends the POST request and parses the result json data.

Before writing the python code, I created a json file to store the id strings of the HS300 power strip and the children outlets:

Now it’s time to write your python code. To send an HTTP POST request, you can refer to the simple tutorial written by w3school. Following the tutorial, I used requests python module to send a POST request and get the corresponding response in a very simple way:

All you need to do is loading the json data and sending a POST request with the necessary id information. In my source code, I repeat this data request every 10 seconds. Later in the visualization, I will resample the energy consumption data with a resampling rate of 1 minute.

Now you need to insert the downloaded energy data to your database table. To do that, I import psycopg2 python module, and the gist below shows the full source code that I wrote for this story:

Since we decoupled all Kasa-related data from the source code, you can easily manipulate the data whenever you change the hardware device without editing the source code itself.

Now everything’s done!! You can run your python code and see if the energy data is stored in the database table successfully! In this story, I will see how much energy my AC consumes:

If you run these command and query, you will see:

The result of SELECT query in PostgreSQL: Aircon emeter data

I deleted the first two columns just to hide my deviceId.

Now we have the last step left, which is visualizing the collected energy usage data! Obviously, postgreSQL interface is not user-friendly at all. So it’ll be better to draw a plot chart to see the energy consumption trend effectively. Since I use Jupyter Notebook in this story, you should install Jupyter Notebook or Jupyter Lab before going further.

Since I use Jupyter Lab, my interface after creating a notebook in the same workspace looks like below:

Creation of a jupyter notebook, emerter-visualization.ipynb

The first thing you need to do after creating the notebook file is importing necessary packages. I import sqlalchemy module to connect to the local database. By creating an sql engine, I can run SQL queries in the jupyter notebook as I do in the local terminal. In addition to sqlalchemy module, I imported numpy, matplotlib, and pandas to manipulate the fetched data and visualize the result. In my notebook, I select energy consumption data collected for last one day by specifying the interval in the SELECT query.

Fetching energy data from the local database

What I want to do in this notebook is plotting the power data of the connected devices using a line graph. But the current Dataframe doesn’t seem to be in the right shape to achieve it. Therefore, I will reshape it by separating the power data corresponding to each device and making a separate column for each of them.

Reshaping the Dataframe

Then I will resample the data with a resampling rate of 1 minute. This resampling is quite useful not only for denoising the data but also having the data points with a consistent interval. I used the mean values of the resampled data in my notebook, but you can use different resampling methods depending on your needs. In the figure below, you can see that the number of rows is now much smaller than that in the figure above (i.e., before resampling the data).

Data resampling with a rate of 1 minute

Finally, we can visualize the resampled data using a simple line graph:

The visualization result using a line graph

Obviously, I can see that my air conditioner is the evil in my house.

In this story, I’ve presented how you can fetch the energy usage data from Kasa HS300 power strip and visualize it using jupyter notebook. I hope you can develop your own energy monitoring solution successfully and my story can be one of the reference materials for you :)

A computer scientist enthusiastic about enabling our surroundings smart and even autonomous! KAIST PhD, now at MIT as a Postdoc

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store