MQTT HVAC configuration help - probably with templating some JSON?

Hi All,

I am having some issues working out how to integrate my Mitsubishi AC via Tasmota HVAC IR.

I can manually send a JSON payload via MQTT Explorer on the ‘house/ac/IRhvac’ topic and it works fine; for example I can send the following to turn on the AC and set the mode to cold and temp etc:

{“Vendor”:“Mitsubishi”,“Power”:1,“Mode”:“Cold”, “FanSpeed”:“5”,“Temp”:17}

After reading the HA Climate, HA Climate MQTT and HA Templating documentation I know I need to make use of some sort of templating to have the HA climate component send the above JSON payload, but I am confused as to how to implement this.

Taking a look through the existing forum posts I think this post is trying to achieve the same thing, but there are no responses.

Any assistance would be awesome!

Cheers,
Adam

2 Likes

Hey, I did actually figure this out, but it’s a bit of a mess. Let me post the part that templates the MQTT string at least, hopefully that will help:)

The action must use a payload_template, not a payload.
I’ve created input_selects and input_number sliders for the various properties.
Then I made automations that trigger the posted automation every time the inputs are changed.

On top of this, I’ve also made another “abstraction layer” with an MQTT-climate object that changes these input_selects and input_booleans and triggers the automation. Let me know if you would like me to post this also.

automation old varmepumpe_send:
  alias: Varmepumpe Send
  trigger:
    platform: state
    entity_id: input_boolean.varmepumpe_send
    from: 'off'
    to: 'on'
  action:
    - service: mqtt.publish
      data:
        topic: "/ESP-stue/cmd"
        payload_template: "heatpumpir,panasonic_dke,{% if states.input_select.varmepumpe_mode.state == 'off' %}0{% else %}1{% endif %},{% if states.input_select.varmepumpe_mode.state == 'off' %}2{% elif states.input_select.varmepumpe_mode.state == 'auto' %}1{% elif states.input_select.varmepumpe_mode.state == 'heat' %}2{% elif states.input_select.varmepumpe_mode.state == 'cool' %}3{% elif states.input_select.varmepumpe_mode.state == 'dry' %}4{% elif states.input_select.varmepumpe_mode.state == 'fan_only' %}5{% endif %},{{states.input_number.varmepumpe_vifte.state|int}},{{states.input_number.varmepumpe_temp.state|int}},{{states.input_number.varmepumpe_retning.state|int}},0"
    - delay: '00:00:01'
    - service: homeassistant.turn_off
      entity_id: input_boolean.varmepumpe_send
1 Like

As you’ve already discovered, MQTT HVAC doesn’t provide a way to change its states into the format required by your Mitsubishi AC. For example, if you turn on MQTT HVAC it will publish ON to power_command_topic. However, your AC unit expects to receive a dictionary containing one or more key-value pairs:

{"Vendor":"Mitsubishi","Power":1,"Mode":"Cold", "FanSpeed":"5","Temp":17}

So how does one convert from simply ON to that? Use what I call a ‘middleman’ automation.

  • Set power_command_topic to a ‘middleman’ topic, perhaps something like temp/ac/IRhvac or temp/ac/IRhvac/power.
  • Create an automation that is triggered by changes to the ‘middleman’ topic.
  • Make action convert the received payload (ON or OFF) into the format needed by your AC unit and then publish this converted payload to house/ac/IRhvac

I provided a simple example here.

The primary challenge will be to create the code that generates the payload. I don’t know anything about your Mitsubishi AC unit. Perhaps it is sufficient to merely send {"Power":1"} to turn it on or maybe it is mandatory to provide everything shown in your example. How much, or how little, is needed will determine the template’s complexity.

1 Like

Forgot about this post.

Basically I ended up using @123’s idea of creating a middleman.

I wrote a quick python script (daemon?) which intercepts commands from HA on one topic and writes the complete JSON payload for the Tasmota AC unit on another topic.

Works great.

Thanks for all the help!

1 Like

Hi @adampdistefano!
Can you post your full code (climate and automation configs)? I used Tasmota HVAC IR too. Thank you for your help!

Hi @123 !
My code for MQTT Climate use tasmota IR (ESP8266)

climate:
  - platform: mqtt
    name: Office
    unique_id: '1'
    qos: 1
    precision: 1.0
    retain: true
    modes:
      - "cool"
      - "auto"
      - "dry"
      - "fan_only"
      - "off"
    fan_modes:
      - "low"
      - "auto"
      - "max"
      - "high"
      - "medium"
    swing_modes:
      - "auto"
      - "off"
    min_temp: 18
    max_temp: 32
    payload_on: 'on'
    payload_off: 'off'

    mode_command_topic: 'ir/office/mode'
    mode_state_topic: 'tele/ir/RESULT'
    mode_state_template: '{{ value_json["IrReceived"]["IRHVAC"]["Mode"] | lower }}'

    fan_mode_command_topic: 'ir/office/fan'
    fan_mode_state_topic: 'tele/ir/RESULT'
    fan_mode_state_template: '{{ value_json["IrReceived"]["IRHVAC"]["FanSpeed"] | lower }}'

    temperature_command_topic: 'ir/office/temp'
    temperature_state_topic: 'tele/ir/RESULT'
    temperature_state_template: '{{ value_json["IrReceived"]["IRHVAC"]["Temp"] }}'

    swing_mode_command_topic: 'ir/office/swing'
    swing_mode_state_topic: 'tele/ir/RESULT'
    swing_mode_state_template: '{{ value_json["IrReceived"]["IRHVAC"]["SwingV"] | lower }}'

and 4 automations

  - alias: hvac_mode controller
    trigger:
      platform: mqtt
      topic: ir/office/mode
    condition:
      condition: numeric_state
      entity_id: climate.office
      value_template:  '{{ state.attributes.temperature }} '
      above: 18
      below: 32
    action: 
    - service: mqtt.publish
      data_template:
        topic:  'cmnd/ir/IRhvac'
        payload: >
          {% if trigger.payload == "off" %}
            {"Protocol":"DAIKIN216","power":"off", "Mode":"{{states('climate.office')}}","FanSpeed":"{{ state_attr('climate.office', 'fan_mode') }}","Temp":{{ state_attr('climate.office', 'temperature')}} }
          {% else %}
            {"Protocol":"DAIKIN216", "Power":"on","Mode": "{{trigger.payload}}","FanSpeed":"{{ state_attr('climate.office','fan_mode')}}","Temp":{{ state_attr('climate.office', 'temperature')}} }
          {% endif %}

 
  - alias: hvac_temp controller
    trigger:
      platform: mqtt
      topic: ir/office/temp
    condition:
      condition: numeric_state
      entity_id: climate.office
      value_template:  '{{ state.attributes.temperature }} '
      above: 18
      below: 32
    action: 
    - service: mqtt.publish
      data_template:
        topic:  'cmnd/ir/IRhvac'
        payload: >
          {"Protocol":"DAIKIN216","Power":"on","Mode":"{{states('climate.office')}}","FanSpeed":"{{state_attr('climate.office','fan_mode')}}","SwingV":"{{state_attr('climate.office','swing_mode')}}","Temp": "{{trigger.payload}}" }

  - alias: hvac_fan controller
    trigger:
      platform: mqtt
      topic: ir/office/fan
    condition:
      condition: numeric_state
      entity_id: climate.office
      value_template:  '{{ state.attributes.temperature }} '
      above: 18
      below: 32
    action: 
    - service: mqtt.publish
      data_template:
        topic:  'cmnd/ir/IRhvac'
        payload: >
          {"Protocol":"DAIKIN216","Power":"on","Mode":"{{states('climate.office')}}","FanSpeed":"{{trigger.payload}}","SwingV":"{{state_attr('climate.office','swing_mode')}}","Temp": {{ state_attr('climate.office', 'temperature')}} }

  - alias: hvac_swing controller
    trigger:
      platform: mqtt
      topic: ir/office/swing
    condition:
      condition: numeric_state
      entity_id: climate.office
      value_template:  '{{ state.attributes.temperature }} '
      above: 18
      below: 32
    action: 
    - service: mqtt.publish
      data_template:
        topic:  'cmnd/ir/IRhvac'
        payload: >
          {"Protocol":"DAIKIN216","Power":"on","Mode":"{{states('climate.office')}}","FanSpeed":"{{state_attr('climate.office','fan_mode')}}", "SwingV":{{trigger.payload}},"Temp": {{ state_attr('climate.office', 'temperature')}} }

These codes work, but when my homeassistant restart, the state of climate is not preset.

hvac_modes: cool, auto, dry, fan_only, off
min_temp: 18
max_temp: 32
target_temp_step: 1
fan_modes: low, auto, max, high, medium
swing_modes: auto, off
current_temperature: null
temperature: null
fan_mode: null
swing_mode: null
friendly_name: Office
supported_features: 41

Can you help me fix or improve this code?
(Sorry for my English)

1 Like

That’s because the configuration for climate.office does not have any state topics. The MQTT HVAC integration use several state topics:
mode_state_topic
fan_mode_state_topic
hold_state_topic
temperature_state_topic
etc

1 Like