Need help with value_template for MQTT HVAC

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.

It’s not very complicated. However, I can’t promise I’ll get around to adding it anytime soon.

As a workaround, you can use an automation.

Example:

In the climate component’s configuration, enter a new topic:

temperature_command_topic: home/setpoint

The automation:

- alias: 'setpoint converter'
  hide_entity: true
  trigger:
    platform: mqtt
    topic: home/setpoint
  action:
    service: mqtt.publish
    data_template:
      topic: eurotronic/setpoint # <-- change this for your Eurotronic thermostat
      payload: "{'temperature': {{trigger.payload}}}" # <-- change this for your needs

How it works:

  • The climate component publishes its setpoint to home/setpoint.
  • The automation is triggered by home/setpoint and re-publishes the payload to eurotronic/setpoint.
  • Assuming the received payload is 22 the format of the re-published payload is in JSON: {'temperature':22}

Adjust eurotronic/setpoint and the payload template to suit your specific needs.

1 Like

Hi @123,

It’s working now. My mistake was to copy init.py also to the custom_mqtt folder.
Tnx

A bit unsure about changing mqtt to my_mqtt in core.entity_registry
Nothing in there related to my HAI thermostats but I have several entries like this of sonoff devices:

            {
            "config_entry_id": "17df94434e514bedbe717c16eb7b5e86",
            "device_id": "339e0abf3fbf4f2dacd5957d5d21ee06",
            "disabled_by": null,
            "entity_id": "sensor.sonoff_ds18b20_temperature",
            "name": null,
            "platform": "mqtt",
            "unique_id": "971098_DS18B20_Temperature"
        },

Does the configuration for your HAI thermostat have unique_id?

If it doesn’t then you don’t need to concern yourself with editing core.entity_registry because the HAI thermostat won’t be in there.

Each HAI theromstat has a MQTT id starting with climate., ie climate.garage_heat or climate.masterbdrmht or climate.guestbdrmht etc.
But none are in the core.entity_registry file with any device_id or anything else. I only see sonoff/tasmota devices and iPhone devices listed there.

Doesn’t look like it is installing correctly. Sure I missed something.
from log file at boot:

2019-04-02 16:08:36 INFO (MainThread) [homeassistant.components.switch] Setting up switch.mqtt
2019-04-02 16:08:42 INFO (MainThread) [homeassistant.loader] Loaded my_mqtt.climate from custom_components.my_mqtt.climate
2019-04-02 16:08:42 WARNING (MainThread) [homeassistant.loader] You are using a custom component for my_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-04-02 16:08:42 INFO (MainThread) [homeassistant.setup] Setting up climate
2019-04-02 16:08:43 INFO (MainThread) [homeassistant.components.climate] Setting up climate.my_mqtt
2019-04-02 16:08:43 INFO (MainThread) [homeassistant.setup] Setup of domain climate took 0.3 seconds.
2019-04-02 16:08:43 INFO (MainThread) [homeassistant.loader] Loaded mqtt.climate from homeassistant.components.mqtt.climate
2019-04-02 16:08:43 INFO (MainThread) [homeassistant.components.climate] Setting up climate.mqtt
2019-04-02 16:08:43 WARNING (MainThread) [homeassistant.helpers.config_validation] Your configuration contains extra keys that the platform does not support.
Please remove [temperature_low_state_topic], [temperature_low_command_topic], [temperature_high_state_topic], [temperature_high_command_topic].
2019-04-02 16:08:43 WARNING (MainThread) [homeassistant.helpers.config_validation] Your configuration contains extra keys that the platform does not support.
Please remove [temperature_low_state_topic], [temperature_low_command_topic], [temperature_high_state_topic], [temperature_high_command_topic].
2019-04-02 16:08:43 WARNING (MainThread) [homeassistant.helpers.config_validation] Your configuration contains extra keys that the platform does not support.
Please remove [temperature_low_state_topic], [temperature_low_command_topic], [temperature_high_state_topic], [temperature_high_command_topic].
2019-04-02 16:08:43 WARNING (MainThread) [homeassistant.helpers.config_validation] Your configuration contains extra keys that the platform does not support.
Please remove [temperature_low_state_topic], [temperature_low_command_topic], [temperature_high_state_topic], [temperature_high_command_topic].
ys that the platform does not support.
Please remove [temperature_low_state_topic], [temperature_low_command_topic], [temperature_high_state_topic], [temperature_high_command_topic].
2019-04-02 16:10:28 INFO (MainThread) [homeassistant.setup] Setting up axis

1 Like