Camera snapshot entity_id in filename

Ah, OK, I thought I might be able to use the entity_id variable defined in the data as part of the filename using a template rather than repeat it in the filename.

The example I gave is literally from the docs, and I thought it might do something different.

Checkout

{
“entity_id”: “camera.mjpeg_camera”,
“filename”: “/tmp/snapshot_{{ entity_id.name }} .jpg”
}

1 Like

Ah, that’s it, thanks!

Well, almost. That gives me a filename of “snapshot_Mjpeg Camera.jpg”, so it’s converting the name to capitalise the words and convert the underscore to a space (I assume that’s a default name because I haven’t set one explicitly in the camera definition)

However, that gave me a clue and I tried:

{
"entity_id": "camera.mjpeg_camera", 
"filename": "/tmp/snapshot_{{ entity_id.entity_id }}.jpg" 
}

And got what I wanted: the filename is snapshot_camera.mjpeg_camera.jpg

How do you actually use it?
entity_id object is not available in automations and scripts :thinking:

I’m capturing a snapshot on an event (in particular the doorbell ringing) and then I have a local file camera defined that uses the snapshot. I’ve got that in a group that I hide by default and then show when the event occurs.

I wrote it up without the snapshot service here, and then updated it to replace the shell command with the new service here.

I was just experimenting with getting the entity ID in the filename as well, and the method of using {{ entity_id.entity_id }} does the job - I’ve just tried it, and it works fine.

2 Likes

Yeah it works but only in dev-service, not in scripts/automations.

automation:
  - alias: All cameras today
    trigger: {platform: time, at: '16:00'}
    action:
      service: camera.snapshot
      data_template:
        entity_id: camera.one, camera.two
        filename: '/Data/cams/{{ now().strftime("%Y-%m-%d %H.%M.%S") }} {{ entity_id.name }}.jpg'
1 Like

Try it using “data” instead of “data_template” - it works for me like that:

- service: camera.snapshot
  data:
    entity_id: camera.mjpeg_camera
    filename: '/tmp/snapshot_{{ now().strftime("%Y-%m-%d %H.%M.%S") }}_{{ entity_id.entity_id }}.jpg'

I assume that’s because the filename is just data and the backend service is treating the value of the data as a template so it doesn’t have to be explicitly defined as a data_template.

2 Likes

YEAS THANK YOU!! Who would have thought…

where the pics will be saved?
do you have to create a folder?
could you explain

@seanb-uk I am unable to use now().strftime within the filename template, appears to be invalid json, can you confirm this works?

@robmarkcole, just had a look and my version had single quotes around the filename, otherwise the parser thinks the first quotes in the strftime function are closing the string. Checking it now, that didn’t work - it appears that within an automation yaml file, single quotes are allowed in the JSON, but not in the front end.

However, it looks like you can use single quotes in the strftime function, so try it with single quotes around the time format. I’ve just tried it with my camera from the front end and it worked.

1 Like

Well I just spent an hour (!) fine tuning a python_script to do this and its working well, so no more yaml :slight_smile:

Verified the following is valid and works

{
  "entity_id": "camera.driveway_dericam", "filename": "/config/www/yourcamera_{{ now().strftime('%Y%m%d-%H%M%S') }}.jpg"
}

Having more of a play, it also works if you escape the inner quotes (i.e. strftime(\"%Y%m%d-%H%M%S\")

I want to send a pic to my phone but it has a huge delay. So how do you guys send the screenshot with time and entity_id to your phone. Can you share your automation.yaml?

I am using the python_script I linked earlier

I’ve been using a similar automation as yours and recently I started getting an warning about a deprecated template variable.

Detected use of deprecated template variable
Warning ⸱ Reported by Camera
This stops working in version 2025.6.0. Please address before upgrading.

The pre-defined template variable entity_id was used when performing action camera.snapshot targeting camera entity camera.doorbell. The pre-defined template variable entity_id is being removed from the filename parameter of camera.snapshot.

I’m not sure to understand exactly what the warning means - will the template {{ entity_id }} not be usable in the “filename” of camera.snapshot service? (and why not?)

When I call the camera.snapshot service, I pass this in the data (from Node Red)
{"filename":"/config/www/tmp/snapshot_{{ entity_id.entity_id }}.jpg"}

Yeah, that was one of those cryptic warnings, without any solution. Someone thinks thats funny, I don’t know.

I just replaced {{ entity_id.entity_id }} by the cam name.

I’m getting the same warnings, at first I just placed {{ entity_id }} without anything else, but then i got the same warning again…

But how to handle actions with multiple entities. Do we need to create an action for every camera?
I tried entity_id as in the documentation Camera - Home Assistant, entity_id.entity_id and entity_id.name, all throw the error through spook.