Sensor in "Unknown" although in Template in Developer Tools it shows up normally

In my config.yaml I have the following sensor for AQI calculation:

  - platform: template
    sensors:
      aqistation_aqi:
        friendly_name: "AQI Sensor"
        value_template: >
          {% set pm2_5_str = states('sensor.aqistation_pms3003_pm2_5') %}
          {% set pm2_5 = pm2_5_str.split(' ')[0] | float(0) %}
          {% set pm10_str = states('sensor.aqistation_pms3003_pm10') %}
          {% set pm10 = pm10_str.split(' ')[0] | float(0) %}
          {% set co = states('sensor.aqistation_gasppm_co') | float(0) %}
          {% set no2 = states('sensor.aqistation_gasppm_no2') | float(0) %}
          {% set nh3 = states('sensor.aqistation_gasppm_nh3') | float(0) %}

          # Calculation for PM2.5 AQI (based on EPA AQI formula)
          {% if pm2_5 <= 12 %}
            {% set aqi_pm2_5 = ((50-0)/(12-0))*(pm2_5-0) + 0 %}
          {% elif pm2_5 <= 35.4 %}
            {% set aqi_pm2_5 = ((100-51)/(35.4-12.1))*(pm2_5-12.1) + 51 %}
          {% elif pm2_5 <= 55.4 %}
            {% set aqi_pm2_5 = ((150-101)/(55.4-35.5))*(pm2_5-35.5) + 101 %}
          {% elif pm2_5 <= 150.4 %}
            {% set aqi_pm2_5 = ((200-151)/(150.4-55.5))*(pm2_5-55.5) + 151 %}
          {% else %}
            {% set aqi_pm2_5 = 300 %}
          {% endif %}
          
          # Calculation for PM10 AQI (based on EPA AQI formula)
          {% if pm10 <= 54 %}
            {% set aqi_pm10 = ((50-0)/(54-0))*(pm10-0) + 0 %}
          {% elif pm10 <= 154 %}
            {% set aqi_pm10 = ((100-51)/(154-55))*(pm10-55) + 51 %}
          {% elif pm10 <= 254 %}
            {% set aqi_pm10 = ((150-101)/(254-155))*(pm10-155) + 101 %}
          {% elif pm10 <= 354 %}
            {% set aqi_pm10 = ((200-151)/(354-255))*(pm10-255) + 151 %}
          {% else %}
            {% set aqi_pm10 = 300 %}
          {% endif %}

          # Example AQI CO calculation (not EPA-based)
          {% set aqi_co = co * 50 %}  # Placeholder, adjust based on actual AQI calculations for CO

          # Example AQI NO2 calculation (not EPA-based)
          {% set aqi_no2 = no2 * 40 %}  # Placeholder, adjust based on actual AQI calculations for NO2

          # Example AQI NH3 calculation (not EPA-based)
          {% set aqi_nh3 = nh3 * 30 %}  # Placeholder, adjust based on actual AQI calculations for NH3

          # Return the maximum AQI among all pollutants
          {{ [aqi_pm2_5, aqi_pm10, aqi_co, aqi_no2, aqi_nh3] | max | float }}

Works like a charm in Template test in Dev tools, but shows “Unknown” in lovelace.
I split “µg/m³” from numbers in PM10 and PM2.5 sensors, the rest comes in float. Anyway, I float it repeatedly, but it doesn’t seem to be the culprit though…

Please help to sort things out.

Are you using those YAML comments in your template?

You need jinja comments. {# to open and #} to close the comment.

{# Calculation for PM2.5 AQI (based on EPA AQI formula) #}

You might also want to use -1 as the default in your floats so you can tell the difference between an error (such as unknown value) and an actual value of zero. Just good programming practice.

Thanks! Yes, it was due to comments inside Jinja… :laughing: