
Sending Telegram alerts with Home Assistant or Hassio
In this post we will see everything you need for your Home Assistant or Hassio to send you notifications to your mobile phone through Telegram. Something super interesting if you are putting motion detectors or door/window opening sensors and you want to get notices, For example.
First of all, in Telegram we will register a bot that we will use to send notifications, we can add it to a Home/Family group or directly send the alerts to us. In this post we already saw how to create a Telegram bot, that we use it to send notifications from Centreon. The steps to generate the bot are the same, as well as to obtain the IDs of the chats to which notifications can be sent. Once this is done, we will move on to configure Hassio, You'll see that with a few lines and 2 minutes we have it ready. And then through Hassio Automations we can configure what interests us, that when it detects a movement and we are not, Then let us know, For example. We start!
Create a bot on Telegram
We open Telegram and look for @botfather, We started a conversation with “/Start”, To create the bot we will type “/newbot”, will ask us to give you a name, In this example I call him “Home Notifications”, perfect, Now it tells us to indicate a user for the bot, must end with “_bot” and should not be used, In this example I'll use “NotificacionesHogar_fake_bot”. If everything goes well, it will give us the Token that we must keep as gold in cloth, since we will use it to authenticate ourselves and use its API when we send the messages. We can start a conversation to test the bot…
If we are not going to create a group with this we are fine and we have more than enough. If you prefer to have a group to notify more people from our Home, because we will have to add the bot to the Telegram group that we have created (or we create a new group and we also get into the bot). We need to add the bot with admin privileges to confirm that it has visibility into the messages.
Open a browser and access “https://api.telegram.org/botTOKEN_DEL_BOT/getUpdates”, and there we will be able to know what the ID of the group to which the bot belongs is, or the ID of our Telegram user to send messages to!
We edit our Home Assistant or Hassio configuration file, called “configuration.yaml”, And if we have it dockerized, possibly his path is “/usr/share/hassio/homeassistant/configuration.yaml”. And we add the following code at the end, modifying and putting the TOKEN of each one, as well as the ID of the chat to send notifications to, note that the ID must include the “-“:
[SourceCode]telegram_bot:
Platform: polling
api_key: TOKEN_DEL_BOT
allowed_chat_ids:
– -ID_CHAT
Notify:
– Name: Telegram
Platform: Telegram
api_key: TOKEN_DEL_BOT
chat_id: -ID_CHAT[/SourceCode]
Create Automations
And then there will be nothing left to do but use it, ¿how? with Home Assistant Automations! I leave you an example so that it sends a Telegram message in case it is detected that a door was opened with one of the sensors that we already saw how to add it to Hassio. To create an Automation, from Home Assistant > “Configuration” > “Automations”.
The code that generates me by doing it through GUI is the following, that would be perfectly defined in our “automations.yaml”:
[SourceCode]- Id: ‘1561334211255’
alias: Telegram Notification – Open Garage Door Garden
Trigger:
– entity_id: binary_sensor.garage_door
from: 'off’
Platform: state
To: 'on’
Condition:
– Condition: state
entity_id: device_tracker.hector
state: not_home
– Condition: state
entity_id: device_tracker.seila
state: not_home
action:
– date:
Message: The door to the Garage-Garden is open
service: notify.telegram[/SourceCode]
We give a name to Automation, in “Triggers” we will indicate when this Automation will be activated, in my case when a Door goes from being closed to open, so in “Guy” Indicate “State”, in “Entity” We select our Door/Window Sensor, and let us know when it goes beyond “Off” a “on”.
We can set some conditions, such as if I am not at home, since I may not be interested in receiving notifications every time that door/window is opened, we would look for the people who make up the Home and mark “not_home” (We will see this in another post, How to register the people who make up the Home). And finally we instruct you to take an action, in Type we select “Call Service” and in Service we indicate “notify.telegram”. All that remains is to define the message that will be sent.
[SourceCode]{
"message":"MESSAGE TO SEND…"
}[/SourceCode]
And nothing, ready! As always, we restart Hassio to reload the settings and we can test and verify that we already receive Telegram messages from the state of our house to our mobile phone, We'll know when someone enters our house, Motion detected, There is smoke… I hope you have been interested!