Personally I changed all my MQTT yaml definitions by automations running at home assistant start when they changed the syntax for MQTT in yaml. Since I had to rewrite it anyway. The good way is to set the payload as a variable where you can use yaml syntax that looks a lot like classical yaml definitions, then do a loop like that:
automation:
- id: Parametragteleinfokitmqtt
alias: Paramétrage Teleinfokit mqtt
description: ''
mode: single
trigger:
- platform: homeassistant
event: start
condition: []
action:
- variables:
common:
device:
name: TeleInfoKit
connections: [["mac", "a8:48:fa:cd:8d:54"]]
- repeat:
for_each:
- type: sensor
unique_id: teleinfokit_papp
config:
state_topic: "teleinfokit/papp"
unit_of_measurement: "W"
name: Puissance instantanée générale
icon: mdi:power-plug
state_class: measurement
device_class: power
- type: sensor
unique_id: teleinfokit_iinst
config:
state_topic: "teleinfokit/iinst"
unit_of_measurement: "A"
name: Intensité générale
icon: mdi:power-plug
- type: sensor
unique_id: teleinfokit_base
config:
state_topic: "teleinfokit/base"
unit_of_measurement: "kWh"
name: Index compteur électricité RAW
icon: mdi:home-analytics
value_template: "{{ '{{' }} (value|float) / 1000.0 {{ '}}' }}"
state_class: total_increasing
device_class: energy
sequence:
- service: mqtt.publish
data:
topic: |
homeassistant/{{repeat.item.type}}/{{repeat.item.unique_id}}/config
payload: |
{{ dict(
common.items()|list
+ repeat.item.config.items()|list
+ [("unique_id", repeat.item.unique_id)]
) | to_json }}
You can see that in the for_each
I put settings in a config
entry that look like the YAML for MQTT configuration (in fact I copy-pasted most of it). I decided to move unique_id
out of the config because it is used in the discovery topic but I could as well have fetched it from the config and not needing to merge it afterwards.
The good part is that you can have common configuration without repeating it, and you also can use other kind of loops to create entities programmatically, as in the following (which is part of a recurring todolist implementation).
automation:
- id: parametragtodolistmqtt
alias: Paramétrage TodoList MQTT
description: ''
mode: single
trigger:
- platform: homeassistant
event: start
condition: []
action:
- repeat:
for_each:
- name: croquettes
duration: 8
- name: draps_lucas
duration: 312
- name: draps_matisse
duration: 312
- name: draps_parents
duration: 312
- name: eponges
duration: 168
- name: filtre_sable
duration: 744
sequence:
- variables:
payload:
device:
name: Todo List
identifiers: todolist
command_topic: "TodoList/Action"
payload_on: "DO {{repeat.item.name}}"
payload_off: "UNDO {{repeat.item.name}}"
state_topic: "TodoList/State/{{repeat.item.name}}"
state_on: "DONE"
state_off: "TODO"
json_attributes_topic: |
TodoList/LastDone/{{repeat.item.name}}
json_attributes_template: "
{{ '{{' }}
{ 'duration': '{{repeat.item.duration}}',
'last_done': value,
'expiry': (value|as_datetime +
timedelta(hours={{repeat.item.duration}})
).isoformat() }
| tojson
{{ '}}' }}"
unique_id: "todo_list_{{repeat.item.name}}"
name: "todo_{{repeat.item.name}}"
- service: mqtt.publish
data:
topic: |
homeassistant/switch/todo_{{repeat.item.name}}/config
payload: |
{{ payload | tojson }}