MQTT value templates

My car returns a state of “heating”, “cooling” or “off” for climatisation, which I am trying to pull into a switch. However, it doesn’t seem to be working. I have:

mqtt:
  switch:
    - name: "Climatisation"
      command_topic: weconnect/vehicles/XXX/controls/climatisation_writetopic
      payload_off: stop
      payload_on: start
      state_topic: "weconnect/vehicles/XXX/domains/climatisation/climatisationStatus/climatisationState"
      value_template: "{% if ((value == 'heating') or (value == 'cooling')) %}on{% else %}off{% endif %}"
      state_on: on
      state_off: off
      json_attributes_topic: "weconnect/vehicles/XXX/domains/climatisation/climatisationStatus/climatisationState"
      json_attributes_template: "{'value': {{value|to_json}}}"

In theory, this config should provide a switch which:

  1. I can toggle to turn climatisation on/off.
  2. Shows on / off based on the current climatisation state (“heating” and “cooling” are both considered to be the “on” state).
  3. Adds a “value” attribute to the entity, containing the original “heating”, “cooling” or “off” value.

(1) is working fine, but the entity’s state is always “unknown” and there is no “value” attribute.

I’m not sure where to go with debugging this. No errors relating to this seem to be logged anywhere.

We would need to see examples of the full payload on the state topic to be able to help.

The payload is literally “heating”, “cooling” or “off”

on and off are booleans. Quote them to make them strings.

I’d start with the following, which fixes:

  • Adds quotes around on or off which must always be quoted anywhere in YAML
  • Simplify the value_template so that on and off don’t appear outside of a template unquoted. I can’t confirm if what you had would cause issues, but I’m confident what I’ve provided will work.
  • Fix the json_attributes_template
mqtt:
  switch:
    - name: "Climatisation"
      command_topic: weconnect/vehicles/XXX/controls/climatisation_writetopic
      payload_off: stop
      payload_on: start
      state_topic: "weconnect/vehicles/XXX/domains/climatisation/climatisationStatus/climatisationState"
      value_template: "{{ 'on' if value in ['heating', 'cooling'] else 'off' }}"
      state_on: "on"
      state_off: "off"
      json_attributes_topic: "weconnect/vehicles/XXX/domains/climatisation/climatisationStatus/climatisationState"
      json_attributes_template: "{{ {'value': value} | tojson }}"
1 Like

Thank you - that did the trick!

That’s going to cause problems, use this instead.

json_attributes_template: >
  {{ dict(value=value) | to_json }}

Also, you can game the template to use the defaults for payload_on and payload_off.

I’d use this entire config:

mqtt:
  switch:
    - name: "Climatisation"
      command_topic: weconnect/vehicles/XXX/controls/climatisation_writetopic
      payload_off: stop
      payload_on: start
      state_topic: "weconnect/vehicles/XXX/domains/climatisation/climatisationStatus/climatisationState"
      value_template: >
        payload_{{ 'on' if value in ('heating', 'cooling') else 'off' }}
      json_attributes_topic: "weconnect/vehicles/XXX/domains/climatisation/climatisationStatus/climatisationState"
      json_attributes_template: >
        {{ dict(value=value) | to_json }}

EDIT: seems like @mekaneck replied and the post never showed up for me… weird. Anyways ignore this, will keep for others.