MQTT Sensor and Switch configuration

Hi all,

I’m trying to update my MQTT platform config from an old HA version to the latest. But I must be doing something wrong. Can’t seem to see where I’ve gone wrong.

This is the original 2021 version config (doesn’t work any more with the latest HA version):

  - platform: mqtt
    name: "Outside Temp"
    state_topic: "outside/waterheater/tele/SENSOR"
    unit_of_measurement: '°C'
    value_template: "{{ value_json['AM2301'].Temperature }}"

switch Water_Heater:
  name: "Water Heater"
  platform: mqtt
  qos: 2
  state_topic: "outside/waterheater/stat/POWER"
  command_topic: "outside/waterheater/cmnd/POWER"

I’ve tried updating it to the current version but I suspect I have something slightly wrong here?

mqtt:
  - sensor:
      name: "Outside Temp"
      unique_id: outside_temp
      state_topic: "outside/waterheater/tele/SENSOR"
      suggested_display_precision: 1
      unit_of_measurement: "°C"
      value_template: "{{ value_json['AM2301'].Temperature }}"

  - switch:
      name: "Water Heater"
      unique_id: water_heater
      state_topic: "outside/waterheater/stat/POWER"
      command_topic: "outside/waterheater/cmnd/POWER"
      qos: 2
      payload_on: "1"
      payload_off: "0"

Why? What makes you think that?

1 Like

Using Quick Start from the Developer tab I get this error:

Cannot quick reload all YAML configurations because the configuration is not valid: Invalid config for [mqtt]: expected dictionary for dictionary value @ data[‘mqtt’]. Got [{‘sensor’: {‘name’: ‘Outside Temp’, ‘unique_id’: ‘outside_temp’, ‘state_topic’: ‘outside/waterheater/tele/SENSOR’, ‘suggested_display_precision’: 1, ‘unit_of_measurement’: ‘°C’, ‘value_template’: “{{ value_json[‘AM2301’].Temperature }}”}}, {‘switch’: {‘name’: ‘Water Heater’, ‘unique_id’: ‘water_heater’, ‘state_topic’: ‘outside/waterheater/stat/POWER’, ‘command_topic’: ‘outside/waterheater/cmnd/POWER’, ‘qos’: 2, ‘payload_on’: ‘1’, ‘payload_off’: ‘0’}}]. (See /config/configuration.yaml, line 444).

Choosing to restart throws this usual message this is indicative of an invalid config file.
image

When I remove the above config, restart is fine.

OK a lot of trial and error and I worked it out. Here’s what I’m using:
NOTE: The specs in the documentation don’t seem right to me. Their examples don’t work in my latest HA version. Note the grouping, dash and indentation differences.

mqtt:
  sensor:
    - name: "Outside Temp"
      unique_id: outside_temp
      state_topic: "outside/waterheater/tele/SENSOR"
      suggested_display_precision: 1
      unit_of_measurement: "°C"
      value_template: "{{ value_json['AM2301'].Temperature }}"

    - name: "Outside Humidity"
      unique_id: outside_humidity
      state_topic: "outside/waterheater/tele/SENSOR"
      unit_of_measurement: "%"
      value_template: "{{ value_json['AM2301'].Humidity }}"

  switch:
    - name: "Generator"
      unique_id: generator
      state_topic: "energy/generator/stat/POWER"
      command_topic: "energy/generator/cmnd/POWER"
      qos: 2

    - name: "Generator 1Hr"
      unique_id: generator1hour
      state_topic: "energy/generator/cmnd/POWERAuto"
      command_topic: "energy/generator/cmnd/POWERAuto"
      qos: 2

Odd, the specs in the docs work fine for me, and others:

mqtt:
  - sensor:
      name: "Outside Temp"
      unique_id: outside_temp
      state_topic: "outside/waterheater/tele/SENSOR"
      suggested_display_precision: 1
      unit_of_measurement: "°C"
      value_template: "{{ value_json['AM2301'].Temperature }}"
  - sensor:
      name: "Outside Humidity"
      unique_id: outside_humidity
      state_topic: "outside/waterheater/tele/SENSOR"
      unit_of_measurement: "%"
      value_template: "{{ value_json['AM2301'].Humidity }}"

  - switch:
      name: "Generator"
      unique_id: generator
      state_topic: "energy/generator/stat/POWER"
      command_topic: "energy/generator/cmnd/POWER"
      qos: 2
  - switch:
      name: "Generator 1Hr"
      unique_id: generator1hour
      state_topic: "energy/generator/cmnd/POWERAuto"
      command_topic: "energy/generator/cmnd/POWERAuto"
      qos: 2

Definitely does not for me. I’m getting this error:

Invalid config for [mqtt]: expected dictionary for dictionary value @ data[‘mqtt’]. Got [{‘sensor’: {‘name’: ‘Outside Temp’, ‘unique_id’: ‘outside_temp’, ‘state_topic’: ‘outside/waterheater/tele/SENSOR’, ‘suggested_display_precision’: 1, ‘unit_of_measurement’: ‘°C’, ‘value_template’: “{{ value_json[‘AM2301’].Temperature }}”}}, {‘sensor’: {‘name’: ‘Outside Humidity’, ‘unique_id’: ‘outside_humidity’, ‘state_topic’: ‘outside/waterheater/tele/SENSOR’, ‘unit_of_measurement’: ‘%’, ‘value_template’: “{{ value_json[‘AM2301’].Humidity }}”}}, {‘switch’: {‘name’: ‘Generator’, ‘unique_id’: … (See /config/configuration.yaml, line 443).

Can you confirm it definitely works for you? I’m convinced either the docs are incorrect or I have a magic different version that nobody else does? What is happening?

mine

Which was built from the docs, however, here’s an interesting thing… it looks like they changed the YAML for MQTT.

I suspect you’re not on 2023.8.