Write better a custom template for battery

Hi to all,
I have several (10+) sensors battery powered with MySensors library.

To get the percentage (%) of the battery with related icon, I wrote these kinda of template.

- platform: template
  sensors:

    battery_percentage_myss_salone:
      value_template: >-
        {{ state_attr('sensor.salone_1_0','battery_level') }}
      icon_template: >-
        {%- set battery_level = state_attr('sensor.salone_1_0','battery_level') -%}
        {% if battery_level <= 30 %}
          mdi:battery-alert
        {% elif battery_level > 30 and battery_level <= 40 %}
          mdi:battery-30
        {% elif battery_level > 40 and battery_level <= 50 %}
          mdi:battery-40
        {% elif battery_level > 50 and battery_level <= 60 %}
          mdi:battery-50
        {% elif battery_level > 60 and battery_level <= 70 %}
          mdi:battery-60
        {% elif battery_level > 70 and battery_level <= 80 %}
          mdi:battery-70
        {% elif battery_level > 80 and battery_level <= 90 %}
          mdi:battery-80
        {% elif battery_level > 90 %}
          mdi:battery
        {% endif %}

    battery_percentage_myss_camera:
      value_template: >-
        {{ state_attr('sensor.camera_2_0','battery_level') }}
      icon_template: >-
        {%- set battery_level = state_attr('sensor.camera_2_0','battery_level') -%}
        {% if battery_level <= 30 %}
          mdi:battery-alert
        {% elif battery_level > 30 and battery_level <= 40 %}
          mdi:battery-30
        {% elif battery_level > 40 and battery_level <= 50 %}
          mdi:battery-40
        {% elif battery_level > 50 and battery_level <= 60 %}
          mdi:battery-50
        {% elif battery_level > 60 and battery_level <= 70 %}
          mdi:battery-60
        {% elif battery_level > 70 and battery_level <= 80 %}
          mdi:battery-70
        {% elif battery_level > 80 and battery_level <= 90 %}
          mdi:battery-80
        {% elif battery_level > 90 %}
          mdi:battery
        {% endif %}

You can see that it is the same for every, every sensor…

Is it possible to better organize them? My goal is to have a single template for every sensor…

Use an include file for your template (partial).

Example of something similar from my config

message: >
  {%- set ns = namespace(message='The battery level of the following device(s) are low:\n') -%}
  !include /config/include/template/low_battery_message_template.yaml

Contents of low_battery_message_template.yaml.

  {%- set t = states('input_number.battery_alert_threshold')|int -%}
  {%- set b = states.zwave|selectattr('attributes.battery_level','defined')
      |rejectattr('state','!=','ready')|selectattr('attributes.battery_level','>=',t) -%}
  {%- for i in b -%}
    {%- set ns.m = ns.m ~ '- ' ~ i.name ~ ' (' ~ '%0.0f'|format(i.attributes.battery_level|float)|string ~ '%)\n' -%}
  {%- endfor -%}
  {%- set nb = expand('group.nest_batteries')|selectattr('state','eq','Replace') -%}
  {%- for i in nb -%}{%- set ns.m = ns.m ~ '- ' ~ i.name ~ '\n' -%}{%- endfor -%}
  {{ ns.m }}

Pay attention to indenting in the include file. It matters.