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