Reducing tasmota (or any) entity config yaml

After setting up several devices running tasmota I found I had the same config repeated many times. A way to minimise this is to use yamls Anchor and Alias pattern.

In my switches.yaml I had entries like this:

- platform: mqtt
    name: "pond_pumps"
    state_topic: "stat/pond_pumps/POWER"
    command_topic: "cmnd/pond_pumps/POWER"
    availability_topic: "tele/pond_pumps/LWT"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    retain: false

Everything from qos down was identical for each device. All of this repeated config can be defined as an anchor with & and injected where we want it with <<: *.
The one hurdle we have is getting around homeassistants yaml validation. The neatest way I found to do this was to add an empty template switch with a spurious object to hold all the repeated config. This does trigger a slightly alarming log entry “No switches loaded”, but it is just because of the empty template switch, all switches are still loaded as normal, so I can live with that.

After this my switches.yaml looks like this:

- platform: template # anchor data trojan horse
  switches: {}
  tasmota: &tasmota_mqtt
    platform: mqtt
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    retain: false

- <<: *tasmota_mqtt
  name: "pond_pumps"
  state_topic: "stat/pond_pumps/POWER"
  command_topic: "cmnd/pond_pumps/POWER"
  availability_topic: "tele/pond_pumps/LWT"

This is identical to the original config once loaded. However now additional device only requires 5 lines instead of 10 and the ‘defaults’ can be overidden at will.
Because I am using common name formats I am trying to find a way to use pyyamls !join operator to do away with the need for the topic config as well, but it doesn’t seem possible to pass the name in.

6 Likes