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.
– name: telegram
chat_id: !secret telegram_chat_id
– platform: polling
api_key: !secret telegram_api
– !secret telegram_chat_id
For Node-red, I installed the following node- 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
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.
See below for the precise configuration of the two nodes.
You need to fill in the Bot from the drop down which you’ve already created. I’ve removed it here for privacy reasons.
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.
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.
You’ll need to enter your ChatId here which you already have set up. Be mindful of the single quotes either side of it.
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 (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.
Note to edit the payload you need to click on the three dots to the side of the expression above.
Add your chatId here, don’t forget the single quotes.
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.