Give your cleverHome one voice

Published by Coenraad Pretorius on

You have too many apps on your phone already and many smart home devices each come with their own app as well. We are going to unify these notifications and create our own customised messages via a Telegram bot.

Adapted from technology house vector created by

So many choices

We live in an exciting time where we have so many choices when considering buying new appliances and smart home devices. Each company has their own apps they use and many times we need to mix and match the devices that would best suite our needs and our budget.

While each device hopefully delivers on the features it promises, we sometimes need different devices to communicate with each other. Many times, we need to combine data from several of them to give us robust insights or take better actions.

The benefit is that most of these smart devices also have APIs that we can use. Therefore, we can collect data we need at the time we need it. This allows us to build our own cleverHome platform.

Monitoring electricity usage

I bought an Efergy electricity meter way back in 2011 and have been using it ever since. It has paid for itself in the first three months as I was able to identify that there was a billing issue on my account. The wiring was done incorrectly, and I was paying for my neighbour’s electricity, and he was paying for mine. The local utility was able to correct this and refund me for several months of incorrect billing using this data. Not so good for my power-hungry neighbour but on the good side he reduced his electricity consumption.

The device has a clip-on current transformer that I use to monitor the main power. The data is collected with the datalogger and sent to the hub via radio frequency. From there it is send to the internet and you can view it on the app. With the available API and token, we can get data from the device very easily using Python’s requests library.

View this gist on GitHub

Now that we have the data, we can start creating some customised notifications. As a start, we want to detect when the main power goes off and returns and send notifications for these events.

Careful considerations and assumptions

Monitoring the mains power sounds simple but there are some considerations and assumptions to be made. With no mains there is probably no internet. Of course, any internet related issue will also impact the functionality if you do not have a backup. We can then assume if there is no recent data (based on the timestamp) that the power is off, assuming a stable internet connection.

The device doing the monitoring must also be powered if it is part of your local cleverHome system. You can also opt to run a cloud hosted server which addresses some of the issues as data is gathered, in the case, from a web API. But if you are collecting data locally, these devices also need to be powered through a backup power bank or UPS. Thus, low power consumption devices are critical.

Fortunately, I have a battery backup system which keeps the router connected for internet and keeps essential loads powered for a few hours. In this case I will be running the code on a standalone Raspberry Pi that should be powered most of the time.

Creating a Telegram bot and group

As we don’t want to use several different apps and unify notifications, we are using Telegram to send notifications to a family group. This we will use in future for all other notifications and even to trigger actions. First, we need to create a Telegram bot and get the unique key from the BotFather.

Create a bot using BotFather and get the API key.

We create a private group and add our bot to it. After we send our bot a start message, i.e. /start, we send message to our newly created group. Using the endpoint<token>/getUpdates we can find the chat id of the group. That will allow us to send the messages to a group and share it.

JSON response to find the group chat id.

Creating a simple monitoring script

All queries to the Telegram Bot API must be served over HTTPS and need to be presented in this form:<token>/METHOD_NAME [1]. We create a simple Python method to allow us to send generic messages using requests library.

We are using in-memory queues to alert only after we have at least three readings to avoid any nuisance alerts, i.e. at least three minutes. To avoid continuously sending alerts, we need to know the current state of the power and only alert when that state changes. Therefore, the first two readings at startup will confirm our initial state and then we only alert when all three values in the queue have changed.

View this gist on GitHub

(Un)fortunately we have electricity supply issues currently which is a good opportunity to test our bot. We successfully get the notifications automatically when the power is switched off and when it returns.

Our bot sending messages when the mains power state changes.


In this post, we started off our journey to unify notifications and the creation create of custom messages for our cleverHome integration. We used Telegram as our main communications app, and we will be building on this in future posts. And of course, improving the code, so we don’t have everything in one file.

Updated code and full repo available on GitHub.


  1. Telegram Bot API


Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *