Setup reusable template for mqtt device

Hi,
I’m new at this, hence I apologize for my ignorant questions in advance.

I will be setting up multiple Shelly EM3 devices and I’m trying to figure out how to use single configuration file and not to repeat the sensor information manually for every sensor.

This is my configuration so far.

mqtt:
  sensor:
  - name: "Shelly 3EM Shed - Phase A - Energy"
    value_template: "{{ value|float / 60000 }}" # Wm to kWh
    unit_of_measurement: 'kWh'
    expire_after: 86400
    device_class: energy
    state_class: total_increasing
    qos: 1
    state_topic: "shellies/shellyem3-shed/emeter/0/energy"
    unique_id: "shellyem3-shed-phase-a-energy"
    device:
      identifiers: "shellyem3-shed"
      manufacturer: "Shelly Group PLC"
      name: "Shelly EM3 Shed"

  - name: "Shelly 3EM Shed - Phase A - Returned Energy"
    value_template: "{{ value|float / 60000 }}"
    unit_of_measurement: 'kWh'
    expire_after: 86400
    device_class: energy
    state_class: total_increasing
    qos: 1
    state_topic: "shellies/shellyem3-shed/emeter/0/returned_energy"
    unique_id: "shellyem3-shed-phase-a-returned-energy"
    device:
      identifiers: "shellyem3-shed"
      manufacturer: "Shelly Group PLC"
      name: "Shelly EM3 Shed"

  - name: "Shelly 3EM Shed - Phase A - Total Energy"
    value_template: "{{ value|float / 60000 }}"
    unit_of_measurement: 'kWh'
    expire_after: 86400
    device_class: energy
    state_class: total_increasing
    qos: 1
    state_topic: "shellies/shellyem3-shed/emeter/0/total_energy"
    unique_id: "shellyem3-shed-phase-a-total-energy"
    device:
      identifiers: "shellyem3-shed"
      manufacturer: "Shelly Group PLC"
      name: "Shelly EM3 Shed"

  - name: "Shelly 3EM Shed - Phase A - Total Returned Energy"
    value_template: "{{ value|float / 60000 }}"
    unit_of_measurement: 'kWh'
    expire_after: 86400
    device_class: energy
    state_class: total_increasing
    qos: 1
    state_topic: "shellies/shellyem3-shed/emeter/0/total_returned"
    unique_id: "shellyem3-shed-phase-a-total-returned"
    device:
      identifiers: "shellyem3-shed"
      manufacturer: "Shelly Group PLC"
      name: "Shelly EM3 Shed"

  - name: "Shelly 3EM Shed - Phase A - Power"
    unit_of_measurement: 'W'
    expire_after: 86400
    device_class: power
    qos: 1
    state_topic: "shellies/shellyem3-shed/emeter/0/power"
    unique_id: "shellyem3-shed-phase-a-power"
    device:
      identifiers: "shellyem3-shed"
      manufacturer: "Shelly Group PLC"
      name: "Shelly EM3 Shed"

  - name: "Shelly 3EM Shed - Phase A - Voltage"
    unit_of_measurement: 'V'
    expire_after: 86400
    device_class: voltage
    qos: 1
    state_topic: "shellies/shellyem3-shed/emeter/0/voltage"
    unique_id: "shellyem3-shed-phase-a-voltage"
    device:
      identifiers: "shellyem3-shed"
      manufacturer: "Shelly Group PLC"
      name: "Shelly EM3 Shed"

  - name: "Shelly 3EM Shed - Phase A - Current"
    unit_of_measurement: 'A'
    expire_after: 86400
    device_class: current
    qos: 1
    state_topic: "shellies/shellyem3-shed/emeter/0/current"
    unique_id: "shellyem3-shed-phase-a-current"
    device:
      identifiers: "shellyem3-shed"
      manufacturer: "Shelly Group PLC"
      name: "Shelly EM3 Shed"

So far sensors only for on phase has been set up. Is there a way to loop trough array of phases instead of repeating all this multiple times? Also would be nice to set up this template so it could be reused for multiple devices by passing identifiers and topic prefix.

Please point me in the right direction.

I believe what you’re looking for is the concept of YAML Anchors and Aliases. I posted an example here:


Here’s the general idea. You can complete the remaining sensor configurations.

mqtt:
  sensor:
  - name: "Shelly 3EM Shed - Phase A - Energy"
    state_topic: "shellies/shellyem3-shed/emeter/0/energy"
    unique_id: "shellyem3-shed-phase-a-energy"
    <<: &shellyem3
      value_template: "{{ value|float / 60000 }}" # Wm to kWh
      unit_of_measurement: 'kWh'
      expire_after: 86400
      device_class: energy
      state_class: total_increasing
      qos: 1
      device:
        identifiers: "shellyem3-shed"
        manufacturer: "Shelly Group PLC"
        name: "Shelly EM3 Shed"

  - name: "Shelly 3EM Shed - Phase A - Returned Energy"
    state_topic: "shellies/shellyem3-shed/emeter/0/returned_energy"
    unique_id: "shellyem3-shed-phase-a-returned-energy"
    <<: *shellyem3

  - name: "Shelly 3EM Shed - Phase A - Total Energy"
    state_topic: "shellies/shellyem3-shed/emeter/0/total_energy"
    unique_id: "shellyem3-shed-phase-a-total-energy"
    <<: *shellyem3

  - name: "Shelly 3EM Shed - Phase A - Total Returned Energy"
    state_topic: "shellies/shellyem3-shed/emeter/0/total_returned"
    unique_id: "shellyem3-shed-phase-a-total-returned"
    <<: *shellyem3

Any progress to report?