I have a device which is successfully doing MQTT autodiscovery for multiple entities. I have set all entities to have an availability_topic which is pointed at the device status and I use a LWT on that status. This works well and if the device goes offline, all the entities show as unavailable. So far, so good.
Now I want to go further. For some entities I want the “availability” to be based on two factors: The device status AND one entity status. If I look at this page, it seems to say that I can set “availability_mode” to “all” and use two “availability_topic” values to get what I want. But I can’t seem to get two “availability_topic” keys into the config entry in the MQTT broker. The second one always “overwrites” the first and I’m left with one. I’m using the Mosquitto broker addon. The JSON I send for the autodiscovery homeassistant config has both “availability_topic” lines, but when I look with MQTT Explorer, only the 2nd one is there. I can change the order and test that each one is individually working. But I can’t get BOTH to work together as the availability_mode says they should.
Is this a limitation of the Mosquitto broker? Or a limit of MQTT? Or …?? It seems that ANY duplicate JSON keys sent to the broker simply overwrite the previous.
But when I look in MQTT explorer, I see the identical JSON except there is only the 2nd availability_topic. I have changed the order and verified that each one is working correctly as a single availability_topic. I just can’t get both into the broker.
OK, I solved this myself. Here’s my solution in case it can help someone else…
Basically I had to give up on using multiple availability_topic keys. Documentation says this works, and I assume that it does work if you configure them in yaml. But you can’t get multiple of them into the Mosquito MQTT broker to allow for MQTT auto-discovery. So instead of having 3 different “state sensors” (one being the device status which uses the LWT and the other two being binary sensors) I combined all three into one MQTT topic as JSON with three internal values. My LWT is JSON with all three set to “offline”, and my device sends an MQTT update with the first value always “online” and the other two set appropriately to either “online” or “offline”. Then for each of my other sensors I set a single availability_topic pointing to that topic, and I use an availability_template to logically parse the three values and determine the availability. So some sensors are online or offline based on just one value. Some others require that a specific two of them be online. And some require all three. For each sensor/entity I can logically “and” or “or” any of the three to produce that device’s availability.