Using templating to create a list of entities

I’m certain this is going to be a simple fix and just comes down to my inability to wrap my head around YAML multiline rules, despite trawling all the documentation I can find on Jinja2.

Here is a valid call I’m making to LLM Vision with a successful response:

action: llmvision.data_analyzer
data:
  provider: *****
  message: >-
    Summarize what's happening in the camera feed (one sentence max). Don't
    describe the scene! If there is a person, describe what they're doing and
    what they look like. If they look like a courier mention that! If nothing is
    happening, say so, along with the name of the camera from the corner of the
    feed.
  sensor_entity: input_text.ai_result
  image_entity:
    - camera.front_door_frame
  max_tokens: 20
  temperature: 0.1
  include_filename: false
  target_width: 1280
  detail: low

I’m trying to dynamically change what is sent in the image_entity field based on a trigger inside the automation which makes this call. The wall I’m hitting is the YAML syntax (the if logic itself has been tested separately and is valid)

image_entity:
    {% if  trigger.to_state.attributes.friendly_name == 'Front Door' %}
      - camera.front_door_frame
    {% endif %}

Result: Incorrect syntax (obv)

image_entity: >
    {% if  trigger.to_state.attributes.friendly_name == 'Front Door' %}
      - camera.front_door_frame
    {% endif %}

Result: Failed to perform the action llmvision.data_analyzer. Entity - does not exist.

Just gave up trying to understand multiline syntax and went all out:

image_entity: >
image_entity: >+
image_entity: >-
image_entity: |
image_entity: |+
image_entity: |-

All with the same result.

Help me Obi Wan Kenobi…

If the following example reports the entity is not found, it implies the image_entity option does not support the use of templates.

  image_entity: >
    {% if trigger.to_state.attributes.friendly_name == 'Front Door' %}
      ['camera.front_door_frame']
    {% else %}
      []
    {% endif %}

I am unfamiliar with the LLM Vision integration.

The assumption here is the image_entity option expects to receive a list of entity_ids. You can’t use a Jinja2 template to produce a list in YAML format (which is what you attempted to do) but you can use it to generate a list in python format (as shown in the example above).

1 Like

Your example worked, implying templates is supported. TIL about Jinja2 data structures, thanks!

1 Like