Problem with new MQTT format

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" 

Hi,

Can you let me know if the below works for you?

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

Thanks

Sorry it took me so long to respond, no excuse but life.

Yes - it worked and have migrated all my sensors to the correct format. Just be careful if you have many like I do you need to - name for each and indent.

Thanks

Yes, looking at your posted example - the problem was too many spaces after the -

As many here have struggled with the new MQTT format, so have I. Mine is related to it being part of a trigger for detecting the state and other various pieces of information from my Plex server. Hoping someone can help me figure out how to reformat this inside my templates.yaml file.

Here is what I currently have:

- trigger:
    - platform: mqtt
      topic: "plex/update"
  sensor:
    - name: "Webhook Plex State"
      state: >
        {% if (trigger.payload_json.event == 'media.play') %}
          playing
        {% elif (trigger.payload_json.event == 'media.pause') %}
          paused
        {% elif (trigger.payload_json.event == 'media.resume') %}
          resumed
        {% elif (trigger.payload_json.event == 'media.stop') %}
          stopped
        {% elif (trigger.payload_json.event == 'media.rate') %}
          rated
        {% elif (trigger.payload_json.event == 'media.scrobble') %}
          90% viewed
        {% endif %}

    - name: "Webhook Plex Player"
      state: "{{ trigger.payload_json.Player.title }}"

    - name: "Webhook Plex Media Type"
      state: "{{ trigger.payload_json.Metadata.type }}"

    - name: "Webhook Plex Media Title"
      state: "{{ trigger.payload_json.Metadata.title }}"

    - name: "Webhook Plex Media Length"
      state: "{{ ((trigger.payload_json.Metadata.duration|float)/60000) }}"

I have this other bit in my automations.yaml that publishes the data that the trigger reads:

- alias: Plex Webhook
  initial_state: 'on'
  trigger:
    platform: webhook
    webhook_id: ************************************
  action:
  - service: mqtt.publish
    data_template:
      topic: plex/update
      payload_template: '{{ (trigger.data[''payload''] | string)[12:][:-2] | replace
        ("\\\\", "\\") | replace ("\\\''", "''") | replace ("\\x","?") }}

        '

I am curious to what/how the new MQTT trigger would be formatted. I’ve not tried it yet, but wondered if the beginning would be something like this:

- trigger:
    mqtt:
        topic: "plex/update"

Any help is appreciated as I don’t want to break things when I update. Thanks in advance.

Is anyone’s HA install still complaining about MQTT switches and sensors even though they’ve moved everything under the mqtt: label?

I’ve moved all mine across and all are working, but I still get the following every time I start HA - and I cannot find anywhere that is still using the old format (I’ve even tried a grep -r 'platform:mqtt' *.yaml in the config directory to be sure.

2022-07-17 20:20:28 WARNING (MainThread) [homeassistant.components.mqtt.mixins] Manually configured MQTT switch(s) found under platform key 'switch', please move to the mqtt integration key, see https://www.home-assistant.io/integrations/switch.mqtt/#new_format
2022-07-17 20:20:32 WARNING (MainThread) [homeassistant.components.mqtt.mixins] Manually configured MQTT sensor(s) found under platform key 'sensor', please move to the mqtt integration key, see https://www.home-assistant.io/integrations/sensor.mqtt/#new_format

You need a space after the colon. You could’ve missed old files or sections.

That’s actually what I did in the console (just mistyped it here) and it shows nothing other than a couple of entries in automations.yaml where I use MQTT as a trigger for a couple of automations.

Other than that, there’s nothing, yet HA still complains that there is…

bet ya you have .yml files

Nope - all .yaml and no .yml.

Then I suggest you find another way to search your files. Only the old method via yaml will produce that warning.

Just tested that on my config which I KNOW has old format mqtt in it, and it just says it could find any files.

However:

grep -rnw '/config/cfg/' -e 'platform: mqtt'

returns loads of results.

(My config files are in /config/cfg/_components/)

Ah! Found it!

I think it comes from having followed the instructions here to setup zigbee2mqtt control.

I’ll raise an issue on the Z2M github to have them update their docs/code.