Hi all,
I have several temperature sensors, and I’d like to calculate the minimum of a variable set of them.
I have an input_boolean for each sensor which appear as a list of toggles in the lovelace card. The expected behaviour is that output of the sensor template is the minimum of all the temperature sensors for which the associated input_boolean is ‘on’ for. If they are all off, it should default the temperature of [sensor 1].
The only way I can see of doing it so far, is to have a separate template for each sensor, which outputs either the value of the sensor if the associated boolean is on, or 1000 if it’s off. Then another template which outputs the minimum of all the previous templates, then a final template which outputs the value of the minimum one, or the value of [sensor 1] if it’s 1000 (ie. all booleans are ‘off’).
Any neater suggestions? I have a feeling there’s more complicated things you can do with templating I’m yet to discover…
You can use a dictionary/mapper to link the input booleans with their corresponding temperature sensors.
{%- set d = {
'input_boolean.test_bool_1': 'sensor.temperature_1',
'input_boolean.test_bool_2': 'sensor.temperature_2',
'input_boolean.test_bool_3': 'sensor.temperature_3'
}%}
{%- set ns = namespace(list = [])%}
{% for v in (expand(d.keys()) | selectattr('state','eq','on') | map(attribute='entity_id') | list) %}
{% set ns.list = ns.list + [states(d.get(v))]%}
{% endfor %}
{{ ns.list | min | default(states('sensor.temperature_1')) }}
Update 2025: This can now be accomplished a couple ways without using a loop
{%- set d_list = [
{'b':'input_boolean.test_bool_1', 't':'sensor.kitchen_temperature'},
{'b':'input_boolean.test_bool_2', 't':'sensor.dining_room_temperature'},
{'b':'input_boolean.test_bool_3', 't':'sensor.living_room_temperature'}]%}
{{ d_list | selectattr('b', 'is_state', 'on') | map(attribute='t')| map('states') | map('float') | min }}
{%- set ent_tuples = [('input_boolean.test_bool_1', 'sensor.temperature_1'),
('input_boolean.test_bool_2', 'sensor.temperature_2'),
('input_boolean.test_bool_3', 'sensor.temperature_3')]%}
{{ ent_tuples | selectattr(0, 'is_state', 'on')| map(attribute=1) | map('states') | map('float') | min }}
1 Like
That works perfectly - thanks for that! Knew there was a neater way…