Attached image not always included in Gmail and Telegram notifications

I’m having a problem with motion notifications from Frigate. I receive the notifications, but way too often the attached image(s) isn’t included in the message.

I’m using Gmail and Telegram broadcast to send the notifications. I’ve looked at the automation and script traces, but I can’t find any differences between the notifications that include the image and the ones that don’t. Also, if I copy the service call and its parameters from the trace and input them into the Service call tool, the notifications always go through fine.

I thought that maybe the image file isn’t written yet when the automation tries to read it. So I added delays, but it hasn’t changed anything. I’ve also added a check that reads the Frigate event data to check that there is a snapshot available.

I’m not posting any yaml (yet) as I can’t see how it could have any errors, since sometimes the same yaml code(s) works.

Seeing the automation yaml is a must otherwise it is just guessing, seeing how it is working can give clues on the intermittent behavior.

Alrighty then, Here is the automation:

alias: Send notification when Frigate detects an object
description: ''
trigger:
  - platform: mqtt
    topic: frigate/events
condition: []
action:
  - condition: template
    value_template: >-
      {{ ( now().timestamp() -
      as_timestamp(states('input_datetime.last_piha_notification'), 0) ) >
      (10*60) }}
  - service: script.turn_on
    target:
      entity_id: >-
        script.send_{{ trigger.payload_json["after"]["camera"]
        }}_camera_motion_notification
    data:
      variables:
        event_data: '{{ trigger.payload }}'
        camera: '{{ trigger.payload_json["after"]["camera"] }}'
        label: '{{ trigger.payload_json["after"]["label"] }}'
        title: >-
          Havaittu liikettä {{ trigger.payload_json["after"]["camera"] ~
          "-kamerassa" }}
        message: >-
          Havaittu liikettä {{ trigger.payload_json["after"]["camera"] ~
          "-kamerassa -- " ~ trigger.payload_json["after"]["start_time"] |
          timestamp_custom('%d.%m.%Y %H:%M') }}
        url: >-
          http://192.168.10.50:5000/api/events/{{
          trigger.payload_json["after"]["id"] }}/thumbnail.jpg
        path: >-
          /media/frigate/clips/{{ trigger.payload_json["after"]["camera"] ~ "-"
          ~ trigger.payload_json["after"]["id"] }}.jpg
        path2: >-
          /media/frigate/clips/{{ trigger.payload_json["after"]["camera"] ~ "-"
          ~ trigger.payload_json["after"]["id"] }}-clean.png
        caption: >-
          {{ trigger.payload_json["after"]["camera"] ~ "-kamera -- " ~
          trigger.payload_json["after"]["start_time"] |
          timestamp_custom('%d.%m.%Y %H:%M') }}
  - service: input_datetime.set_datetime
    target:
      entity_id: >-
        input_datetime.last_{{ trigger.payload_json["after"]["camera"]
        }}_notification
    data:
      timestamp: '{{ now().timestamp() }}'
    enabled: false
trace:
  stored_traces: 5
mode: single

Here’s the script:

alias: Send piha camera motion notification
sequence:
  - if:
      - condition: template
        value_template: event_data["after"]["has_snapshot"] == false
    then:
      - delay:
          hours: 0
          minutes: 5
          seconds: 0
          milliseconds: 0
    else:
      - delay:
          hours: 0
          minutes: 0
          seconds: 15
          milliseconds: 0
  - service: input_datetime.set_datetime
    target:
      entity_id: input_datetime.last_piha_notification
    data:
      timestamp: '{{ now().timestamp() }}'
  - service: script.send_motion_notification_to_telegrams
    data_template:
      title: '{{title}}'
      message: '{{message}}'
      path: '{{path}}'
      caption: '{{caption}}'
  - service: script.send_motion_notifications_to_emails
    data_template:
      title: '{{title}}'
      message: '{{message}}'
      path: '{{path}}'
      path2: '{{path2}}'
mode: restart

Here’s the Telegram script:

alias: Send motion notification to Telegrams
sequence:
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.lasse_telegram
            state: 'on'
        sequence:
          - service: notify.lasse_telegram
            data_template:
              title: '{{ title }}'
              message: '{{ message }}'
              data:
                photo:
                  - file: '{{ path }}'
                    caption: '{{caption}}'
    default: []
mode: queued
max: 5

And the email script:

alias: Send motion notifications to emails
sequence:
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.kaija_email
            state: 'on'
        sequence:
          - service: notify.kaija_email
            data:
              title: '{{ title }}'
              message: ''
              data:
                images:
                  - '{{ path }}'
                  - '{{ path2 }}'
                html: >
                  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                  <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head>
                      <meta charset="UTF-8">
                      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                  </head> <body>
                      <p>{{ message }}</p>
                      <p>Tunnistettu kohde:<br>
                      <img src="cid:{{ path.split('/')|last }}" /></p>
                      <p>Koko kuva:<br>
                      <img src="cid:{{ path2.split('/')|last }}" width="720" /></p>
                  </body> </html>
    default: []
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.lasse_email
            state: 'on'
        sequence:
          - service: notify.lasse_email
            data:
              title: '{{ title }}'
              message: ''
              data:
                images:
                  - '{{ path }}'
                  - '{{ path2 }}'
                html: >
                  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                  <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head>
                      <meta charset="UTF-8">
                      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                  </head> <body>
                      <p>{{ message }}</p>
                      <p>Tunnistettu kohde:<br>
                      <img src="cid:{{ path.split('/')|last }}" /></p>
                      <p>Koko kuva:<br>
                      <img src="cid:{{ path2.split('/')|last }}" width="720" /></p>
                  </body> </html>
    default: []
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.risto_email
            state: 'on'
        sequence:
          - service: notify.risto_email
            data:
              title: '{{ title }}'
              message: ''
              data:
                images:
                  - '{{ path }}'
                  - '{{ path2 }}'
                html: >
                  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                  <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head>
                      <meta charset="UTF-8">
                      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                  </head> <body>
                      <p>{{ message }}</p>
                      <p>Tunnistettu kohde:<br>
                      <img src="cid:{{ path.split('/')|last }}" /></p>
                      <p>Koko kuva:<br>
                      <img src="cid:{{ path2.split('/')|last }}" width="720" /></p>
                  </body> </html>
    default: []
mode: single