I have manually crafted discovery topics for a home-brew device that has several sensor entities that are regularly updated through a single json over mqtt, i.e. I’ve published six different topics - one for each of its sensors. That all works fine (even though I’m wondering why this can’t be accomplished by publishing a single discovery topic with a suitable structure?).
Regardless, I actually have a whole bunch of these devices (i.e. one for each room in my house) and they all emit the same formatted json object over mqtt differing only by a unique name in their published topic - I feel there ought to be a way to on-board them all to HA using some sort of ‘class’ of discovery.
I think my motivation stems from not having the discovery messages being emitted by the devices themselves (not without a lot of effort to update legacy embedded firmware). So I’m faced with doing a lot of cut & paste work to publish numerous near-identical messages which will be tedious and error prone.
So far, I’ve created and published the discovery topics from within node-red as I understand you can’t combine multiple entities into a device using yaml config. I suppose I could create a function to automate the process but it all feels a bit clunky.
Just mentioning this in case someone notices that I’m missing a trick?
Make an automation that does this. That’s what I do. Trigger the automation on home assistant startup. Use a for loop, to iterate a list of topics and unique_ids. Or build the topic from the list of unique_ids.
Not sure I understand the “problem”.
Discovery topics are a 1-time thing if the messages are retained, and can be published from anywhere (plain script can do).
I didn’t see any suggestion that automations could be used to set up mqtt discovery in the docs (not in the MQTT Discovery section at least) but I guess it makes sense.
It’s just the sheer number of messages required to ‘automate’ device discovery when you have a large number of devices each with a significant set of individual entities. No great problem if the devices themselves are programmed to be compatible with HA discovery - but a pain when having to being done manually if not.
Good question! If the device’s topics were something like home.kitchen.gadget and home.lounge.gadget and home.bedroom.gadget then, with gadget being the common name shared by all the identical devices, auto discovery could be a single topic relating to gadget such that when HA sees topics ending in gadget it knows how to access the entities.
Class gadget therefore only needs to de defined once and the path leading to it can be used to id the individual member entities.
Sure, issuing service calls from an automation could do it but I just knocked-up a function in node-red that wildcard subscribed to the topics and then sent the discovery messages accordingly. In JS it was a doddle to parse out the location names from the topics (which were converted to first letter uppercase and “_” replaced by " ") and use that as ‘friendly names’ etc. Not sure I could have done that in yaml (not with my skill set anyway)
Good job HA treats subsequent discovery messages as updates as it took a few attempts to make everything look pretty! I intend to just toggle the enable for this function in NR if and when I add more gadgets.