core icon indicating copy to clipboard operation
core copied to clipboard

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

Open thrust15 opened this issue 5 years ago • 52 comments

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

thrust15 avatar Sep 18 '20 13:09 thrust15

camera documentation camera source (message by IssueLinks)

Workaround for now is to also set entity_id on the parent definition. Example:

  - trigger:
      platform: state
      entity_id: binary_sensor.main_door
      to: 'on'
    action:
      - service: camera.record
        entity_id: camera.hall
        data:
          entity_id: camera.hall
          filename: '/tmp/{{ entity_id.name }}_{{ now().strftime("%Y%m%d-%H%M%S") }}.mp4'
          duration: 8

ruimarinho avatar Oct 13 '20 10:10 ruimarinho

I can confirm this issue from another angle. I've posted it on the forum for feedback and then discovered this bug report.

This fails for me running core 0.116.2 on Python 3.9.0 (the last known version this worked on for me was also 0.114 – I skipped 0.115).

test_snapshot:
  sequence:
    - service: camera.snapshot
      data:
        entity_id: camera.security_camera
        filename: '/tmp/snapshot_{{ entity_id.entity_id }}.jpg'

Also, the example in the docs won't work.

(I'll try your suggestion to see if it works for me in this case.)

parautenbach avatar Oct 18 '20 19:10 parautenbach

@parautenbach

(I'll try your suggestion to see if it works for me in this case.)

I am running into this same issue currently and my test scenario is nearly identical to yours. Have you been able to utilize the suggested workaround to find success? Unfortunately I haven't been able to get around it :(

justin-arens avatar Dec 06 '20 02:12 justin-arens

Also having this issue. The workaround posted above doesn't seem to work for me with the snapshot service:

image
 1: Error rendering data template: UndefinedError: 'entity_id' is undefined
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 353, in async_render
    render_result = compiled.render(kwargs)
  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

@parautenbach were you able to get this working?

uchagani avatar Dec 09 '20 03:12 uchagani

Also having this issue. The workaround posted above doesn't seem to work for me with the snapshot service:

image
 1: Error rendering data template: UndefinedError: 'entity_id' is undefined
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 353, in async_render
    render_result = compiled.render(kwargs)
  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

@parautenbach were you able to get this working?

I decided to wait for the fix so didn't try the workaround. It would be better to see more of your config but from what I understand something like this should work (based on my setup):

    - service: camera.snapshot
      entity_id: camera.security_camera  # note entity id here
      data:
        entity_id: camera.security_camera
        filename: '/tmp/{{ entity_id.entity_id }}_{{ now().strftime("%Y%m%d-%H%M%S") }}.jpg'

parautenbach avatar Dec 09 '20 15:12 parautenbach

Hmm, sorry, I lied (inadvertently): I just discovered that I had a test script. It's the same format as above but nonetheless:

test_snapshot:
  sequence:
    - service: camera.snapshot
      entity_id: camera.security_camera
      data:
        entity_id: camera.security_camera
        filename: '/tmp/snapshot_{{ entity_id.name }}.jpg'

parautenbach avatar Dec 09 '20 19:12 parautenbach

I am running into the same issue, and the suggested workaround does not work for me.

memoryz avatar Dec 28 '20 10:12 memoryz

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.

  - trigger:
      platform: state
      entity_id: binary_sensor.main_door
      to: 'on'
    action:
      - service: camera.record
        entity_id: camera.hall
        data:
          entity_id: camera.hall
          filename: '/tmp/hall_{{ now().strftime("%Y%m%d-%H%M%S") }}.mp4'
          duration: 8

memoryz avatar Dec 28 '20 11:12 memoryz

I am running into the same issue, and the suggested workaround does not work for me.

You need to give more details. What exactly did you do? Which version of HA? I can categorically say that my test script illustrates the issue – and still does, at least with HA 0.118.5. Removing the parent entity_id will cause the filename to fail – for a given valid camera entity.

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'.

What you say above is what's stated in the original bug report, and confirmed by this error:

jinja2.exceptions.UndefinedError: 'entity_id' is undefined

So to workaround this issue, you just need to remove the reference to 'entity_id' in the 'filename' template, and hardcode it.

This completely defeats the purpose. The point is not having to hardcode it. I don't understand how your workaround is one. It's just the non-generic way to do it.

parautenbach avatar Dec 28 '20 20:12 parautenbach

I am running into the same issue, and the suggested workaround does not work for me.

You need to give more details. What exactly did you do? Which version of HA? I can categorically say that my test script illustrates the issue – and still does, at least with HA 0.118.5. Removing the parent entity_id will cause the filename to fail – for a given valid camera entity.

The configuration I used was almost identical to @ruimarinho's config, but it doesn't work. The error was the same: jinja2.exceptions.UndefinedError: 'entity_id' is undefined. HA version is 2020.12.1. Whether or not having the parent entity_id does not make any difference. I get same error as long as I reference entity_id in the filename template.

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'.

What you say above is what's stated in the original bug report, and confirmed by this error:

jinja2.exceptions.UndefinedError: 'entity_id' is undefined

So to workaround this issue, you just need to remove the reference to 'entity_id' in the 'filename' template, and hardcode it.

This completely defeats the purpose. The point is not having to hardcode it. I don't understand how your workaround is one. It's just the non-generic way to do it.

I didn't realize that's the purpose. For me, I started with having entity_id in the filename template and ran into this issue. Hardcoding the entity id is acceptable in my scenario.

memoryz avatar Dec 28 '20 22:12 memoryz

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.

github-actions[bot] avatar Mar 28 '21 22:03 github-actions[bot]

This is still an issue on version 2021.3.3.

parautenbach avatar Mar 29 '21 09:03 parautenbach

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.

  - trigger:
      platform: state
      entity_id: binary_sensor.main_door
      to: 'on'
    action:
      - service: camera.record
        entity_id: camera.hall
        data:
          entity_id: camera.hall
          filename: '/tmp/hall_{{ now().strftime("%Y%m%d-%H%M%S") }}.mp4'
          duration: 8

Hello, This issue is still on version 2021.4.4. Hard code entity name in filename is not useful if you have multiple camera as entity_id i think

service: camera.record
target:
  entity_id:
    - camera.one
    - camera.two
data:
  duration: 20
  filename: >-
    /somepath/whatnamehere_{{ now().strftime("%d%m%Y-%H%M%S") }}.mp4
  lookback: 10 

bennydente avatar Apr 13 '21 11:04 bennydente

Still an issue on core-2021.6.6.

blalor avatar Jun 22 '21 22:06 blalor

Still an issue :(

Extreme79 avatar Aug 15 '21 14:08 Extreme79

Unbelievable... still not working

Someone has a good workaround?

extera-nl avatar Oct 05 '21 18:10 extera-nl

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.

github-actions[bot] avatar Jan 03 '22 19:01 github-actions[bot]

Still exists on 2021.12.7.

blalor avatar Jan 03 '22 20:01 blalor

The two template rendering lines are here and here for reference. They do not appear to be setting up entity_id in the Jinja context.

mcoms avatar Jan 03 '22 21:01 mcoms

Can confirm still an issue.... unless I'm missing something. How else are people enabling recording? enable_motion_recording or an addon?

loca5790 avatar Jan 15 '22 05:01 loca5790

Issue still present. Anyone have a workaround for it? I want take snapshot of multiple camera and store with name..

Thanks

polcape avatar Jan 15 '22 13:01 polcape

The workaround is to hardcode whatever information you want to use in the template.

As for taking multiple camera snapshots, I can't speak for that, except to say this issue has no relation to that.

parautenbach avatar Jan 15 '22 14:01 parautenbach

issue is you can't remote record with the camera and access it at the same time through the IP. I've resorted to storing local on SD and having a back up pull from it.

loca5790 avatar Jan 16 '22 05:01 loca5790

That has nothing to do with this issue, which is about templating. What you mention has to do with your camera and the number of streams it can handle or is configured to handle.

parautenbach avatar Jan 16 '22 06:01 parautenbach

That has nothing to do with this issue, which is about templating. What you mention has to do with your camera and the number of streams it can handle or is configured to handle.

It's directly related. Wouldn't even be discussing it if the record feature worked as intended. What is an example of hard coding it?

loca5790 avatar Jan 16 '22 13:01 loca5790

This issue is about using entity_id within the fields that can be templated. Refer to this comment which also shows the workaround. The issue is not about whether the camera.record service works. It does. You just can't use the entity_id in the filename field (as per the linked comment). Also check the bug report – the very first post.

parautenbach avatar Jan 16 '22 15:01 parautenbach

This issue is about using entity_id within the fields that can be templated. Refer to this comment which also shows the workaround. The issue is not about whether the camera.record service works. It does. You just can't use the entity_id in the filename field (as per the linked comment). Also check the bug report – the very first post.

"Error while executing automation automation.xxxx_record: Error rendering data template: UndefinedError: 'entity_id' is undefined"

Still an issue when done the way above, tried it again. Script executes but log has the above error

I had to manually enter the entity id in the field... oh well.

loca5790 avatar Jan 16 '22 18:01 loca5790

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

mcoms avatar Jan 17 '22 15:01 mcoms

@mcoms that's a lot clearer, thanks for that. This should help the justification to get this fixed/working.

parautenbach avatar Jan 17 '22 16:01 parautenbach