Getting a string instead of a number from a value_template

Hi,

I’m relatively new to HA. I have installed the VeSync integration for Levoit air purifiers. Whilst it adds an entity for each air purifier, it only adds attributes for ‘air quality’ - it’s built-in sensor.

I added this to my configuration.yaml:

  - platform: template
    sensors:
      study_air_quality:
        friendly_name: "Study Air Quality"
        value_template: '{{ (states.fan.study_air_purifier.attributes.air_quality|int) }}'.

This creates a sensor for air purity, but the value is a string.

What am I doing wrong?

Thank you so much in advance

It’s by definition. States are always strings, but attributes can have other types. Your template looks fine (just be sure to provide int with a default value). If you’re worried how HA will interpret it e.g. when plotting the value, just make sure to se the desired device_class.

Thanks for your speedy reply. Would you mind elaborating on device_class. I am having a problem with the graph of the value showing discrete bands of data rather than a line graph.

You need to specify unit_of_measurement.

unit_of_measurement string (Optional, default: None)

Defines the units of measurement of the sensor, if any. This will also display the value based on the user profile Number Format setting and influence the graphical presentation in the history visualization as a continuous value.

Thank you both.

Adding: unit_of_measurement: "µg/m" worked a treat.