question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Camera.record service not working with entity_id.name in template, says entity_id is undefined

See original GitHub issue

When I call the camera.record service with a filename template, it fails with the following error: jinja2.exceptions.UndefinedError: ‘entity_id’ is undefined.

This was not the case in the previous version of home assistant.

Environment

  • Home Assistant Core release with the issue: 0.115
  • Last working Home Assistant Core release (if known): 0.114
  • Operating environment (OS/Container/Supervised/Core): Supervised
  • Integration causing this issue: Generic Camera
  • Link to integration documentation on our website: https://www.home-assistant.io/integrations/camera/

Problem-relevant configuration.yaml

- alias: 'persoon voor de deur'
  trigger:
    - platform: numeric_state
      entity_id: image_processing.voortuin
      above: 0
  action:
    - service: camera.record
      entity_id: camera.frontyard
      data:
        duration: 60
        lookback: 30
        filename: '/share/motioneye/tensorflow/{{ entity_id.name }}_{{ now().strftime("%Y%m%d-%H%M%S") }}.mp4'
    - service: notify.security_telegram
      data_template:
        title: "Tensorflow"
        message: "Tensorflow"
        data:
          photo:
            file: "/config/www/tensorflow.jpg"
            caption: "Persoon in voortuin gedetecteerd {{states.image_processing.voortuin.attributes.matches.person[0].score}}%"

Traceback/Error logs

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 285, in async_render
    return compiled.render(kwargs).strip()
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 1090, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/usr/local/lib/python3.8/site-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 1, in top-level template code
  File "/usr/local/lib/python3.8/site-packages/jinja2/sandbox.py", line 407, in getattr
    value = getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'entity_id' is undefined

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 138, in async_prepare_call_from_config
    service_data.update(template.render_complex(config[conf], variables))
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 81, in render_complex
    return {
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 82, in <dictcomp>
    render_complex(key, variables): render_complex(item, variables)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 86, in render_complex
    return value.async_render(variables)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 287, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: 'entity_id' is undefined

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 206, in _async_step
    await getattr(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 385, in _async_call_service_step
    domain, service, service_data = async_prepare_call_from_config(
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 140, in async_prepare_call_from_config
    raise HomeAssistantError(f"Error rendering data template: {ex}") from ex
homeassistant.exceptions.HomeAssistantError: Error rendering data template: UndefinedError: 'entity_id' is undefined

Additional information

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:10
  • Comments:39 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
mcomscommented, Jan 17, 2022

I think what some of us are trying to do is:

service: camera.record
entity_id:
  - camera.one
  - camera.two
  - camera.three
data:
  duration: 30
  filename: "{{ entity_id.friendly_name }} - {{ now().strftime("%Y%m%d-%H%M%S") }}.mp4"

in order to create three simultaneous recordings, one per camera. There is also the possibility people are trying to access other attributes on the entity. Whilst @thrust15 looked to be using a single entity, it’s all part of the same problem — the templating which used to allow this has been broken.

Hardcoding the filename (i.e. not using entity_id at all and just typing the name yourself) has been suggested several times, and will work if you’re recording a single camera, and know everything about it. Anyone planning to suggest hardcoding again, please consider the use-cases where that information isn’t available ahead of time (i.e. with multiple entities, or dynamic attributes); this seems to be the original intent of the templating feature.

It’s also possibly worth pointing out the canonical example of the camera.record service remains “broken” (that is, it’ll give you an error if you run the example). If this issue isn’t likely to be resolved, a “quick win” might be updating the docs not to reference templating, so as not to confuse new users.

Broken example code
3reactions
natekspencercommented, Aug 18, 2022

Until this is fixed, you can do something like this:

service: camera.snapshot
data:
  filename: /media/{{ '{{ entity_id.entity_id }}' }}/{{ now().strftime("%Y%m%d-%H%M%S") }}.jpg
target:
  entity_id:
    - camera.backyard_camera
    - camera.doorbell_camera

Notice the {{ '{{ entity_id.entity_id }}' }} in the filename string. The service helper template will first render the entire filename as /media/{{ entity_id.entity_id }}/20220818-163758.jpg and pass it on to the camera service (snapshot/record), which will then render appropriately as /media/camera.backyard_camera/20220818-163758.jpg and /media/camera.doorbell_camera/20220818-163758.jpg, etc.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Referencing entity ID causing a template error - Configuration
This template used to work fine until I moved to 0.116. ... service: camera.snapshot data: entity_id: camera.security_camera filename: ...
Read more >
Security Desk User Guide 5.5
Troubleshooting : Cameras are not recording . ... name, and your Service maintenance agreement (SMA) number. ... <Entity ID>+Enter. Forward.
Read more >
Response Status and Error Codes - Infobip
Browse through the list of status and error codes that you might encounter to find out more about their meaning and how to...
Read more >
Ivanti Service Manager and Asset Manager Release Notes
Ivanti retains the right to make changes to this document or related product specifications and descriptions, at any time, without notice.
Read more >
Angular NgRx Entity - Complete Practical Guide
Scaffolding an Entity using NgRx Schematics; The NgRx Entity Update<T> type; Github repo with running example; Conclusions. Note that this post ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found