I have a question related to creating a for loop to declare sensors. For example, I have several door/window sensors and have a dedicated page that shows current battery level percentage. It seems rather inefficient to declare each one individually when I can filter out the devices I need (see snippet below). Is there a way to transfer this “query” to instantiate sensors? I’m pretty new to this stuff so any help/direction would be greatly appreciated.
Loop to gather the sensors I need that have a battery
{% for state in states.zwave %}
{% if state.attributes.battery_level is defined %}
{{ state.entity_id}} = {{ state.attributes.battery_level }}
{% endif %}
{%- endfor -%}
Sensor declaration
- platform: template
sensors:
first_sliding_door_battery_level:
friendly_name: 'First Floor Sliding Door - Battery'
value_template: '{{ states.zwave.first_sliding_door.attributes.battery_level }}'
unit_of_measurement: '%'