Template sensor, value_template, find max int in a list

I have four sensors with integer state value. What I am trying is to make is a sensor getting the highest value of the four sensors.

  - platform: template
        friendly_name: "Air index"
        value_template: '{%set mylist = "0",states('sensor.air_pm_25'),states('sensor.air_pm_10'),states('sensor.air_co'),states('sensor.air_no2')%}
                                            {{ max(mylist)|int }}'

Could someone please help me out.

For others trying the same. This works:

value_template: '{%set mylist = states("sensor.air_pm_25"),states("sensor.air_pm_10"),states("sensor.air_co"),states("sensor.air_no2")%} {{ mylist|max |int }}'

1 Like

Sorry, but this won’t work correctly in all cases, because you’re comparing strings instead of numbers. E.g., try this:

{% set mylist = '200','40' %}
{{ mylist|max|int }}

The answer is 40, not 200, which is correct for string comparison, but not if you want to compare the numeric values.

I would suggest this instead:

value_template: >
  {%set mylist = states("sensor.air_pm_25")|int,
                 states("sensor.air_no2")|int %}
  {{ mylist|max }}'

Or just use the min/max sensor.


Huh. I thought that was only applicable to one sensor. Seems it will take the min and max from a list of sensors.

Yup. That’s the sensor to use. Lesson learned, always check for a standard sensor.


I whant to make something like that but for energy, but i whant to pick the max Watts then turn it off, but how can i find the entity_id of the max ?

Check out this

1 Like