Mqtt light entity not discovered when setting availibility_topic

Have an mqtt device with multiple entities that are discovered by HomeAssistant.
The entities :

  • Light
  • Sensor
  • Number
  • Select

I want HomeAssistant to show the entities (or actually the device, but I read that is is feature request) as unavailable when my light goes offline.
So I connect to the broker with a Last Will topic and message.
And, when I publish the discovery topic to HomeAssistant (through the broker), I add the following to the payload of the topic:

doc[“avty_t”] = “stat/my_id/available” ;

I do this for all entities and it works.
EXCEPT when I add it to the light_entity. Then the light entity is not discovered

The entire code for creating the light_entity is:

char stacom[30];            // create the status_topics and cmnd_topics
    char pl_buffer[512];

        // Topic  
          strcpy(topic,"homeassistant/light/");         
          strcat(topic, devUniqueID_);                   
                                                        
          strcat(topic,"S/config");               
          Serial.print("Add topic ");  Serial.println(topic);  
        // Json payload
        // unique id based on devUniqueID_
          strcpy(uid,devUniqueID_);
          strcat(uid,"S"); 
          doc.clear();
          doc["name"]       = "My MQTT Light";            
          doc["ojb_id"]     = "mqtt_light";               
          doc["uniq_id"]    = uid;  
          
          strcpy(stacom,"stat/");
          strcat(stacom,sta_comtop);
          strcat(stacom,"/switch");
          doc["stat_t"]     = stacom;

          strcpy(stacom,"cmnd/");
          strcat(stacom,sta_comtop);
          strcat(stacom,"/switch");
          doc["cmd_t"]      = stacom;  
          
          doc["brightness"] = "true";  
          doc["bri_scl"]    = "255";  
          doc["sup_clrm"]   = "brightness"; 

          strcpy(stacom,"stat/");
          strcat(stacom,sta_comtop);
          strcat(stacom,"/brightness");
          Serial.println(stacom);
          doc["bri_stat_t"]     = stacom;
          
          strcpy(stacom,"cmnd/");
          strcat(stacom,sta_comtop);
          strcat(stacom,"/brightness");
          doc["bri_cmd_t"]     = stacom;

  // availability topic.. the light entity is not created when i add this
/*          
          strcpy(stacom,"stat/");
          strcat(stacom,sta_comtop);
          strcat(stacom,"/avail");
          doc["avty_t"] = stacom;                         //"availability_topic" = "avty_t" 
*/

          // BELONGS TO Device
          JsonObject device = doc.createNestedObject("device");                                                          
          device["ids"]   = MyMQTTDevice[dv_ids_].value;              // those are from an array of char[]    
          device["name"]  = MyMQTTDevice[dv_name].value;              // it works fine if i don't add the availibility_topic
          device["mf"]    = MyMQTTDevice[dv_manu].value;            
          device["mdl"]   = MyMQTTDevice[dv_modl].value;            
          device["sw"]    = MyMQTTDevice[dv_sofw].value;           
          device["hw"]    = MyMQTTDevice[dv_harw].value;           
          device["sn"]    = MyMQTTDevice[dv_sern].value;            
          device["sa"]    = "Office";
          device["cu"]    = "http://192.168.0.203/config";          
          
          serializeJson(doc,pl_buffer);                               // creates a single char_array from doc, puts it into buffer1 (payload) 
          MyPSClient.publish(topic, pl_buffer, true);                 // publish

The length of that payload WITH the availability topic is 495 chars. (so it fits in the buffer just fine)
I have tried to increase the buffer, but that does not solve the problem

Additional observation(s)

First:

  • Every entity (except the light entity) gets its HomeAsssistant entity_id the /config payload’s object_id
    So that:
    doc[“object_id”] = “mqtt_temperature”
    gives in homeAssistant sensor.mqtt_temperature

  • The light entity gets its HomeAssistant entity_id from a combination of the device_name + the entity_name

Entity:
doc[“name”] = “My MQTT Light”;
doc[“obj_id”] = “something”;
Device:
device[“name”] = “Office Light”;
gives in homeAssistant light.office_light_my_mqtt_light

More importantly:

The light_entity does NOT get created when it’s declaration includes the availability_topic
But it DOES get created even when it has the availability_topic IF it is not the first entity to be discovered (from the same device)
So sending the /config topic of the temperature sensor FIRST (with availability_topic, and the /config topic of the light entity SECOND (with reference to the device specified in the temperature sensor) and with the availabilty_topic set… DOES WORK

Mind blown.