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.