MQTT cannot discover my home made ESPeasy device

Not an answer… Just a thought.
I have switched all of my MQTT devices to use the ESPHome API. I wasn’t having any real problems with MQTT, but using the API just seemed a whole lot easier. Also, making all of my ESPHome devices speak the same language has greatly reduced the complexity of maintenance.

1 Like

Thanks for the tip. It’s a different road which I will explore later.

Hi, Also trying to get my head how to publish MQTT data and device being autodiscovered by HA.

Using the code below, can see the data being published via MQTT explorer. But device isn’t getting integrated or recognised by HA. Have other off the shelf tasmota firmware sensors on the network that automatically are added to my HA.

Question is, how does HA autodiscover a MQTT device?
Have re-used some of the code from this thread to form the publish data using micropython as follows:

from umqtt.simple import MQTTClient

payload={
	"name": "Water temperature zone 1",
	"unique_id": "Sensors-12_zone-1",
	"object_id": "Sensors-12_zone-1",
	"icon": "mdi:thermometer-lines",
	"state_topic": "Sensors-12/Sensor01/Zone-1",
	"unit_of_measurement": "°C",
	"device": {
		"name": "Sensors-12",
		"identifiers": "Sensors-12"
	}
}

def main():
    client = MQTTClient('config','192.168.0.111')
    client.connect()
    print("Connected to {}".format(CONFIG['broker']))

    while True:
        client.publish('{}/{}'.format('homeassistant/sensor/Water_temperature_zone_1',
                                          CONFIG['client_id']),
                                          bytes(str(payload), 'utf-8'))        
        time.sleep(5)

Thanks in advance!

Ensure the MQTT integration is installed and connected to your MQTT broker. This integration automatically discovers entities and devices via MQTT Discovery.

If you publish a discovery payload (JSON containing specific field names, like what you have created above) to a discovery topic (specific topic path starting withhomeassistant and similar to what you created above), Home Assistant will create the entity (and optional device) described in the discovery payload. It’s important to publish the payload as a retained message (otherwise when you restart the broker, the published discovery payload is purged from the broker).


NOTE

Unless there’s a good reason for your MQTT topics to include uppercase letters, I suggest you use lowercase letters exclusively (it doesn’t hurt legibility).

Thanks Taras,

Getting close (I think), can at least see a discovery error in HA as follows:
2023-08-28 00:55:08.817 WARNING (MainThread) [homeassistant.components.mqtt.discovery] Unable to parse JSON esp32: ‘{‘name’: ‘water temperature zone 1’}’
Tried a “few” variations of the code, but here’s a simplified version of it:

payload={"name": "water temperature zone 1"}
client = MQTTClient('config','192.168.0.111')
client.connect()
client.publish('{}/{}'.format('homeassistant/sensor','esp32/config'),bytes(str(payload), 'utf-8'))

Your example uses characters that aren’t permitted in certain options. Try this version:

{
	"name": "Water temperature zone 1",
	"unique_id": "sensors_12_zone_1",
	"object_id": "sensors_12_zone_1",
	"icon": "mdi:thermometer-lines",
	"state_topic": "Sensors-12/Sensor01/Zone-1",
	"unit_of_measurement": "°C",
	"device": {
		"name": "Sensors-12",
		"identifiers": ["sensors_12"]
	}
}

Also, the identifiers option expects a list value.

I’m out and about for a few days, but now hanging to test this out.

keep you posted.

Again thanks for the help.

Rob.

Using your version still returns the error.
Not sure if this could be an issue, but comparing the published data in MQTT explorer with other devices, noticed Tasmota devices publish using double quotation as opposed to mine are single quotations.
2023-08-28 10:17:09.634 WARNING (MainThread) [homeassistant.components.mqtt.discovery] Unable to parse JSON esp32: ‘{‘name’: ‘Water_temperature_zone_1’, ‘state_topic’: ‘Sensors-12/Sensor01/Zone-1’, ‘unit_of_measurement’: ‘\xb0C’, ‘object_id’: ‘sensors_12_zone_1’, ‘icon’: ‘mdi:thermometer-lines’, ‘device’: {‘name’: ‘Sensors-12’, ‘identifiers’: [‘sensors_12’]}, ‘unique_id’: ‘sensors_12_zone_1’}’
Only change to your payload is changes spaces to underscore

payload={
	"name": "Water_temperature_zone_1",
	"unique_id": "sensors_12_zone_1",
	"object_id": "sensors_12_zone_1",
	"icon": "mdi:thermometer-lines",
	"state_topic": "Sensors-12/Sensor01/Zone-1",
	"unit_of_measurement": "°C",
	"device": {
		"name": "Sensors-12",
		"identifiers": ["sensors_12"]
	}
}

Here’s a screenshot of MQTT Explorer displaying a switch’s discovery payload. I use a (Home Assistant) script to create various types of entities (switch, binary_sensor, cover, climate, etc). It shows that the JSON payload employs double-quotes.

image

You’re saying the same view for you shows single-quotes in the payload?


EDIT

I’m no python expert but I get the impression, from the example below, that you need to convert the python dictionary into a JSON string using json.dumps()

import json

# ... your other code here ...

payload={
	"name": "Water_temperature_zone_1",
	"unique_id": "sensors_12_zone_1",
	"object_id": "sensors_12_zone_1",
	"icon": "mdi:thermometer-lines",
	"state_topic": "Sensors-12/Sensor01/Zone-1",
	"unit_of_measurement": "°C",
	"device": {
		"name": "Sensors-12",
		"identifiers": ["sensors_12"]
	}
}
client = MQTTClient('config','192.168.0.111')
client.connect()
client.publish('homeassistant/sensor/esp32/config', bytes(json.dumps(payload), 'utf-8'))

you can also just keep it a string that’s json

client.publish('homeassistant/sensor/esp32/config', json.dumps(payload))

Relevant MicroPython docs:

https://docs.micropython.org/en/latest/library/json.html

We have a winner!!!
before conversion:


After changing to bytes(json.dumps we get this…

And HA accepted the discovery packet and added the device.

Thanks all for your help here. Very appreciated.

really just haven’t got my head around with the formatting structure of MQTT
All I’m attempting below is to publish Water_temperature_zone_1 with a value of 1.
Have tried all sorcery, with no luck againg.

payload1={
	"name": "Water_temperature_zone_1",
	"unique_id": "sensors_1",
	"object_id": "sensors_1",
	"icon": "mdi:thermometer-lines",
	"state_topic": "Sensors-1/Sensor01/Zone-1",
	"unit_of_measurement": "°C",
	"device": {
		"name": "Sensors-1",
 		"manufacturer": "xx",
 		"model": "123456",
		"identifiers": ["sensors_1"]
	}
}
temperature=1
payload={
	"name": "Water_temperature_zone_1",
	"unique_id": "sensors_1",
	"object_id": "sensors_1",
	"icon": "mdi:thermometer-lines",
	"state_topic": "Sensors-1/Sensor01/Zone-1",
	"unit_of_measurement": "°C",
	"value_template":   temperature,
	"device": {
		"name": "Sensors-1",
 		"manufacturer": "xx",
 		"model": "123456",
		"identifiers": ["sensors_1"]
	}
}

client.connect()
client.publish("{}/{}".format("homeassistant/sensor","VBAT/config"),bytes(json.dumps(payload1), "utf-8"))

def main():
    client.connect()
    while True:
        client.publish("{}/{}".format("homeassistant/sensor","VBAT"),bytes(json.dumps(payload), "utf-8"))
        print('Sensor state: {}'.format(payload))
        
        time.sleep(5)

To create an MQTT Sensor via MQTT Discovery, you publish a Discovery payload to the discovery topic. You succeeded in doing that when you published the payload in your previous post to homeassistant/sensor/esp32/config. The resulting entity is sensor.sensors_1.

You have configured sensor.sensors_1 to receive payloads via the topic Sensors-1/Sensor01/Zone-1. The physical device represented by sensor.sensors_1 must publish its temperature value to Sensors-1/Sensor01/Zone-1.