So i got annoyed that all solutions out there for awesome frigate notifications on telegram, using home assistant, were not 100% thought out, so i decided to take whats been made and perfect it.
Of course it’s important to note that i made this into a opiniated system, and by no means is the underlying code and blueprints perfected. In fact, you need to tweak a little bit to get this all working to your wishes, but that’s why i made this thread, in order to give back a little to the home assistant tweaking community.
I’m by no means a good coder, i just spend quite some hours today making this a well working notification system for my home, the blueprint could be way more polished, but the notifications themselfs are pretty much perfected I’d say.
A big thanks goes to NdR91 on github for the original blueprint.
What does this do and why is it better?!
Perhaps this is best described with two screenshots:
(not the same event in the above screenshot, ignore the photo and time and see the text)
As you can see, in the first screenshot, a event has been detected and you are notified of it including a photo. you could also select one without a bounding box, or a tiny thumbnail. or a crop. As soon as the event ends, the notification is updated/editted. You dont get a new message, therefore things look more organised. In the background, a video is also being send to a seperate groupchat, with notifications off. Then you have a clip of what happened without a chance your server is disconnected and you can no longer retrieve the video. Obviously you could remove this though. In the editted message (or rather a caption of the photo), you also see the clip as a link. I added two camera’s to this notification message on telegram because people are walking a certain direction where also a camera is placed, you could of course remove one, add one, do whatever you’d like.
So how do i make this for myself?
Editting messages on telegram isnt super easy. You need a way to intercept the message_ID and tags. Without making stuff too complicated, i will share the blueprint needed. You need Studio Code Server or a other editor to edit and copy the blueprints for yourself. For every camera, you need a seperate blueprint. You also need to create a input_number helper entity, and make a automation that grabs the message_id. But because you are using frigate, home assistant as well…I assume you can mess with code a little bit using guides such as this one…So lets get started.
Here is my blueprint with the name: frigate_telegram_frontdoor_notifications.yaml
Put it in the config//blueprints/NdR91/automation/ folder on your home assistant machine.
blueprint:
name: Frigate - Telegram Notifications Front Door
description: Create automations to receive Snapshots and Clips from Frigate
domain: automation
input:
camera:
name: Frigate Camera
description:
The name of the camera as defined in your frigate configuration
(/conf.yml).
target_chat:
name: Target
description:
"The chat_id to be used by the Telegram bot. !secret chat_id is
not allowed on Blueprint, you will need the chat_id code.
"
notification:
name: Notification
description:
'Select "true" to disable notification, lave "false" to receive
notification. '
selector:
select:
options:
- "true"
- "false"
multiple: false
custom_value: false
sort: false
default: "false"
base_url:
name: (Optional) Base URL
description:
The external url for your Home Assistant instance. This will default
to a relative URL and will open the clips in the app instead of the browser,
which may cause issues on some devices.
default: ""
zone_filter:
name: (Optional) Zone Filter
description: Only notify if object has entered a defined zone.
default: false
selector:
boolean: {}
zones:
name: (Optional) Trigger Zones
description: A list (-) of zones you wish to recieve notifications for.
default: []
selector:
object: {}
labels:
name: (Optional) Trigger Objects
description: A list (-) of objects you wish to recieve notifications for.
default: []
selector:
object: {}
presence_filter:
name: (Optional) Presence Filter
description: Only notify if selected presence entity is not "home".
default: ""
selector:
entity:
multiple: false
source_url: https://gist.github.com/NdR91/5486a1e55101e062c48545395b7dd9a3#file-frigate_telegram_notification-yaml
mode: single
max_exceeded: silent
trigger:
platform: mqtt
topic: frigate/events
payload: !input camera
value_template: "{{ value_json['after']['camera'] }}"
variables:
id: "{{ trigger.payload_json['after']['id'] }}"
camera: "{{ trigger.payload_json['after']['camera'] }}"
camera_name: "{{ camera | replace('_', ' ') | title }}"
target_chat: !input target_chat
object: "{{ trigger.payload_json['after']['label'] }}"
label: "{{ object | title }}"
entered_zones: "{{ trigger.payload_json['after']['entered_zones'] }}"
type: "{{ trigger.payload_json['type'] }}"
base_url: !input base_url
zone_only: !input zone_filter
input_zones: !input zones
zones: "{{ input_zones | list }}"
input_labels: !input labels
labels: "{{ input_labels | list }}"
presence_entity: !input presence_filter
notification: !input notification
condition:
- "{{ type != 'end' }}"
- "{{ not zone_only or entered_zones|length > 0 }}"
- "{{ not zones|length or zones|select('in', entered_zones)|list|length > 0 }}"
- "{{ not labels|length or object in labels }}"
- "{{ not presence_entity or not is_state(presence_entity, 'home') }}"
action:
- service: telegram_bot.send_photo
data:
target: "{{ target_chat }}"
disable_notification: "{{ notification }}"
caption: "🔴 | 🚪🧍 Front Door person detected!\n\nWatch: <a href='https://yourstreamurl.com/stream.html?src=front-door-cam'>Front Door</a> | <a href='https://yourstreamurl.com/stream.html?src=front-garden'>Front Garden</a>"
parse_mode: html
url: "{{base_url}}/api/frigate/notifications/{{id}}/snapshot.jpg?bbox=1"
message_tag: telegram_notifications_frontdoor
- repeat:
sequence:
- wait_for_trigger:
- platform: mqtt
topic: frigate/events
payload: "{{ id }}"
value_template: "{{ value_json['after']['id'] }}"
timeout:
minutes: 6
continue_on_timeout: false
- condition: template
value_template: "{{ wait.trigger.payload_json['type'] == 'end' }}"
- service: telegram_bot.edit_caption
data:
message_id: "{{ states('input_number.telegram_frontdoor_message_id') }}"
chat_id: "{{ target_chat }}"
disable_notification: true
caption: "✅ | 🚪🧍 Front Door event complete.\nID: {{id}}.\n\nWatch: <a href='{{base_url}}/api/frigate/notifications/{{id}}/{{camera}}/clip.mp4'>Clip</a> | <a href='https://yourstreamurl.com/stream.html?src=front-door-cam'>Front Door</a> | <a href='https://yourstreamurl.com/stream.html?src=front-garden'>Front Garden</a>"
parse_mode: html
- delay: "00:00:06"
- service: telegram_bot.send_video
data:
target: "-30692814"
disable_notification: true
caption: "Front Door Event ID: {{id}}."
url: "{{ base_url }}/api/frigate/notifications/{{ id }}/{{ camera }}/clip.mp4"
until: "{{ wait.trigger.payload_json['type'] == 'end' }}"
Obviously, you need to edit the above blueprint to your liking and as said before, copy it for every seperate camera. Some notes:
- The delay near the end is used to make sure the video and events has ended to not get errors sending the video. You can play around with this, see what works for you.
- In the end the target (chat group) is different because it should be the seperate group chat you have for only videos, you can remove this part if you have no requirement for videos. Note that telegram only sends videos below 50mb, anyway. this is a bot api limit. As you can see, notifications are silenced for the videos posted, so you dont get bothered by them and simply have 1 clean message of a new event. You can use the ID in your event-end message to search the group with videos to find the corresponding video very fast.
- the message_tag: telegram_notifications_frontdoor, is the tag telegram gives to a message. this is used to grab the telegram message_ID in a seperate automation. It can probably be implemented in the blueprint as well, but im not smart enough for that =) In every seperate blueprint you make, you need a seperate message_tag.
- You should have created a input_number helper entity. be sure to link to it correctly in the above blueprint. It is in my case input_number.telegram_frontdoor_message_id. Be also sure to make the max number of the entity to like 10000000 or so.
Let’s now create the automation needed to grab the message ID and write it to the input_number.telegram_frontdoor_message_id input_number helper entity.
alias: "Frigate Telegram Message_ID Safe: FrontDoor Cam"
description: ""
triggers:
- trigger: event
event_type: telegram_sent
event_data:
message_tag: telegram_notifications_frontdoor
conditions: []
actions:
- action: input_number.set_value
metadata: {}
data:
value: "{{ trigger.event.data.message_id | int }}"
target:
entity_id: input_number.telegram_frontdoor_message_id
mode: single
As you can see, as soon as a telegram message (or photo with caption in this case) is send and the message_tag in it is detected, the message_id is written to the numeric value. This makes us able to edit the message like we are doing in the blueprint.
Now, we reload yaml or restart home assistant, and then create a new automation, select the blueprint, and then populate the fields. See the below screenshot.
That should be it! You will now get notified when a person enters the specific camera and specific zone, and the same message will be editted when the event has ended, including the link to the clip which will be added. No more clutter!
I dont have the knowledge, im pretty dumb to coding stuff =) But if anyone appriciates this, you could probably code this all to not require the additional automation or maybe not even require the input_number entity, maybe it is possible to update with a new photo as well, of the object leaving the zone or camera. I know frigate allows this, but not sure telegram allows editting a photo message. In any case, that’s not for me, and i’m pretty happy how this turned out.