MQTT entities dissapear after updating config.yaml to new Mqtt format

Hi everyone.

I tried to update the mqtt sensors in my config.yaml to meet the new format requirements that will be coming in a future update release.

My old entries were as follows:-

- platform: mqtt
  state_topic: "/inverter"
  name: "_acRadiatorTemperature"
  expire_after: 360
  value_template: "{{   value_json._acRadiatorTemperature }}"
  unit_of_measurement: '°C'
  force_update: true
- platform: mqtt
  state_topic: "/inverter"
  name: "_dcRadiatorTemperature"
  expire_after: 360
  value_template: "{{   value_json._dcRadiatorTemperature }}"
  unit_of_measurement: '°C'
  force_update: true
- platform: mqtt
  state_topic: "/inverter"
  name: "_transformerTemperature"
  expire_after: 360
  value_template: "{{   value_json._transformerTemperature }}"
  unit_of_measurement: '°C'
  force_update: true
- platform: mqtt
  state_topic: "/inverter"
  name: "_battCurrent"
  expire_after: 360
  value_template: "{{   value_json._battCurrent }}"
  unit_of_measurement: 'A'
  device_class: power
  force_update: true

My new entries look like this:-

mqtt:
  sensor:
    state_topic: "/inverter"
    name: "_acRadiatorTemperature"
    expire_after: 360
    value_template: "{{   value_json._acRadiatorTemperature }}"
    unit_of_measurement: '°C'
    force_update: true
  sensor:
    state_topic: "/inverter"
    name: "_dcRadiatorTemperature"
    expire_after: 360
    value_template: "{{   value_json._dcRadiatorTemperature }}"
    unit_of_measurement: '°C'
    force_update: true
  sensor:
    state_topic: "/inverter"
    name: "_transformerTemperature"
    expire_after: 360
    value_template: "{{   value_json._transformerTemperature }}"
    unit_of_measurement: '°C'
    force_update: true
  sensor:
    state_topic: "/inverter"
    name: "_battCurrent"
    expire_after: 360
    value_template: "{{   value_json._battCurrent }}"
    unit_of_measurement: 'A'
    device_class: power
    force_update: true

After making the changes, I ran CHECK CONFIGURATION and there were no errors.

However after I restart HA, all of the Mqtt entities seem to disappear.

Any pointers as to where I may be going wrong?

Thanks

Your syntax is not quite right

Use

mqtt:
  sensor:
    - state_topic: "/inverter"
      name: "_acRadiatorTemperature"
      expire_after: 360
      value_template: "{{   value_json._acRadiatorTemperature }}"
      unit_of_measurement: '°C'
      force_update: true
    - state_topic: "/inverter"
      name: "_dcRadiatorTemperature"
      expire_after: 360
      value_template: "{{   value_json._dcRadiatorTemperature }}"
      unit_of_measurement: '°C'
      force_update: true
    - state_topic: "/inverter"
      name: "_transformerTemperature"
      expire_after: 360
      value_template: "{{   value_json._transformerTemperature }}"
      unit_of_measurement: '°C'
      force_update: true
    - state_topic: "/inverter"
      name: "_battCurrent"
      expire_after: 360
      value_template: "{{   value_json._battCurrent }}"
      unit_of_measurement: 'A'
      device_class: power
      force_update: true

Thanks very much!!

This seems to have fixed the issue.

I’d like to post a good recommendation on how to do the new mqtt syntax

First create a directory called mqtt in the config folder of Home Assistant

In configuration.yaml add this one line

mqtt: !include_dir_merge_named mqtt

For each old “platform” create a file. You can name it anything but it must end with .yaml
and it makes sense to make e.g. a file called mqtt-lights.yaml

I chose to prefix my file names with “mqtt-” because if I get an error in the yaml check I can clearly see in the log if it is the old sensors.yaml file which you may still have for other non-mqtt platforms or the new mqtt-sensors.yaml that contains the syntax error

And inside the file the format will be (ignore that I have a ventillator I pretend it s light)

light:
  - name: "Bedroom Ventilator"
    unique_id: "Bedroom Ventilator as Light"
    state_topic: "bedroom-ventilator/state"
    command_topic: "bedroom-ventilator/set"
    brightness_state_topic: "bedroom-ventilator/speed/state"
    brightness_command_topic: "bedroom-ventilator/speed/set"
    payload_on: "on"
    payload_off: "off"
    retain: "on"
    icon: "mdi:fan"
    
  - name: Magic Mirror
    unique_id: "Magic Mirror"
    command_topic: "magicmirror/set"
    state_topic: "magicmirror/state"
    payload_on: "on"
    payload_off: "off"

and for a few more examples

a file called mqtt-sensor.yaml

sensor:
   
  - name: "nfcmqtt"
    state_topic: "alarm/nfcid"
    unique_id: "nfcmqtt"
    
  - name: "Bathroom Rising"
    state_topic: "fan/rising"
    unique_id: "Bathroom Rising"
    unit_of_measurement: '%'
  
  - name: "Bathroom Falling"
    state_topic: "fan/falling"
    unique_id: "Bathroom Falling"
    unit_of_measurement: '%'
    
  - name: "Bathroom Humidity"
    state_topic: "fan/humidity"
    unique_id: "Bathroom Humidity"
    unit_of_measurement: '%'
    
  - name: "Fan Temperature"
    unique_id: "Fan Temperature"
    state_topic: "fan/temperature"
    unit_of_measurement: '°C'

and same with mqtt-binary-sensor.yaml

binary_sensor:


  - name: "Mailbox"
    state_topic: "mailserver/state"
    unique_id: "Mailbox sensor"
    payload_on: "on"
    payload_off: "off"
   
  - name: "Bed Sensor Kenneth"
    state_topic: "bedsensor/state/kenneth"
    unique_id: "Bed Sensor Kenneth"
    payload_on: "on"
    payload_off: "off"
    
  - name: "Bed Sensor Diane"
    state_topic: "bedsensor/state/diane"
    unique_id: "Bed Sensor Diane"
    payload_on: "on"
    payload_off: "off"
  
  - name: "Front Door"
    state_topic: "sensor/frontdoor"
    unique_id: "Front Door Sensor"
    payload_on: "on"
    payload_off: "off"
    device_class: "door"
    

Here is one for blinds mqtt-blinds.yaml

cover:
  - name: "Left Blinds"
    unique_id: "Left Blinds"
    command_topic: "blinds/set/left"
    state_topic: "blinds/position/left"
    position_topic: "blinds/position/left"
    set_position_topic: "blinds/set/left"
    retain: false
    payload_open: "50"
    payload_close: "0"
    payload_stop: "85"
    state_open: "50"
    state_closed: "0"
    optimistic: false
    device_class: blind
   
  - name: "Right Blinds"
    unique_id: "Right Blinds"
    command_topic: "blinds/set/right"
    state_topic: "blinds/position/right"
    position_topic: "blinds/position/right"
    set_position_topic: "blinds/set/right"
    retain: false
    payload_open: "50"
    payload_close: "0"
    payload_stop: "85"
    state_open: "50"
    state_closed: "0"
    optimistic: false
    device_class: blind
   
  - name: "Office Blinds"
    unique_id: "Office Blinds"
    command_topic: "office-blinds/set"
    state_topic: "office-blinds/position"
    position_topic: "office-blinds/position"
    set_position_topic: "office-blinds/set"
    retain: false
    payload_open: "50"
    payload_close: "0"
    payload_stop: "85"
    state_open: "50"
    state_closed: "0"
    optimistic: false
    device_class: blind
  

And here are some device_trackers in a mqtt-device-tracker.yaml
Note that device_trackers do not get loaded unless you restart Home Assistant. This is a known bug and there is a PR in the pipe line for this

device_tracker:
  - name: "BLE Button Kenneth1"
    unique_id: "ble-button-kenneth1"
    state_topic: "blescan/1/kenneth"
    payload_home: "on"
    payload_not_home: "off"
    source_type: "bluetooth_le"
  
  - name: "BLE Button Diane1"
    state_topic: "blescan/1/diane"
    unique_id: "ble-button-diane1"
    payload_home: "on"
    payload_not_home: "off"
    source_type: "bluetooth_le"
    
  - name: "BLE Button Kenneth2"
    state_topic: "blescan/2/kenneth"
    unique_id: "ble-button-kenneth2"
    payload_home: "on"
    payload_not_home: "off"
    source_type: "bluetooth_le"
    
  - name: "BLE Button Diane2"
    state_topic: "blescan/2/diane"
    unique_id: "ble-button-diane2"
    payload_home: "on"
    payload_not_home: "off"
    source_type: "bluetooth_le"

Here are fans mqtt-fans.yaml

fan:
  - name: "Fan"
    unique_id: "Fan Bathroom"
    state_topic: "fan/state"
    command_topic: "fan/set"
    payload_on: "on"
    payload_off: "off"
  
  - name: "Utility Fan"
    unique_id: "Utility Fan"
    state_topic: "utility-fan/state"
    command_topic: "utility-fan/set"
    payload_on: "on"
    payload_off: "off"

And then some switches in a mqtt-switches.yaml

switch:
  - name: "Short Toilet Flush"
    unique_id: "Short Toilet Flush"
    state_topic: "toiletflushshort/state"
    command_topic: "toiletflushshort/set"
    payload_on: "ON"
    payload_off: "OFF"
    state_on: "ON"
    state_off: "OFF"
    optimistic: false
    qos: 0
    retain: false
  
  - name: "Long Toilet Flush"
    unique_id: "Long Toilet Flush"
    state_topic: "toiletflushlong/state"
    command_topic: "toiletflushlong/set"
    payload_on: "ON"
    payload_off: "OFF"
    state_on: "ON"
    state_off: "OFF"
    optimistic: false
    qos: 0
    retain: false

This is a long post. But I know that what people really need is real life working examples and not condensed documentation.

2 Likes

this perfectly sums up me banging my head against a wall for the last 3 days trying to figure something out.

I’m going to do this right now! I’ve actually been meaning to do it for a while, not just for the MQTT stuff, but splitting everything up to make it more readable

1 Like

Thank you Kenneth.

I will give you suggestion a try. Seems like a much neater approach.

Sorry to dredge an ageing thread but I have the same issue and can’t see what is wrong with my formatting. Everything worked before but when I move to the new format I can not see and entry for eg this

mqtt:
  switch:
    - name: "Lounge Lamp"
      state_topic: "stat/christmaslights/POWER1"
      command_topic: "cmnd/christmaslights/POWER1"
      availability:
        - topic: "tele/christmaslights/LWT"
      qos: 0
      payload_on: "ON"
      payload_off: "OFF"
      payload_available: "Online"
      payload_not_available: "Offline"

Before it was like this (and worked fine):

switch:
  - platform: mqtt
    name: "Lounge Lamp"
    state_topic: "stat/christmaslights/POWER1"
    command_topic: "cmnd/christmaslights/POWER1"
    availability_topic: "tele/christmaslights/LWT"
    qos: 0
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"

Note - I still have some mqtt switches and in the old format - I changed one of them to the new format, could this be part of the issue? (So I still have Switch:Platform:mqtt AND mqtt:switch: in my configuration.yaml file.)

Can anyone see what looks wrong? Many thanks

If you came here, as I did, searching a reason why my mqtt sensors dissapear when configuring in the new mqtt format. In my case it was because I include the tag ‘sensor:’ at the top of EACH file in the new mqtt subfolder for splitting. That made only the first file for sensors to be processed.

I finally have been able to order everything how I like now. This is my working splitted config, in which I have been able to separate different appliances by file with the new MQTT syntax (as I used to do with the previous ‘- platform: mqtt’ format:

This is my new configuration.yaml file, dividing mqtt sensors, switches, etc, by subfolders:

---configuration.yaml
[...]
input_select: !include_dir_merge_named programaciones/input_selects/
input_text: !include_dir_merge_named programaciones/input_texts/
mqtt:
  sensor: !include_dir_merge_list programaciones/mqtts/sensors/
  switch: !include_dir_merge_list programaciones/mqtts/switches/
notify: !include_dir_merge_list programaciones/notify/
[...]
---

This is a detail of my folders tree:

subfolder for mqtt sensors only:
  programaciones/mqtts/sensors/
                               mqtt_sensors_piscina.yaml
                               mqtt_sensors_zigbee.yaml
                               etc...

subfolder for mqtt switches only:
  programaciones/mqtts/switches/
                                mqtt_switches_piscina.yaml
                                mqtt_switches_gas_detector.yaml
                                etc...

This is a sample of a mqtt sensors file for the pool (do not include ‘sensor:’ at the top of it. That’s implicit in the configuration.yaml file entry):

---mqtt_sensors_piscina.yaml
#############################################
####   DHT22 TEMP & HUMIDITY POOL ROOM    ###
#############################################

- name: "DHT22 temperatura"
  state_topic: "/arduino/dht22temperatura"
  unit_of_measurement: "C"


- name: "DHT22 humedad"
  state_topic: "/arduino/dht22humedad"
  unit_of_measurement: "%"


##################################################
####   ULTRASONICO DE DISTANCIA, WATERPROOF    ###
##################################################

- name: "Ultrasonico nivel piscina"
  state_topic: "/arduino/ultrasonicodistancia"
  unit_of_measurement: "cm"



############################################
###   PT100 (max31865) (temp tuberia)    ###
############################################

- name: "pt100 temperatura"
  state_topic: "/arduino/pt100temperatura"
  unit_of_measurement: "C"
---

This is a sample of a second mqtt sensors file only for the zigbee sensors:

---mqtt_sensors_zigbee.yaml
### CREA UN SENSOR DEL ESTADO DEL BRIDGE MQTT<->ZIGBEE
- name: Bridge state
  state_topic: "zigbee2mqtt/bridge/state"
  icon: mdi:router-wireless

### CREA UN SENSOR PARA EL ADDON NETWORKMAP DE HACS DE ZIGBEE
- name: Zigbee2mqtt Networkmap
  state_topic: zigbee2mqtt/bridge/response/networkmap
  value_template: >-
    {{ now().strftime('%Y-%m-%d %H:%M:%S') }}
  json_attributes_topic: zigbee2mqtt/bridge/response/networkmap
  json_attributes_template: "{{ value_json.data.value | tojson }}" 
---

And finally a sample of file for mqtt switches only for the pool zone. (do not include ‘switch:’ at the top of it. That’s implicit in the configuration.yaml file too):

---mqtt_switches_piscina.yaml
######################
####   ARDUINO     ###
######################

### PIN22
- unique_id: arduino_pin_22
  name: "Arduino pin 22"
  state_topic: "/arduino/switch22"
  command_topic: "/arduino/switch22"
  payload_on: "1"
  payload_off: "0"
#  qos: 0
  optimistic: false
  retain: true

### PIN23
- unique_id: arduino_pin_23
  name: "Arduino pin 23"
  state_topic: "/arduino/switch23"
  command_topic: "/arduino/switch23"
  payload_on: "1"
  payload_off: "0"
#  qos: 0
  optimistic: false
  retain: true

[etc...]
---

The adventage of this config is that I can have subfolders division for mqtt switches and mqtt sensors, etc, and also separate files for each device inside those subfolders. And not all the mqtt-sensors.yaml file or mqtt-switches.yaml file, with all the sensors, switches, for all devices in this same file which could end up being a large text file hard to maintain.

Hope it helps someone…