Troubles with action variables

I must be missing something about how action variables work.

I’m trying to use a mapping variable to convert a trigger entity_id to a different set of values.

When I create the map as a variable directly in the template locally it all works fine.

here is the working automation:

  - id: test_mapper_embedded
    alias: Test Mapper Embedded
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: 
          - input_boolean.bool_11
          - input_boolean.bool_12
          - input_boolean.bool_13
          - input_boolean.bool_14
        to: 'on'
    action:
      - service: camera.snapshot
        data: 
          entity_id: >
            {% set mapper = { 'input_boolean.bool_11': ['computer_room', 'computer_room_1'],
                  'input_boolean.bool_12': ['kitchen', 'kitchen_1'],
                  'input_boolean.bool_13': ['dining_room', 'diningroom_1'],
                  'input_boolean.bool_14': ['living_room', 'livingroom_1'] } %}
            camera.{{ mapper[trigger.entity_id][0] }}
          filename: >
            {% set mapper = { 'input_boolean.bool_11': ['computer_room', 'computer_room_1'],
                  'input_boolean.bool_12': ['kitchen', 'kitchen_1'],
                  'input_boolean.bool_13': ['dining_room', 'diningroom_1'],
                  'input_boolean.bool_14': ['living_room', 'livingroom_1'] } %}
            /config/www/snapshots/{{ mapper[trigger.entity_id][1] }}.jpg
      - delay: '00:00:05'
      - service: notify.pushbullet
        data:
          message: 'The automation has been triggered by the {{ trigger.to_state.attributes.friendly_name }}!'
          data:
            file: >
              {% set mapper = { 'input_boolean.bool_11': ['computer_room', 'computer_room_1'],
                  'input_boolean.bool_12': ['kitchen', 'kitchen_1'],
                  'input_boolean.bool_13': ['dining_room', 'diningroom_1'],
                  'input_boolean.bool_14': ['living_room', 'livingroom_1'] } %}
              /config/www/snapshots/{{ mapper[trigger.entity_id][1] }}.jpg

but it would be nice to extract the mapper variable outside of the local scope of each template since there is obviously of lot of code repetition like that.

here is what I’ve tried to use that isn’t working:

  - id: test_mapper_variable
    alias: Test Mapper Variable
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: 
          - input_boolean.bool_11
          - input_boolean.bool_12
          - input_boolean.bool_13
          - input_boolean.bool_14
        to: 'on'
    action:
      - variables:
          mapper: >
            { 'input_boolean.bool_11': ['computer_room', 'computer_room_1'],
              'input_boolean.bool_12': ['kitchen', 'kitchen_1'],
              'input_boolean.bool_13': ['dining_room', 'diningroom_1'],
              'input_boolean.bool_14': ['living_room', 'livingroom_1'] }
      - service: camera.snapshot
        data: 
          entity_id: camera.{{ mapper[trigger.entity_id][0] }}
          filename: /config/www/snapshots/{{ mapper[trigger.entity_id][1] }}.jpg
      - delay: '00:00:08'
      - service: notify.pushbullet
        data:
          message: 'The automation has been triggered by the {{ trigger.to_state.attributes.friendly_name }}!'
          data:
            file: /config/www/snapshots/{{ mapper[trigger.entity_id][1] }}.jpg

much more compact but unfortunately when I turn on any of the booleans to test it I get the following error in the automation trace:

for some reason it doesn’t look like the mapper substitution is getting done.

I’ve looked at this for a long time to see if I can see any syntax errors but I’m just not seeing it.

So either I have a misunderstanding of how variables work of I’m blind to the syntax error.

Any help is appreciated.

Hi finity,
Ever use YAML Anchors?
YAML Anchors

Help with YAML anchor and input_select options - #9 by petro.

Solved: Reuse code with YAML Anchors and more.

Maybe Gin up a custom_template macro to call?

1 Like

No, I’ve never used them. I’ve looked into them before and from what I remember they look kind of complicated. :laughing:

It would be nice to know where my failure point is with variables before I give up there and use something else.

Fair point, and that I don’t know how to actually fix the problem.
Once you wrap your head around anchors though, they work pretty nice.
Petro is the master with anchors.

1 Like

Remove the >

Or keep it and make it the output of a template

1 Like

Yup that was it.

I lost track of the single (vs double) brackets not making it a template.I figured it was something simple that I was overlooking.

thanks for the help!

1 Like

FYI, I do this instead, it’s alittle more verbose but it reads better IMO


      - variables:
          config:
              input_boolean.bool_11: 
                camera: computer_room
                image: computer_room_1
              input_boolean.bool_12: 
                camera: kitchen
                image: kitchen_1
              input_boolean.bool_13: 
                camera: dining_room
                image: diningroom_1
              input_boolean.bool_14: 
                camera: living_room
                image: livingroom_1
          found: >
           {{ config.get(trigger.entity_id) }}
      - service: camera.snapshot
        data: 
          entity_id: camera.{{ found.camera }}
          filename: /config/www/snapshots/{{ found.image }}.jpg
      - delay: '00:00:08'
      - service: notify.pushbullet
        data:
          message: 'The automation has been triggered by the {{ trigger.to_state.attributes.friendly_name }}!'
          data:
            file: /config/www/snapshots/{{ found.image }}.jpg

edit: config.get(..) over config[..] is essentially error proof. .get will not error if the key doesn’t exist. Which should never happen… unless you press the test button in the UI that fires without the trigger.

1 Like

I agree that’s definitely a bit more readable.