Use value of MQTT payload to set HVAC mode

I am very new to Home Assistant and MQTT. I have a node-red application that I am setting up to manage a thermostat that is in Home Assistant. Node-red is publishing and MQTT message with the HVAC_mode setting. I have created an automation to change the thermostat mode when triggered by a change in the MQTT message. I am trying to set the HVAC_mode based on the payload of the message (off, cool, heat). When I save the code, I get an error that complains that the mode only wants the literal settings for the mode. How do I express this programatically, based on the message. Here is what I have tried. It doesn’t work.

  alias: Front Tstat Mode
  description: ''
  trigger:
  - platform: mqtt
    topic: home/front_tstat/mode
  action:
  - device_id: 78daf49102bbbd7db898a7df3443274e
    domain: climate
    entity_id: climate.t6_pro_z_wave_programmable_thermostat
    type: set_hvac_mode
    hvac_mode: 
    - platform: mqtt    
      name: "mqtt mode set"
      state_topic: "home/front_tstat/mode"
      qos: 0
      mode: single

Can anyone point me in the right direction to accomplish this?

I am making an assumption that the topic’s payload contains the desired hvac_mode and it is one of the supported modes (heat, cool, auto, off, etc).

  alias: Front Tstat Mode
  description: ''
  trigger:
  - platform: mqtt
    topic: home/front_tstat/mode
  action:
  - service: climate.set_hvac_mode
    target:
      entity_id: climate.t6_pro_z_wave_programmable_thermostat
    data:
      hvac_mode: "{{ trigger.payload }}"

Thanks for the response. I get errors:

Logger: homeassistant.components.automation.first_automation
Source: components/automation/__init__.py:505
Integration: Automation (documentation, issues)
First occurred: 5:02:14 PM (1 occurrences)
Last logged: 5:02:14 PM

Error while executing automation automation.first_automation: extra keys not allowed @ data['mode']
Logger: homeassistant.components.automation.first_automation
Source: helpers/script.py:1344
Integration: Automation (documentation, issues)
First occurred: 5:02:14 PM (1 occurrences)
Last logged: 5:02:14 PM

Front Tstat Mode: Error executing script. Invalid data for call_service at pos 1: extra keys not allowed @ data['mode']

Thanks for any help you can give.

It’s complaining about the presence of an option named mode.

In the example I posted, there’s no option with that name. The example uses hvac_mode which is a valid option.

Double-check your automation.

I checked my code. The only place the word ‘mode’ appeared was in the message topic. Thinking that it may be a reserved word, I changed it to ‘modex’ to see if it would help. Here is the code:

- id: '1630619596999'
  alias: Front Tstat Mode
  description: ''
  trigger:
  - platform: mqtt
    topic: home/front_tstat/modex
  action:
  - service: climate.set_hvac_mode
    target:
      entity_id: climate.t6_pro_z_wave_programmable_thermostat
    data:
      hvac_mode: "{{ trigger.payload }}"
      mode: single

Here are the errors I got when I tried to run it:

Logger: homeassistant.components.automation.first_automation
Source: components/automation/__init__.py:505
Integration: Automation (documentation, issues)
First occurred: 7:16:50 AM (1 occurrences)
Last logged: 7:16:50 AM

Error while executing automation automation.first_automation: value must be one of ['auto', 'cool', 'dry', 'fan_only', 'heat', 'heat_cool', 'off'] for dictionary value @ data['hvac_mode']
Logger: homeassistant.components.automation.first_automation
Source: helpers/script.py:1344
Integration: Automation (documentation, issues)
First occurred: 7:16:50 AM (1 occurrences)
Last logged: 7:16:50 AM

Front Tstat Mode: Error executing script. Invalid data for call_service at pos 1: value must be one of ['auto', 'cool', 'dry', 'fan_only', 'heat', 'heat_cool', 'off'] for dictionary value @ data['hvac_mode']
Logger: homeassistant.helpers.template
Source: helpers/template.py:1406
First occurred: 7:16:50 AM (1 occurrences)
Last logged: 7:16:50 AM

Template variable warning: 'dict object' has no attribute 'payload' when rendering '{{ trigger.payload }}'

Thanks again.

The error messages are self-explanatory. The payload you are publishing to the topic isn’t correct.

That’s what I had referred to in my first post when I said I am assuming your payload contains one of the supported modes.

Post a few examples of what is published to the topic.

Here is one from MQTT Explorer. I originally tried just cool without the quotes. I have also tried heat.

My reading of the errors are the first one is complaining it doesn’t have a legal value. The MQTT messages are correct as the screenshots show.

Maybe the key is the 3rd message? It says that ‘payload’ is not a valid attribute of ‘trigger’. If the attribute is not valid, would referring to it produce a null or error value which would make the first error? I also tried to use 'trigger.payload_json and got the same errors.

Is the problem syntax or formatting? Also, this is in the 'automations.yaml file, if that matters.

I really appreciate your time helping with this.

Now I see the problem. It’s a syntax error. The last line is not indented correctly:

    data:
      hvac_mode: "{{ trigger.payload }}"
      mode: single  <--------------- This line is not indented correctly.

By putting it there, Home Assistant thinks it’s an option that belongs with data but there’s no valid option called mode for the climate.set_hvac_mode service call.

Either move it four spaces to the left, so the word mode is vertically aligned with action, or delete the entire line (because mode: single is the default for any automation or script).

- id: '1630619596999'
  alias: Front Tstat Mode
  description: ''
  trigger:
  - platform: mqtt
    topic: home/front_tstat/modex
  action:
  - service: climate.set_hvac_mode
    target:
      entity_id: climate.t6_pro_z_wave_programmable_thermostat
    data:
      hvac_mode: "{{ trigger.payload }}"
  mode: single

If you wish, you can return to using home/front_tstat/mode as the topic because the word mode in that topic was never the cause of the error message.

The payloads you are publishing, cool and heat, are correct and trigger.payload is also correct for use with an MQTT Trigger. Using trigger.payload_json would be incorrect because the payload you are publishing isn’t in JSON format.

Thanks for your help. It’s working! The problem was with the way I was triggering it while testing. Instead of changing the payload, I was running it from the Automation tab. That is what my problem was. I was running the automation without a trigger. Since there was no trigger, there were no attributes or data. When I change the payload, everything runs.

I’m slow, but I get there eventually. Thanks a lot for your help. I’ll probably be back with more questions as my automations become more complex.

2 Likes