Convert a value in a template sensor

Hi HA community. I want to convert a integer value in a „text“. I get no error but also doesn’t work. I want give out a text when the value of my power plug is in a certain range. That’s my approach, maybe is wrong. How can I setup properly?

  - platform: template
    sensors:
      luftstufe: 
        friendly_name: 'Air Level'
        unit_of_measurement: "level"
        value_template: >-
            {% set level = states('sensor.stecker_klima_weiss_leistung')|int (0) %}
            {% if level <= 50 %}
              one
            {% elif 50 < level <= 90 %}
              two
            {% else %}
              under construction 
            {% endif %}

Remove this line:

unit_of_measurement: "level"

If you include it, Home Assistant expects the sensor’s value to be a number but your sensor is designed to report a text value.

Wow….you are my hero of the day. Thanks for your quick help.
I was so close.

I added an icon an finalize to my application. Here is the final code

  - platform: template
    sensors:
      luftstufe: 
        friendly_name: 'Ventilator Stufe'
        icon_template: mdi:fan
        value_template: >-
            {% set power = states('sensor.stecker_klima_weiss_leistung')|int (0) %}
            {% if power <= 5 %}
              aus
            {% elif 5 < power <= 28 %}
              1
            {% elif 28 < power <= 48 %}
              2
            {% elif 48 < power <= 92 %}
              3
            {% else %}
              STÖRUNG 
            {% endif %}

You should know that it’s customary to assign the Solution tag to the post that identifies the problem and provides a correction. That’s this one.

You marked your own post with the Solution tag, after applying the correction I provided you because you didn’t know how to solve the problem.

If everyone marked their own post with the Solution tag, it would look like everyone ultimately solves their own problem … which is not true.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

2 Likes

Sorry, didn‘t know that. I tagged now the post (yours) which gives me the solution. I thought to tag the entire code is needed to show the community hoe it worked for me.
Thanks for advice!

1 Like