Translation help

Hi guys,

I am trying to translate the output of OpenUV using a template. I think I am not that far off but it doesn’t work. Any guidance appreciated!

  - platform: template
    sensors:
      openuv:
        friendly_name: "Indice UV"
        value_template: >-
          {% if is_state('sensor.current_uv_level', 'High') %}
          Elevé
          {% if is_state('sensor.current_uv_level', 'Low') %}
           Bas
          {% endif %}

Formatting is critical in yaml.
Please edit your post, highlight your code, then press the code formatting button </> so we can see the formatting.

Also include any errors given when you run the configuration check.

That’s the error message I am receiving:

Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: Unexpected end of template. Jinja was looking for the following tags: ‘elif’ or ‘else’ or ‘endif’. The innermost block that needs to be closed is ‘if’.) for dictionary value @ data[‘sensors’][‘openuv’][‘value_template’]. Got "{% if is_state(‘sensor.current_uv_level_2’, ‘High’) %} Elevé {% if is_state(‘sensor.current_uv_level_2’, ‘Low’) %}\n Bas\n{% endif %} ". (See ?, line ?). Please check the docs at Template - Home Assistant

you got 2 if’s in there with 1 endif. The error is telling you what you did wrong btw, you just have to read the error. I’ve found that most people don’t actually read the error, instead come here and post. Let me tell you, it will save you a ton of time if you just read the error. The errors have gotten much better.

It’s telling you that it can’t find a ‘elif’ or ‘else’ or ‘endif’ where it needs to find one. Then it even tells you what it did find, ‘if’.

So if it found an ‘if’ but it can’t figure out the template, what is it looking for next? This is your current template:

if
if
endif

What in that template looks wrong? Does that follow the if->elif->else->endif flow? No, it does not.

Judging by the code, I would assume you’d want your second if statement to be elif. But be warned, that platform outputs 5 states and you only have 2 covered. This sensor will be blank for the other 3uv levels.

  - platform: template
    sensors:
      openuv:
        friendly_name: "Indice UV"
        value_template: >-
          {% if is_state('sensor.current_uv_level', 'High') %}
          Elevé
          {% elif is_state('sensor.current_uv_level', 'Low') %}
           Bas
          {% endif %}
1 Like