MQTT add-on advertising mqtt.local

Is there anyway to get the MQTT add-on to advertise itself with mqtt.local? I realize that running in a container and all it’s probably not straightforward. I could “settle” for hassio to double publish hassio.local AND mqtt.local but haven’t found a way to do that either.
My ideal solution would be to have a extra mqtt add-on config option (mdns-name or something).
Reason I’m asking is that I have more and more devices under custom firmware (mostly Tasmota) and love the idea of not having to configure the IP address of the mqtt server for each of them. Right now I’m running mosquito on its own VM but having all in one hassio VM is very attractive.
Hope this makes sense.

JLuc

TL;DR A lengthy paragraph with discoveries, but you could jump right to the steps and get it over with.

I honestly was thinking the same and just couldn’t help but to dig deeper into how the publishing of mdns services are happening. For the record and at the time of writing, HassOS VM (or HassIO VM) in Oracle VB publishes three mdns services automatically on start for discovery:

  • _ssh._tcp
  • _sftp-ssh._tcp
  • _home-assistant._tcp

They are linked to hassio.local mdns host (which has an IP defined automatically). The first two are published using the avahi-daemon of HassOS but the last one is registered by the home-assistant python container (container running in docker in HassOS).

Although you asked for advertising mqtt.local, but the right way is actually to register an _mqtt._tcp service linked to hassio.local host. The reason why is, technically, any mqtt enabled device would search for an _mqtt._tcp service, which has details like IP of a host and the port number, on the network (not search for mqtt.local host, as far as I know). That’s how service discovery is, by finding the host having the service, which is most probably the hassio.local of HassOS VM, and connect to it’s IP and the port defined by the service (MQTT port is 1883).

To do that, and the simplest way I see it, is nothing more than configuring avahi-daemon in HassOS to register _mqtt._tcp service.

At the time of writing (again), here’s what you need to do with HassOS running in Oracle VB (note: part of the steps include a custom docker image which I have open-sourced here and built myself, tested to work successfully on my setup) :

  1. Start the HassOS VM, wait for it to boot and eventually for home-assistant to start, I’d say give it like 2~3 minutes till the screen stops writing new lines.

  2. Press the Enter key and you should see the below:

    Welcome to HassOS
    hassio login:
    
  3. Type root and press the Enter key. You should then see the below:

      _    _                 _       
     | |  | |               (_)      
     | |__| | __ _ ___ ___   _  ___  
     |  __  |/ _` / __/ __| | |/ _ \ 
     | |  | | (_| \__ \__ \_| | (_) |
     |_|  |_|\__,_|___/___(_)_|\___/ 
    
    Welcome on Hass.io CLI.
    
    For more details use 'help' and 'exit' to close.
    If you need access to host system use 'login'.
    
  4. Type login and press Enter, you should see a # symbol at the start of the line and now your in a shell terminal of HassOS (note: this is not the same as using the ssh with the ssh-addon of home-assistant).

  5. Now type the below command which will start publishing _mqtt._tcp using avahi:

    docker run -d -v /run/dbus:/var/run/dbus -v /run/avahi-daemon:/var/run/avahi-daemon --network host --restart always --name mqtt-mdns ahasbini/avahi-tools:latest avahi-publish-service -s $HOSTNAME _mqtt._tcp 1883
    
  6. All done! The above command is persistent, meaning the publishing will be done automatically after restarts or in case of failures (can’t think of one right now but looking forward to knowing about them).

I have to say that docker was to the rescue after a lengthy digging :sweat_smile: You might wonder why it wasn’t done without using docker, the reason is due to the file-system being used (SquashFS) by the HassOS VM. It is in read-only mode and we can’t create files for avahi to load on startup so the only other way to issue a command to the avahi-daemon to publish the service. Even making it persistent was difficult without docker as the entire / directory is made read-only except for the directories belonging to docker and other unhelpful ones (tmp and var). This comes to reason why it’s better to install hassio on an Linux machine like Ubuntu instead of using the HassOS VM, however I still find it nice to use HassOS provided by Home Assistant team.

Thanks so much for this very detailed reply! Definitely gonna give a try. Though I agree that looking for _mqtt._tcp is the “proper” way to go, getting the “MQTT.local.” IP seems to be the common default way for some of the custom ESP firmware. As I am starting to have more and more of the Wifi devices, I’m trying to keep their config as stable and minimum as possible. You gave me great pointers here, thanks again.

So I had a go at trying to publish mqtt.local. The thing is one host cannot have two mdns hostnames, it can only have one mdns hostname published since the IP needs to be unique. Meaning if I tried to publish mqtt.local with hassio.local being already published with the same IP address, avahi complained and threw an error. The only way it would work is not having hassio.local published, and I was able to do that with the below command which changes the hostname of the VM from hassio.local to mqtt.local, this can be run instead of step 5 of my previous post:

docker run -d -v /run/dbus:/var/run/dbus -v /run/avahi-daemon:/var/run/avahi-daemon --network host --restart always --name mqtt-mdns ahasbini/avahi-tools:latest sh -c "avahi-set-host-name mqtt; echo done; tail -f /dev/null"

Probably there is a better way to do this but I only came up with this at the top of my head.

Out of curiosity, which devices and firmware are these? And by any chance are you trying to the mqtt discovery feature of HA for automatic config?