Sending picture notifications, with Node-RED and Telegram

(Amazon links are affiliate links, where I get a small commission of purchased item)

I have a SkyBell HD doorbell, and it sends notifications to my wife and I with the SkyBell app when it detects motion or the doorbell button is pressed. It works well enough, but it’s another separate app sending separate notifications. The entire point of automation is to make life easier, so let’s see about getting those notification in Telegram, with the rest of our smart home notifications. Here’s how the native app looks:

Here’s how the notification looks within Telegram:

There’s a few different parts to this flow. We’re going to send a notification for both motion, and button press. Once motion or button press is detected, a call service node takes a snapshot of the camera, stores it locally on your Home Assistant hub, waits one second to allow it to save properly, and then another call service node sends it as a notification.

Let’s do eet.

First part is easy, it’s two state nodes monitoring the Skybell, one for the button press, one for the motion.

Here’s how the configurations looks for the doorbell motion:

Here’s how the doorbell button looks:

Next is the call service node taking a snapshot:

Here’s how it looks on the inside:

A few things we need to go over with this. The “snapshot” service does what it’s called, it takes a snapshot of the camera you specify in entity ID. In this case, “camera.front_door_last_activity” is the camera we’ll be taking a snapshot from. In the Data field, you’ll notice “filename” and the location of where the snapshot will be stored. So a snapshot is taken of the camera, and stored where you specify in the data field. We then use a delay node set to 1 second to give the snapshot time to copy to the file location. We then use a call service node to send the snapshot from the save location to whatever notify service you are using. I’m using Telegram, so I send it with the send_photo service. Below is the configuration.

With this flow configuration, you will receive a photo of your doorbell when motion is detected, or the button is pressed. Here’s the code.

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