Need help with value_template for MQTT HVAC

Hi

Could you share your MQTT HVAC version that supports command templates? I have similar issue that you had, so it will be very helpful to me if I can benefit from your work.

Thanks

Yes, of course. However, I’ll need another or day or two before I can share it because I’m in the middle of enhancing it and haven’t tested it fully.

  • This version of MQTT HVAC supports command templates for mode, fan_mode, and hold_mode.
  • Default temperature_step is 0.5 for Celsius and 1 for Fahrenheit (automatically configured). Alternately, it can be specified with temp_step in configuration.yaml.

https://pastebin.com/yfVPJfj0

1 Like

I like the changes that you have made. I was also looking for the mode_command_template and fan_mode_command_template capability. Is there a reason you didn’t implement a temperature_command_template too? My HVAC system uses Celsius while my user interface utilizes Fahrenheit. For the mqtt sensors I’ve been converting the values back and forth when retrieving and saving the values to mqtt. Looking to do the same here. I already doing it when retrieving the values using a temperature_state_template.

The HVAC system that I’m using also supports two separate set points. One for cool and the other for heat. I’m looking at doing further modes to support dual set points. Any suggestions would be appreciated.

I also saw another post about the component showing when the system is actually running as opposed to just the operation state. It looks like you made that change a few days after you posting this version. Do you have that and/or other enhancements available?

Thanks!

Short answer: I didn’t need it. I extended the platform to suit my own needs.

Having said that, you’ve provided a compelling example of why a temperature_command_template would be useful. When I have the time, I’ll toss that one into the mix and post it in this thread: Enhanced version of MQTT HVAC (Climate platform) with proper History Chart

I believe the low/high temperature control is in the Pull Request pipeline: Add target temperature low high to MQTT climate by rwagoner · Pull Request #17391 · home-assistant/core · GitHub

1 Like

I went ahead and download the updated version of the MQTT HFAC component with the low/high temperature control. Works great. I went ahead and updated it to include the following command templates:

mode_command_template
temperature_command_template
temperature_low_command_template
temperature_high_command_template
fan_mode_command_template
swing_mode_command_template
hold_command_template

Here is a link to my updated version: mqtt.py

You had some of these templates covered in your version. I added those and others to the version posted. I’d be interested incorporating your additional updates too. Please share.

I’m new to HA development, so I don’t know the best way to get these incorporated into a released version.

1 Like

How do I install this? I need mode_command_template …

In the directory where you have configuration.yaml, create a sub-directory named custom_components containing yet another sub-directory named climate.

./custom_components/climate

Put the enhanced mqtt.py file in the climate sub-directory. Restart Home Assistant. It will report a warning in your log file that you are using an “untested custom component”. That’s just to remind you that if something in Home Assistant now behaves badly, it might be due to the use of the custom component.

In my case, I named the file mqtt_climate.py and, in configuration.yaml, changed the climate component’s platform definition from:
platform: mqtt
to:
platform: mqtt_climate
This helps to remind me that I’m using a customized version instead of the default one.

1 Like

Thank you very much! First time I’ve used custom components…

1 Like

I have a problem getting this to work.
When I use the custom component the log outputs:

2019-01-25 21:42:15 WARNING (MainThread) [homeassistant.loader] You are using a custom component for climate.mqtt_climate which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you do experience issues with Home Assistant.
2019-01-25 21:42:16 ERROR (MainThread) [homeassistant.components.climate] Error while setting up platform mqtt_climate
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/entity_platform.py", line 128, in _async_setup_platform
    SLOW_SETUP_MAX_WAIT, loop=hass.loop)
  File "/usr/lib/python3.5/asyncio/tasks.py", line 400, in wait_for
    return fut.result()
  File "/usr/lib/python3.5/asyncio/futures.py", line 293, in result
    raise self._exception
  File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
    result = coro.send(None)
  File "/home/homeassistant/.homeassistant/custom_components/climate/mqtt_climate.py", line 149, in async_setup_platform
    await _async_setup_entity(hass, config, async_add_entities)
  File "/home/homeassistant/.homeassistant/custom_components/climate/mqtt_climate.py", line 234, in _async_setup_entity
    discovery_hash,
  File "/home/homeassistant/.homeassistant/custom_components/climate/mqtt_climate.py", line 250, in __init__
    payload_available, payload_not_available)
TypeError: __init__() takes 2 positional arguments but 5 were given

Any idea?

What is your version of Home Assistant?

The code for this custom component is a modified version of MQTT HVAC from version 0.80. Maybe there’s a compatibility issue.

Just figured it out. It seems that the code has changed since 0.86.
I’ll need to update the new code, to support the templating for publishing.

I find it odd that this isn’t implemented in the official release. It’s a great feature.

1 Like

OK, thanks. I just started testing 0.86.1 on a test system. I now know I’ll need to revise one or more custom components before upgrading the production system.

Thanks for your help with this. I’ll stay on 0.85.1 until this is working again. Nothing in the newer versions has made my socks go up and down, and this functionality is key to many of my automations.

Hello 123,
I have tested our mqtt HVAC enhanced, but it isn’t work correctly. I have the same errors than gijbelsy. My version of Hassio is 0.88.1. Do you have an idea to how to modify your file .py to work properly with versions after 0.85.1?.

Pedro

First thing you should know is that I’m still using 0.80 and I’m in not in a hurry to upgrade. However, I did create a new version that works with 0.86 (I tested it with 0.86.4).

My new version may not work with 0.88. I believe something in MQTT changed yet again in 0.87 so it’s possible my new version will need additional modifications. I am not certain because I have not tested it with 0.87 or 0.88 yet.

I intend to download the docker image of 0.88.1 and do some more testing next week.

1 Like

I have created a new version based on the MQTT climate.py code from Home Assistant 0.88.1.

I tested it briefly (on my test system running 0.88.1, because my production system continues to run version 0.80) and it seems to work correctly. It did not produce any errors in the system log.

Here is how it looks in Lovelace using three different thermostat cards. All three work correctly. You’ll notice it indicates the current operating mode is Heat and the current activity is Idle (the HVAC system is not actively heating at the moment).

Here is how it looks after running for few minutes. Notice it now indicates the current activity is Heat. The Heating Hours Today sensor now shows a few minutes of activity (0.03 h). The History Chart (not shown here) also correctly shows when the HVAC system is actively heating (couldn’t test cooling but it should work).

This revised version is slightly different from the previous one.

  • status_state_topic and status_state_template are now called activity_state_topic and activity_state_template.
  • It does not automatically adjust the temperature step-size based on the chosen unit system. If you want a step-size of 0.5 degrees you must specify it in the configuration: temp_step: 0.5

Here is the updated version on pastebin.

For Home Assistant version 0.88.X you must copy this custom component into the following directory:

<config>/custom_component/mqtt/climate.py

That’s different from previous versions where it would go into <config>/custom_component/climate/mqtt.py

Here is a sample configuration. Notice it uses activity_state_topic, activity_state_template, and temp_step.

climate:
 - platform: mqtt
   name: "Thermostat"
   unique_id: '1234567890'
   payload_on: 1
   payload_off: 0
   activity_state_topic: "premise/thermostat/heatingstatus"
   activity_state_template: >-
     {% set values = { '1':'heat', '2':'cool', '4':'idle'} %}
     {{ values[value] if value in values.keys() else 'idle' }}
   modes:
     - auto
     - heat
     - cool
     - 'off'
   mode_state_topic: "premise/thermostat/temperaturemode" 
   mode_state_template: >-
     {% set values = { '0':'auto', '1':'heat',  '2':'cool', '4':'off'} %}
     {{ values[value] if value in values.keys() else 'off' }}
   mode_command_topic: "premise/command/thermostat/temperaturemode"
   mode_command_template: >-
     {% set values = { 'auto':'0', 'heat':'1',  'cool':'2', 'off':'4'} %}
     {{ values[value] if value in values.keys() else '4' }}
   fan_modes:
     - auto
     - 'on'
   fan_mode_state_topic: "premise/thermostat/fancontrol"
   fan_mode_state_template: >-
     {% set values = { '0':'auto', '1':'on'} %}
     {{ values[value] if value in values.keys() else 'auto' }}
   fan_mode_command_topic: "premise/command/thermostat/fancontrol"
   fan_mode_command_template: >-
     {% set values = { 'auto':'0', 'on':'1'} %}
     {{ values[value] if value in values.keys() else '0' }}
   current_temperature_topic: "premise/thermostat/temperature"
   min_temp: 17
   max_temp: 28
   temp_step: 0.5
   temperature_state_topic: "premise/thermostat/currentsetpoint"
   temperature_command_topic: "premise/command/thermostat/currentsetpoint"
   hold_state_topic: "premise/thermostat/mode"
   hold_state_template: "{{ 'hold' if value == '2' else 'auto' }}"
   hold_command_topic: "premise/command/thermostat/mode"
   hold_command_template: "{{ '2' if value == 'hold' else '0' }}"

Hi Taras,
Thanks for your time to upgrade your climate enhanced component. Now I can see the climate entity in hassio 0.88.1 release, but I can’t change the mode or the temperature. No updates in mqtt are done in any address: from homeassistant towards mqtt server or from mqtt server towards homeassistant.

This is my configuration of the climate component in the configuration.yaml file:

climate:

  • platform: mqtt
    name: “thermostat”
    unique_id: ‘1234567890’
    payload_on: 1
    payload_off: 0
    activity_state_topic: “domuino004/get/impresora3d”
    activity_state_template: >-
    {% set values = { ‘1’:‘heat’, ‘2’:‘cool’, ‘0’:‘idle’} %}
    {{ values[value] if value in values.keys() else ‘idle’ }}
    modes:
  • auto
  • heat
  • cool
  • ‘off’
    mode_state_topic: “domuino004/get/termomatr”
    mode_state_template: >-
    {% set values = { ‘0’:‘auto’, ‘1’:‘heat’, ‘2’:‘cool’, ‘4’:‘off’} %}
    {{ values[value] if value in values.keys() else ‘off’ }}
    mode_command_topic: “domuino004/set/termomatr”
    mode_command_template: >-
    {% set values = { ‘auto’:‘0’, ‘heat’:‘1’, ‘cool’:‘2’, ‘off’:‘4’} %}
    {{ values[value] if value in values.keys() else ‘4’ }}
    current_temperature_topic: “domuino004/get/tempmatr”
    current_temperature_template: “{{float(value) / 100 }}”
    min_temp: 17
    max_temp: 24
    temp_step: 0.5
    temperature_state_topic: “domuino004/get/consignamatr”
    temperature_command_topic: “domuino004/set/consignamatr”

It works for me so I suggest you confirm the following topics and templates are correct:

  • mode_command_topic
  • mode_command_template
  • temperature_command_topic

I’ve re-formatted your configuration to make it easier (for me) to understand.

climate:
 - platform: mqtt
   name: "thermostat"
   unique_id: '1234567890'
   payload_on: 1
   payload_off: 0
   activity_state_topic: "domuino004/get/impresora3d"
   activity_state_template: >-
     {% set values = { '1':'heat', '2':'cool', '0':'idle'} %}
     {{ values[value] if value in values.keys() else 'idle' }}
   modes:
     - auto
     - heat
     - cool
     - 'off'
   mode_state_topic: "domuino004/get/termomatr"
   mode_state_template: >-
     {% set values = { '0':'auto', '1':'heat', '2':'cool', '4':'off'} %}
     {{ values[value] if value in values.keys() else 'off' }}
   mode_command_topic: "domuino004/set/termomatr"
   mode_command_template: >-
     {% set values = { 'auto':'0', 'heat':'1', 'cool':'2', 'off':'4'} %}
     {{ values[value] if value in values.keys() else '4' }}
   current_temperature_topic: "domuino004/get/tempmatr"
   current_temperature_template: "{{float(value) / 100 }}"
   min_temp: 17
   max_temp: 24
   temp_step: 0.5
   temperature_state_topic: "domuino004/get/consignamatr"
   temperature_command_topic: "domuino004/set/consignamatr"

Based on what I see in the configuration for your thermostat:

  • Mode is a string value ‘0’, ‘1’, ‘2’, ‘4’ (auto/heat/cool/off) and sent via topic domuino004/set/termomatr
  • Temperature is a float value (18, 18.5, 19, 19.5, etc) sent via topic domuino004/set/consignamatr

I would suggest you use Home Assistant’s MQTT Publish page to confirm the topic and values are correct.

MQTT%20Publish

Hi again,
Now it’s working properly. It was my fault: My router changed the local IP of the raspberry where is working homeassistant, so mqtt server didn’t receive information from the climate component. Thanks a lot. You have done a very good work.