How to make long list of MQTT binary sensor entries more concise

Hi,

I have a long, long list (several hundred) of similar MQTT binary_sensor entities that share a lot of the configuration variables (like device_class and payload_on/off - there are actually many more shared parameters that I omit here for clarity):

- name: Water Hazard FTA
  state_topic: "building/rss/BIS/WaterHazard/FTA/Safe"
  device_class: problem
  payload_off: True
  payload_on: False
- name: Water Hazard Vault
  state_topic: "building/rss/BIS/WaterHazard/Vault/Safe"
  device_class: problem
  payload_off: True
  payload_on: False

Is there any (easy) way of making this list more concise, e.g. by declaring some kind of new type of MQTT binary sensor, that sets default options for parameters like device_class and payload_on/off so I could just avoid copy/pasting these for every entry?

Ideally I would just end up with a list like this:

- name: Water Hazard FTA
  state_topic: "building/rss/BIS/WaterHazard/FTA/Safe"
- name: Water Hazard Vault
  state_topic: "building/rss/BIS/WaterHazard/Vault/Safe"

Or perhaps something this:

- name: Water Hazard FTA
  state_topic: "building/rss/BIS/WaterHazard/FTA/Safe"
  type: my_custom_mqtt_binary_sensor
- name: Water Hazard Vault
  state_topic: "building/rss/BIS/WaterHazard/Vault/Safe"
  type: my_custom_mqtt_binary_sensor

You may wish to consider using YAML Anchors and Aliases.

Example:

Great, that was exactly what I was looking for but couldn’t find. Thank you!

1 Like