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…