How to send frigate event snapshot to telegram?

i am able to get snapshots from my ONVIF cameras to my mobile via Telegram whenever Frigate detects human. unfortunately 1 onvif cam, named Alley, refuses to do so… so my next option is to get the photo through Frigate’s event.
this is the generic yml that i got from the Frigate’s website but does not work. could you please advise what the syntax should be?

alias: auto send photo from alley camera human detect 2
description: ''
trigger:
  - platform: mqtt
    topic: frigate/events
action:
  - service: telegram_bot.send_photo
    data_template:
      message: A {{trigger.payload_json["after"]["alley"]}} was detected.
      data:
        image: >-
          http://192.168.1.229/api/frigate/notifications/{{trigger.payload_json["after"]["alley"]}}/thumbnail.jpg?format=android
        tag: '{{trigger.payload_json["after"]["alley"]}}'
        when: '{{trigger.payload_json["after"]["start_time"]|int}}'
mode: single

i dont need the whole picture. bonus if i just get the cropped section of the person as seen here:

I think you need to use the url property for the image in the service to call Telegram. Here is what I’ve got working.

alias: Native Frigate Alerts
description: ''
trigger:
  - platform: mqtt
    topic: frigate/events
condition:
  - condition: template
    alias: Is object moving?
    value_template: '{{ trigger.payload_json[''after''][''stationary''] != true }}'
  - condition: template
    alias: Is object in the correct camera?
    value_template: |-
      {% if is_state('alarm_control_panel.alarmo', 'armed_away') %}
        {{ trigger.payload_json['after']['camera'] in ['driveway', 'front', 'garage', 'back', 'kitchen', 'mezannine'] }}
      {% elif is_state('alarm_control_panel.alarmo', 'armed_home') %}
        {{ trigger.payload_json['after']['camera'] in ['driveway', 'front'] }}
      {% elif is_state('input_boolean.camera_motion_alerts', 'on') %}
        {{ trigger.payload_json['after']['camera'] in ['driveway', 'front'] }}
      {% endif %} 
action:
  - service: notify.telegram_notify
    data:
      title: A title
      message: A message
      data:
        inline_keyboard:
          - >-
            Disable Camera Alerts - 30m:/disable_cam_30, Disable Camera Alerts -
            1h:/disable_cam_60
          - >-
            Disable Camera Alerts Forever:/disable_cam_forever, Sound Panic
            Alarm:/sound_alarm
        photo:
          - url: >-
              http://localhost:8123/api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/snapshot.jpg
            caption: >-
              A *{{trigger.payload_json["after"]["label"]}}* was detected on the
              {{trigger.payload_json["after"]["camera"]}} camera
mode: single

The following works, “south_side” is the camera name, but can be a zone name as well.

If you just want the cropped image, replace “snapshot.jpg” with “thumbnail.jpg”

alias: Frigate - Notify Detected Object On South Side
trigger:
  - platform: mqtt
    topic: frigate/events
condition:
  - condition: template
    value_template: >-
      {{
      (as_timestamp(now())-as_timestamp(state_attr('automation.frigate_notify_detected_object_on_south_side','last_triggered')
      | default(0)) | int > 60) }}
  - condition: template
    value_template: '{{ ''south_side'' in trigger.payload_json[''after''][''camera''] }}'
action:
  - service: script.turn_on
    target:
      entity_id: script.1620211106675
    data:
      variables:
        where: living_room
        what: >-
          'A {{trigger.payload_json["after"]["label"]}} was detected on the
          South Side Camera.'
  - service: notify.group
    data_template:
      message: Some Message.
      data:
        photo:
          - url: >-
              http://ha_lan_ip:8123/api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/snapshot.jpg
            caption: >-
              'A {{trigger.payload_json["after"]["label"]}} was detected on the
              South Side Camera. Event ID
              {{trigger.payload_json["after"]["id"]}}'
mode: single

Looks like
image

1 Like

thank you so much for the yaml sample!
i adjusted it for my Telegram since i dont have your script 1620211106675

but for some reason, it still does not work :frowning:

alias: auto send Notify telegram Detected human in alley camera frigate
description: ''
trigger:
  - platform: mqtt
    topic: frigate/events
condition:
  - condition: template
    value_template: >-
      {{
      (as_timestamp(now())-as_timestamp(state_attr('automation.frigate_notify_detected_object_on_alley','last_triggered')
      | default(0)) | int > 60) }}
  - condition: template
    value_template: '{{ ''alley'' in trigger.payload_json[''after''][''camera''] }}'
action:
  - service: telegram_bot.send_photo
    data_template:
      message: Someone spotted in the alley2.
      data:
        photo:
          - url: >-
              http://192.168.1.229:8123/api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/snapshot.jpg
            caption: >-
              'A {{trigger.payload_json["after"]["label"]}} was detected in the
              alley. Event ID {{trigger.payload_json["after"]["id"]}}'
mode: single

Do you actually have the following automation?

automation.frigate_notify_detected_object_on_alley

And for video lip.

  - delay:
      seconds: 30
  - service: notify.group
    data_template:
      message: Some Message.
      data:
        video:
          - url: >-
              http://192.168.200.211:8123/api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/clip.mp4
            timeout: 5000
            caption: >-
              'Video of {{trigger.payload_json["after"]["label"]}}" detected on
              the South Side Camera.'

thanks for the reminder. i dont have that automation so i added it in.
so this is what it looks like:

alias: auto send photo to telegram Detected human in alley camera frigate
description: ''
trigger:
  - platform: mqtt
    topic: frigate/events
condition:
  - condition: template
    value_template: >-
      {{
      (as_timestamp(now())-as_timestamp(state_attr('automation.auto_send_photo_from_alley_camera_human_detect_in_alley','last_triggered')
      | default(0)) | int > 60) }}
  - condition: template
    value_template: '{{ ''alley'' in trigger.payload_json[''after''][''camera''] }}'
action:
  - service: telegram_bot.send_photo
    data_template:
      message: Someone spotted in the alley2.
      data:
        photo:
          - url: >-
              http://192.168.1.229:8123/api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/snapshot.jpg
            caption: >-
              'A {{trigger.payload_json["after"]["label"]}} was detected in the
              alley. Event ID {{trigger.payload_json["after"]["id"]}}'
mode: single

so for automation.auto_send_photo_from_alley_camera_human_detect_in_alley’ it looks like this:

alias: alert alley camera human detect
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.alley_person_motion
    to: 'on'
action:
  - service: notify.telegram
    data:
      message: someone in the alley
mode: single

Is it working?, (I doubt it).

That’s not the automation you should be using for the condition, you can omitt the condition altogether if you want to test, but the condition is so that the automation don’t run multiple times within 60 seconds, and should be the same automation you’re using to send the message.

This should be used for the condition.

thanks for the sample!
i revised your code with my personal info but it didnt work…
do you see anything out of the ordinary? thanks!

alias: auto send photo to telegram Detected human in alley camera frigate (bash)
description: ''
trigger:
  - platform: mqtt
    topic: frigate/events
condition:
  - condition: template
    alias: Is object moving?
    value_template: '{{ trigger.payload_json[''after''][''stationary''] != true }}'
  - condition: template
    alias: Is object in the correct camera?
    value_template: |-
      {% if is_state('alarm_control_panel.alarmo', 'armed_away') %}
        {{ trigger.payload_json['after']['camera'] in ['alley', 'front', 'kitchen', 'dining'] }}
      {% elif is_state('alarm_control_panel.alarmo', 'armed_home') %}
        {{ trigger.payload_json['after']['camera'] in ['alley', 'front'] }}
      {% elif is_state('input_boolean.camera_motion_alerts', 'on') %}
        {{ trigger.payload_json['after']['camera'] in ['alley', 'front'] }}
      {% endif %} 
action:
  - service: notify.telegram_notify
    data:
      title: A title
      message: A message
      data:
        photo:
          - url: >-
              http://192.168.1.229:8123/api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/snapshot.jpg
            caption: >-
              A *{{trigger.payload_json["after"]["label"]}}* was detected on the
              {{trigger.payload_json["after"]["camera"]}} camera
mode: single

i am sorry i still cannot get anything to work.
could you please share all the yaml code? including this "
automation.frigate_notify_detected_object_on_south_side"

Try this.

alias: auto send photo to telegram Detected human in alley camera frigate
description: ''
trigger:
  - platform: mqtt
    topic: frigate/events
condition:
  - condition: template
    value_template: >-
      {{ (as_timestamp(now())-as_timestamp(state_attr('automation.auto_send_photo_to_telegram_detected_human_in_alley_camera_frigate','last_triggered') | default(0)) | int > 60) }}
  - condition: template
    value_template: '{{ ''alley'' in trigger.payload_json[''after''][''camera''] }}'
action:
  - service: telegram_bot.send_photo
    data_template:
      message: Someone spotted in the alley2.
      data:
        photo:
          - url: >-
              http://192.168.1.229:8123/api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/snapshot.jpg
            caption: >-
              'A {{trigger.payload_json["after"]["label"]}} was detected in the
              alley. Event ID {{trigger.payload_json["after"]["id"]}}'
mode: single
initial_state: true

You probably need to change “service: telegram_bot.send_photo” to "service: notify.(whatever your notifier name is), go to “Developer Tools > SERVICES” and type “notify.” and see what your notifier name is configured as, or post your telegram notify configuration.

1 Like

im happy to say that something is working because i can see the time it was last triggered

BUT got nothing in Telegram. i use the same “service: telegram_bot.send_photo” service for other alerts so i know that service is working with my phone.
i really appreciate your time and help in this!

Try this and see if you atleast get a message.

alias: auto send photo to telegram Detected human in alley camera frigate
description: ''
trigger:
  - platform: mqtt
    topic: frigate/events
condition:
  - condition: template
    value_template: >-
      {{ (as_timestamp(now())-as_timestamp(state_attr('automation.auto_send_photo_to_telegram_detected_human_in_alley_camera_frigate','last_triggered') | default(0)) | int > 60) }}
  - condition: template
    value_template: '{{ ''alley'' in trigger.payload_json[''after''][''camera''] }}'
action:
  - service: telegram_bot.send_message
    data:
      title: Test message
      message: Someone spotted in the alley2.

Also, do you access HA via https or http?

You can also try adding api to your configuration.yaml

i just installed API into my config file as recommended.
i access HA via http with my desktop to do all changes, such as this automation now.
using your latest code provided on July 1st, i do get the text "
Someone spotted in the alley2.
"

The only other thing I can think of to try would be to whitelist the media directory.

Hello,
I am a bit confused right now.

Can anyone confirm that it is possible to send telegram message with snapshot, video with Telegram( Polling or broadcast) without external access to the HA instance?
Everything works with telegram_bot to send a message without attached image or video.

Thanks!!

Hello friends! Please help with the problem. I use Frigate for motion detection and recording. Also telegram for receiving notifications. But for some reason it doesn’t work as it should. There is automation:

  • id: “”
    alias: Notify of events
    trigger:
    platform: mqtt
    topic: frigate/events
    action:
    • service: telegram_bot.send_photo
      data_template:
      message: ‘A {{trigger.payload_json[“after”][“label”]}} was detected.’
      data:
      photo:
      # this url should work for addon users
      - url: ‘http://192.168.1.134:5000/api/events/{{trigger.payload_json[“after”][“id”]}}/thumbnail.jpg’
      caption: ‘A {{trigger.payload_json[“after”][“label”]}} was detected on {{ trigger.payload_json[“after”][“camera”] }} camera’

if you change the telegram_bot.send_message, the tex message comes. And telegram_bot.send_photo does not send anything.

After two weeks of torment, I found a solution for myself. Maybe it will be useful to someone. The only thing I don’t understand is why my strange code works. And others don’t:

'alias: Telegram sent snapshot

trigger:

platform: mqtt

topic: frigate/events

action:

- service: notify.telegram

  data_template:

    message: 'A {{trigger.payload_json["after"]["label"]}} was detected.'

    data:

      photo:

        - url: >-

            http://192.168.1.134:8123/api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/snapshot.jpg

          caption: >-

            'A {{trigger.payload_json["after"]["label"]}} was detected at the

            Front {{trigger.payload_json["after"]["entered_zones"]}}'.'
3 Likes

Hello,

Thanks for sharing. It is working fine for me with the “mqtt frigate/events” solution.

Nevertheless, I’m receiving a lot of photos with the same event ID {{trigger.payload_json[“after”][“id”]}}.

Can I put a filter to send just one time the photo with the same ID?

Thank you very much.