Configuration templates

My configuration.yaml file is growing and I’d like to do some clean up. I’ve started with splitting it out into multiple files, but there’s still a lot of duplication. I use mostly shelly devices and they need to have various settings defined which are repeated between them. Is there a way to configure an MQTT template and then re-use that?

For example, I’d like to remove the duplication in these entries:

- platform: mqtt
  schema: template
  name: TV Top
  command_topic: "shellies/shellyrgbw2-tvtop/color/0/set"
  state_topic: "shellies/shellyrgbw2-tvtop/color/0/status"
  effect_list:
    - 0
    - 1
    - 2
  command_on_template: >
    {"turn": "on"
    {%- if brightness is defined -%}
    , "gain": {{brightness | float | multiply(0.3922) | round(0)}}
    {%- endif -%}
    {%- if red is defined and green is defined and blue is defined -%}
    , "red": {{ red }}, "green": {{ green }}, "blue": {{ blue }}
    {%- endif -%}
    {%- if white_value is defined -%}
    , "white": {{ white_value }}
    {%- endif -%}
    {%- if effect is defined -%}
    , "effect": {{ effect }}
    {%- endif -%}
    }
  command_off_template: '{"turn":"off"}'
  state_template: "{% if value_json.ison %}on{% else %}off{% endif %}"
  brightness_template: "{{ value_json.gain | float | multiply(2.55) | round(0) }}"
  red_template: '{{ value_json.red }}'
  green_template: '{{ value_json.green }}'
  blue_template: '{{ value_json.blue }}'
  white_value_template: '{{ value_json.white }}'
  effect_template: '{{ value_json.effect }}'
  qos: 2
- platform: mqtt
  schema: template
  name: TV Bottom
  command_topic: "shellies/shellyrgbw2-tvbottom/color/0/set"
  state_topic: "shellies/shellyrgbw2-tvbottom/color/0/status"
  effect_list:
    - 0
    - 1
    - 2
  command_on_template: >
    {"turn": "on"
    {%- if brightness is defined -%}
    , "gain": {{brightness | float | multiply(0.3922) | round(0)}}
    {%- endif -%}
    {%- if red is defined and green is defined and blue is defined -%}
    , "red": {{ red }}, "green": {{ green }}, "blue": {{ blue }}
    {%- endif -%}
    {%- if white_value is defined -%}
    , "white": {{ white_value }}
    {%- endif -%}
    {%- if effect is defined -%}
    , "effect": {{ effect }}
    {%- endif -%}
    }
  command_off_template: '{"turn":"off"}'
  state_template: "{% if value_json.ison %}on{% else %}off{% endif %}"
  brightness_template: "{{ value_json.gain | float | multiply(2.55) | round(0) }}"
  red_template: '{{ value_json.red }}'
  green_template: '{{ value_json.green }}'
  blue_template: '{{ value_json.blue }}'
  white_value_template: '{{ value_json.white }}'
  effect_template: '{{ value_json.effect }}'
  qos: 2

Ideally, I would like to do something like this:

- platform: configTemplate
  schema: template
  name: MqttRgbw
  command_topic: "shellies/shellyrgbw2-<namevariable>/color/0/set"
  state_topic: "shellies/shellyrgbw2-<namevariable>/color/0/status"
  effect_list:
    - 0
    - 1
    - 2
  command_on_template: >
    {"turn": "on"
    {%- if brightness is defined -%}
    , "gain": {{brightness | float | multiply(0.3922) | round(0)}}
    {%- endif -%}
    {%- if red is defined and green is defined and blue is defined -%}
    , "red": {{ red }}, "green": {{ green }}, "blue": {{ blue }}
    {%- endif -%}
    {%- if white_value is defined -%}
    , "white": {{ white_value }}
    {%- endif -%}
    {%- if effect is defined -%}
    , "effect": {{ effect }}
    {%- endif -%}
    }
  command_off_template: '{"turn":"off"}'
  state_template: "{% if value_json.ison %}on{% else %}off{% endif %}"
  brightness_template: "{{ value_json.gain | float | multiply(2.55) | round(0) }}"
  red_template: '{{ value_json.red }}'
  green_template: '{{ value_json.green }}'
  blue_template: '{{ value_json.blue }}'
  white_value_template: '{{ value_json.white }}'
  effect_template: '{{ value_json.effect }}'
  qos: 2

And then configure both entities like this:

- platform: configTemplateImplementation
  template: MqttRgbw
  var_name: tvtop
  name: TV Top
- platform: configTemplateImplementation
  template: MqttRgbw
  var_name: tvtbottom
  name: TV Bottom