[mqtt hvac]command payload template

I Have a MQTT Thermostat. All is working fine, exept one thing:
When I publish to the broker, the published value needs to be formatted.
In my case it only sends the string values of operation modes, but these need to be converted to nummeric values.
I got this working for the received topics, but how to do this for the published ones?

- platform: mqtt
  name: Bureel
  unique_id: thermostaat_bureel
  qos: 1
  modes:
    - "cool"
    - "fan_only"
    - "heat"
  fan_modes:
    - "auto"
    - "high"
    - "medium"
    - "low"
  min_temp: 16
  max_temp: 26
  temp_step: 0.5
  payload_on: 'TRUE'
  payload_off: 'FALSE'
  power_command_topic: "beckhoff/thermostaat/bureel/power/set"
  mode_command_topic: "beckhoff/thermostaat/bureel/mode/set"
  temperature_command_topic: "beckhoff/thermostaat/bureel/temperature/set"
  fan_mode_command_topic: "beckhoff/thermostaat/bureel/fanmode/set"
  current_temperature_topic: "beckhoff/thermostaat/bureel/temperature/current"
  mode_state_topic: "beckhoff/thermostaat/bureel/mode/get"
  temperature_state_topic: "beckhoff/thermostaat/bureel/temperature/get"
  fanmode_state_topic: "beckhoff/thermostaat/bureel/fanmode/get"
  mode_state_template: >
    {% set values = {'1':'cool', '4':'fan_only', '8':'heat'} %}
    {{ values[value] if value in values.keys() else 'heat' }}
  fan_mode_state_template: >
    {% set values = {'2':'low', '4':'medium', '8':'high', '128':'auto'} %}
    {{ values[value] if value in values.keys() else 'auto' }}

The official code doesn’t support templates for published payloads.
Check this topic:

This holds a fork of the official code, supporting the templating on publish.

I also need to template the temperature_command_topic .

I am using esp32_mqtt_eq3 : https://github.com/softypit/esp32_mqtt_eq3 , that use EQ3 in bluetooth then an esp32 to transmitt messages in mqtt over wifi

I need to send message like ab:cd:ef:gh:ij:kl settemp 20.0 over a specific topic.

I have to use @123 workaround to template it with an alias Need help with value_template for MQTT HVAC

Hey @lhanneus,
would you like to share some lines of your hvac config for the esp32–>eq3bt bridge? It seems, that there’s no example specific to the esp32_mqtt_eq3 in combination to HA and I’m curious to see how it works. I could write a wiki article for softypit and could bring this up as a PR so everybody could see how the interaction could work.

Thank you for your answer :slight_smile:

@jan-hendrikscholz I will write a wiki. Wait a week, I’m currently without access to my files …

Don’t worry, I appreciate every kind of response :slight_smile:

Update:
If tried to figure out, how MQTT HVAC works… Here’s my current status for an ESP32_MQTT_EQ3 with a single thermostat connected:
Basic explanation:

  • Standard json parsing for the attributes, hopefully the state matching is working with all states
  • MQTT republish using automations for setting the state and the temperature
  • MQTT publish for display unlock for getting the status once HA is started
  • Not working: Boost mode (Presets not available in MQTT HVAC) , setting away mode (state is working, personally I’m not using this mode, so I kept it read-only)
  • Some things I could bring up in future: Binary sensors for battery, window open

Configuration.yaml:

climate:
  - platform: mqtt
    name: Badezimmer
    device:
     identifiers: '00:1A:22:XX:XX:XX'
    modes:
      - "off"
      - "auto"
      - "heat"
    precision: 0.5
    min_temp: 4.5
    max_temp: 29.5
    temp_step: 0.5
    mode_state_topic: "/badezimmerradout/status"
    mode_state_template: >
      {% if value_json.mode == "manual" %}
        {% if value_json.temp == "4.5" %}
          off
        {% else %}
          heat
        {% endif %}
      {% elif value_json.mode == "auto" %}
        auto
      {% endif %}
    mode_command_topic: "/ha_badezimmerradin/trv_mode"
    temperature_state_topic: "/badezimmerradout/status"
    temperature_state_template: "{{ value_json.temp }}"
    temperature_command_topic: "/ha_badezimmerradin/trv_temp"
    current_temperature_topic: "/badezimmerradout/status"
    current_temperature_template: "{{ value_json.temp }}"
    away_mode_state_topic: "/badezimmerradout/status"
    away_mode_state_template: >
      {% if value_json.mode == "holiday" %}
        on
      {% else %}
        off
      {% endif %}

First automation as a helper for the temperature:

- id: '1573053873261'
  alias: Badezimmer_Temperature_Helper
  description: ''
  trigger:
  - platform: mqtt
    topic: /ha_badezimmerradin/trv_temp
  condition: []
  action:
    service: mqtt.publish
    data_template:
      payload: "{% if trigger.payload == \"4.5\" %}\n  00:1A:22:XX:XX:XX off\n{% else\
        \ %}\n  00:1A:22:XX:XX:XX settemp {{trigger.payload}}\n{% endif %}"
      topic: /badezimmerradin/trv

Second automation for setting the modes

- id: '1573063154330'
  alias: Badezimmer_Mode_Helper
  description: ''
  trigger:
  - platform: mqtt
    topic: /ha_badezimmerradin/trv_mode
  condition: []
  action:
    service: mqtt.publish
    data_template:
      payload: "{% if trigger.payload == \"heat\" %}\n  00:1A:22:XX:XX:XX manual\n\
        {% else %}\n  00:1A:22:XX:XX:XX {{trigger.payload}}\n{% endif %}"
      topic: /badezimmerradin/trv

Third automation for updating the state after a restart of HA

- id: '1561930189154'
  alias: Update_Thermostat
  trigger:
  - event: start
    platform: homeassistant
  condition: []
  action:
  - data:
      payload: 00:1A:22:XX:XX:XX unlock
      topic: /badezimmerradin/trv
    service: mqtt.publish

Just bear in mind: It’s the first time I created this. As far as I can see, it’s working for me, but nevertheless, there might be better approaches…

@lhanneus Does your config look heavily different? If you want, I can create the wiki article as well.

Hi @jan-hendrikscholz, thanks a lot for your automations! They work fine.
I was just wondering if you have found a way to transmit the identifier of the climate mqtt device in the payload for the temperature_command_topic?
That way it would only be needed one set of automations for all eq3 thermostat, otherwise I need to copy the automatons hardcoding the identifier of each eq3 device :confused:

I have been thinking about it and I do not find a way, maybe you already solved it :stuck_out_tongue:

Thanks!

I found a way around to have multiple thermostats with a single set of automatons (not one set per thermostat).

configuration.yaml

climate:
  - platform: mqtt
    name: Bedroom heating
    device:
      identifiers: '00:1A:22:0C:XX:XX'
    modes:
      - "off"
      - "auto"
      - "heat"
    precision: 0.5
    min_temp: 4.5
    max_temp: 29.5
    temp_step: 0.5
    mode_command_topic: "/eq3mqtt/00:1A:22:0C:XX:XX/trv_mode"
    temperature_command_topic: "/eq3mqtt/00:1A:22:0C:XX:XX/trv_temp"
    mode_state_topic: "/eq3mqtt/00:1A:22:0C:XX:XX/status"
    mode_state_template: >-
      {% if value_json.mode == "manual" %}
        {% if value_json.temp == "4.5" %}
          off
        {% else %}
          heat
        {% endif %}
      {% elif value_json.mode == "auto" %}
        auto
      {% endif %}
    temperature_state_topic: "/eq3mqtt/00:1A:22:0C:XX:XX/status"
    temperature_state_template: "{{value_json.temp}}"
    current_temperature_topic: "/eq3mqtt/00:1A:22:0C:XX:XX/status"
    current_temperature_template: "{{value_json.temp}}"
    json_attributes_topic: "/eq3mqtt/00:1A:22:0C:XX:XX/status"
    json_attributes_template: "{{ {'mac':value_json.trv, 'valve':value_json.valve, 'boost':value_json.boost,   'window':value_json.window, 'battery':value_json.battery} | tojson}}"
    away_mode_state_topic: "/eq3mqtt/00:1A:22:0C:XX:XX/status"
    away_mode_state_template: >-
      {% if value_json.mode == "holiday" %}
        on
      {% else %}
        off
      {% endif %}

as you see I post and listen everything related to a given thermostat in its own topics /eq3mqtt/<mac address>/...

Then the automatisation for temperature becomes:

- alias: eq3mqtt_Temperature_Helper
  description: ''
  trigger:
  - platform: mqtt
    topic: /eq3mqtt/+/trv_temp
  condition: []
  action:
    service: mqtt.publish
    data_template:
      payload: >
        {% if trigger.payload == 4.5 %} {{trigger.topic.split('/')[2]}} off 
        {% else %}  {{trigger.topic.split('/')[2]}} settemp {{trigger.payload}}
        {% endif %}
      topic: /eq3mqtt/radin/trv

Notice the wildcard to listen to all topics of all sensors, later I extract the MAC address from the topic that actually triggered the automation.

And the automation for modes (same trick as the temp automation):

- alias: eq3mqtt_Mode_Helper
  description: ''
  trigger:
  - platform: mqtt
    topic: /eq3mqtt/+/trv_mode
  condition: []
  action:
    service: mqtt.publish
    data_template:
      payload: >
        {% if trigger.payload == "heat" %} {{trigger.topic.split('/')[2]}} manual
        {% else %} {{trigger.topic.split('/')[2]}} {{trigger.payload}}
        {% endif %}
      topic: /eq3mqtt/radin/trv

But now it is needed a kind of “forwarder” between the generic topic of the esp32_mqtt_eq3 and the individual topics of each thermostat:

- alias: eq3mqtt_status_proxy
  trigger:
  - platform: mqtt
    topic: "/eq3mqtt/radout/status"
  action:
    service: mqtt.publish
    data_template:
      topic: "/eq3mqtt/{{ trigger.payload_json.trv }}/status"
      payload: "{{ trigger.payload_json | tojson }}"

Here I extract the mac address from the json response.

For pulling the status of the thermostats, in case they are changed manually, I found that it is better to do it separately otherwise the ESP32 misses some messages and the thermostats are not updated.
I pull my different thermostats at different second every 2 minutes. (Not sure if it is too much and will affect the battery or not, I will see)

- alias: Pulling_Thermostat_Bedroom
  description: ''
  trigger:
  - platform: homeassistant
    event: start
  - platform: time_pattern
    minutes: /2
    seconds: 15
  condition: []
  action:
  - service: mqtt.publish
    data_template:
      payload: "00:1A:22:0C:XX:XX unlock"
      topic: /eq3mqtt/radin/trv

I hope this helps others to add multiple thermostats :slight_smile:

I have 4 eQ-3 Bluetooth thermostats (CC-RT-BLE-EQ). I programmed my ESP32 chip according to THIS manual and I can control thermostats from the website.

A

04

juangrados I have added your first code (climate:…) to the file “configuration.yaml” and the other four codes (eq3mqtt_Temperature_Helper, eq3mqtt_Mode_Helper, eq3mqtt_status_proxy, Pulling_Thermostat_Bedroom) to the file “automations.yaml”. In HA view I added the “Thermostat” tab where I can choose different things but I don’t see the current temperature and the thermostats do not react to changes at all.

C

However, I can control them all the time via the website and I can see it in “MQTT Explorer” …

I am asking for help, what am I doing wrong.

I solved my problem :slight_smile:
Instead of typing:

/ eq3mqtt / 00: 1A: 22: 0C: XX: XX / trv_mode

I typed:

/ “my MQTT ID” radin / “MAC address of my thermostat” / trv_mode

and instead of:

/ eq3mqtt / radin / trv

I typed:

/ “my MQTT ID” radin / trv

OMG, @CichY sorry for not replying before, I do not receive notification and I haven’t been around lately.
I am glad that you managed to solve it at the end :slight_smile:

When executing your scripts above I am receiving such errors in HA logs for a long time. Do you know how to solve them?

Logger: homeassistant.components.mqtt.climate
Source: components/mqtt/climate.py:513
Integration: MQTT (documentation, issues)
First occurred: 8:06:27 (530 occurrences)
Last logged: 12:28:42

Invalid _away mode: off

I deleted these entries and there are no more errors. Are these entries needed?

Hi @CichY I think it is safe to delete that section, I guess it complains because it is not supported by this device models.

Witaj CichY
trafiłem na Twój post i walczę od dwóch dni z tymi termostatami ale przegrywam.
Mogę sterować nimi poprzez ESP.
Dodałem w HA w configuration.yaml pierwszą sekcję a w automation.yaml kolejne.
Niestety brak sterowania.
Skorygowałem wpisy w obu plikach zgodnie z Twoją wzmianką ale nadal przegrywam.
Czy mógłbyś podpowiedzieć a może podesłać swój fragment?

I have this entry in the configuration file.

Dziękuję,
a w automatyzacjach też dokonywałeś zmian czy wg. pierwotnego wzoru?
ja walczę kolejny dzień i nadal mam taki stan, że przez stronę esp ładnie zmienia się ale już komendy z HA nie idą. W Explorerze wygląda to tak (na żółto efekt z HA, a na szaro przez stronę ESP, który skutkuje faktycznie zmianą w termostacie

My automations:

1

2

3

4