A Guide to using Telegram with Node-red and Home-Assistant

A Guide to Using Telegram with Node-Red and Home-Assistant

I thought I’d publish a post to show you how to use Telegram with Node-red. Telegram is an instant messaging platform with a great channel feature. It means that not only can you receive notifications from your Home-Assistant installation, but you can also send commands in your private “chat room” and have your system respond to you as required. This could be asking a question about the temperature or air quality, or it could be telling the house to shut down for the evening and having all the lights switch off. In this post, I am going to skip through some of the telegram set up features as they’re already quite well laid out in the official instructions (see below) but I will show you my code, and how to work with the nodes and flows in Node-Red. By the end of the post you should be able to use the chat room to communicate with your Home-Assistant and receive feedback as needed.

Initial configuration steps in Home-Assistant: https://www.home-assistant.io/integrations/telegram/

Initial bot creation and configuration in Telegram: https://core.telegram.org/bots#6-botfather

Creating the bot can be a little bit funky, it basically involves you sending a series of commands to a telegram entity and then receiving the chat_id and api token from there. Once you have all the info, we can proceed.

Once your steps are completed above, you should have the following set up in yaml. Note I am using secrets, and advise you to use it too, if you’re not, then you would just paste the values directly into configuration.yaml.

(configuration.yaml)

notify:

  – name: telegram

    platform: telegram

    chat_id: !secret telegram_chat_id

 

telegram_bot:

  – platform: polling

    api_key: !secret telegram_api

    allowed_chat_ids:

      – !secret telegram_chat_id

 

(secrets.yaml)

telegram_api: 8…<a_really_long_string_went_here>..c

telegram_chat_id: 123456789

 

For Node-red, I installed the following node- https://flows.nodered.org/node/node-red-contrib-telegrambot

https://flows.nodered.org/node/node-red-contrib-telegrambot
https://flows.nodered.org/node/node-red-contrib-telegrambot

Drag a node out onto your workspace and double click to configure. Here you should already have the Bot-Name, Token (api), Users, and ChatIds as you’ve set them up above. Fill the top 4 text boxes, you can ignore the rest.

telegram node configuration

telegram node configuration

Now let’s create a simple command flow. I will write in my telegram chat /test and then I want to be able to toggle on and off my Office Lamp.

Simple Command Flow
Simple Command Flow

See below for the precise configuration of the two nodes.

A Receive Node
A Receive Node

You need to fill in the Bot from the drop down which you’ve already created. I’ve removed it here for privacy reasons.

 

Simple Service Call Node
Simple Service Call Node

The switch I am toggling on and off is a Blitzwolf SHP5 that is running Tasmota. You can read about how I set that up here. Now deploy your flow. If everything’s been set up correctly, just write /test and send it in the chatroom, you should now be able to toggle your light switch on and off.

Still with me? Now I am going to show you how to set up a notification node in node-red. I use a standard format which might look a bit long winded, but it covers any kind of payload and well it works for me! I’ll show you a simple notification, and then I’ll show you a more advanced payload in the same format and how we can incorporate the receive and the send together to do something more useful than just toggling a light 🙂

Below is a send flow.

Telegram Send Notification Flow
Telegram Send Notification Flow

Don’t be confused by the name on last node, it’s the send node, i just labelled it as “received command.” You can label it what you like. See below for the full configuration.

Simple Inject Node
Simple Inject Node

 

set msg.payload
set msg.payload
create message
create message

You’ll need to enter your ChatId here which you already have set up. Be mindful of the single quotes either side of it.

send msg node
send msg node

Again I’ve removed the Bot for privacy reasons. You will need to fill this in.

Once you’ve got the above set up, deploy the flow and test it out. Once you hit inject, the chat room should post “Received your command.” If everything’s working as it should, we are now able to receive commands and send notifications. Let’s combine them together and create a slightly more complex example.

 

Consider the flow below:

AQI flow with additional Inject
AQI flow with additional Inject

AQI (Air Quality Indicator) is a sensor that I have running in Home-Assistant. You can read about it here. I’m using this, but you could use pretty much any sensor reading. I also added a simple inject node so I could test it out without having to flood your chat room with the command /aqi. See below for the flow breakdown. You’ll see I am using the same format and layout as my previous message. I find it easy to copy and paste this “group” when creating new flows and simply change the text out.

current state node
current state node
AQI change node
AQI change node

Note to edit the payload you need to click on the three dots to the side of the expression above.

AQI JSON editor
AQI JSON editor
create message
create message

Add your chatId here, don’t forget the single quotes.

send msg node
send msg node

Again remember to fill in the Bot. You can change the Name here to whatever you want. It’s just a label.

I didn’t show you the first node /aqi, you can just copy what you did in the first example with /test. Deploy the node. Click the inject and if everything’s working correctly, you should receive a message in the chatroom: The Air Quality Index is currently XX. XX is the reading the sensor is giving you, in my case it’s 70. If you’ve received this, then you’re good to go. All that remains to be done is to try the command /aqi in the chat room. Post that in, and you should receive back the same message with the current sensor reading.

I hope that helps to give you a quick overview of how to work with Telegram in Node-red and Home-Assistant. I want to reiterate that there are probably more simple ways to send a straight forward notification, but I like to stick with a structured approach. It makes it easier to fault find if there’s an issue, and also it’s just easier to keep to a single scheme for copy and pasting new commands etc.

If you found this useful, feel free to check out the other posts in my blog here, and if you’re considering a smart home renovation or looking at a general smarthome set up, maybe my book might be of interest. You can find it here in all major bookstores. if you would be interested in sharing your own solutions, tips and tricks with like minded people perhaps you’d consider joining our facebook group. The aim of this group will hopefully be more show and tell rather than support, but that’s not to say we can’t lend a helping hand 🙂

https://www.facebook.com/groups/386238285944105

Cheers!

The Smarthome Book
The Smarthome Book

 

Comments

  1. Due to your excellent detailed Telegram tutorial, I have been able to setup an Telegram account , create a Bot and integrate it into HA and Node-Red. So far I have tested it with your simple Send command example , in my case I am toggling a lamp that is connected via a Tasmotized Sonoff mini plug. I am staggered how fast and instant the lamp switches on/off knowing that the chat message is going via a Telegram server, presumerably.

    1. Hey there. Thank you for the kind words. Really pleased you found it useful. The speed is indeed impressive. I’m such a fan of node-red. The possibilities are endless. I wish I could show it to more of the community !

  2. Hi, thank you for your contribution. I have set up the telegram api in node-red only (not home assistant) and it works great. Do you think it’s better to set it up in home assistant too?

    1. Hey. Am glad you found it useful. It’s up to you, but I had telegram in both and it chucked an error out regularly due to only one instance being allowable at once or words to that effect. Frankly speaking if it’s working in NR I was happy to bypass the HA integration. Any time you want to notify something in telegram you can just use a state change node and take it into NR anyway so I don’t think you’d be missing much. Unless you have a specific use case?

    1. Ok, so if you want a word to be in bold then you need to surround that word in *….* ie. this is a *bold* font. Also add an additional function node after the original one (containing chat id details etc) and in that function node, add the following:

      msg.payload.options = { “parse_mode”: “Markdown” }
      return msg;

      That should then work out!

  3. here?
    msg.payload = {
    chatId: ‘myid’,
    type: ‘message’,
    content: msg.payload
    }
    return msg;

  4. Thanks!! Very useful and detailed guide. I had a problem, I was naming my bot with the @, no @ needed if anybody has the same problem.

    Thanks again!

Leave a Reply