Mqtt device discovery / device registry

Hey guys,
I know there are many posts to this topic but since no solution worked out for me, I thought I post my problem and hopefully someone can help me.
So I am trying to add two entities via the topics from my esp32 and group them in a device via mqtt discovery:

 "homeassistant/switch/"+clientId+"/switch_1/config"
 "homeassistant/switch/"+clientId+"/switch_2/config"

The payloads are:

char json[512];
  size_t n = sprintf(json, "{"
    "\"name\":\"Switch 1\","
    "\"uniq_id\":\"switch_1\","
    "\"stat_t\":\"%s\","                                //sw1_state_topic
    "\"cmd_t\":\"%s\","                               //sw1_command_topic
    "\"pl_on\":\"on\","
    "\"pl_off\":\"off\","
    "\"ret\":true,"
    "\"dev\":{"
      "\"ids\":[\"%s\"],"                                //clientId
      "\"name\":\"%s\","                              //host
      "\"mdl\":\"%s\","                                 //host
      "\"sw\":\"%s\","                                  //version
      "\"mf\":\"lg\""
    "}"
  "}", sw1_state_topic, sw1_command_topic, clientId, host, host, version);

and

char json[512];
  size_t n = sprintf(json, "{"
    "\"name\":\"Switch 2\","
    "\"uniq_id\":\"switch_2\","
    "\"stat_t\":\"%s\","                                //sw2_state_topic
    "\"cmd_t\":\"%s\","                               //sw2_command_topic
    "\"pl_on\":\"on\","
    "\"pl_off\":\"off\","
    "\"ret\":true,"
    "\"dev\":{"
      "\"ids\":[\"%s\"],"                                //clientId
      "\"name\":\"%s\","                             //host
      "\"mdl\":\"%s\","                                //host
      "\"sw\":\"%s\","                                 //version
      "\"mf\":\"lg\""
    "}"
  "}", sw2_state_topic, sw2_command_topic, clientId, host, host, version);

The interesting part is that the entities are added when i leave out the “dev”-Part. But with the dev-Part nothing happens at all. Homeassistant logs dont show anything either. I am using the Homeassistant docker-container with a mosquitto-container.

What do you actually have in MQTT?

I am publishing the json data with

client.publish(discoveryTopic.c_str(), json, n);

client is an instance of the PubSubClient Library.
Is this what you are talking about? If not can you be more precise?

Show the actual mqtt message.

Ok. I just set up an mqtt logger. Thats why i did not see mqtt messages in the logs. I found out that homeassistant does not receive anything. But I just verified again that the message is received when leaving out the dev part. The Esp does not throw any Error :confused:
Is it more likely that the message is not even sent or that it gets lost on the way/ is not received?

I found the Issue. In the Arduino Library i am using (PubSubClient) is a hardcoded max package size of 256. When the “dev”-Part is included, the byte array exceeds that limit and the publishing fails.
It is possible to resize it by calling

setBufferSize(uint16_t size)
1 Like

Happy for you finding out the solution. I’m developing an object oriented Arduino MQTT Discovery library, it may simplify building the messages and topics. https://github.com/cyijun/HAMqttDiscoveryHandler