# How to get the minimum of a variable set of sensors?

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')) }}
``````

That works perfectly - thanks for that! Knew there was a neater way…