openHAB 2 comes with a Telegram binding which allows to run a Telegram Bot. This bot can both send messages to users and groups, and can receive commands and respond to them. That’s useful: your home automation system can send all kind of details to your mobile phone.
For this to make it work it needs a couple things:
First of all a mobile phone with the Telegram app on it. You can either have the bot message you directly, but this only works for one person. Or you create a group, and have the bot send the messages to the group instead. Find out about the group ID here.
Then you need to create a Telegram Bot. Instructions are available here.
And everything needs to be hooked up in openHAB.
Let’s start with installing the Telegram binding. As usual I use Ansible to install and deploy everything, instructions are available in some of these blog postings.
Store the Bot Token in a text file (it’s
credentials/telegram-bot.txt in my case). Make sure only your user can read the file - anyone with access to the Token can control the bot. (
chmod 0600 or
I use a couple different Telegram groups:
- One for the family, the bot is sending important messages there
- One for more detailed messages I’m interested in (kind of a debugging channel)
- A testing channel which I use to try out new features
The (negative) channel ids are stored in text files. The ids are loaded by Ansible when I deploy the
Looks a bit long, because of the many Ansible Lookups. The deployed file in
/etc/openhab2/things/telegram.things is much shorter:
Note: “HA_Bot” is the name of this bot. This name is required for all other activities with this bot.
The Binding provides a couple channels, I’m mostly interested in:
lastMessageText: the last command sent by someone
lastMessageDate: the time someone sent the last command
chatId: the group or private chat someone sent a command to - the bot needs to reply to that
All channels defined as Items:
Once the bot is running, it can send messages on it’s own:
This will send a test message to the Telegram group defined in
For responding to messages, the bot needs to be triggered by an event. It’s not useful to work on
lastMessageText, because a user can send the same message multiple times - in this case no change event is triggered. An update is triggered, but that’s not helpful here. However with every message the
lastMessageDate will change, and that can be used for a trigger:
This example picks up a
/test command, and sends a simple message back.