Node-RED and Telegram

If you already have a Telegram bot setup in Node-RED, see my post, Better notifications with Telegram and Node-RED to send notifications with Telegram formatting and logging.

In my humble opinion, Telegram is probably the best way to send and receive notifications from Home Assistant. You can create groups for everyone in your house, you can respond to notifications, receive pictures, videos or even gifs. It’s a great service…and doesn’t cost a thing. The setup isn’t too bad, but it does require some programming in YAML.

Here is the official documentation on creating a chatbot within Home Assistant. Although you’ll have to create a Telegram Bot first. Make sure do both of these things. Also, here’s all the cool things a Telegram Bot can do once it’s setup.

Alright so now that I’ve gotten all my swooning out of the way, you’ll need to first add the bot to your configuration file, as per the official documentation. If you’re not, you should definitely be using the secrets.yaml

**One thing to remember, for how we’re setting up our Telegram bot in this particular instance, you’ll want to set the platform to “broadcast” and not “polling”. If you set it up as polling, your Home Assistant log will get flooded with Telegram bot multiple instances errors, after you setup the Node-RED side of things.**

Once this is done, you can use a call service node to test the Telegram bot. Here’s how it should look to test:

Connect an inject node, and press the tab to test.

You should receive a “Test” message from your telegram bot. You can also see the notifications with a picture from my doorbell. We’ll get to that in another post.

Now you have working Telegram bot notifications within Home Assistant. Not to bad right? Here’s an example of a notification with a button to respond. I use these in my “Alarm has been armed” notifications, as well as the “Alarm has been triggered” notification. We can disarm the alarm when it’s armed for whatever reason, or disarm it when it is triggered. Here’s what the call service node configuration looks like:

This is how it looks when the notification is received.

Telegram bots respond to commands, that are sent with a forward slash. /arm and /disarm are examples, but your Telegram bot doesn’t know what to do with them yet.

So how do we get Telegram to respond to the /disarm command that is sent when the Disarm button is pressed? This requires installing some new nodes so we can run the bot in Node-RED. To start, click the top right hamburger menu and go to “Manage palette”.

Once there, in the search bar, search for “node-red-contrib-chatbot“. You’ll see one result. This is Redbot, an incredibly good Node-RED chatbot. Install it and wait a few seconds. You’ll get a notification when it’s done. This installs a bunch of new nodes, 74 to be exact, as of this writing. This initially will look very daunting at first, but it’s really not, and we’ll go through the basic setup.

Before we continue, if you don’t recall or can’t find the API token that was given to you during your bots creation, go talk to the BotFather and send him the /token command, and he’ll give you the API token for your bot. On we go…

There are two nodes that you need to pull out, Telegram In, and Telegram Out.

Put those guys in a new tab in Node-RED. Open up one of them, doesn’t matter which, and click the edit button to the right of Bot configuration (development).

Fill it out with all this info:

Click Add, which will take you back to the main node configuration. Use the drop down under Bot configuration (development) and select the bot you just created.

Go to the other Telegram node, and use the pull down to select the bot you created. Both of these nodes are now configured.

The Telegram Receiver, or Telegram In, node is what listens for commands when you send them to your bot, which it grabs and sends out through it’s output. The Telegram Sender, or Telegram Out, node receives any commands or messages and sends them to the chat. We need to put some logic in between them so the bot knows what do when we tell it. We’ll start super simple so you can see exactly what is happening.

First, grab a Command node; and place it after the Receiver node. Open up the node and type /hello in the command field.

The command node will listen to everything coming from the Receiver Node and will grab all of the /hello commands. Next, grab a text node and place it after the command node. Type Hello, {{firstName}}! in the message box. This will respond with your first name (or whatever you used when creating you Telegram account). If you want a smiley, add :smile:.

Here’s what you have now:

You have simple logic that will respond with “Hello, Justin!” when it receives the /hello command. Let’s test it out. Open up your Telegram app and send /hello to your bot.

Now you have a working Telegram bot.

So, lets take what we know now, and make the /arm and /disarm commands (or whatever commands you want) work. We’re going to need another command node, and set it to grab /arm commands. Connect a new text node to the command node and set the message to “The House has been Armed”, and finally add a call service node to the end of it. Below is how the service node is configured in my example, but you can use it to anything you like. You can use it to send you a picture of one of your cameras, tell you what temperature the house is, etc.

Now, connect everything up to the Send and Receiver nodes and you’ll have this:

To review, the receiver node receives any messages you send to the bot. The command node filters out the command you program it with. The text node sends whatever message you program it with as a response from the bot. The calls service node is not part of the bot, it’s just wired in so that the bots messages will trigger the service. Here’s what the completed bot looks like with arm and disarm both setup

Node-RED + Home Assistant Security System

In this post, I will show how I setup my home security system. Setup this way, it is a manual system, and will need to be manually armed and disarmed. I will share how I’ve automated the system in later posts, that will be linked here.

My last two houses were smart homes. SmartThings was my hub of choice at the beginning, and it was good. One of the things I liked so much about SmartThings was the Smart Home Monitor that is built in. This does a few things, one of which is act as a security system, which is what we used it for. There is nothing like this built into Home Assistant out of the box, so one of my first goals when learning Node-RED was to create a home security system.

Home Assistant has a built in Manual Alarm Control Panel that can be used to build a security system, which all has to happen in the config files. Since I’m doing all of my automations in Node-RED, I wanted to try building a security system from a combination of the built in panel and Node-RED. I wanted to be able to set the alarm mode from the panel and have Node-RED handle the rest.

To start off, I created an “alarm.yaml” in the config directory of Home Assistant, and made sure to add “alarm_control_panel: !include alarm.yaml” in the config file. This is the code that I used, which will allow the away alarm to trigger after 120 seconds, and give you 30 seconds to leave the house once it’s armed. All other modes arm instantly.

This will create a “alarm_control_panel.home_alarm” entity, which when used in Lovelace, looks like this:

So now that the front-end is done, time to move on to the back-end of the machine. I built all of the logic within Node-Red. This is how the finished result looks.

The “events: state” node undefined grabs the status of the Manual Alarm Control Panel, and passes it on to the switch node undefined which then filters the alarm status to the appropriate output.

Below that, is the trigger automation. The “Alarm Triggers” sub-flow contains all of the doors and windows in the house, which is connected to another sub-flow that checks if the alarm is armed either home, or away, and then passes it on to a call service node that triggers the alarm, and finally sends a Telegram notification letting us know the alarm has been triggered, along with a button to Disarm the alarm. Example of the “Armed Home” sub-flow.

Sub-flow for the Armed Home mode for reference,

Since there’s so much going on with this flow, I won’t go through it in detail, but I will post the code which you should be able to work off. It’s pretty straight forward.

Here’s the Node-RED code: Security System
Here’s the yaml code for the panel: alarm.yaml

Currently, there is zero automation involved. This has to be manually set, but I have location and timed based automations which I will share in future postings.


Updates:

8/24/2020 – Updated the alarm.yaml code to reflect recent updates Home Assistant has had regarding the manual alarm panel.