Defining a key-value map in trigger_variables

I’m trying to define static maps in my trigger_variables that I can use to look things up in my automation actions. Here’s a broken minimal example of the kind of thing I’m trying to do:

description: ""
mode: single
trigger:
  - platform: state
    entity_id:
      - binary_sensor.liftmaster_button_1
      - binary_sensor.liftmaster_button_2
    to: "on"
    trigger_variables:
      door: > {{ {
        "0": "switch.front_door",
        "1": "switch.back_door",
        } }}
condition: []
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: "{{ door[trigger.idx] }}"

When I try to save this, unfortunately, I get an error about “extra keys not allowed @ data['trigger_variables']”. I know I can do things with choose, but I have automations with multiple parameters, some derived from the triggering entity, some derived from, say, the trigger ID or the trigger ID mod the number of actions per device.

Is there any way to define automation-wide maps that can then be consulted in multiple actions?

Thanks.

Try this

description: ""
mode: single
trigger:
  - platform: state
    entity_id:
      - binary_sensor.liftmaster_button_1
      - binary_sensor.liftmaster_button_2
    to: "on"
variables:
  door: >
    {{ {
      0: "switch.front_door",
      1: "switch.back_door",
    } }}
condition: []
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: "{{ door[trigger.idx] }}"

Thanks. It looks like I just indented trigger_variables too much. It works with either variables or trigger_variables. I assume the latter is preferable when I don’t ever expect the value to change. In fact, this seems to be legal syntax as well:

trigger_variables:
  door:
    "0": switch.front_door
    "1": switch.back_door

However I’m not sure if that would run. Maybe have to say door[trigger.idx|string]?

No you want a number. Mine were numbers, see how I removed the quotes in mine?

If you use a string then do this:

door[trigger.idx|int]

Based on the results of my tests, the value of trigger.idx is a string. So if you want to use it to match a dictionary key, ideally the key should be a string.

alias: Trigger Variables 1
description: ""
trigger:
 - platform: state
   entity_id:
     - input_boolean.test
   to: "on"
condition: []
action:
 - service: notify.persistent_notification
   data:
     message: "{{ door.get(trigger.idx, 'none') }}"
     title: TV 1
mode: single
trigger_variables:
 door:
   "0": switch.front_door
   "1": switch.back_door


NOTE

For future reference, if you use the Automation Editor to create this:

  door:
    0: switch.front_door
    1: switch.back_door

after saving the file, the Automation Editor will convert it to this:

  door:
    "0": switch.front_door
    "1": switch.back_door

It effectively changed the type of the keys from integer to string. It’s just one of Automation Editor’s quirks that can interfere with one’s ability to create more advanced automations (and a reason why I don’t use it).

1 Like