Help with simple IF-ELSE-THEN

Hi all,

i created a template helper in which I have the following to retrieve the value of an attribute:
{{ state_attr(“sensor.sensorname_electricity_price”,“price_level”) }}

this works fine
now I want to add an IF statement to it to achieve the following:
if value = CHEAP then Goedkoop else Duur

what is the correct syntax for this?

or is there a better way to achieve this considering it’s a translation to local language that I am trying to achieve.

Not sure what you mean by this. Perhaps you mean:

{{ "Goedkoop" if state_attr("sensor.sensorname_electricity_price", "price_level") == "CHEAP" else "Duur" }}

You can try this in Developer Tools / Template:

It’s returning Duur for me as I don’t have your sensor.

Many thanks! that worked!
how would the syntax look if multiple conditions were added?

There are a few options. Simple if / elif / else, reading the sensor into a variable first to keep it short and neat:

{% set p = state_attr("sensor.sensorname_electricity_price", "price_level") %}
{% if p == "CHEAP" %}
  Goedkoop
{% elif p == "EXPENSIVE" %}
  Duur
{% else %}
  Medium
{% endif %}

A lookup table:

{% set p = state_attr("sensor.sensorname_electricity_price", "price_level") %}
{% set t = {"CHEAP": "Goedkoop",
            "MEDIUM": "Medium",
            "EXPENSIVE": "Duur"} %}
{{ t.get(p, "Onbekend") }}

Developer Tools / Template is your playground, and the two links on that page (visible in my screenshot above) are the key reference documentation.

Thanks a lot!