Help with multi option template sensor

I’m really battling this one… I have a sensor set up:

sensor.esph_house_stove_eco_mode_2 is either 1 or 0 depending on the ECO mode. I can watch it change in Dev Tools when I change it on the wood stove.

I want to display the ECO mode ECO2 or ECO2 in a Lovelace card instead of 0 or 1.

my sensors.yaml has this:

  - platform: template
    sensors:
      pellet_stove_eco_mode:
        value_template: >
          {% set Pval = states(states.sensor.esph_house_stove_eco_mode_2) | int  %}
          {% if   Pval  = 0 %} "ECO1" 
          {% elif Pval  = 1 %} "ECO2" 
          {% endif %}

And i get this error when checking yaml config.

Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: expected token ‘end of statement block’, got ‘=’) for dictionary value @ data[‘sensors’][‘esph_house_eco_mode’][‘value_template’]. Got ‘{% set Pval = states(states.sensor.esph_house_stove_eco_mode_2) | int %} {% if Pval = 0 %} “ECO1” {% elif Pval = 1 %} “ECO2” {% endif %}\n’. (See ?, line ?).

what am I doing wrong???

Double the equals:



          {% if   Pval  == 0 %} "ECO1" 
          {% elif Pval  == 1 %} "ECO2" 
          {% endif %}

And it should be


  {% set Pval = states('sensor.esph_house_stove_eco_mode_2')

If you were to turn off the device representing sensor.esph_house_stove_eco_mode_2 wouldn’t the sensor’s value be reported as unavailable?

That worked so it passes the config test, but it shows as “unavailable” in dev tools after a restart. I’m not sure why.

I found it… it’s type is float. Final version that worked:

        value_template: >
          {% set Pval = states('states.sensor.esph_house_stove_eco_mode_2') | float(0)  %}
          {% if   Pval  == 0 %} ECO1
          {% elif Pval  == 1 %} ECO2
          {% endif %}

Yes, and that sensor could become unavailable if the stove goes offline, why?

Because the template you created isn’t designed to handle anything other than 1 and 0. At best, it will report 0 if the sensor’s value is unavailable (becausefloat(0) will report0 when given a non-numeric value). If that’s what you want it to do then it’s fine otherwise you’ll need to decide how it should handle unavailable.

It’s not working the way you think it is.

This is an incorrect usage of the states() function and will always report unknown.

states('states.sensor.esph_house_stove_eco_mode_2')

unknown isn’t a numeric value so float can’t convert it and will always report its default value 0.

states('states.sensor.esph_house_stove_eco_mode_2') | float(0)

This is the correct usage of states().

states('sensor.esph_house_stove_eco_mode_2') | float(0)

This is always an education. The syntax of templates has ALWAYS been confusing for folks like me who are hack programmers at best. While I appreciate the full control, I despise the time it takes to figure th3 see out.

Perhaps there is an easier way to do such a simple thing?

I also need a way to actually set those values. There are 2 items I need to set in my pellet stove, eco and power level. Both are tuya datapoints, very much like speed control on a fan. Any insight there?

I have an inverter that is able to set value’s from Lovelace using mqtt. I suspect I’ll have to figure out if I can do the same thing here.

The correct use of the states() function is explained near the beginning of the Templating documentation (includes several examples).