Need help with value_template for MQTT HVAC

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.

Hi!
I’d be interested by this custom component, but is it compatible with th changes of version 0.89? I’ve tried to install it but it seems the regular component is loading, not the cutom one?
2019-03-11 12:36:27 INFO (MainThread) [homeassistant.loader] Loaded mqtt.climate from homeassistant.components.mqtt.climate

Prior to 0.88, you would copy the customized version of MQTT HVAC to:
custom_components/climate/mqtt.py

For 0.88, you copy the customized version of MQTT HVAC to:
custom_components/mqtt/climate.py

For 0.89, you copy the customized version of MQTT HVAC to:
custom_components/mqtt/climate.py

  • You also have to copy components/mqtt/__init.py__ to custom_components/mqtt/__init.py__
  • If you you use any other MQTT components (light, switch, lock, cover, sensor, binary_sensor, etc) you will also have to copy them from components to custom_components.
  • If you have automations that use mqtt.publish then you will also have to copy all of components/automation/mqtt to custom_components/automation/mqtt.

There are ways of not having to copy so many files but these alternative approaches have their disadvantages. I recommend you review the available approaches, by reading my post here, and then decide how you wish to proceed.


NOTE: The customized version of MQTT HVAC that I created for 0.88 (see above) does not work in 0.89.

I have revised version for 0.89 and will post it soon.

ok thanks I’ll wait for the update!

The following installation instructions represent (what I believe to be) the easiest way to implement this custom component, in version 0.89, without affecting any of the existing MQTT components. Please be aware that if there are changes made to the MQTT component in 0.90, or later versions, this custom component may fail to work (and require additional modifications).

Create a new directory under custom_components called my_mqtt.

config/custom_components/my_mqtt

Copy the contents of this pastebin link this pastebin link into a new file called climate.py.

config/custom_components/my_mqtt/climate.py

In configuration.yaml, find your existing MQTT climate entity and replace:

platform: mqtt

with:

platform: my_mqtt

The next step is needed ONLY if your climate entity is configured with a unique_id. If it doesn’t then skip the next two bullet-points and go directly to the Config Check step.

  • Use a text editor to modify the following file: config/.storage/core.entity_registry
  • Find your climate entity and replace: "platform": "mqtt" with "platform": "my_mqtt"

Run Config Check and, if no errors are reported, restart Home Assistant.


Revised the pastebin link to the corrected version of the code.

Hi
I just tried that and had the following error:

    Traceback (most recent call last):
      File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/websocket_api/commands.py", line 148, in handle_call_service
        connection.context(msg))
      File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/core.py", line 1133, in async_call
        self._execute_service(handler, service_call))
      File "/usr/lib/python3.5/asyncio/futures.py", line 380, in __iter__
        yield self  # This tells Task to wait for completion.
      File "/usr/lib/python3.5/asyncio/tasks.py", line 304, in _wakeup
        future.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 "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/core.py", line 1155, in _execute_service
        await handler.func(service_call)
      File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/entity_component.py", line 188, in handle_service
        self._platforms.values(), func, call, service_name
      File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/service.py", line 278, in entity_service_call
        future.result()  # pop exception if have
      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 "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/service.py", line 292, in _handle_service_platform_call
        await getattr(entity, func)(**data)
      File "/home/homeassistant/.homeassistant/custom_components/my_mqtt/climate.py", line 687, in async_set_operation_mode
        _LOGGER.info("Operation_mode_command: %s", operation_mode_command)
    UnboundLocalError: local variable 'operation_mode_command' referenced before assignment```

I think you found a bug in the code (and I now believe there’s more than just one).

Are you using mode_command_template? My guess is that you are not.

Try this version and let me know if you get any errors. Also, it would help me if you posted the configuration for your MQTT climate component.

  1. you were right, I tried the previous version for one of the climate components for which I didn’t add the mode_command_template
  2. I tried the new version without the mode_command_template and nothing happens (as it should!)
  3. with the right mode_command_template it works fine!

for information, here is an example of the climate component (I have 6)

    name: Chambre d'amis
    unique_id: chambre_d_amis
    current_temperature_topic: homegear/climate/plain/3/1/ACTUAL_TEMPERATURE
    temperature_state_topic: homegear/climate/plain/3/1/SET_TEMPERATURE
    temperature_command_topic: homegear/climate/set/3/1/SET_TEMPERATURE
    min_temp: 10
    max_temp: 30
    temp_step: 0.5
    modes: ["auto","manual","party","boost"]
    mode_state_topic: homegear/climate/plain/3/1/CONTROL_MODE
    mode_state_template:  >-
      {% set values = { '0':'auto', '1':'manual', '2':'party', '3':'boost'} %}
      {{ values[value] if value in values.keys() else 'off' }}
    mode_command_topic: homegear/climate/set/3/1/CONTROL_MODE
    mode_command_template: >-
      {% set values = { 'auto':'0', 'manual':'1', 'party':'2', 'boost':'3'} %}
      {{ values[value] if value in values.keys() else '0' }} 

Could it be that there is an issue with the custom climate.py in version 0.90.1?
Got the following error:
Platform not found: climate.mqtt_custom
Check here:

Great job what you did !

Any idea is it complicated to add a temperature_command_template?

For my Eurotronic Spirit the setpoins needs to be send as json format like this:

temperature_command_template: '{{ value_json.current_heating_setpoint }}'

I’m using it with 0.90.1 and it works without any issues.

The Platform not found message suggests to me that the custom component was not installed in the correct directory.

Here are the installation instructions I posted earlier in this thread:

Create a new directory under custom_components called my_mqtt.

config/custom_components/my_mqtt

Copy the contents of this pastebin link into a new file called climate.py.

config/custom_components/my_mqtt/climate.py

In configuration.yaml, find your existing MQTT climate entity and replace:

platform: mqtt

with:

platform: my_mqtt

The next step is needed ONLY if your climate entity is configured with a unique_id. If it doesn’t then skip the next two bullet-points and go directly to the Config Check step.

  • Use a text editor to modify the following file: config/.storage/core.entity_registry
  • Find your climate entity and replace: "platform": "mqtt" with "platform": "my_mqtt"

Run Config Check and, if no errors are reported, restart Home Assistant.