I’m trying to create template to list sensors (friendly names of the sensors) with states: ‘low’ or <= 20.0
Sensor match is working but there is problem with adding matched sensors to the comma separated list
Code
{% set sensors_to_check = [
'sensor.contact_sensor_battery',
'sensor.entrance_door_battery',
'sensor.mateusz_s_door_battery',
'sensor.shower_door_contact_sensor_battery',
'sensor.shower_motion_battery_state',
'sensor.shower_sensor_battery',
'sensor.motion_sensor_sink_battery_state',
'sensor.t_h_sensor_battery_state',
'sensor.motion_sensor_toilet_battery_state',
'sensor.tomasz_s_door_battery'
] %}
{% set string_sensors = [
'sensor.shower_motion_battery_state',
'sensor.motion_sensor_sink_battery_state',
'sensor.t_h_sensor_battery_state',
'sensor.motion_sensor_toilet_battery_state'
] %}
{% set numeric_sensors = sensors_to_check | reject('in', string_sensors) %}
{# Define thresholds and matching criteria #}
{% set numeric_threshold = 20.0%}
{% set string_match = 'low' %}
{# Initialize the list for matching sensor names #}
{% set matching_sensors = [] %}
{# Collect matching sensors for string sensors #}
{% set string_matches = [] %}
{% for sensor in string_sensors %}
{% set sensor_state = states(sensor) | string | trim %}
{% if sensor_state == string_match %}
{% set string_matches = string_matches + [sensor.split('.')[1]] %}
{{ sensor }}
{% endif %}
{% endfor %}
{# Collect matching sensors for numeric sensors #}
{% set numeric_matches = [] %}
{% for sensor in numeric_sensors %}
{% set sensor_state = states(sensor) | string | trim %}
{% set sensor_value = sensor_state | float(0) %}
{% if sensor_value <= numeric_threshold %}
{% set numeric_matches = numeric_matches + [sensor.split('.')[1]] %}
{{ sensor }}
{% endif %}
{% endfor %}
{# Combine and format matching sensor names #}
{% set all_matches = string_matches + numeric_matches %}
{% set formatted_matches = all_matches | join(', ') %}
{# Output the results #}
Matching Sensors:
{% if true %}
{{ formatted_matches }}
{% else %}
Off
{% endif %}
Debug code
{% set sensors_to_check = [
'sensor.contact_sensor_battery',
'sensor.entrance_door_battery',
'sensor.mateusz_s_door_battery',
'sensor.shower_door_contact_sensor_battery',
'sensor.shower_motion_battery_state',
'sensor.shower_sensor_battery',
'sensor.motion_sensor_sink_battery_state',
'sensor.t_h_sensor_battery_state',
'sensor.motion_sensor_toilet_battery_state',
'sensor.tomasz_s_door_battery'
] %}
{% set string_sensors = [
'sensor.shower_motion_battery_state',
'sensor.motion_sensor_sink_battery_state',
'sensor.t_h_sensor_battery_state',
'sensor.motion_sensor_toilet_battery_state'
] %}
{% set numeric_sensors = sensors_to_check | reject('in', string_sensors) %}
{# Define thresholds and matching criteria #}
{% set numeric_threshold = 100.0 %}
{% set string_match = 'high' %}
{# Initialize the list for matching sensor names #}
{% set matching_sensors = [] %}
{# Collect matching sensors for string sensors #}
{% set string_matches = [] %}
{% for sensor in string_sensors %}
{% set sensor_state = states(sensor) | string | trim %}
{% if sensor_state == string_match %}
{% set string_matches = string_matches + [sensor.split('.')[1]] %}
{{ sensor }}
{% endif %}
{% endfor %}
{# Collect matching sensors for numeric sensors #}
{% set numeric_matches = [] %}
{% for sensor in numeric_sensors %}
{% set sensor_state = states(sensor) | string | trim %}
{% set sensor_value = sensor_state | float(0) %}
{% if sensor_value <= numeric_threshold %}
{% set numeric_matches = numeric_matches + [sensor.split('.')[1]] %}
{{ sensor }}
{% endif %}
{% endfor %}
{# Combine and format matching sensor names #}
{% set all_matches = string_matches + numeric_matches %}
{% set formatted_matches = all_matches | join(', ') %}
{# Output the results #}
Matching Sensors:
{% if true %}
{{ formatted_matches }}
{% else %}
Off
{% endif %}
Debug output
sensor.shower_motion_battery_state
sensor.motion_sensor_sink_battery_state
sensor.motion_sensor_toilet_battery_state
sensor.contact_sensor_battery
sensor.entrance_door_battery
sensor.mateusz_s_door_battery
sensor.shower_door_contact_sensor_battery
sensor.shower_sensor_battery
sensor.tomasz_s_door_battery
Matching Sensors:
Sorry for any errors English isn’t my first language and i’m new to the forum