Frigate to Telegram alerts with callbacks to pause....must be a more efficient way

Okay, I am always a fan of giving back, but I think with better skills this could be more concise.

I have created from various sources a nest of automations that will…

  1. Alert when motion is found to a Telegram Bot.
  2. Send a photo and or video of the alert.
  3. Link to the live feed on the Frigate Server in the caption of the image or video.
  4. Give the option to pause this camera for 30 or 60 minutes(very helpful for mowing the lawn or my son shooting some hoops.)

With 10 cameras I would have 30 automations to do all this the way I have it, but it’s working so I figure this is a give back with a collab to narrow this down to less automations.

First you need to have:

1.Telegram Bot setup
8.Frigate up and running with MQTT
9. NdR91’s Blueprint installed. (then some edits like changing the language if needed and adding the frigate server URL so you can jump to the live feed)
10. Some basic script editing skills.

Here is the flow.

  1. NdR91’s blueprint to get the alerts from MQTT.
  2. This calls the automation for specific camera. (consolidation opportunity #1 somehow make one blueprint work off one automation for all cameras, not sure this can be done)
  3. Script runs and sends a photo and a video(if it exists) along with a call back to Pause the Automation for 30 or 60 minutes. (You can do whatever you want here, those work for me) There is also a simple URL in the caption to link to the live feed. (this can be internal or external but I’m not getting into reverse proxies here)
  4. If a callback is selected the automation is paused for 30 or 60 mins.

As you can see this means you have the

  • The Blueprint (Frigate - Telegram Notification · GitHub)
  • An automation for each camera created by the blueprint.
  • A script related to each camera for each callback option. (opportunity #2, there has to be a way to pull the camera name and pass it from callback into an automation so you can have one script for each callback and it manages which camera/automation to pause.)

For me that’s 31 items, a lot and I know it can be condensed, I just don’t have the skills.

Here are samples of each.

  1. Blue Print (with associated script in /local/blueprints/NdR91/firgate_telegram_notifcations.yaml)
  2. The automation created by the Blueprint. (just for completeness, this gets created automatically when you run the blueprint.)
  3. Automation script for pausing the automation based on clicking the callback button in Telegram.

Doing it my way you lather rinse repeat for each camera and callback for #3.

Not sure the correct way to go about this but I installed NdR91’s blueprint and then just edited the frigate_telegram_notifications.yaml file afterwards. You just need to search for “YOUR” and edit or remove. Reality is the entire caption prompt is up to you.

################frigate_telegram_notifications.yaml##################

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, leave "false" to receive
        notification. '
      selector:
        select:
          options:
          - 'true'
          - 'false'
          custom_value: false
          multiple: 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
mode: single
max_exceeded: silent
trigger:
  trigger: 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
  clip: '{{base_url}}/api/frigate/notifications/{{id}}/{{camera}}/clip.mp4'
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:
- action: telegram_bot.send_photo
  data:
    target: '{{ target_chat }}'
    disable_notification: '{{ notification }}'
    caption: 'Image - Camera Alert on {{ camera_name }} camera. Click here to goto {{trigger.payload_json["after"]["camera"]}} camera. http://{YOUR_FRIGATE_NAME_OR_IP_HERE}/#{{trigger.payload_json["after"]["camera"]}}'
    url: '{{base_url}}/api/frigate/notifications/{{id}}/snapshot.jpg'
    inline_keyboard:
      - 30 Min Pause {{ camera_name }} :/pause_for_30_{{ camera_name }} #####Tailor this to your liking.
      - 60 Min Pause {{ camera_name }} :/pause_for_60_{{ camera_name }} 
- repeat:
    sequence:
    - wait_for_trigger:
        trigger: 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'' }}'
    - action: telegram_bot.send_video
      data:
        target: '{{ target_chat }}'
        disable_notification: '{{ notification }}'
        caption: 'Video - Camera Alert on {{ camera_name }} camera. Click here to goto {{trigger.payload_json["after"]["camera"]}} camera. http://{YOUR_FRIGATE_NAME_OR_IP_HERE}/#{{trigger.payload_json["after"]["camera"]}}'
        url: '{{base_url}}/api/frigate/notifications/{{id}}/{{camera}}/clip.mp4'
        inline_keyboard:
          - 30 Min Pause:/pause_for_30
          - 60 Min Pause:/pause_for_60
    until: '{{ wait.trigger.payload_json[''type''] == ''end'' }}'

#############Call to Frigate Camera Automation##############
#This is created automatically when you run the blueprint but again if there is room for consolidation, it takes more than I know.

alias: Frigate - Telegram Notification - BackYard
description: ""
use_blueprint:
  path: NdR91/frigate_telegram_notification.yaml
  input:
    camera: BackYard
    target_chat: "{THIS_SHOULD_BE_YOUR_CHAT_ID}"
    base_url: "{THIS_SHOULD_BE_YOUR_HA_URL}"

######################Sample Pause Script####################
#This is where if you can pass the Camera ID/Name around, I think you can get to just 1 script for any callback option.

alias: pause_for_30_Backyard
description: Pause BackYard Camera Alerts for 30 Minutes
triggers:
  - event_type: telegram_callback
    event_data:
      data: /pause_for_30_Backyard
    trigger: event
conditions: []
actions:
  - entity_id: automation.frigate_telegram_notification_backyard
    action: automation.turn_off
  - delay: "00:30:00"
  - entity_id: automation.frigate_telegram_notification_backyard
    action: automation.turn_on
mode: single

From there you just create more automations for whatever you want for callback options, I thought about a “Pause Indefinitely” but then you have to remember you did it…it kind of goes against the idea of automation.

There you have it. Technically its a solution since it all works but I just think it’s a kludge and I don’t posses the YAML or HA skills to make it any neater. I am merely a descent code blender and not much more.

My thanks goes out to NdR91 for the blueprint that got me started and at least 20 other threads to get it working.

Thoughts and feedback welcome. I never saw all of this in once place so I hope someone finds it helpful. I’ll end the longest post ever now. - YodaCoda