I am trying to create dynamic trigger based on dictionary input
I created an input
light_data:
name: Light data
default:
toggle: {'entity_id_1': 'state', 'entity_id_2': 'state'}
Then I define variables:
variables:
light_data: !input light_data
toggle_data: "{{ light_data['toggle_data'] if 'toggle_data' in light_data else {} }}"
And create dynamic trigger
trigger:
- if:
- condition: template
value_template: "{{ toggle_data }}"
then:
{% for entity_id in toggle_data %}
- id: toggle
platform: state
entity_id: "{{ entity_id }}"
to: "{{ toggle_data[entity_id] }}"
{% endfor %}
What I am doing wrong?
Full code
blueprint:
name: Controller Light
description: Turn on a light when motion is detected.
domain: automation
input:
entity_light:
name: Light
selector:
target:
entity:
domain: light
entity_motion:
name: Motion Sensor
selector:
entity:
domain: binary_sensor
device_class: motion
delay_motion:
name: Delay
description: Time to leave the light on after last motion is detected.
default: 120
selector:
number:
min: 0
max: 3600
unit_of_measurement: seconds
light_data:
name: Light data
default:
turn_on: {}
turn_off: {}
toggle: {}
brightness: {}
mode: restart
max_exceeded: silent
variables:
light_data: !input light_data
toggle_data: "{{ light_data['toggle_data'] if 'toggle_data' in light_data else {} }}"
turn_on_data: "{{ light_data['turn_on_data'] if 'turn_on_data' in light_data else {} }}"
turn_off_data: "{{ light_data['turn_off_data'] if 'turn_off_data' in light_data else {} }}"
brightness_data: "{{ light_data['brightness_data'] if 'brightness_data' in light_data else {} }}"
trigger:
- id: motion_on
platform: state
entity_id: !input entity_motion
from: "off"
to: "on"
- id: motion_off
platform: state
entity_id: !input entity_motion
from: "on"
to: "off"
for:
seconds: !input delay_motion
- if:
- condition: template
value_template: "{{ toggle_data }}"
then:
{% for entity_id in toggle_data %}
- id: toggle
platform: state
entity_id: "{{ entity_id }}"
to: "{{ toggle_data[entity_id] }}"
{% endfor %}
action:
- if:
- condition: trigger
id: motion_on
then:
- service: light.turn_on
target: !input entity_light
- if:
- condition: trigger
id: motion_off
then:
- service: light.turn_off
target: !input entity_light
- if:
- condition: trigger
id: toggle
then:
- service: light.toggle
target: !input entity_light
Error: while scanning for the next token found character ‘%’ that cannot start any token in “/config/blueprints/automation/kostya/controller_light.yaml”, line 66, column 8
Any Ideas how to fix this?
I have decided to try a different approach.
To listen for all actions using single trigger
And check if they exist in my config
I have defined action_data
action_data:
sensor.switch_test_action:
single: toggle
And update automation the following way:
variables:
action_data: !input action_data
action_entity: "{{ action_data.keys() | list }}"
trigger:
- id: action
platform: state
entity_id: "{{ action_entity }}"
action:
- variables:
entity_trigger: "{{ trigger.entity_id }}"
state_trigger: "{{ trigger.to_state.state }}"
actions: "{{ action_data[entity_trigger] }}"
- if:
- condition: trigger
id: action
- condition: template
value_template: "{{ entity_trigger in actions }}"
then:
- service: "{{ 'light.' + actions[state_trigger] }}"
target: !input entity_light
But I am still getting an error
Need help!
Entity {{ action_entity }} is neither a valid entity ID nor a valid UUID for dictionary value @ data['entity_id']. Got None