Problem with new MQTT format

I noticed the format for mqtt has changed.

So I changed it like this:
configuration.yaml

mqtt:
  sensor:
    - state_topic: "emon/emonesp/stat/srssi"
      value_template: "{{ value }}"
      name: "mqtt_emonesp_rssi"
      unique_id: "mqtt_emonesp_rssi"
      device_class: "signal_strength"
      unit_of_measurement: "dBm"
      qos: 1

    # - state_topic: "emon/emonesp/stat/Vrms"
    #   value_template: "{{ value }}"
    #   name: "mqtt_emonesp_vrms"
    #   unique_id: "mqtt_emonesp_vrms"
    #   device_class: "voltage"
    #   unit_of_measurement: "V"
    #   qos: 1

    # - state_topic: "emon/emonesp/stat/LF"
    #   value_template: "{{ value }}"
    #   name: "mqtt_emonesp_lf"
    #   unique_id: "mqtt_emonesp_lf"
    #   device_class: "frequency"
    #   unit_of_measurement: "Hz"
    #   qos: 1

I got a lot more, but for simplicity sake I put in just one for now.
Still I get this kind of error.

What am I doing wrong? I just don’t see it.

@hneel As far as the formatting, it all looks correct.
When I converted all mine, i created a separate mqtt.yaml file and moved all my sensors there. The order of my fields is slightly different, but I don’t think it matters. You could try it on one to see if it works, otherwise, i am not of much help. Here is an example of mine.

  sensor:
    - name: "Session Charge Current"
      unique_id: "evsesessionchargecurrent"
      state_topic: "openevse/amp"
      unit_of_measurement: "A"
      icon: mdi:flash-circle
      value_template: "{{ value | float(default=0) * 1000 | round(1, default=0) }}"

It seems the system doesn’t detect that sensor: is under mqtt:.
Maybe some blanks issue in the actual YAML?

The beginning yaml is not good.
Is supposed to be:

mqtt:
  sensor:
    - name: "mqtt_emonesp_rssi"

Nope. The order does not matter. You could start with this if you wanted to:

mqtt:
  sensor:
    - qos: 1

I can’t see anything wrong with it.

Not sure what went wrong. I might have ruined my original sensor.yaml when I took out the mqtt sensors.
I decided to do a system restore. Then I tried again and now everything works just fine.

I also have problems configuring the new mqtt format. Even if I copy the example from the mqtt switch page directly in my configuration.yaml I do not get the defined switch. I still have many configured via the old format (- platform: mqtt) can these styles be mixed or must I convert them all at once?

Are you sure you updated to 2022.6.x first?

I managed to convert them gradually without an issue as I had them in multiple packages and configuration.yaml
Once you get one set working it’s pretty easy to do the rest.

Just tried the transition from old format to new one yet using core 2022.5.5. For some reason I’m getting the validation error:

Invalid config for [mqtt]: [switch] is an invalid option for [mqtt]. Check: mqtt->mqtt->switch. (See /config/configuration.yaml, line 22).

On the other hand, the documentation says mqtt switch is supported for long time already.

The MQTT Switch integration was introduced in Home Assistant 0.7, and it’s used by 40.7% of the active installations. Its IoT class is Configurable.

Here is source code of my test switch:

mqtt:
  switch:
    - unique_id: test_pc_switch
      name: "test_pc_switch"
      command_topic: "shellies/shelly1-pc/relay/0/command"
      state_topic: "shellies/shelly1-pc/relay/0"
      availability_topic: "shellies/shelly1-pc/online"
      payload_on: "on"
      payload_off: "off"
      payload_available: "true"
      payload_not_available: "false"
      qos: 1
      retain: false

The new format requires 2022.6. The MQTT switch has been around for ages, and the old format will continue to work until 2022.9, but the new format won’t work with 2022.5 or earlier.

If you haven’t yet, try to set the MQTT Logs to debug in the Config file

logger:
  default: info
  logs:
    homeassistant.components.mqtt: debug

Maybe we can get a bit more info on this Error

Got it.
So the documentation doesn’t reflect the fact that there are still 2 formats available. Especially it doesn’t mention that new one is valid since 2022.6 only. No comment.

Also is there a reason your value_template is just ‘value’ not ‘value_json.value’?
I wasn’t aware that would work that way

Thanks will try this, I am on 2022.6.4 so it should work must be something obvious I am overlooking.

value_json is only required if the mqtt payload is actually JSON.

eg: the payload for this sensor is actually JSON:

  - name: "Boiler Return"
    state_topic: "building/dhjm/eobs/99031647c54fff28"
    unit_of_measurement: '°C'
    value_template: "{{ value_json['obs']['temperature']|float }}"
    device_class: temperature
    unique_id: onewire-cb6a6c2d-b7e1-4369-9088-dd559fd6e9e9

But the payload for this sensor:

  - state_topic: media/download/status
    name: Remaining Files To Download
    value_template: "{{ value|int }}"
    unit_of_measurement: 'Files'
    unique_id: putio-16f25db7-ccfa-4905-b4d4-1088b169d8b6

is just a simple number.

For people still struggling to move to the new format:

mqtt:
  sensor: !include_dir_merge_list cfg/_component/mqtt/sensor/
  binary_sensor: !include_dir_merge_list cfg/_component/mqtt/binary_sensor/
  switch: !include_dir_merge_list cfg/_component/mqtt/switch/

These are the new lines in my configuration.yaml and then I have moved my old platform: mqtt stuff to new files under these directories.

eg:

cfg/_component/mqtt/binary_sensor/rf_sensors.yaml

  - name: "Greenroom Motion"
    unique_id: rf-885a0851-dc23-4937-9596-0aeeb2144472
    state_topic: "dhjm/rfxgateway/AC/0x00018D7C/2"
    device_class: motion
    payload_on: "On"
    payload_off: "Off"
    off_delay: 30

  - name: "Daylight Sensor"
    state_topic: "dhjm/rfxgateway/AC/0x0020CF1E/10"
    payload_on: "Off"
    payload_off: "On"
    device_class: light
    unique_id: rf-4a34c013-9579-48f5-875f-3eee3eaa2362

#Doors
  - name: "Greenroom Exit"
    state_topic: "dhjm/rfxgateway/AC/0x00016FD0/10"
    payload_on: "On"
    payload_off: "Off"
    device_class: door
    unique_id: rf-bd494957-88bf-47d5-b134-b423b6f4dede

  - name: "Foyer Door"
    state_topic: "dhjm/rfxgateway/AC/0x00019A38/10"
    payload_on: "On"
    payload_off: "Off"
    device_class: door
    unique_id: rf-05ec4053-bd2f-4ac9-ab9c-82597fa7ba4b
7 Likes

Thank you @mobile.andrew.jones it is working now an I like your suggestion to still kep sensors, lights, switches separated per directory. Unfortunately I did not find my error, I started with my switch but now moved to light and binary_sensor which are both working. Still a lot of work to convert them all but I still have time :wink:

Changing to the new format was simpler than I expected - all I had to do was take something like this:

sensor:
  - platform: mqtt
    name: "Outside Temperature"  
    state_topic: "building/dhjm/eobs/external/wx"
    unit_of_measurement: '°C'
    value_template: "{{ value_json['obs']['temperature'] }}"

Delete the platform line

sensor:
  name: "Outside Temperature"  
  state_topic: "building/dhjm/eobs/external/wx"
  unit_of_measurement: '°C'
  value_template: "{{ value_json['obs']['temperature'] }}"

and then stick a hypen before name and then a space

sensor:
  - name: "Outside Temperature"  
  state_topic: "building/dhjm/eobs/external/wx"
  unit_of_measurement: '°C'
  value_template: "{{ value_json['obs']['temperature'] }}"

and finally add 2 spaces to the rest of the lines

sensor:
  - name: "Outside Temperature"  
    state_topic: "building/dhjm/eobs/external/wx"
    unit_of_measurement: '°C'
    value_template: "{{ value_json['obs']['temperature'] }}"

I then took the opportunity to add device_class where possible and unique_id while I was at it.

Sorry to hijack this thread, but I currently have MQTT devices split across various configuration files (sensors.yaml, switches.yaml etc…) I’m unsure how to maintain this with the new format:

configuration.yaml
sensor: !include sensor.yaml

sensots.yaml

- platform: mqtt
  name: "Bedroom Temperature"
  state_topic: "sensor/bedroom/temperature"
  qos: 0
  unit_of_measurement: "°C"
  expire_after: 2100 
- platform: mqtt
  name: "Bedroom Humidity"
  state_topic: "sensor/bedroom/humidity"
  qos: 0
  unit_of_measurement: "%"
  expire_after: 2100

Do I need to pull all these out into a separate mqtt.yaml file ?

Thanks

Reformatted my - platform: mqtt sensor by removing the platform line
and changing the rest of the sensor to as follows. now I get this message
when checking my configuration. Invalid config for [sensor]: required key not provided @ data[‘platform’]. Got None.

I’m lost can anyone help me.

 -  name: "Refrigator Amps"
    state_topic: "tele/garageref/SENSOR"
    value_template: "{{value_json['ENERGY'].Current}}"
    unit_of_measurement: "A"
    unique_id: "testit"