Referencing entity ID causing a template error

Tags: #<Tag:0x00007f73a56d3e70> #<Tag:0x00007f73a56d36c8> #<Tag:0x00007f73a56d3218>

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.