Camera: Record error

So I have post in the “Developer tools” section in “Services” to test if the camera will record and I get the error
Failed to call service camera.record. Error rendering data template: UndefinedError: 'entity_id' is undefined

Here’s the yaml

service: camera.record
target:
  entity_id: camera.computer_camera
data:
  duration: 30
  lookback: 0
  filename: /tmp/{{ entity_id.name }}_{{ now().strftime("%Y%m%d-%H%M%S") }}.mp4

Any idea what I’m doing wrong?

Check that this entity id is correct:

Also please format your whole config correctly, not just each line individually. See https://community.home-assistant.io/t/how-to-help-us-help-you-or-how-to-ask-a-good-question/114371#oneone-format-it-properly-16

Sorry about that, I fixed it. Also, the entity_id is correct.

Does Home Assistant have permission to write to:
/tmp/

https://www.home-assistant.io/docs/configuration/basic/#allowlist_external_dirs

Also this:

{{ entity_id.name }}

Should be:

{{ state_attr(entity_id, 'name' }}

Or maybe this depending on your entity’s attributes (look in developer tools states):

{{ state_attr(entity_id, 'friendly_name' }}

I get this error
Failed to call service camera.record. template value should be a string for dictionary value @ data['sequence'][0]['data']. Got {'duration': 30, 'lookback': 0, 'filename': '/tmp/{{ state_attr(entity_id, \'computer_camera\' }}_{{ now().strftime("%Y%m%d-%H%M%S") }}.mp4'}

with this
{{ state_attr(entity_id, 'name' }}

When I load with the example data in HA, it fills in this
filename: /tmp/snapshot_{{ entity_id.name }}.mp4

1 Like

You’ve hit a long-standing bug, but there is a workaround: lg_netcast platform fails to load if no channels defined · Issue #4024 · home-assistant/core · GitHub.

So this worked for me.

service: camera.record
target:
  entity_id: camera.computer_camera
data:
  filename: /media/mnt/recordings{{ computer_camera }}.mp4 

What does this actually resolve to? I’ve never seen this kind of usage. Or do you have a variable defined elsewhere?

This is the name of my camera in the configuration file.

Are you missing apostraphe around entity_id? Without the apostraphe I am receiving the following error:

Failed to call service camera.record. Error rendering data template: UndefinedError: ‘entity_id’ is undefined

If I add apostrophe around entity_id when I use either ‘name’ or ‘friendly_name’ the service executes but the jinja returns none in the filename.

service: camera.record
target:
  entity_id: camera.video_doorbell_clear
data:
  duration: 5
  lookback: 5
  filename: /media/{{ state_attr("entity_id", "friendly_name") }}_doorbell_video.mp4

If I replace ‘entity_id’ with the actual entity id the service executes and the filename does contain the name (if I use ‘friendly_name’)

service: camera.record
target:
  entity_id: camera.video_doorbell_clear
data:
  duration: 5
  lookback: 5
  filename: /media/{{ state_attr("camera.video_doorbell_clear", "friendly_name") }}_doorbell_video.mp4

No because entity_id is a variable. If you put it in quotes it becomes the literal string “entity_id”.

Also use the pre-format text button to format your post. Not the quote button.

No because entity_id is a variable. If you put it in quotes it becomes the literal string “entity_id”.

This has not been my experience, here is what I’m seeing in Developer tools>Services:
The following service call returns the error

Failed to call service camera.record. Error rendering data template: UndefinedError: ‘entity_id’ is undefined

service: camera.record
target:
  entity_id: camera.video_doorbell_clear
data:
  duration: 5
  lookback: 5
  filename: /media/{{ state_attr(entity_id, "friendly_name")}}_doorbell_video.mp4

The following service call returns the filename None_doorbell_video.mp4:

service: camera.record
target:
  entity_id: camera.video_doorbell_clear
data:
  duration: 5
  lookback: 5
  filename: /media/{{ state_attr("entity_id", "friendly_name")}}_doorbell_video.mp4

The following service call returns the error

Failed to call service camera.record. Error rendering data template: UndefinedError: ‘camera’ is undefined

service: camera.record
target:
  entity_id: camera.video_doorbell_clear
data:
  duration: 5
  lookback: 5
  filename: /media/{{ state_attr(camera.video_doorbell_clear, "friendly_name")}}_doorbell_video.mp4

The following service call returns the filename Video doorbell clear_doorbell_video.mp4:

service: camera.record
target:
  entity_id: camera.video_doorbell_clear
data:
  duration: 5
  lookback: 5
  filename: /media/{{ state_attr("camera.video_doorbell_clear", "friendly_name")}}_doorbell_video.mp4

camera.video_doorbell_clear isn’t a variable. It is an entity_id and must be put in quotes, that is how the state_attr() function requires its arguments.

Variables must not be quoted or they become just the variable name as a string, rather than the contents of the variable.

Try this in the template editor:

{% set 'foobar' = 'camera.video_doorbell_clear' %}
{{ state_att(foobar,'friendly_name') }}

That will work because the variable foobar resolves to an entity id in the state_attr() function.

Now try this:

{% set 'foobar' = 'camera.video_doorbell_clear' %}
{{ state_att('foobar','friendly_name') }}

This does not work because you are using the actual string 'foobar' in the state_attr() function, strings don’t resolve to their contenst like variables do.

This works:

{{ state_att('camera.video_doorbell_clear','friendly_name') }}

Because you are supplying the required quoted entity id.

1 Like

So then it appears that the camera.record service is not recognising entity_id as a variable (despite the documentation) as the following call returns the error message:

Failed to call service camera.record. Error rendering data template: UndefinedError: ‘entity_id’ is undefined

service: camera.record
target:
  entity_id: camera.video_doorbell_clear
data:
  duration: 5
  lookback: 5
  filename: /media/{{ state_attr(entity_id, "friendly_name")}}_doorbell_video.mp4

Where have you defined the variable entity_id ?