Multiple MQTT discovery prefixes

Hi All,

I have been wondering if it possible for the home assistant to have multiple discovery prefixes. This is to ensure that I can develop several devices with appropriate naming for the prefix. Just to have better readability. However, I wonder if there is a possibility to do so in the configuration.yaml.

The current idea that I have in my is to have a node-red function to modify the published discovery message from my device lineup, then re-publish them with the topic that home assistant does the discovery.

I am all ears to have an input on this matter. Thanks in advance.

@anuradhawick - I have been researching this exact issue today as well. The documentation for HA MQTT only references a single discovery prefix (https://www.home-assistant.io/docs/mqtt/discovery/) and throughout forums I have found no evidence of alternatives.

In my case, I have both zwave2mqtt and zigbee2mqtt feeding Mosquitto. What was not clear to me is the HA discovery integration for each of the 2mqtt’s give’s impression that each can essentially publish to prefix [zwave2mqtt | zigbee2mqtt] and by nature of HA discovery intergrations also [homeassistant]. Kind of fuzzy and I am unable to see that work or find documentation which spells out how.

So, it seems its a single prefix for the HA intance (e.g. homeassistant, or whatever single prefix you specify in discovery_prefix: within mqtt: configuration.yaml)

1 Like

The root of the discovery topic can be changed (the default is homeassistant) but you can only have one discovery topic.

As mentioned, you can use a ‘middleman’ to receive via a custom discovery topic and re-publish it to Home Assistant’s discovery topic. This can be done using an automation or Node-Red (whatever you prefer).

1 Like

Thanks for the information. It seems we’ll have to use a middle man like node red to do this as @123 mentioned.

If you’re interested, it can also be done with an automation. I would need to see a sample of your custom discovery topic to provide a suitable automation example.

1 Like

Thanks for the offer. My mqtt discovery prefixes are like wickmesh-light, wickmesh-sensor, etc. I am publishing these topics from a bridge device that I made. I am not familiar with automation. I’ll be much obliged if you could show me how to do one for say a lighting device, so I could follow the rest up.

Cheers

I suggest you change the format from:

wickmesh-light
wickmesh-sensor

to:

wickmesh/light
wickmesh/sensor

The reason is because it becomes easier to listen to all topics that begin with wickmesh. This is true if you use an automation or a Node-Red flow.

  trigger:
    platform: mqtt
    topic: wickmesh/#

For more information: MQTT Essentials: Topics & Best Practices

Assuming the topics are in this format:

wickmesh/light/kitchen
wickmesh/light/front
wickmesh/sensor/exterior
wickmesh/binary_sensor/garage_door

the automation is as simple as this:

- alias: 'Example 1'
  trigger:
    platform: mqtt
    topic: wickmesh/#
  action:
    service: mqtt.publish
    data_template:
      topic: "{{ trigger.topic.replace('wickmesh', 'homeassistant') }}"
      payload: "{{ value }}"

On the other hand, if you are unable to change the format of the discovery topic, then the automation is slightly more complicated. It will require multiple triggers, one for each type of topic. The template will have to perform more string manipulation in order to extract the needed information before reformatting it

2 Likes

It totally makes sense to change the topics as you have suggested. Clean and “to the point” explanation!

Thank you very much for this detailed reply. Really appreciate your time and it is going to help me out a lot with my work.

Best,
Anuradha

1 Like

So here’s what I have finally got setup and working. It does not speak directly to OP question, but may proove useful for the concepts. Here’s how I set up my mqtt connections:

Zigbee2MQTT service configuration file (abstracted to relevent settings):

mqtt:
  base_topic: zigbee2mqtt
advanced:
  homeassistant_discovery_topic: homeassistant
  homeassistant_status_topic: homeassistant/status

Zwave2MQTT (settings UI abstract):

mqtt settings
  prefix -> zwave2mqtt
gateway settings
  discoveryprefix -> homeassistant
  Hass Discovery -> true

HA MQTT integration is using default settings connecting to Mosquitto. MQTT Explorer (or similar) shows a clean structure with Zigbee2MQTT, Zwave2MQTT and homeassistant as root topics. HA is looking for topics under device classes such as light, sensor, switch, binary_sensor, fan etc…

As an added bonus, I created friendly names in both Zigbee2MQTT and Zwave2MQTT using / which creates a nice structured topology. So ultimately, HA is limited to a single discovery topic, but each MQTT client / publisher can isolate their content from others so long as it provides for HA discovery as both of these do.

I’ve come across numerous articles / threads where folks were guided towards setting all topics to homeassist, effectively co-mingling all of the topics together. This is not so good from a performance perspective (IMHO) as all clients / pubs will be reading all messages from all other clients / pubs causing unncessary overhead reading and rejecting out of scope messages.

2 Likes