Using trigger.idx to index a list

I’ve got the following automation that works well enough:

# Security - Motion
# if motion is sensed
# when no one is home or we're in bed
- alias: motion?
  trigger:
    platform: state
    entity_id:
      - binary_sensor.living_room_motion_motion
      - binary_sensor.dining_room_motion_motion
      - binary_sensor.upstairs_motion_sensor_motion
    from: 'off'
    to: 'on'
  condition:
    condition: or
    conditions:
      - condition: template
        value_template: "{{ states('group.family') != 'home' }}"
      - condition: state
        entity_id: input_boolean.in_bed
        state: 'on'
  action:
    - service: notify.mobile_app_russ_phone
      data:
        message: '{{ trigger.to_state.name }} motion'

It gives a much-more-verbose-than-I’d-like message like “binarary_sensor.living_room_motion_motion motion”.
What I’d like to do is use trigger.idx to index into a less verbose list of device names, but I’m not quite sure how to set up the list so that less_verbose_list[trigger.idx] would return something like “living room”.
Any suggestions?

Your automation is using trigger.to_state.name which should be displaying the entity’s friendly_name and not its entity_id. However, if it has no friendly_name then it will display entity_id. Do the binary_sensors have friendly names?

Do you want it to display the entity’s friendly_name or something shorter?

something shorter … and I’m thinking I can create my own list of “something shorter” and use trigger.idx as an index into that list.

The automation has one State Trigger so the value of trigger.idx will always be the same.

I suggest creating three separate State Triggers and give each one’s id property the desired name.

- alias: motion?
  trigger:
    - id: 'Living Room'
      platform: state
      entity_id: binary_sensor.living_room_motion_motion
      from: 'off'
      to: 'on'
    - id: 'Dining Salon'
      platform: state
      entity_id: binary_sensor.dining_room_motion_motion
      from: 'off'
      to: 'on'
    - id: 'Sleeping Quarters'
      platform: state
      entity_id: binary_sensor.upstairs_motion_sensor_motion
      from: 'off'
      to: 'on'
  condition: "{{ states('group.family') != 'home' or is_state('input_boolean.in_bed', 'on') }}"
  action:
    - service: notify.mobile_app_russ_phone
      data:
        message: '{{ trigger.id }} motion'

If you want to only use a single State Trigger, you will need to use trigger.to_state.object_id as the key for a dictionary to find the associated short name.

- alias: motion?
  trigger:
    platform: state
    entity_id:
      - binary_sensor.living_room_motion_motion
      - binary_sensor.dining_room_motion_motion
      - binary_sensor.upstairs_motion_sensor_motion
    from: 'off'
    to: 'on'
  condition: "{{ states('group.family') != 'home' or is_state('input_boolean.in_bed', 'on') }}"
  action:
    - variables:
        names:
          living_room_motion_motion: 'Living Room'
          dining_room_motion_motion: 'Dining Salon'
          upstairs_motion_sensor_motion: 'Sleeping Quarters'
    - service: notify.mobile_app_russ_phone
      data:
        message: "{{ names.get(trigger.to_state.object_id, 'Unknown') }} motion"
1 Like

Those examples are very helpful. Both are useful in their own way and I’ll probably use the second for some other things I have in mind, but the first is exactly what I want in this instance.
(I didn’t know you could do that with id:)
Thanks for this.

1 Like