MQTT Climte with mode_state_template if else

Hi. Im have a problem with my climete state in mqtt
In can read and set all data, except state
My MQTT returns O if off and 1 if heat.
How can I use mode_state_tempate to correct 0 to “off” and 1 to “heat”
Here is my code:

climate:
  - platform: mqtt
    name: Ogrzewanie_Biuro
    unique_id: Ogrzewanie_Biuro
    modes:
      - "heat"
      - "off"
    mode_command_topic: "ampio/to/1E9F/rm/6/cmd"
    mode_state_topic: "ampio/from/1C19/state/o/7"
    mode_state_template: >-
      {% if value==1 %}
          "heat"
      {% else %} 
          "off"
      {% endif %}
    temperature_state_topic: "ampio/from/1E9F/state/rs/6"
    current_temperature_topic: "ampio/from/1E9F/state/t/6"
    temperature_command_topic: "ampio/to/1E9F/rs/6/cmd"
    temperature_unit: "C"
    precision: 0.5
    max_temp: 30
    min_temp: 10
    temp_step: 0.5
    send_if_off: false

Can you edit your post, and use the Code button in the editor to format your YAML correctly.
It’s the button that looks like this: </> (because until you do, we can’t tell if the indentation is correct)

EDIT: It’s possible that Home Assistant is seeing the received value as a string not a number, so either use:

value|int(0) == 1
or
value == "1"

and see if that makes a difference.

its not working,
Tried:

modes:
      - "heat"
      - "off"
    mode_command_topic: "ampio/to/1E9F/rm/6/cmd"
    mode_state_topic: "ampio/from/1C19/state/o/7"
    mode_state_template: >-
      {% if value == "1" %}
          "heat"
      {% else %} 
          "off"
      {% endif %}

and

 modes:
      - "heat"
      - "off"
    mode_command_topic: "ampio/to/1E9F/rm/1/cmd"
    mode_state_topic: "ampio/from/1C22/state/o/3"
    mode_state_template: >-
      {% if (value|int(0))==1 %}
          "heat"
      {% else %} 
          "off"
      {% endif %}

any ideas what could be wrong?
Double checked if mode_state_topic is ok (via MQTT explorer), but even if I made some error there, the template should return “off” but it dont.

I think its not a value rewrite problem. I get error in log: Invalid modes mode: “off”

The problem is due to the double-quotes used in the mode_state_template. They are are used literally and therefore the string the template produces includes them.

Change it to this:

    mode_state_template: >-
      {% if value|int(0) == 1 %}
          heat
      {% else %} 
          off
      {% endif %}

Or simply do this (quotes are not handled literally when used like this in an inline-if statement):

    mode_state_template: "{{ 'heat' if value|int(0) == 1 else 'off' }}" 
3 Likes

You are a lifesaver :slight_smile: That was it. Thanku You very much for time and help. @ Andrew Jones thanks too :stuck_out_tongue:

2 Likes

Glad it’s fixed. I should really have picked up on the quotes thing.