Camera snapshot notification attachment is black video instead of image?

I feel bad starting this topic, because there are so many of them already. I swear I’ve read through ALL of them, and no one has had this exact problem, nor have any of the other solutions worked. Beyond searching through the docs and forums, I also tweaked with this for about three hours last night and couldn’t get it to work.

When motion is detected on my camera, I’m trying to have a notification sent to my phone with a snapshot of the motion.

Here is what I’m running:

Script

    alias: Motion Notification with Photo
    sequence:
      - service: camera.snapshot
        data_template:
          entity_id: "{{camera_entity_id}}"
          filename: /config/snapshots/{{file_name}}
      - delay: "00:00:08"
      - service: notify.mobile_app_the_emissary
        data_template:
          message: "{{message_string}}"
          data:
            attachment:
              content-type: jpg
              url: /config/snapshots/{{file_name}}
    mode: single

Automation

    alias: Playroom record on motion
    description: ""
    trigger:
      - platform: state
        entity_id:
          - binary_sensor.playroom_motion_detected
        from: "off"
        to: "on"
        for:
          hours: 0
          minutes: 0
          seconds: 0
    condition: []
    action:
      - service: amcrest.enable_recording
        data:
          entity_id: all
      - service: script.motion_notification_with_photo
        data_template:
          camera_entity_id: camera.playroom
          message_string: Motion Detected in the Playroom!
          file_name: playroom_{{now().strftime('%Y_%m_%d_%H_%M_%S')}}.jpg
    mode: single

I also have this in my config.yaml:

    allowlist_external_dirs:
        - "/tmp"
        - "/config/snapshots"
      allowlist_external_urls:
        - "http://192.168.1.171/config/snapshots"
      legacy_templates: false

For allow_external_urls, I’ve tried:

        http://192.168.1.171/config/snapshots"
        
        http://homeassistant.local/config/snapshots
        
        https://EXTERNALURL/config/snapshots

I also tried changing the script to put the snapshot in the standard recommended www folder, but the result is the same. This is the notification I get- black video instead of the image.

A couple of quick notes:

  • I can see the snapshots making it into the correct folder, so that half of the script is working. It seems to be the notification part that isn’t working.

  • When I try to open the url in a browser window, I don’t get the image either. It just looks like the lovelace frame and sidebar, but with a black page, as if the overview didn’t load.

Any help is GREATLY appreciated.

Thanks!

You’re going to want to look at this:

Yep. Read through it several times.

You’re using neither the www directory nor the media_source That’s why you can’t access them from the browser. If they need authentication, use mediasource. That means you’re going to put your images in media/ directory somewhere. and you’ll need to define which directory here unless you use the default.

Once they are there, you can send that to your companion app and it’ll authenticate and display that image.

So do this:

# Example configuration.yaml
homeassistant:
  media_dirs:
    snapshots: /snapshots

Yeah, this does the same thing– I get a black video. The snapshots: /snapshots part of your example code threw a dictionary error, so I had to change it to snapshots: /config/snapshots to get a valid config.yaml.

In my post I mentioned that I did try the www folder, and got the same result. I was under the impression that as long as the directory was listed under…

homeassistant:
  allowlist_external_dirs:
    - "/tmp"
    - "/config/snapshots"

…It would be publicly accessible.

The path is wrong though then. If you want to use www then.

alias: Motion Notification with Photo
    sequence:
      - service: camera.snapshot
        data_template:
          entity_id: "{{camera_entity_id}}"
          filename: /config/www/snapshots/{{file_name}}
    mode: single

Then you can see the file here:

http://192.168.1.171/local/snapshots/<file_name>

No, I don’t want to use www. I said I tried it, as a method of troubleshooting. I want to use /config/snapshots.

That’s not an option

This is an incorrect impression, it does not work this way. A select few integrations that look at files like folder watcher care what is in that config option, that’s really all it does. It does not change what is or isn’t accessible outside of HA via URL.

You have three options that are detailed out here:

  1. (Not recommended) Snapshot to /config/www and link to it using {HA Base URL}/local/{snapshot name}
  2. Snapshot to /media and use a relative URL like /media/local/{snapshot name}
  3. Just camera: {camera entity ID} to the notification data. It’ll take a snapshot and include it in the notification for you.

Awesome, thanks. I will try these options in a bit.

The home Assistant basic setup, says you can list external URLs to be fetched:

allowlist_external_urls list (Optional)

List of external URLs that can be fetched. URLs can match specific resources (e.g., http://10.10.10.12/images/image1.jpg) or a relative path that allows access to resources within it (e.g., http://10.10.10.12/images would allow access to anything under that path)

I understand that I’ve included allowlist_external_dirs here, but I did some experimenting with allowlist_external_urls as well to no avail. Is this not what that function is for? What else would that option be used for?

Could you explain a little more about that third option? I must be missing something– is there documentation about using this particular function in a notification setting?

It’s this one:

Sorry my bad, it’s not just adding camera to the notification data. I have a send_notification script that handles a bunch of things for me and sometimes I forget what is an option for everyone and what only works for me :laughing:

On Android you set /api/camera_proxy/<camera.name> as the image url. On iOS you follow this, basically add entity_id: '<camera entity id>' to the data in the notification service call instead of specifying an image url.

Dude. I have no idea why I was trying to make it harder than it needed to be. Thanks for you help! This was definitely the way to go!

For anyone that finds this thread, here is the code I used for my automation (no script necessary):

alias: Playroom record on motion
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.playroom_motion_detected
    from: "off"
    to: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 0
condition:
  - condition: not
    conditions:
      - condition: state
        entity_id: camera.playroom
        state: recording
action:
  - service: amcrest.enable_recording
    data:
      entity_id: all
  - service: notify.mobile_app_the_emissary
    data:
      message: Motion Detected in the Playroom!
      data:
        entity_id: camera.playroom
mode: single

…and here is a screenshot from the visual editor.

2 Likes