Method to simplify entity creation for duplicates?

I have a working mqtt template which creates the corresponding entity but need to duplicate it for “thermostat1” to “thermostat11”

is there a method to simplify this vs copy/pasting for all 11 thermostats?

Thanks!


    - unique_id: thermostat1_floor_control_mode
      name: thermostat1 floor control mode
      retain: true
      command_topic: "zigbee2mqtt/THERMOSTAT1/set"
      command_template: >-
        {% set options = { 'ambiant':'{"floor_control_mode": "ambiant"}', 'floor':'{"floor_control_mode": "floor"}'} %}
        {{ options[value] if value in options.keys() else 'not set' }}
      state_topic: "zigbee2mqtt/THERMOSTAT1"
      value_template: "{{ value_json.floor_control_mode }}"
      options:
        - ambiant
        - floor

    - unique_id: thermostat2_floor_control_mode
      name: thermostat2 floor control mode
      retain: true
      command_topic: "zigbee2mqtt/THERMOSTAT2/set"
      command_template: >-
        {% set options = { 'ambiant':'{"floor_control_mode": "ambiant"}', 'floor':'{"floor_control_mode": "floor"}'} %}
        {{ options[value] if value in options.keys() else 'not set' }}
      state_topic: "zigbee2mqtt/THERMOSTAT2"
      value_template: "{{ value_json.floor_control_mode }}"
      options:
        - ambiant
        - floor

etc....

The usual way of minimizing duplication of YAML is to use Anchors and Aliases.

The following post contains links to a description and several examples:

Thank you @123

Ended up with the format below;

    - name: thermostat1 floor control mode
      unique_id: thermostat1_floor_control_mode
      device:
        identifiers: THERMOSTAT1
        name: "THERMOSTAT1"
        model: "TH1300ZB"
        manufacturer: "Sinope"  
      state_topic: "zigbee2mqtt/THERMOSTAT1"
      command_topic: "zigbee2mqtt/THERMOSTAT1/set"
      <<: &floor
        command_template: >-
          {% set options = { 'ambiant':'{"floor_control_mode": "ambiant"}', 'floor':'{"floor_control_mode": "floor"}'} %}
          {{ options[value] if value in options.keys() else 'not set' }}
        value_template: "{{ value_json.floor_control_mode }}"
        options:
          - ambiant
          - floor
        retain: true
  
    - name: thermostat2 floor control mode
      unique_id: thermostat2_floor_control_mode
      device:
        identifiers: THERMOSTAT2
        name: "THERMOSTAT2"
        model: "TH1300ZB"
        manufacturer: "Sinope"  
      state_topic: "zigbee2mqtt/THERMOSTAT2"
      command_topic: "zigbee2mqtt/THERMOSTAT2/set"
      <<: *floor

    - name: thermostat3 floor control mode
      unique_id: thermostat3_floor_control_mode
      device:
        identifiers: THERMOSTAT3
        name: "THERMOSTAT3"
        model: "TH1300ZB"
        manufacturer: "Sinope"  
      state_topic: "zigbee2mqtt/THERMOSTAT3"
      command_topic: "zigbee2mqtt/THERMOSTAT3/set"
      <<: *floor


etc.....

I believe you have a pending feature request for it but we can’t use variables in HA YAML except for automations and scripts right?

would be quite useful here as I still have to repeat 6 times the same “thermostatX”…
would be nice to set variable tstat = thermostat2 for instance and anchor everything else… something in these lines:

    - variables:
        tstat: thermostat1
      <<: &floor        
        name: {{ tstat + " floor control mode" }}
        unique_id: {{ tstat + "_floor_control_mode" }}
        device:
          identifiers: {{ tstat }}
          name: {{ tstat }}
          model: "TH1300ZB"
          manufacturer: "Sinope"  
        state_topic: {{ "zigbee2mqtt/" + tstat }}
        command_topic: {{ "zigbee2mqtt/" + tsat + "/set" }}
        command_template: >-
          {% set options = { 'ambiant':'{"floor_control_mode": "ambiant"}', 'floor':'{"floor_control_mode": "floor"}'} %}
          {{ options[value] if value in options.keys() else 'not set' }}
        value_template: "{{ value_json.floor_control_mode }}"
        options:
          - ambiant
          - floor
        retain: true
  
    - variables:
        tstat: thermostat2
      <<: *floor

    - variables:
        tstat: thermostat3
      <<: *floor

etc.

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

Already done :wink:

So my assumption above is right? can’t use variables in HA YAML except for automations and scripts?

Or anything that would resemble my second code format?

I have refreshed this web page in my browser but there’s no sign of any message in this topic having a green Solution tag. Is it showing in your browser?

Screenshot_20231130-075929~2


Regarding scripting variables (not to be confused with Jinja variables), you are correct; they’re not supported in the configuration of Template entities like Template Sensor, Template Binary Sensor, Template Switch, etc.

There’s a way to define variables in a Trigger-based Template entity, in its action section, but that’s the only exception (to my knowledge).

tl:dr
They’re currently only supported by scripts and automations.

For some reason the solution tag didn’t work originally… done.

1 Like