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.
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.