Skip to content

Instantly share code, notes, and snippets.

@NdR91
Last active May 2, 2024 15:23
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 26 You must be signed in to fork a gist
  • Save NdR91/5486a1e55101e062c48545395b7dd9a3 to your computer and use it in GitHub Desktop.
Save NdR91/5486a1e55101e062c48545395b7dd9a3 to your computer and use it in GitHub Desktop.
Frigate - Telegram Notification
blueprint:
name: Frigate - Telegram Notification
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'
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: {}
source_url: https://gist.github.com/NdR91/5486a1e55101e062c48545395b7dd9a3
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: |
Movimento rilevato. Telecamera: {{ camera_name }} (ID: {{ id }})
url: >-
{{base_url}}/api/frigate/notifications/{{id}}/snapshot.jpg
- repeat:
sequence:
- wait_for_trigger:
- platform: mqtt
topic: frigate/events
payload: '{{ id }}'
value_template: '{{ value_json[''after''][''id''] }}'
timeout:
minutes: 2
continue_on_timeout: false
- condition: template
value_template: '{{ wait.trigger.payload_json[''type''] == ''end'' }}'
- service: telegram_bot.send_video
data:
target: '{{ target_chat }}'
disable_notification: '{{ notification }}'
caption: 'Movimento rilevato. Telecamera: {{ camera_name }}'
url: >-
{{base_url}}/api/frigate/notifications/{{id}}/{{camera}}/clip.mp4
until: '{{ wait.trigger.payload_json[''type''] == ''end'' }}'
@TimoHornSerrig
Copy link

does not work with group chat_id

@deviant77
Copy link

@TimoHornSerrig
Sending to group chats is working here (Home Assistant 2021.11.5).
It uses the inbuilt HA services telegram_bot.send_photo and telegram_bot.send_video (https://www.home-assistant.io/integrations/telegram_bot#notification-services) so if HA works this will too.
Make sure your group chat ID starts with a "-" and is in your allowed_chat_ids in configuration.yml.
You could instead send to multiple users (or groups) by entering [chat_id,chat_id,-group_chat_id] in the "Target" field.

@patmood
Copy link

patmood commented Dec 14, 2021

I'm getting errors like this in logs that seem to prevent the notification being sent:

2021-12-13 19:56:27 ERROR (SyncWorker_1) [homeassistant.components.telegram_bot] Can't load data into ByteIO: Invalid URL '/api/frigate/notifications/1639454176.217215-dez4om/camera_1/clip.mp4': No schema supplied. Perhaps you meant http:///api/frigate/notifications/1639454176.217215-dez4om/camera_1/clip.mp4?

Where does the base_url come from?

@alienbyday
Copy link

Great stuff, worked perfectly. Thanks

@thematrixdev
Copy link

I'm getting errors like this in logs that seem to prevent the notification being sent:

2021-12-13 19:56:27 ERROR (SyncWorker_1) [homeassistant.components.telegram_bot] Can't load data into ByteIO: Invalid URL '/api/frigate/notifications/1639454176.217215-dez4om/camera_1/clip.mp4': No schema supplied. Perhaps you meant http:///api/frigate/notifications/1639454176.217215-dez4om/camera_1/clip.mp4?

Where does the base_url come from?

I see the same error. Have you solved it finally? @patmood

@pan07gaz94
Copy link

I don't receive clip, I receive only snapshot....anyone help?

@fama-lama
Copy link

this works beautifully! The only thing i changed was the message body to english.

@fjavimer
Copy link

fjavimer commented Oct 26, 2022

I'm getting errors like this in logs that seem to prevent the notification being sent:

2021-12-13 19:56:27 ERROR (SyncWorker_1) [homeassistant.components.telegram_bot] Can't load data into ByteIO: Invalid URL '/api/frigate/notifications/1639454176.217215-dez4om/camera_1/clip.mp4': No schema supplied. Perhaps you meant http:///api/frigate/notifications/1639454176.217215-dez4om/camera_1/clip.mp4?

Where does the base_url come from?

I see the same error. Have you solved it finally? @patmood

Me ocurre lo mismo. lo habeis solucionado?
I see the same error. Have you solved it finally?

@xfaustus
Copy link

xfaustus commented Nov 16, 2022

Hello i got same issue,
looking at source code i see we "need" to specify Base URL in the blueprint automation, base url is our homeassistant ip.

@Kabre555
Copy link

Anyone figure out how to get this working under frigate 12 beta, the automation fires but I don't get a snapshot sent to my chat id

@xfaustus
Copy link

I'm also on frigate12, and automation i used on frigate 11 still works

@Kabre555
Copy link

Kabre555 commented Jan 31, 2023 via email

@xfaustus
Copy link

Can you help me out with the settings, did you have to use a base url, put the - in the target chat, notification set to true/false? maybe post your YAML (without the chat Id #)

Here you are the frigate automation

alias: Frigate - Notification Telegram
description: ""
trigger:
  - platform: mqtt
    topic: frigate/events
    payload: new
    value_template: "{{ value_json.type }}"
action:
  - service: notify.telegram_main
    data_template:
      message: rilevato {{trigger.payload_json["after"]["label"]}}
      data:
        photo:
          - url: >-
              http://localhost:5000/api/events/{{trigger.payload_json["after"]["id"]}}/snapshot.jpg?quality=100&bbox=1
            caption: >-
              **{{trigger.payload_json["after"]["camera"]}}**: rilevato
              {{trigger.payload_json["after"]["label"]}}.        
mode: single

@Kabre555
Copy link

Kabre555 commented Feb 1, 2023

service: notify.telegram_main

I don't think you are working off the current blueprint, your code references service: notify.telegram_main and the new code uses the service: telegram_bot.send_video

@hardwareadictos
Copy link

hardwareadictos commented Feb 17, 2023

For me it doesn't work. Cameras aren't autodetected on the camera field (tried putting camera.cameranameonfrigateconf and cameranameonfrigateconf with no success). Tried with my Home Assistant IP / Frigate IP on Baase URL with the same results, can someone who has it working share the UI configuration that is using?

I don't receive any type of message on the desired group, that's what i see on logs:

image

Edit: Just fixed it by addind home assistant URL:PORT on BaseURL and adding a little delay on the yaml config:

image

Suggestion: Is it posible to add this delay on the blueprint GUI? And also the message customization? Here it's done, if you want to check:

https://gist.github.com/SgtBatten/10a2dc7f16af9f55f3ba83fe4523d8d7#file-frigate_0-11_notification-yaml

Nice job :)

@Kabre555
Copy link

Kabre555 commented Feb 17, 2023 via email

@hardwareadictos
Copy link

Sounds like you didn't load the integration part, can you view the cameras entities in lovelace?

On Fri, Feb 17, 2023 at 6:35 AM hardwareadictos @.> wrote: @.* commented on this gist. ------------------------------ For me it doesn't work. Cameras aren't autodetected on the camera field (tried putting camera.cameranameonfrigateconf and cameranameonfrigateconf with no success). Tried with my Home Assistant IP / Frigate IP on Baase URL with the same results, can someone who has it working share the UI configuration that is using? — Reply to this email directly, view it on GitHub https://gist.github.com/5486a1e55101e062c48545395b7dd9a3#gistcomment-4474209 or unsubscribe https://github.com/notifications/unsubscribe-auth/AIZ2PT55EWLLKXPY3XWJ7N3WX5PB5BFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFQKSXMYLMOVS2I5DSOVS2I3TBNVS3W5DIOJSWCZC7OBQXE5DJMNUXAYLOORPWCY3UNF3GS5DZVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTCMRZGU4DMOJTU52HE2LHM5SXFJTDOJSWC5DF . You are receiving this email because you commented on the thread. Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub .

It's a already fixed (I edited the reply). Thanks anyways.

@borgqueenx
Copy link

I tried to add this today, but the automation did not execute from this blueprint.
The trace gives the following info:
image

In frigate, the event was registered.

@borgqueenx
Copy link

i see it is not working because i defined some zones.
I removed the zones and it works.
In the field for zones, i tried (zone_name), zone_name and -zone_name. I don't know how we should enter the zones. But as soon as filtering is on and i define the zone, it stops working. tried on two camera's, both stop working then for telegram notifications. But when i disable zone filtering, it works immediately.
would love some assistance there.

@borgqueenx
Copy link

So it was adding lines/minus characters to get it fixed.

  • zone_0
  • zone_1

Now working good, except that sometimes i do not get videos send. Photos are always being send, videos only sometimes. Could the video file be too big? Or too short after another? How can i debug? In frigate the events can be played without a problem.

@fama-lama
Copy link

@borgqueenx I had the same issue too. The only thing that seemed to help is adding timeout: 1000 like this.

- service: telegram_bot.send_video
      data:
        target: '{{ target_chat }}'
        disable_notification: '{{ notification }}'
        caption: 'Movimento rilevato. Telecamera: {{ camera_name }}'
        timeout: 1000
        url: >-
          {{base_url}}/api/frigate/notifications/{{id}}/{{camera}}/clip.mp4

It changes the send_video timeout to 1000 seconds instead of the default. Videos might arrive 10 mins+ after the event but it can't be helped.

Last time I tried to troubleshoot, I noticed the automation hanging in the send_video section, even though the source video is already playable locally.

10mb files shouldn't take so long to upload on my internet speed so I really believe it's Telegram themselves limiting video upload speeds. 🫤

@nixxon94
Copy link

nixxon94 commented Jan 14, 2024

I am getting the following error message

2024-01-14 13:18:51.917 ERROR (SyncWorker_2) [homeassistant.components.telegram_bot] Can't load data into ByteIO: Invalid URL '/api/frigate/notifications/1705234731.428055-zw79cd/snapshot.jpg': No scheme supplied. Perhaps you meant https:///api/frigate/notifications/1705234731.428055-zw79cd/snapshot.jpg? 2024-01-14 13:18:51.918 ERROR (SyncWorker_2) [homeassistant.components.telegram_bot] Can't send file with kwargs: {'disable_notification': False, 'caption': 'Movimento rilevato. Telecamera: Garage (ID: 1705234731.428055-zw79cd)', 'url': '/api/frigate/notifications/1705234731.428055-zw79cd/snapshot.jpg'} 2024-01-14 13:19:13.164 ERROR (SyncWorker_8) [homeassistant.components.telegram_bot] Can't load data into ByteIO: Invalid URL '/api/frigate/notifications/1705234731.428055-zw79cd/garage/clip.mp4': No scheme supplied. Perhaps you meant https:///api/frigate/notifications/1705234731.428055-zw79cd/garage/clip.mp4? 2024-01-14 13:19:13.164 ERROR (SyncWorker_8) [homeassistant.components.telegram_bot] Can't send file with kwargs: {'disable_notification': False, 'caption': 'Movimento rilevato. Telecamera: Garage', 'url': '/api/frigate/notifications/1705234731.428055-zw79cd/garage/clip.mp4'}

@denzhel
Copy link

denzhel commented Jan 29, 2024

Is it possible to send the snapshot to telegram with the motion box detected by Frigate ?

@elmaswebon
Copy link

Works like a charm, thanks a lot!

Could you consider the possibility that we can modify the Italian texts for custom ones?

@balvant813
Copy link

Just added blueprint, activate and tested telegram bot. I ran the automation from automation page, it just does not run! it shows 'This node was not executed and so no further trace information is available.' . see screenshot below. What I am doing wrong?
Screenshot 2024-03-09 153913 - thanks

@balvant813
Copy link

I have also included mqtt message data from Frigate.

@jakesOneFourSeven
Copy link

jakesOneFourSeven commented Mar 31, 2024

If anyone is interested, I created a fork that incorporates Gemini AI generative comments.

(https://gist.github.com/jakesOneFourSeven/cc8fab1e8712a34dbd596ebd355d76ea)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment