i have a series of ble sensors and would like to create a text sensor that will tell me which sensors are unavailable if needed.
But my problem is: I can find these sensors, but the variables are deleted again after the if or the for loop, so they are empty again. what am I doing wrong?
{% set sensors = [
'sensor.klimasensor_1',
'sensor.klimasensor_2',
'sensor.klimasensor_3',
'sensor.klimasensor_4',
'sensor.klimasensor_5',
'sensor.klimasensor_6',
'sensor.klimasensor_7',
'sensor.klimasensor_8',
] %}
{% set counter = 0 %}
{% set offline_sensors = [] %}
{% for sensor in sensors %}
{% set voltage_state = states(sensor + '_voltage') %}
{% set temperature_state = states(sensor + '_temperature') %}
{% set humidity_state = states(sensor + '_humidity') %}
{% if voltage_state == 'unavailable' or temperature_state == 'unavailable' or humidity_state == 'unavailable' %}
{% set counter = counter + 1 %}
{% set offline_sensors = offline_sensors + [sensor.object_id.split('_')[1]] %}
{% endif %}
{% endfor %}
{% if counter > 0 %}
{{ 'Die folgenden Sensoren sind offline: ' + ', '.join(offline_sensors) }}
{% else %}
Alles Sensoren sind online.
{% endif %}
I have adopted your code. But the problem with the variables formulated in the title can indeed be solved by namespace. Both work, but I find the second solution easier.