Referencing entity ID causing a template error

This template used to work fine until I moved to 0.116. Has anybody experienced this issue and know what changed?

I have checked the docs, read all the upgrade notes and I’m following several discussions and haven’t come across this.

    - service: camera.snapshot
      data:
        entity_id: camera.security_camera
        filename: '/tmp/{{ entity_id.entity_id }}_{{ now().strftime("%Y%m%d-%H%M%S") }}.jpg'
homeassistant.exceptions.HomeAssistantError: Error rendering data template: UndefinedError: 'entity_id' is undefined

What is quite strange to me, is that I can manually execute what’s in the data: section via the developer tools on the UI but even there if I then call either of the scripts it fails.

For reference, here are the full scripts:

security_camera_record_clip:
  sequence:
    - service: camera.record
      data:
        entity_id: camera.security_camera
        filename: '/tmp/{{ entity_id.entity_id }}_{{ now().strftime("%Y%m%d-%H%M%S") }}.mp4'
        duration: 10
    - delay: "00:00:15"

security_camera_create_snapshot:
  sequence:
    - service: input_datetime.set_datetime
      entity_id: input_datetime.security_camera_last_snapshot
      data_template:
        datetime: "{{ now() }}"
    - service: camera.snapshot
      data:
        entity_id: camera.security_camera
        filename: '/tmp/{{ entity_id.entity_id }}_{{ now().strftime("%Y%m%d-%H%M%S") }}.jpg'
    - service: shell_command.copy_last_snapshot_image
    - condition: state
      entity_id: group.security_automations
      state: "on"
    - service: notify.family
      data:
        title: Security Camera
        message: "Motion detected!"
        data:
          push:
            thread-id: "security-camera-snapshots"
          attachment:
            url: !secret security_camera_last_snapshot_url

I’ve simplified how to reproduce this (and to double-check myself).

Here is a script:

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

Triggering this script with service script.test_snapshot or service script.turn_on and providing entity_id: script.test_snapshot both fails with the previously reported error.

But, using what’s in the data: section above works when calling the camera.snapshot service from the UI.

Screenshot 2020-10-18 at 17.32.09

I’m happy to report this as a bug but I want to be sure I’m not doing anything silly.

Note: The filename example in the docs also fails for me.

I think you are correct that something has changed in the processing of such templates.

Using this:
trigger.device_tracker.attributes.friendly_name
in the template of an automation used to work perfectly up to 0.116. I had to change it to:
state_attr(trigger.entity_id, 'friendly_name')

My hunch is ‘entity_id.entity_id’ might need rephrasing as well, but I’m sorry I don’t know how to rephrase it.

Edit: you might want to raise a bug report anyway since the documentation you linked to is also faulty right now, needs fixing anyway :slight_smile:

Thanks, @Emphyrio.

After you confirmed I found an issue (with a workaround). I reported this example there.

Hi, do you have some workaround?
Issue seems still present

Thanks

I answered on the Github issue. The workaround is to hardcode the specific information you want to extract from the entity, as previously stated. You can’t use entity_id or any of its attributes in templates.

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.

2 Likes

Interesting. So it’s rendereding a template within a template. I’ll try this over the weekend.

Hello,

the problem is still there.

Fehler beim Ausführen einer Aktion

Error rendering data template: UndefinedError: ‘entity_id’ is undefined

code:

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

Yes, it is.

Please comment on the GitHub issue linked previously.

Also, please format your code.

Lastly, see the workaround post above your message. I haven’t tested it myself yet, but I believe it’s working.