Templating entity_id from trigger attribute works for one action but not for the other

As in the topic, I’m trying to write “generic” code, for few triggers. Each trigger entity has related entities id as attributes. Example below is an attempt to chain 2 actions, first being condition, to trigger the snapshot for certain zone only if door in that zone were not opened before.

The latter works image entity_id is properly templated depending on the trigger, while the initial condition, fails to save with error:

Message malformed: Entity {{ state_attr(trigger.entity_id is neither a valid entity ID nor a valid UUID for dictionary value @ data['actions'][0]['entity_id']

actions:
  - condition: state
    entity_id: "{{ state_attr(trigger.entity_id, 'zone_door') }}"
    state: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 15
  - action: image.snapshot
    metadata: {}
    data:
      filename: /media/snapshots/test.jpg
    target:
      entity_id: "{{ state_attr(trigger.entity_id, 'zone_snapshot') }}"

Am I doing something wrong, or is it just the case of “it is what it is”… …being conditions not accepting templating of entity_id?

  - condition: state
    entity_id: "{{ state_attr(trigger.entity_id, 'zone_door') }}"
    state: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 15

The condition you have included is mostly nonsense… The error code is telling you that the value you have provided in entity_id is not an entity ID. This is because:

  1. State conditions don’t allow templating.
  2. Even if they could accept templates, the included template returns the value of some random attribute… likely not an entity ID.

It is unclear exactly what you are trying to do with the condition. If you can clarify your intent and goal, we can likely help figure out the correct approach.

1 Like

You are aware that this part (below) works, quite flawlessly - and it does contain entity id:

  - action: image.snapshot
    metadata: {}
    data:
      filename: /media/snapshots/test.jpg
    target:
      entity_id: "{{ state_attr(trigger.entity_id, 'zone_snapshot') }}"

As I wrote in the first post:
“Each trigger entity has related entities id as attributes.”
and:
"The latter works image entity_id is properly templated depending on the trigger, "

In both templating cases I am using same template to reach attributes of a trigger.
First fails. Second works.

Also, it would be wise to refrain from using sentences like:
“The condition you have included is mostly nonsense…”

It kills the open source community spirit, and apart from templating is a perfectly fine condition, working otherwise. So to be technically correct, it’s definitely not “mostly” nonsense :wink:

State triggers cannot use templates on entity_id. While the documentation on state conditions makes no mention of templates at all, it would not surprise me if the same restriction applies there as well, seeing as they are sort of related. None of the documentation examples for the state condition uses a templated entity_id so…

1 Like

That’s understandable, though it leaves me wondering why the second example works (actions behave differently)

@Didgeridrew already told you what your issue is and you argued it. You cannot template the field you’re trying to template. The field does not allow templates.

1 Like

" If you can clarify your intent and goal, we can likely help figure out the correct approach."

I know I like making things a bit hard for myself, and I could probably do it other way around :wink:

My overall goal was to write to automation as generic as possible, and depending on the trigger use different values for actions and conditions.

The general goal is to:

  1. When any of the Frigate integration zones trigger person occupancy (AI camera image recognition) and it wasn’t triggered in the past 30 seconds.
  2. Check if the door to that zone did not open in past 20 or so seconds (which would imply that someone exited - so a false positive)
  3. template current time to var - as it might change during runtime, and I wan to use it in step 4 and 6
  4. Save a snapshot for appropriate camera for triggered zone (proper camera per zone taken from zone attributes, save as var from step 3)
  5. Voice notification
  6. Notify on mobile:
    a) Zone name from trigger attributes
    b) Image path << previously templated var
    c)Action - zone URI from trigger attribute

Most of this goal I already achieved. I’ll attach the code below if anyone would like to take a peek.

Current WIP

These are the “metadata” attributes I added to the entities used as triggers:

homeassistant:
  customize:
    binary_sensor.front_yard_person_occupancy:
      zone_name: "Front yard"
      zone_link: "/lovelace/front-yard"
      zone_door: "binary_sensor.esphome_front_door"
      zone_snapshot: image.cam2_person
    binary_sensor.backyard_person_occupancy:
      zone_name: "Backyard"
      zone_link: "/lovelace/backyard"
      zone_door: "binary_sensor.ewelink_ds01_garden_door"
      zone_snapshot: image.cam3_person

If you want to write things in a generic way, you need to migrate to things that allow templates.

The only condition that allows templates is a template condition.

Templates are typically available everywhere in an action, unless it’s the root entity_id field. You can get around that by templating the entity_id inside data, or inside target.

Ha, that’s the idea! Thanks! Template condition, why I didn’t think about it! :smiley:

as a sidebar, it’s very rare that you would need to use state_attr(trigger.entity_id, ...

You can get states from state triggers without need to access the state machine.

trigger.to_state.attributes.zone_snapshot

1 Like

Tested, this works fine!