Using a Telegram Bot for Home Assistant Notifications

Google announced they were going to discontinue Hangouts what seems like forever ago (I mentioned it in my 2020 predictions). They still haven’t completely retired it, but I expect they will soon. The expectation is that most users will migrate to Google Chat. I rely on the Google Hangouts Home Assistant Integration for most of my smart home notifications. Not wanting to chance how things will work when are forced to migrate, I switched to a Telegram Bot for my notifications. Here’s why, and how I did it.

Telegram Pinterest Pin

How I use notifications in my smart home

Notifications are very important in my smart home. I like to know when key events have taken place, when security events have happened, and I like confirmation of certain activities. Some of the notifications I have set up in Home Assistant are sent to me when:

  • The alarm isn’t armed at night
  • When the alarm is disarmed
  • A panic button is pressed
  • When the robot vacuum is scheduled to run
  • When a garage door has been left open too long
  • A new Home Assistant release is available
  • The generator is running
  • A code is used to open the front door lock (and who’s code it was)

These notifications and many others are important for knowing what’s going on in my home.

Why I chose Telegram

The main reason I’ve been using Google Hangout notifications is that they are quick and I can access them on multiple platforms, including Linux. I want to see notifications on the desktop, on mobile, and on Chromebooks too. I also like having a log of all of the notifications. It sometimes helps when troubleshooting automations.

See also  My Top 10 Home Assistant Integrations

Telegram fits all of these scenarios. It’s timely, it has Chrome, Linux, and Android clients. It also keeps a timestamped log of notifications. It ticks all the boxes.

One feature it has that I currently don’t use is the ability to send commands back to Home Assistant. But it is good to know I can do this if I want to in the future.

Setting Telegram notifications in Home Assistant

In short, this takes 3 steps: Getting a Telegram account and setting up a bot, configuring Home Assistant, and sending notifications.

Step 1: Set up Telegram account and create and configure your bot.

Telegram uses “bots” for communication. You’ll need to create a bot for your notifications and then get its access token and chat ID for integration into Home Assistant.

  1. Install the Telegram client on your mobile device. Sign up for an account.
  2. Search for the BotFather-bot afbeelding in your Telegram Messenger app afbeelding.
  3. Ask BotFather-bot to create new bot for your notifications by sending the /newbot command. You’ll be asked to fill in some information to create the bot.
  4. After you create your bot you’ll be shown an access token. Copy/Write this down. You’ll need it later. If you forget to write it down or need to find it again for some reason you send the /token command to see it.
  5. Go to the bot you just created and send a message (with any content).
  6. Time to get the chat id. Open a web browser and go to the URL: https://api.telegram.org/bot<token>/getUpdates (without the brackets around token). <token> is the token you wrote dearn in step 4.
    Check the output and look for the value after "id": This is your chat ID.
See also  Planning Your pfSense Router Install and Configuration

Now you have everything you need to set up your Home Assistant Telegram integration. But you may want to make a few other customizations to your bot. I recommend adding a picture. Go back to BotFather-bot and issue the command /setuserpic. This will allow you to add a picture. If you don’t like the name you selected for your bot you can also ask BotFather-bot to set a new name with the /setname command.

Step 3: Configure Telegram Home Assistant integration

After setting up the bot you are now ready to integrate with Home Assistant. There are three ways to integrate with Home Assistant with Telegram:

  • Polling platform – If you don’t want to expose your Home Assistant instance to the Internet you can use the polling platform to send and receive messages with your Telegram bot.
  • Webhooks platform – If you have your Home Assistant securely exposed to the Internet or are okay with doing so you can use the webhooks platform to send and receive messages.
  • Broadcast platform – If you are only interested in sending messages (notifications) to your Telegram Bot and not having your bot send messages to Home Assistant then this is probably the best option, and it is the one I chose.

In your configuration.yaml add the following:

telegram_bot:
  - platform: broadcast
    api_key: YOUR_API_KEY
    allowed_chat_ids:
      - 123456789 # example chat id

You may want to store your API Key and chat id(s) in your secrets.yaml file. Then set up the telegram notification platform to make it easier to send messages (notifications) to Telegram via scripts and automations.

notify:
  - name: NOTIFIER_NAME
    platform: telegram
    chat_id: USER_CHAT_ID

Make note of the NOTIFIER_NAME that you choose. You’ll use this when sending Telegram notifications. I use “telegram_main” for my notifications. You’ll need to restart Home Assistant for this to become effective.

See also  How to Make a Smart Home Test Bench

Step 4: Send notifications using Telegram

Sending notifications using Telegram is straight forward. Just place something like below in whatever automation or script that you want to send a notification:

 - service: notify.telegram_main
   data:
     message: "URGENT: This is a telegram notification"

Final thoughts

For years I’ve been using Google Hangouts for reliable Home Assistant notifications. Unfortunately, Google is discontinuing the platform. Notifications are too important to leave to chance, so I migrated to Telegram. I’ve been pretty happy with it, and hopefully I won’t need to change notification engines for a while.

What platform do you use for notifications? Let me know on Twitter or in the comments.

Share this:

Using a Telegram Bot for Home Assistant Notifications

by HomeTechHacker time to read: 4 min