Working Automatic average of multiple & variable temps (based on name of sensor)

Hello
following my recent post about templating in min_max, wich is not posible, I post here a solution with pure template.
The goal is to select temperature sensor by name (so whith cleaver naming, by zone|room|…)
Supports disappearance of signal
When there is one value left at least

here we only get active sensor with ‘bureau’ in name
(the template part can be pasted directly in DEVELOPPER TOOLS
uncomment to debug)
Ps: in the end a yaml of markdown card for testing…

sensor: 
 - platform: template
    sensors:
      moyenne_2023_ex:
        unique_id: moyenne_2023_ex
        friendly_name: "moyenne automatique par zone"
        value_template: >-
          {# liste des sensor de temperature  utilisables  #}
          {% set liste = states 
          | selectattr('entity_id', 'match', 'sensor.*temp*') 
          | rejectattr('state', 'in', ['unavailable', 'unknown', 'none'])
          | selectattr('entity_id', 'match', 'sensor.*bureau*')
          | selectattr('state', 'match', "^[0-9]") 
          | map(attribute='state')
          | list
          -%}
          {# {{ liste }} #}
          {% if liste|count > 0 %}
          {%    set ns = namespace(a_max=0) %}
          {%    for dev in liste -%}
          {%      set ns.a_max=ns.a_max + ( dev | float)  %}
          {#  {{- dev}} ...{{ns.a_max}} #}
          {%    endfor -%}
          {%  set moyenne=ns.a_max/(liste|count) %}
          {# {{moyenne|float(2)}}  KO #}
          {{ "%.2f"|format(moyenne) }}
          {% else %}
          {# ERROR...no value #}
          {{'ERROR'}}
          {% endif %} 
# type: markdown
# content: |
#   *---------------- MOYENNE --------------------*
#   {{states('sensor.moyenne_2023_ex')}}
# title: Moyenne automatique.

Hope it can help.