New MQTT format

Hi all,

I’m a little confused about the new MQTT formatting. My current door sensors setup using an unflashed Sonoff bridge that works took me ages to figure out.

This is my working MQTT config:

######MQTT Sensors########
  - platform: mqtt
    name: "Front Door"
    state_topic: "home/rfbridge/doorstate"
    payload_on: "frontdooropen"
    payload_off: "frontdoorclosed"
    qos: 0
    device_class: door
  - platform: mqtt
    name: "Kitchen Door Main"
    state_topic: "home/rfbridge/doorstate"
    payload_on: "kitchendooropen"
    payload_off: "kitchendoorclosed"
    qos: 0
    device_class: door
  - platform: mqtt
    name: "Kitchen Door L"
    state_topic: "home/rfbridge/doorstate"
    payload_on: "kitchendoorlopen"
    payload_off: "kitchendoorlclosed"
    qos: 0
    device_class: door
  - platform: mqtt
    name: "Kitchen Window L"
    state_topic: "home/rfbridge/doorstate"
    payload_on: "kitchenwindowopen"
    payload_off: "kitchenwindowclosed"
    qos: 0
    device_class: window
  - platform: mqtt
    name: "Kitchen Window R"
    state_topic: "home/rfbridge/doorstate"
    payload_on: "kitchenwindowropen"
    payload_off: "kitchenwindowrclosed"
    qos: 0
    device_class: window 
  - platform: mqtt
    name: "Bedroom Door Left"
    state_topic: "home/rfbridge/doorstate"
    payload_on: "bedroomdoorleftopen"
    payload_off: "bedroomdoorleftclosed"
    qos: 0
    device_class: door
  - platform: mqtt
    name: "Bedroom Door Right"
    state_topic: "home/rfbridge/doorstate"
    payload_on: "bedroomdoorrightopen"
    payload_off: "bedroomdoorrightclosed"
    qos: 0
    device_class: door
    
    ######MQTT Sensors bridge2###################################
    
  - platform: mqtt
    name: "Fridge Door"
    state_topic: "home/rfbridge/doorstate"
    payload_on: "fridgedooropen"
    payload_off: "fridgedoorclosed"
    qos: 0

I’ve tried this but it didn’t work:

# mqtt:
#   sensor:
mqtt:
  sensor:
    - name: "Front Door"
      state_topic: "home/rfbridge/doorstate"
      value_template: "{% if value == '1' %}
        Open
        {% else %} 
        Closed
        {% endif %}"
    #   payload_on: "frontdooropen"
    #   payload_off: "frontdoorclosed"
      qos: 0
    #   device_class: door
    - name: "Kitchen Door Main"
      state_topic: "home/rfbridge/doorstate"
      value_template: "{% if value == '1' %}
        Open
        {% else %} 
        Closed
        {% endif %}"
    #   payload_on: "kitchendooropen"
    #   payload_off: "kitchendoorclosed"
      qos: 0
    #   device_class: door
    - name: "Kitchen Door L"
      state_topic: "home/rfbridge/doorstate"
      value_template: "{% if value == '1' %}
        Open
        {% else %} 
        Closed
        {% endif %}"
    #   payload_on: "kitchendoorlopen"
    #   payload_off: "kitchendoorlclosed"
      qos: 0
    #   device_class: door
    - name: "Kitchen Window L"
      state_topic: "home/rfbridge/doorstate"
      value_template: "{% if value == '1' %}
        Open
        {% else %} 
        Closed
        {% endif %}"
    #   payload_on: "kitchenwindowopen"
    #   payload_off: "kitchenwindowclosed"
      qos: 0
    #   device_class: window
    - name: "Kitchen Window R"
      state_topic: "home/rfbridge/doorstate"
      value_template: "{% if value == '1' %}
        Open
        {% else %} 
        Closed
        {% endif %}"
    #   payload_on: "kitchenwindowropen"
    #   payload_off: "kitchenwindowrclosed"
      qos: 0
    #   device_class: window 
    - name: "Bedroom Door Left"
      state_topic: "home/rfbridge/doorstate"
      value_template: "{% if value == '1' %}
        Open
        {% else %} 
        Closed
        {% endif %}"
    #   payload_on: "bedroomdoorleftopen"
    #   payload_off: "bedroomdoorleftclosed"
      qos: 0
    #   device_class: door
    - name: "Bedroom Door Right"
      state_topic: "home/rfbridge/doorstate"
      value_template: "{% if value == '1' %}
        Open
        {% else %} 
        Closed
        {% endif %}"
    #   payload_on: "bedroomdoorrightopen"
    #   payload_off: "bedroomdoorrightclosed"
      qos: 0
    #   device_class: door
    
    ######MQTT Sensors bridge2###################################
    
    - name: "Fridge Door"
      state_topic: "home/rfbridge/doorstate"
      value_template: "{% if value == '1' %}
        Open
        {% else %} 
        Closed
        {% endif %}"
    #   payload_on: "fridgedooropen"
    #   payload_off: "fridgedoorclosed"
      qos: 0
    #   device_class: door

Any tips on where I am going wrong?

Thanks in advance :slight_smile:

  • Your original configuration is for creating MQTT Binary Sensors.

  • Your new configuration is for creating MQTT Sensors.

Is the change from binary_sensor to sensor intentional or accidental?

Is the new configuration you posted located in configuration.yaml and what exactly isn’t working? Do the MQTT Sensors fail to be created or are they created successfully but fail to show the correct state?

@nckslater i think you are making it more complex then needed as the new changes are very simple.

old format:
sensor:
  - platform: mqtt
    state_topic: "home/bedroom/temperature"

new format:
mqtt:
  sensor:
    - state_topic: "home/bedroom/temperature"
2 Likes

My working MQTT are binary_sensor so I guess I am configuring this incorrectly.

The format needs to be:

mqtt:
  binary_sensor:
    - state_topic: "home/rfbridge/doorstate"

What about the payload ON/OFF? How will these need to be configured?

Thanks

I too am confused. Where do all the “old” sensor parameters like name, payload_xx, qos etc go?
If the example included a few more parameters, it would be helpful.

Looking through the documentation maybe something to do with payload_available (payload_on), payload_not_unavailable (payload_off) ?

I think if I change this to binary_sensor it should work

You leave all that as is. Example only showing what needs to be modified

The example you posted won’t work because a binary_sensor’s nominal states are on or off (your example uses Open and Closed).

Try this version:

mqtt:
  binary_sensor:
    - name: "Front Door"
      state_topic: "home/rfbridge/doorstate"
      value_template: "{{ iif(value == '1', 'ON', 'OFF') }}"
      device_class: door

That may also fail to work because I noticed yet another difference between your working MQTT config and your new MQTT config. In the old one, payload_on was fridgedooropen but in the new one you’re using a template to check if the received value is simply the number 1. Are you saying that when the door opens the RF bridge now transmits 1 instead of fridgedooropen?

Hi sorry for breaking in this topic, but I am stuck with the same because of the new MQTT format.
I have in my configuration an inclusion for the sensors to a separate sensor.yaml file which looks like below.

in the configuration.yaml:

sensor: !include sensors.yaml

In the sensors.yaml file:

- platform: mqtt
  state_topic: "ITHO/Fan/State"
  name: Fan Speed

However after the update with the new format I get this warning:

Your manually configured MQTT sensor(s) needs attention
This stops working in version 2022.12.0. Please address before upgrading.
Manually configured MQTT sensor(s) found under platform key sensor.

Please move the configuration to the mqtt integration key and restart Home Assistant to fix this issue. See the documentation, for more information.

I tried to fit in the new format in the sensors.yaml file but I can not find the correct notation to put it in there.
Does this mean I have to move all sensor configs back to the configuration.yaml ?

There is no clear documentation regarding this new format in cinjunction with a separated sensor file.

1 Like

Thank you for pointing this out…

I am very confused about how to get this in the new format.

Should it be:

mqtt:
  binary_sensor:
    - name: "Front Door"
      state_topic: "home/rfbridge/doorstate"
      value_template: "{{ if(value == 'fridgedooropen ', 'ON', 'OFF') }}"
      device_class: door

Thanks

No need to apologize, just move your post to a new topic where the community can focus on your problem exclusively.

I just went through all this earlier tonight. The formats follow the general YAML formatting. I had binary_sensors, sensors, lights, and fan MQTT entities affected by this. Essentially you are moving all of your entities from their end points (binary_sensors, sensors etc.) to the MQTT section. From there, if you have multiple entities for any of these, you only define those once and use - to start a new device/entity. For example, I have in my configuration.yaml file

mqtt: !include mqtt.yaml

In that file, I have all of my sensors, binary_sensors, fan, and lights setup as such:

   binary_sensor:
    - name: "Bedroom Window"
      device_class: Window
      state_topic: "tele/sonoff/RESULT"
      value_template: >-
        {% if value_json.RfReceived.Data == 'D93D0A' %}
          {{'ON'}}
        {% elif value_json.RfReceived.Data == 'D93D0E' %}
          {{'OFF'}}
        {% else %}
          {{states('binary_sensor.bedroom_window') | upper}}
        {% endif %} 

    - name: "Front Room Window 2"
      device_class: Window
      state_topic: "tele/sonoff/RESULT"
      value_template: >-
        {% if value_json.RfReceived.Data == 'D0610A' %}
          {{'ON'}}
        {% elif value_json.RfReceived.Data == 'D0610E' %}
          {{'OFF'}}
        {% else %}
          {{states('binary_sensor.front_room_window_2') | upper}}
        {% endif %} 

   
    - name: "Front Room Window 1"
      device_class: Window    
      state_topic: "tele/sonoff/RESULT"
      value_template: >-
        {% if value_json.RfReceived.Data == 'D0870A' %}
          {{'ON'}}
        {% elif value_json.RfReceived.Data == 'D0870E' %}
          {{'OFF'}}
        {% else %}
          {{states('binary_sensor.front_room_window_1') | upper}}
        {% endif %} 
    
   
    - name: "Living Room Window 1"
      device_class: Window    
      state_topic: "tele/sonoff/RESULT"
      value_template: >-
        {% if value_json.RfReceived.Data == 'D03C0A' %}
          {{'ON'}}
        {% elif value_json.RfReceived.Data == 'D03C0E' %}
          {{'OFF'}}
        {% else %}
          {{states('binary_sensor.living_room_window_1') | upper}}
        {% endif %} 

    - name: "Living Room Window 2"
      device_class: Window    
      state_topic: "tele/sonoff/RESULT"
      value_template: >-
        {% if value_json.RfReceived.Data == 'D9540A' %}
          {{'ON'}}
        {% elif value_json.RfReceived.Data == 'D9540E' %}
          {{'OFF'}}
        {% else %}
          {{states('binary_sensor.living_room_window_2') | upper}}
        {% endif %}    

    - name: "Side Bedroom Window"
      device_class: Window    
      state_topic: "tele/sonoff/RESULT"
      value_template: >-
        {% if value_json.RfReceived.Data == 'D1D20A' %}
          {{'ON'}}
        {% elif value_json.RfReceived.Data == 'D1D20E' %}
          {{'OFF'}}
        {% else %}
          {{states('binary_sensor.side_bedroom_window') | upper}}
        {% endif %}    

    - name: "Extra 1"
      device_class: Window    
      state_topic: "tele/sonoff/RESULT"
      value_template: >-
        {% if value_json.RfReceived.Data == 'D2810A' %}
          {{'ON'}}
        {% elif value_json.RfReceived.Data == 'D2810E' %}
          {{'OFF'}}
        {% else %}
          {{states('binary_sensor.extra_1') | upper}}
        {% endif %}    

    - name: "Extra 2"
      device_class: Window    
      state_topic: "tele/sonoff/RESULT"
      value_template: >-
        {% if value_json.RfReceived.Data == 'D9970A' %}
          {{'ON'}}
        {% elif value_json.RfReceived.Data == 'D9970E' %}
          {{'OFF'}}
        {% else %}
          {{states('binary_sensor.extra_2') | upper}}
        {% endif %}    

    - name: "Motion Sensor RF"
      device_class: motion    
      state_topic: "tele/sonoff/RESULT"
      value_template: >-
        {% if value_json.RfReceived.Data == '3D0989' %}
          {{'ON'}}
        {% else %}
          {{states('binary_sensor.motion_sensor_rf') | upper}}
        {% endif %}
      off_delay: 5     
   
    - name: "Motion Sensor RF 2"
      device_class: motion    
      state_topic: "tele/sonoff/RESULT"
      value_template: >-
        {% if value_json.RfReceived.Data == 'F2E109' %}
          {{'ON'}}
        {% else %}
          {{states('binary_sensor.motion_sensor_rf_2') | upper}}
        {% endif %}
      off_delay: 5     
  
    - name: "Motion Sensor RF 3"
      device_class: motion    
      state_topic: "tele/sonoff/RESULT"
      value_template: >-
        {% if value_json.RfReceived.Data == 'D82219' %}
          {{'ON'}}
        {% else %}
          {{states('binary_sensor.motion_sensor_rf_3') | upper}}
        {% endif %}
      off_delay: 5  
  
    - name: "Motion Sensor RF 4"
      device_class: motion    
      state_topic: "tele/sonoff/RESULT"
      value_template: >-
        {% if value_json.RfReceived.Data == 'FD2F3E' %}
          {{'ON'}}
        {% else %}
          {{states('binary_sensor.motion_sensor_rf_4') | upper}}
        {% endif %}
      off_delay: 5   
      
   light:
     - schema: json
       name: 'AiLight1-DeskLamp'
       state_topic: 'AiLight1DL'
       command_topic: 'AiLight1DL/set'
       availability_topic: 'AiLight1DL/status'
       color_temp: true
       brightness: true
       rgb: true
       effect: false
       optimistic: true
       retain: true           
      
     - schema: json
       name: 'AiLight2-GoldLamp'
       state_topic: 'AiLight2GL'
       command_topic: 'AiLight2GL/set'
       availability_topic: 'AiLight2GL/status'         
       color_temp: true
       brightness: true
       rgb: true
       effect: true
       optimistic: true 
       retain: true     
      
     - schema: json
       name: 'Ailight3-Hallway'
       state_topic: 'Ailight3HL'
       command_topic: 'Ailight3HL/set'
       availability_topic: 'Ailight3HL/status'       
       color_temp: true
       brightness: true
       rgb: true
       effect: false
       optimistic: true 
       retain: true     
      
     - schema: json
       name: 'Lohas1-RedLamp'
       state_topic: 'Lohas1RL'
       command_topic: 'Lohas1RL/set'
       availability_topic: 'Lohas1RL/status'   
       color_temp: true
       brightness: true
       rgb: true
       effect: false
       optimistic: true
       retain: true           
      
     - schema: json
       name: 'Lohas2-Entry1'
       state_topic: 'Lohas2E1'
       command_topic: 'Lohas2E1/set'
       availability_topic: 'Lohas2E1/status'   
       color_temp: true
       brightness: true
       rgb: true
       effect: false
       optimistic: true
       retain: true           
      
     - schema: json
       name: 'Lohas3-Entry2'
       state_topic: 'Lohas3E2'
       command_topic: 'Lohas3E2/set'
       availability_topic: 'Lohas3E2/status'      
       color_temp: true
       brightness: true
       rgb: true
       effect: false
       optimistic: true
       retain: true           
      
     - schema: json
       name: 'Lohas4-WhiteLamp'
       state_topic: 'Lohas4WL'
       command_topic: 'Lohas4WL/set'
       availability_topic: 'Lohas4WL/status'  
       color_temp: true
       brightness: true
       rgb: true
       effect: false
       optimistic: true
       retain: true           
      
     - schema: json
       name: 'Lohas5'
       state_topic: 'Lohas5'
       command_topic: 'Lohas5/set'
       availability_topic: 'Lohas5/status'       
       color_temp: true
       brightness: true
       rgb: true
       effect: false
       optimistic: true
       retain: true      

     - schema: json
       name: 'Lohas6'
       state_topic: 'Lohas6'
       command_topic: 'Lohas6/set'
       availability_topic: 'Lohas6/status'    
       color_temp: true
       brightness: true
       rgb: true
       effect: false
       optimistic: true
       retain: true         

     - schema: json
       name: 'Lohas7-Laundry1'
       state_topic: 'Lohas7L1'
       command_topic: 'Lohas7L1/set'
       availability_topic: 'Lohas7L1/status'      
       color_temp: true
       brightness: true
       rgb: true
       effect: false
       optimistic: true
       retain: true      

     - schema: json
       name: 'Lohas8-Laundry2'
       state_topic: 'Lohas8L2'
       command_topic: 'Lohas8L2/set'
       availability_topic: 'Lohas8L2/status'       
       color_temp: true
       brightness: true
       rgb: true
       effect: false
       optimistic: true
       retain: true  

     - schema: json
       name: 'Lohas9-Laundry3'
       state_topic: 'Lohas9L3'
       command_topic: 'Lohas9L3/set'
       availability_topic: 'Lohas9L3/status'
       color_temp: true
       brightness: true
       rgb: true
       effect: false
       optimistic: true
       retain: true 

     - schema: json
       name: 'Lohas-LivingFan1'
       state_topic: 'Lohas-LivingFan1'
       command_topic: 'Lohas-LivingFan1/set'
       availability_topic: 'Lohas-LivingFan1/status'
       color_temp: true
       brightness: true
       rgb: true
       effect: false
       optimistic: true
       retain: true     
      
     - schema: json
       name: 'Lohas-LivingFan2'
       state_topic: 'Lohas-LivingFan2'
       command_topic: 'Lohas-LivingFan2/set'
       availability_topic: 'Lohas-LivingFan2/status'
       color_temp: true
       brightness: true
       rgb: true
       effect: false
       optimistic: true
       retain: true    
      
     - schema: json
       name: 'Lohas-LivingFan3'
       state_topic: 'Lohas-LivingFan3'
       command_topic: 'Lohas-LivingFan3/set'
       availability_topic: 'Lohas-LivingFan3/status'
       color_temp: true
       brightness: true
       rgb: true
       effect: false
       optimistic: true
       retain: true    
      
     - schema: json
       name: 'Lohas-LivingFan4'
       state_topic: 'Lohas-LivingFan4'
       command_topic: 'Lohas-LivingFan4/set'
       availability_topic: 'Lohas-LivingFan4/status'
       color_temp: true
       brightness: true
       rgb: true
       effect: false
       optimistic: true
       retain: true    
      
     - schema: json
       name: 'Lohas-LivingFan5'
       state_topic: 'Lohas-LivingFan5'
       command_topic: 'Lohas-LivingFan5/set'
       availability_topic: 'Lohas-LivingFan5/status'
       color_temp: true
       brightness: true
       rgb: true
       effect: false
       optimistic: true
       retain: true 
      
     - schema: json
       name: 'Lohas-LivingFan6'
       state_topic: 'Lohas-LivingFan6'
       command_topic: 'Lohas-LivingFan6/set'
       availability_topic: 'Lohas-LivingFan6/status'
       color_temp: true
       brightness: true
       rgb: true
       effect: false
       optimistic: true
       retain: true 
      
   fan:
    unique_id: "ceilingfan"
    name: "Living Room Fan"
    command_topic: "living_fan/on/set"
    state_topic: "living_fan/on/state"
    payload_on: "on"
    payload_off: "off"
    percentage_command_topic: 'living_fan/speed/set'
    percentage_command_template: >
        {% if value < 10 %}
            off
        {% elif value < 50 %}
            low
        {% else %}
            high 
        {% endif %}
    percentage_state_topic: 'living_fan/speed/state'
    percentage_value_template: >
        {% if value == 'low' %}
            33
        {% elif value == 'high' %}
            100
        {% endif %}

   sensor:
    - name: "Node Red Hassio"
      state_topic: "CheckIn/nodeREDHass"
      expire_after: 200
    
    - name: "Node Red Windows"
      state_topic: "CheckIn/nodeREDWin"
      expire_after: 200        

This passes the configuration check and seems to return all function to my entities.

1 Like

Hi. Thanks for your post.

Having dug a bit further into my config, it was so long ago I setup the door/ window sensors, I have found I have the following config for my unflashed Sonoff RFBridge:

  - platform: mqtt
    name: "Front Door"
    state_topic: "home/rfbridge/doorstate"
    payload_on: "frontdooropen"
    payload_off: "frontdoorclosed"
    qos: 0
    device_class: door

And this is the automation for each sensor:

alias: MQTT Front Door Open
description: ""
trigger:
  - platform: event
    event_type: sonoff.remote
    event_data:
      name: Front Door Open
condition: []
action:
  - service: mqtt.publish
    data:
      topic: home/rfbridge/doorstate
      payload: frontdooropen
mode: single

I need to figure out how to get this in the new format. Your post has given me some clues but I’m still scratching my head how to get this to work.

In my case would it be the following?

 binary_sensor:
    - name: "Front Door"
      device_class: Door
      state_topic: "home/rfbridge/doorstate"
      value_template: >-
        {% if value_json.RfReceived.Data == 'frontdooropen' %}
          {{'ON'}}
        {% elif value_json.RfReceived.Data == 'frontdoorclosed' %}
          {{'OFF'}}
        {% else %}
          {{states('binary_sensor.front_door') | upper}}
        {% endif %} 

You are right.

I ended up pretty much with the same config:

binary_sensor:
    - state_topic: "home/rfbridge/doorstate"
      name: "Front Door"
      payload_on: "frontdooropen"
      payload_off: "frontdoorclosed"
      qos: 0
      device_class: door

1 Like

In your case, is the topic home/rfbridge/doorstate limited to receiving the values frontdooropen or frontdoorclosed? Or can the topic receive other values (such as the status of other doors)?

I’m using two unflashed Sonoff RF Bridges with GS-WDS07 contact sensors. I found a guide a while back and applied it to my setup and it works perfectly.

I was really confused about getting this to work but as @randr had said I was overcomplicating it.

These are my sensors:

binary_sensor:
    - state_topic: "home/rfbridge/doorstate"
      name: "Front Door"
      payload_on: "frontdooropen"
      payload_off: "frontdoorclosed"
      qos: 0
      device_class: door

    - state_topic: "home/rfbridge/doorstate"
      name: "Kitchen Door Main"
      payload_on: "kitchendooropen"
      payload_off: "kitchendoorclosed"
      qos: 0
      device_class: door
      
    - state_topic: "home/rfbridge/doorstate"
      name: "Kitchen Door L"
      payload_on: "kitchendoorlopen"
      payload_off: "kitchendoorlclosed"
      qos: 0
      device_class: door
      
    - state_topic: "home/rfbridge/doorstate"
      name: "Kitchen Window L"
      payload_on: "kitchenwindowopen"
      payload_off: "kitchenwindowclosed"
      qos: 0
      device_class: window
      
    - state_topic: "home/rfbridge/doorstate"
      name: "Kitchen Window R"
      payload_on: "kitchenwindowropen"
      payload_off: "kitchenwindowrclosed"
      qos: 0
      device_class: window 
      
    - state_topic: "home/rfbridge/doorstate"
      name: "Bedroom Door Left"
      payload_on: "bedroomdoorleftopen"
      payload_off: "bedroomdoorleftclosed"
      qos: 0
      device_class: door
      
    - state_topic: "home/rfbridge/doorstate"
      name: "Bedroom Door Right"
      payload_on: "bedroomdoorrightopen"
      payload_off: "bedroomdoorrightclosed"
      qos: 0
      device_class: door
    
    ######MQTT Sensors bridge2###################################
    
    - state_topic: "home/rfbridge/doorstate"
      name: "Fridge Door"
      payload_on: "fridgedooropen"
      payload_off: "fridgedoorclosed"
      qos: 0
      device_class: door
      
    - state_topic: "home/rfbridge/doorstate"
      name: "Garden Gate"
      payload_on: "gardengateopen"
      payload_off: "gardengateclosed"
      qos: 0
      device_class: door
1 Like

When the payload frontdooropen is published to home/rfbridge/doorstate, it’s received by all three binary sensors. The only binary_sensor that knows what to do with that payload is binary_sensor.front_door because its configuration has payload_on: "frontdooropen".

The other binary_sensors don’t know what to do with frontdooropen because it doesn’t match their payload_on or payload_off. What state do they report when this happens? Do they report off or unknown?


EDIT

No need to reply; I performed a test and got the answer (the entities ignore payloads that fail to match their payload_on or payload_off).

1 Like

Anyone who is good at scripting able to come up with a YAML format converter for the new MQTT?

I really don’t get what all the drama is about.

This is probably one of the most straightforward fixes for any breaking change that I can remember.

What exactly are you having trouble with that you need a script to do this?

from:

sensor:
  - platform: mqtt
    name: something
    .
    .
  - platform: mqtt
    name: something_else
    .
    .

to this:

mqtt:
  sensor:
    - name: something
      .
      .
    - name: something_else
      .
      .
3 Likes