How to access target entities in scripts?

Hi!

How do I access entities id in scripts, when target are multiple entities?

One example is my snapshot script, that didn’t work.
What I want is: when my script is executed, the script takes a snapshot for all entities targets using different path’s for filename.
I’m trying this, without success:

alias: Snapshot Camera Garagem
sequence:
  - service: camera.snapshot
    data:
      filename: "/config/www/tmp/snapshot_{{entity_id}}.jpg"
    target:
      entity_id:
        - camera.cam_garagem
        - camera.cam_poste_garagem
mode: single
icon: mdi:cctv

I’m getting this error:
Failed to call service script/snapshot_camera_garagem. Error rendering data template: UndefinedError: 'entity_id' is undefined

And this is why I’m tryinf to use template variable entity_id, but it’s not working (see the filename field):

So my question is: how can I access target id’s inside script actions?
For automations, I know that I can use trigger.entity_id for source of trigger…but for actions, how do I access?

Tks!

Seems like a place to use the recently added Repeat-For Each.

alias: Snapshot Camera Garagem
sequence:
  - repeat:
      for_each:
        - "camera.cam_garagem"
        - "camera.cam_poste_garagem"
      sequence:
        - service: camera.snapshot
          data:
            filename: "/config/www/tmp/snapshot_{{repeat.item}}.jpg"
          target:
            entity_id: "{{ repeat.item }}"
mode: single
icon: mdi:cctv

EDIT: Corrected indent error

1 Like

Message malformed: required key not provided @ data['sequence'][0]['repeat']['sequence']

Didn’t worked :frowning:

Indent sequence one more level. sequence is a field of repeat, same level as for_each.

1 Like