Number -> Description: How?

Hi,

I’m trying to convert a number range in to a text based description of what that range means.

But I’m struggling to get the syntax right, this is what I have in my configuration.yaml

Any pointers as to where I’m going wrong?

Thanks!

  - platform: template
    name: "Bedroom IAQ"
    icon: "mdi:air-filter"
    lambda: |-
      if (sensor.dustsensor1_iaq) <= 6) {
        return {"Unhealty"};
      }
      else if (sensor.dustsensor1_iaq) <= 9) {
        return {"Poor"};
      }
      else if (sensor.dustsensor1_iaq <= 12) {
        return {"Moderate"};
      }
      else if (sensor.dustsensor1_iaq <= 14) {
        return {"Good"};
      }
      else if (sensor.dustsensor1_iaq > 14) {
        return {"Excellent"};
      }

Format your if and else if statements like this:

    if id(sensor.dustsensor1_iaq).state <= 6 {
      return {"Unhealty"};
    }

Thanks for the reply…

Getting closer - but it doesn’t like the name attribute…

Invalid config for [sensor.template]: [name] is an invalid option for [sensor.template]. Check: sensor.template->name. (See ?, line ?). 
  - platform: template
    name: "Bedroom IAQ"
    icon: "mdi:air-filter"
    lambda: |-
      if id(sensor.dustsensor1_iaq).state <= 6 {
        return {"Unhealty"};
        }
      else if id(sensor.dustsensor1_iaq).state >= 6) {
        return {"Poor"};
      }
      return {};
    update_interval: 60s

I think you have to use a template text sensor. A template sensor is probably expecting a number.

Edit: yeah that’s your problem. From the template sensor docs, you can only return a floating point number, NAN, or no new state {}. You need to use the template text sensor. Which fortunately is just a matter of putting what you have under text_sensor: instead of sensor: