Camera snapshot entity_id in filename

I’m using the new snapshot service on the camera component and trying to get the camera name in the filename as per the docs.

The entity ID of my camera is camera.mjpeg_camera.

Making the call with the following service data:

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

I was expecting output filename to be:
/tmp/snapshot_camera.mjpeg_camera.jpg

Instead I get:
/tmp/snapshot_<Entity Mjpeg Camera: idle> .jpg

Am I missing something in the templating? Instead of substituting the entity_id variable it seems to be rendering a version of the entity ID and the state of the entity.

{{ “states.component.entity.attribute” }} returns the state value of the component. You’ll need to explicitly name your file “snapshot_camera.mjpeg_camera” or find the correct attribute of the entity to grab the state value you want.

Use the template tester in the front end if you’re ever unsure or want to test the output.

In the template tester:
{{ states.camera.mjpeg_camera.state }} returns “idle” as that is the current state of the component.

Try {{ states.camera.mjpeg_camera.attributes.friendly_name }} or look in the states tab for the entity for the exact attribute. This returns the friendly name of the entity

EDIT: fixed syntax

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