Referencing a list from another YAML node

Hello,

I’m new to Home Assistant so I don’t have a lot of reference yet.

I’m using a trigger from a motion sensor to snapshot some camera feeds to local files, and then send an email with the snapshots.

I have a list of actions where the first one takes snapshots from three cameras and then uses a jinja reference to write each to a unique filename, but then in the next list item where I dispatch the email, I’m stuck referring to the full image paths.

How do I reference the list of filenames I created in the first action?

- id: '1573232500325'
  alias: Motion Sensor Test
  description: ''
  trigger:
  - entity_id: sensor.homeseer_technologies_hs_fls100_floodlight_sensor_burglar
    platform: state
    to: '8'
  condition: []
  action:
    - service: camera.snapshot
      data:
        entity_id:
          - camera.dvr1_camera1_stream
          - camera.dvr1_camera2_stream
          - camera.dvr1_camera8_stream
        filename: '/data/snapshot.{{ entity_id.name }}.jpg'
    - service: notify.motion_email
      data:
        title: "Motion Detected"
        message: "Motion on J1 west sensor"
        data:
          images:
            - '/data/snapshot.dvr1-camera1-stream.jpg'
            - '/data/snapshot.dvr1-camera2-stream.jpg'
            - '/data/snapshot.dvr1-camera8-stream.jpg'

Edit: I’ve gotten this far:

- id: '1573232500325'
  alias: Motion Sensor Test
  description: ''
  trigger:
  - entity_id: sensor.homeseer_technologies_hs_fls100_floodlight_sensor_burglar
    platform: state
    to: '8'
  condition: []
  action:
    - service: camera.snapshot
      data:
        entity_id:
          - camera.dvr1_camera8_stream
          - camera.dvr1_camera2_stream
          - camera.dvr1_camera1_stream
        filename: '/data/snapshot.{{ entity_id.name }}.jpg'
    - service: notify.motion_email
      data_template:
        title: "Motion Detected"
        message: "Motion on J1 west sensor"
        data:
          images: >
            {%- for file in service["camera.snapshot"].filename %}
            - {{ file }}
            {%- endfor %}

… but HA complains that Error rendering data template: UndefinedError: 'service' is undefined

You can’t create a list with a template since a template will only output a single string. Even if you put line feeds and dashes in the template output, it will just still be a single string that happens to contain line feeds and dashes. This is a common mistake. Remember, YAML processing happens first, way before the template is rendered.

So I would suggest sticking with your first solution. However, I don’t think you can use {{ entity_id.name }}. I think it should be:

- id: '1573232500325'
  alias: Motion Sensor Test
  description: ''
  trigger:
  - entity_id: sensor.homeseer_technologies_hs_fls100_floodlight_sensor_burglar
    platform: state
    to: '8'
  condition: []
  action:
    - service: camera.snapshot
      data:
        entity_id:
          - camera.dvr1_camera1_stream
          - camera.dvr1_camera2_stream
          - camera.dvr1_camera8_stream
        filename: '/data/snapshot.{{ entity_id }}.jpg'
    - service: notify.motion_email
      data:
        title: "Motion Detected"
        message: "Motion on J1 west sensor"
        data:
          images:
            - '/data/snapshot.camera.dvr1-camera1-stream.jpg'
            - '/data/snapshot.camera.dvr1-camera2-stream.jpg'
            - '/data/snapshot.camera.dvr1-camera8-stream.jpg'

Or, if you really want to remove the “camera.” part, then:

- id: '1573232500325'
  alias: Motion Sensor Test
  description: ''
  trigger:
  - entity_id: sensor.homeseer_technologies_hs_fls100_floodlight_sensor_burglar
    platform: state
    to: '8'
  condition: []
  action:
    - service: camera.snapshot
      data:
        entity_id:
          - camera.dvr1_camera1_stream
          - camera.dvr1_camera2_stream
          - camera.dvr1_camera8_stream
        filename: '/data/snapshot.{{ entity_id.replace('camera.', '') }}.jpg'
    - service: notify.motion_email
      data:
        title: "Motion Detected"
        message: "Motion on J1 west sensor"
        data:
          images:
            - '/data/snapshot.dvr1-camera1-stream.jpg'
            - '/data/snapshot.dvr1-camera2-stream.jpg'
            - '/data/snapshot.dvr1-camera8-stream.jpg'

This is a common mistake. Remember, YAML processing happens first, way before the template is rendered.

Oh wow, okay. I thought for sure that the templating engine chewed through the file, rendered YAML, and then the YAML processing occurred at the end. That makes things a lot clearer. Thanks!

However, I don’t think you can use {{ entity_id.name }}. […] Or, if you really want to remove the “camera.” part … {{ entity_id.replace('camera.', '') }}

When I originally used entity_id, I got filenames like snapshot1.[dvr1_camera1_stream].jpg, or possibly even weirder names… I don’t quite recall. Adding .name yielded my desired result. Out of curiosity, is there a way to browse that namespace?

Thanks for the help!

Wow, that entity_id variable is very different from what I expected. And the doc page doesn’t explain it properly either. It’s actually a Camera Python object! Or more importantly, a Python object of a class derived from the Camera class. So apparently when you do {{ entity_id.name }} it’s invoking the object’s name property. Wow!!

FWIW, the name property will usually show itself as the entity’s friendly_name attribute. May not be what you want here. So maybe:

- id: '1573232500325'
  alias: Motion Sensor Test
  description: ''
  trigger:
  - entity_id: sensor.homeseer_technologies_hs_fls100_floodlight_sensor_burglar
    platform: state
    to: '8'
  condition: []
  action:
    - service: camera.snapshot
      data:
        entity_id:
          - camera.dvr1_camera1_stream
          - camera.dvr1_camera2_stream
          - camera.dvr1_camera8_stream
        filename: '/data/snapshot.{{ entity_id.entity_id }}.jpg'
    - service: notify.motion_email
      data:
        title: "Motion Detected"
        message: "Motion on J1 west sensor"
        data:
          images:
            - '/data/snapshot.camera.dvr1-camera1-stream.jpg'
            - '/data/snapshot.camera.dvr1-camera2-stream.jpg'
            - '/data/snapshot.camera.dvr1-camera8-stream.jpg'

or

- id: '1573232500325'
  alias: Motion Sensor Test
  description: ''
  trigger:
  - entity_id: sensor.homeseer_technologies_hs_fls100_floodlight_sensor_burglar
    platform: state
    to: '8'
  condition: []
  action:
    - service: camera.snapshot
      data:
        entity_id:
          - camera.dvr1_camera1_stream
          - camera.dvr1_camera2_stream
          - camera.dvr1_camera8_stream
        filename: '/data/snapshot.{{ entity_id.entity_id.split()[1] }}.jpg'
    - service: notify.motion_email
      data:
        title: "Motion Detected"
        message: "Motion on J1 west sensor"
        data:
          images:
            - '/data/snapshot.dvr1-camera1-stream.jpg'
            - '/data/snapshot.dvr1-camera2-stream.jpg'
            - '/data/snapshot.dvr1-camera8-stream.jpg'

If you ask me, I think that entity_id variable is a bug. It shouldn’t be exposing internal implementation details.