Sensor displaying history as string when value is an Integer

I have coerced the value of this sensor into an Integer in every way possible. But for some reason beyond me it is displaying the history of the sensor as a string. What am I not understanding here?

Here is my template sensor:

- name: "AQI 2H Average"
  unique_id: aqi_2h_avg
  state: >-
    {% macro aqi(val, val_l, val_h, aqi_l, aqi_h) -%}
      {{ (((aqi_h-aqi_l)/(val_h - val_l) * (val - val_l)) + aqi_l) | round(0) | int(0) }}
    {%- endmacro %}

    {% set v = states('sensor.average_outdoor_pm2_5_2h')| float(0) | round(1) %}

    {% if v <= 12.0 %}
       {{ aqi(v, 0, 12.0, 0, 50) | int(0) }}
    {% elif 12.0 < v <= 35.4 %}
       {{ aqi(v, 12.1, 35.4, 51, 100) | int(0) }}
    {% elif 35.4 < v <= 55.4 %}
       {{ aqi(v, 35.5, 55.4, 101, 150) | int(0) }}
    {% elif 55.4 < v <= 150.5 %}
       {{ aqi(v, 55.5, 150.4, 151, 200) | int(0) }}
    {% elif 150.4 < v <= 250.4 %}
       {{ aqi(v, 150.4, 250.4, 201, 300) | int(0) }}
    {% elif 250.5 < v <= 500.4 %}
       {{ aqi(v, 250.5, 500.4, 301, 500) | int(0) }}
    {% else %}
       500 | int(0)
    {% endif %}

Here is how the history displays

Yes I know I’m calling int(0) in too many places

Give it a unit_of_measurement:

1 Like

it doesn’t allow a unit_of_measurement of aqi, so what should I set it to?

nevermind I’m an idiot it does allow unit_of_measurement: aqi. I think I tried that with a device_class: aqi and it was causing issues. Thank you Tom, you’ve been answering my questions through the years like the saint that you are.

2 Likes