[MQTT HVAC ]Template command_topic

Hello,

Do you know any way to modify the payload of the command topic in this type of thermostat?

I have seen in some old post that some used the mode_command_template parameter but it is no longer supported according to the documentation.

example of this parameter:

mode_command_template: >-
      {% set values = { 'auto':'0', 'heat':'1',  'cool':'2', 'off':'4'} %}
      {{ values[value] if value in values.keys() else '4' }}

The problem is that when I click on the flame icon, the payload that sends is heat and I need to modify it so that it sends a 1

This is my config:

climate:
  - platform: mqtt
    name: Salon
    payload_on: 1
    payload_off: 0
    modes:
      - "off"
      - "heat"
    mode_state_topic: "salon/get/Calefaccion"
    mode_state_template: >-
      {% set values = { '0':'off', '1':'heat'} %}
      {{ values[value] if value in values.keys() else 'off' }}
    mode_command_topic: "salon/set/Calefaccion"
    current_temperature_topic: "salon/get/temperatura"
    current_temperature_template: "{{float(value) / 100 }}"
    temperature_command_topic: "salon/set/Consigna"
    temperature_state_topic: "salon/get/Consigna"
    temperature_state_template: "{{float(value) / 100 }}"
    temp_step: 1
    max_temp: 28
    min_temp: 14

I believe you maybe referring to the custom_component I made for MQTT HVAC that included (among other things) support for several command templates. The official MQTT HVAC integration does not, and never has, supported command templates for mode, fan_mode, hold, etc.

The technique I currently use is documented in this post:

Here’s an example of a single automation that handles the conversion of mode, fan_mode, and hold.

Click to show automation
- alias: 'Convert Climate'
  mode: parallel
  trigger:
  - platform: mqtt
    topic: temp/climate/hvac_mode
  - platform: mqtt
    topic: temp/climate/fan_mode
  - platform: mqtt
    topic: temp/climate/hold_mode
  action:
    choose:
    - conditions: "{{ trigger.topic == 'temp/climate/hvac_mode' }}"
      sequence:
        service: mqtt.publish
        data:
          topic: thermostat/temperaturemode
          payload: >
            {% set values = { 'auto':'0', 'heat':'1',  'cool':'2',  'off':'4'} %}
            {{ values[trigger.payload] if trigger.payload in values.keys() else '4' }}

    - conditions: "{{ trigger.topic == 'temp/climate/fan_mode' }}"
      sequence:
        service: mqtt.publish
        data:
          topic: thermostat/fancontrol
          payload: >
            {% set values = { 'auto':'0', 'on':'1'} %}
            {{ values[trigger.payload] if trigger.payload in values.keys() else '0' }}

    - conditions: "{{ trigger.topic == 'temp/climate/hold_mode' }}"
      sequence:
        service: mqtt.publish
        data:
          topic: thermostat/mode
          payload: "{{ '2' if trigger.payload == 'Hold' else '0' }}"

Alternately, you can wait for the following PR to be incorporated into a future release of Home Assistant. It adds several command templates to MQTT HVAC.

Thank you very much for the clarification

Hi friend,

Today I have created the automation following the example you gave but I can’t make it work. When I click on the flame icon (heat) the payload is still heat instead of 1.

This is my automation

- alias: 'Convert Climate'
  mode: parallel
  trigger:
  - platform: mqtt
    topic: salon/set/Calefaccion
  action:
    - service: mqtt.publish
      data:
        topic: salon/set/Calefaccion
        payload: >
          {% set values = { 'off':'0', 'heat':'1' } %}
          {{ values[trigger.payload] if trigger.payload in values.keys() else '0' }

any suggestion why it doesn’t work?

Cheers

It doesn’t work because it’s using the same MQTT topic in the trigger and in the service call.

Look at my example again. The topic in the trigger is different from the topic in the service call.

Change the trigger’s topic to something different like temp/salon

- alias: 'Convert Climate'
  mode: parallel
  trigger:
  - platform: mqtt
    topic: temp/salon

Then set the climate entity’s mode_command_topic to temp/salon and restart Home Assistant.

    mode_command_topic: "temp/salon"

The idea is this:

 Climate entity -> temp/salon -> Conversion -> salon/set/Calefaccion

Thank you very much again for the clarification, after you do the topic change and correct an error in the automation (I was missing a key at the end) I have managed to make it work.

a greeting

I’m glad to hear the correction was successful and now it works.

For the benefit of other users, please consider marking my first post with the Solution tag. By doing this, it will automatically place a check-mark next to the topic’s title. This signals to other users that your problem has been resolved and has an accepted solution. This helps people find answers to similar questions.

Hello,

The solution is totally valid, but it happens to me that after a while the thermostat goes back to off, even though everything is working.

The configuration for climate.salon contains this:

    mode_state_topic: "salon/get/Calefaccion"
    mode_state_template: >-
      {% set values = { '0':'off', '1':'heat'} %}
      {{ values[value] if value in values.keys() else 'off' }}

If it fails to receive either a 0 or a 1 it sets the mode to off. You should investigate the values that are being published to salon/get/Calefaccion.

In addition, when you use climate.salon to set the mode, Home Assistant expects a prompt reply from the device, confirming the selected choice of mode. Make sure the device publishes its mode promptly.

OK, thank you very much for everything, I believe that thanks to you once again I have already detected the problem and it is indeed that the status topic sometimes receives an unexpected value.

Cheers