Need help with value_template for MQTT HVAC

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.

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.