Create generic template

Hi,
I’m new to Home-Assistant and was wondering If it is possible to create a template that provides a sensor for every climate device that is connected. I read:

and

This is already working eg.:

"{{ state_attr('climate.wohnzimmer_nord', 'current_temperature') }}"

"21.0"

Is it possible to use somekind of wildcard like climate.*? So that I would get an additional temperature sensor entity for all my climate devices without having to write a template for every climate device.

Cheers

I’m sure there’s some yaml technique like anchors that could be used, but I’d just use templates to build templates. You could paste something like the following into the template editor, then copy the ouput and paste it into your config.

#For use in Template Editor ONLY
# DO  NOT USE DIRECTLY AS CONFIGURATION

template:
  sensors:
    {%- set clim = states.climate | map(attribute='entity_id') | list %}
    {% for c in clim %}
    - name: {{ state_attr(c, 'friendly_name') }} Temp
      state: >
        {% raw %}{{state_attr('{% endraw %}{{c}}{% raw %}', 'current_temperature')}}{%endraw%}
      unit_of_measurement: '°C'
      state_class: measurement
      availability: >
        {% raw %}{{state_attr('{%endraw%}{{c}}{%raw%}', 'current_temperature')| is_number }}{%endraw%}
  {% endfor %}
3 Likes

That was exactly what i was looking for! :+1:

@Didgeridrew Followup:

How would I implement this in configuration?

This won’t work and I don’t know what the error wants me to fix:

template:
  - sensors:
      {%- set clim = states.climate | map(attribute='entity_id') | list %}
      {% for c in clim %}
      - name: {{ state_attr(c, 'friendly_name') }} Temp
        state: {{'"{{'}}{{'state_attr(\''}}{{c}}', 'current_temperature')}}"
        unit_of_measurement: '°C'
        state_class: measurement
        availability: >
          {{'{{'}}{{-'state_attr(\''}}{{c}}', 'current_temperature')| is_number }}
      {% endfor %}
TypeError: Cannot read properties of null (reading 'tag') Error(s): - TypeError: Cannot read properties of null (reading 'tag')

The way Didgeridrew explained it:

paste something like the following into the template editor, then copy the ouput and paste it into your config

  1. Copy-paste Didgeridrew’s template into the Template Editor.
  2. The result of the template will appear on the right hand side of the Template Editor.
  3. Copy the result and paste it into your configuration.yaml file.
1 Like