I have automations such as
- alias: 'lampe bureau'
trigger:
platform: mqtt
topic: rfbridge-1/rfin
condition:
condition: template
value_template: "{{ trigger.payload.endswith('415E1') }}"
action:
service: light.toggle
entity_id: light.lampe_bureau
They manage RF 433Mhz devices which messages are received by a RF-MQTT bridge which then publishes them to the MQTT broker under rfbridge-1/rfin
topic. The payload is the signal sent by the devices.
Each of the device corresponds to a wall button which manages one or several switches. The example above is for one switch.
I will end up having many of such setups - identical except for the message (which ends with 415E1
above, so that’s one variable) and makes an action on a specific entity (light.lampe_bureau
above, so that’s teh second variable).
Is there a way to simplify a set of such code blocks?
I am thinking about something like the Python code below, which defines a reference (data
) and iterates though it looking for the right condition:
rec = "CC"
data = ( ("AAA", "BBB"), ("CCC", "DDD"))
for d in data:
if d[0].endswith(rec):
print(rec[1])
(the data reference store can be more fancy (a dict) and the code above just shows the idea of grouping similar checks/actions under an iteration over the data reference)