core: Camera.record service not working with entity_id.name in template, says entity_id is undefined
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
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 12
- Comments: 48 (8 by maintainers)
Until this is fixed, you can do something like this:
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.jpgand pass it on to the camera service (snapshot/record), which will then render appropriately as/media/camera.backyard_camera/20220818-163758.jpgand/media/camera.doorbell_camera/20220818-163758.jpg, etc.It’s kind of rooted in an architectural issue. The templates in the service call are now evaluated before the service calls are sent. The service calls are then sent to multiple entities at once. Unfortunately, this specifically breaks rendering things that are entity related when sending to multiple entities. Any fix would just be doing what the workaround is doing - i.e., passing the entity_id templates through unrendered. Making a PR to do this still makes sense, as it avoids the service handlers from having to process the templates themselves. See https://github.com/home-assistant/core/pull/75693#issuecomment-1194182822 I suggest using the above workaround method for now.
I think what some of us are trying to do is:
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_idat 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.recordservice 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.I think I just figured out the proper workaround.
The issue is not that ‘entity_id’ needs to be defined on the parent definition, but rather that the ‘filename’ template compilation runs into undefined error when looking for the reference ‘entity_id’.
So to workaround this issue, you just need to remove the reference to ‘entity_id’ in the ‘filename’ template, and hardcode it.
Unfortunately still an issue in 2022.04-6.
My use case is that I want all my cameras to take a snapshot with the entity_id as the name and send that to me.
There hasn’t been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.
I’ll try to fix this.
2023.9.3: Still experiencing this when attempting to create a snapshot as documented at https://www.home-assistant.io/integrations/camera/#service-snapshot.
https://github.com/home-assistant/core/issues/40241#issuecomment-1233073391 workaround worked for me.
Sorry, my PR to fix it was rejected. I can have another look at it later but it won’t be in time for 2022.8.
@mcoms that’s a lot clearer, thanks for that. This should help the justification to get this fixed/working.