How to get conditional statement working (If, elif etc)

This topic appears lots of times but i wasnt able to get mine working.

- state_topic: "sensor/InverterMode"
      name: Inverter Mode
      state_class: measurement
      device_class: enum
      value_template: >-
       {% if sensor.inverter_mode=="0" %} On Grid
       {% elif sensor.inverter_mode=="3" %} Standby
       {% endif %}

InverterMode comes via MQTT from my solar inverter and I need to convert the number to Text.
However the result is “unknown”

What am i doing wrong?

Try this:

- state_topic: "sensor/InverterMode"
      name: Inverter Mode
      state_class: measurement
      device_class: enum
      value_template: >-
       {% if states('sensor.inverter_mode')=="0" %} On Grid
       {% elif states('sensor.inverter_mode')=="3" %} Standby
       {% endif %}

You might need an else in case it is not 0 or 3, change xxxxxx for whatever:

- state_topic: "sensor/InverterMode"
      name: Inverter Mode
      state_class: measurement
      device_class: enum
      value_template: >-
       {% if states('sensor.inverter_mode')=="0" %} On Grid
       {% elif states('sensor.inverter_mode')=="3" %} Standby
       {% else %} xxxxxxx
       {% endif %}

Thanks for your reply!
I used the following which covers all possible states according to the manual

- state_topic: "sensor/InverterMode"
      name: Inverter Mode
      state_class: measurement
      #device_class: enum
      value_template: >-
       {% if states('sensor.inverter_mode')=="0" %} Initial
       {% elif states('sensor.inverter_mode')=="3" %} On Grid
       {% elif states('sensor.inverter_mode')=="1" %} Standby
       {% elif states('sensor.inverter_mode')=="4" %} Off Grid
       {% elif states('sensor.inverter_mode')=="5" %} Fault
       {% elif states('sensor.inverter_mode')=="9" %} Shutdown
       {% else %} undefined
       {% endif %}

However instead of the option 3 - ‘On Grid’ which is the correct state , I get ‘undefined’ which means that none of the If statements is valid. So there is something wrong with the comparison…

If you paste

{{ states('sensor.inverter_mode') }}

in the template editor what do you get ?

Is the Result type a string or number ?

Also, not sure you need the state_class: measurement as the output of the sensor will be a string

Its a string. It also says undefined

That’s because you’ve made a circular reference: the template is looking at the state of the sensor you’re trying to update! You should be using value in the template to refer to the MQTT topic contents, assuming that topic contains just a single number:

- state_topic: "sensor/InverterMode"
  name: Inverter Mode
  value_template: >-
    {% if value == "0" %} Initial
    {% elif value == "3" %} On Grid
    {% elif value == "1" %} Standby
    {% elif value == "4" %} Off Grid
    {% elif value == "5" %} Fault
    {% elif value == "9" %} Shutdown
    {% else %} undefined
    {% endif %}

Well spotted, I assumed it was a template sensor…

    - state_topic: "sensor/InverterMode"
      name: Inverter Mode
      state_class: measurement
      value_template: >
        {{ {'0': 'Initial', '1': 'Standby',
            '3': 'On Grid', '4': 'Off Grid',
            '5': 'Fault', '9': 'Shutdown'}.get(value, 'undefined') }}
1 Like

That worked! Thanks very much for your help!

1 Like

Hi again…
Had to return to the same topic as my setup stopped working after system core updated to the latest version couple of days ago.
The value_template shown above now gives “unknown” ! Any ideas what changed?

Please paste the complete sensor configuration as you now have it, plus the MQTT message for that topic, plus any relevant log entries.

As a random guess based on other topics, have you inserted a unit_of_measurement into the sensor?

Hmm no! There is no unit of measurement…
This is what I have :

    - state_topic: "sensor/InverterMode"
      name: Inverter Mode
      state_class: measurement 
      
      #device_class: enum
      value_template: >-
       {% if   value=="0" %} Initial
       {% elif value=="3" %} On Grid
       {% elif value=="1" %} Standby
       {% elif value=="4" %} Off Grid
       {% elif value=="5" %} Fault
       {% elif value=="9" %} Shutdown
       {% else %} undefined
       {% endif %}

EDIT: Just tried to un-comment device_class: enum and it worked again!