Hello everyone
I have many switches which are triggered through a specific MQTT message. I know how to build an automation of the kind “if the MQTT payload is X, then toggle switch Y”.
Since there are many switches, I end up with a lot of blocks which are identical, except for these two information.
Is there a way to define the pairs (payload, corresponding switch) and
- have one block surrounded by a loop over these values – effectively building n automation blocks
- or use a dictionary inside a block?
In pseudocode (Python and Jinja like), the first case would be something like
{% switches = [(A, B), (C, D), (E, F)] %}
{% for payload, switch in switches %}
trigger:
- platform: mqtt
topic: rfbridge-1/rfin
condition:
- condition: template
value_template: '{{ %payload% in trigger.payload }}'
action:
- data:
entity_id: switch.%switch%
service: switch.toggle
{% endfor %}
The second case is just one block, there is no need for iteration:
{% switches = {A: B, C: D, E: F} %+
trigger:
- platform: mqtt
topic: rfbridge-1/rfin
action:
- data:
entity_id: switch.%switch[trigger.payload]%
service: switch.toggle
In other words: do I have to list very similar blocks, or is there a way to use a variable to have them “virtually built” by HA?