Numeric sensor values are always strings

I have the following template sensor:

       lux_draussen:
         unit_of_measurement: "lx"
         device_class: illuminance
         value_template: >-
           {{ [states("sensor.lumi_lumi_sen_ill_mgl01_74b4783c_illuminance"), states("sensor.lumi_lumi_sen_ill_mgl01_98cf783c_illuminance")]|max }}   

But the sensor does not reflect the maximum of both. The reason is that the sensor values are strings and not float values. This means that 9 is greater then 10. Why are sensor values strings?

The state value of all entities is a string (but that might change in the future). In contrast, the value of an entity’s attribute can have a type other than string (integer, float, list, boolean, etc).

If you want to perform any kind of mathematical operation with an entity’s state value, you must convert it from string to number using either the int or float filter (the choice depends on the value and the desired precision).

If your illuminance sensors report their values as whole numbers then you can use the int filter (otherwise float is preferable).

       lux_draussen:
         unit_of_measurement: "lx"
         device_class: illuminance
         value_template: >-
           {{ [states("sensor.lumi_lumi_sen_ill_mgl01_74b4783c_illuminance") | int, states("sensor.lumi_lumi_sen_ill_mgl01_98cf783c_illuminance") | int] | max }}   
1 Like