MQTT help needed - starting at the basics

OK, if I had any hair, I’d be pulling it out right now.

HA Setup @ 192.168.10.100
RPi4 running HassOS 4.17 and latest stable Supervisor.
Mosquitto installed via the Add-On Store
MQTT sensors defined in configuration.yaml
NO other mqtt definitions in configuration.yaml (I read a bunch of topics here that stated to not double-configure through yaml and UI.
Install is default from add-on store (I’ve confirmed the ‘Enable Discovery’ is checked.) with the exception of:
Add-On configuration - only change is to enable anonymous as one of my clients does not have the facility to provide user/pass.

MQTT client setup @ 192.168.10.30:
MQTT.fx on Mac OS High Sierra

Standalone MQTT broker on FreeBSD @ 192.168.10.197 running only mosquitto (test/verification broker)

Observations:

I can see clients connecting to the HA broker instance in the logs

I have entities for the mqtt sensor definitions contained in the configuration.yaml but no entities for anything not defined in there

All clients publish successfully to the test/verification broker at 192.168.10.197

MQTT.fx and mosquitto_sub clients successfully read the mqtt stream from the test/verification broker at 192.168.10.197

HA mosquitto does not seem to receive the messages directed to it.

MQTT.fx and mosquitto_sub do not read/receive any messages from HA mosquitto broker.

Issue:
The publishing clients are publishing correctly and are received by the test/verification broker. My clients can read the messages published to the test/verification server by subscribing to it. This confirms operation of both the publishing and subscribing clients, leaving the broker as the only changing variable.

I must be missing something along the way. I’ve read the topics where people say “I was way over-thinking it!” and to that end my expectation is that I install the add-on, point the client to the IP address of HA (192.168.10.100) and it works. It creates an entity and I use that entity in HA. I’m not sure that I would consider that to be over-thinking things. Problem is, that isn’t happening and I’m getting frustrated at how long it is taking for me (and many others, it seems) to resolve this ‘undocumented feature’.

Request:
Can someone that has a working MQTT broker on HA please clearly define what is required to get the messages from my publishing clients into HA so that I can use them in monitoring and automation? I need it to work WITHOUT a username and password; yes, I know the security issues with that and will resolve them later, for what they are. Right now, I just need to solve this issue and move on.

My gratitude will be limitless! :smiley:

EDIT:
For what it is worth, I think that the issue is connected to the anonymous status as the MQTT.fx client will not even read the homeassistant/status topic which I assume is getting published by HA. I have no user/pass details in the client.

Further Edit:
If I publish a message using ‘Configuration>Integrations>MQTT Configure’ interface, both the broker listener AND the MQTT.fx client see it. The broker doesn’t see anything else.

You are correct. The add-on does not work without a user/password.

Why not use this one ?

OK I will try to help you… So basically you have a MQTT server up and working on FreeSD… You want HA to connect as a client to this broker to read topics and eventually publish…

So in the configuration.yaml file or in the integration menu, you have to give the parameters to access the server on FreeBSD:

mqtt:
  broker: <ip address or dns name of FreeBSD>
  port: 1883
  username: !secret mosquitto_username
  password: !secret mosquitto_password

Than you have to read the topics on the broker…

For sensors, here is what I am doing (stored in sensors.yaml):

- platform: mqtt
  state_topic: "homeassistant/input_boolean/rad_cozytouch_thomas/state"
  name: "rad cozytouch thomas"

Where state topic is obviously the topic you want to follow and “name” the name of the sensor so in this case: “sensor.rad_cozytouch_thomas”

For other elements like switches (in fact elements where you can change the state using a service), here is what I am doing in automations.yaml:

- alias: mqtt_chambre_cave_sol
  initial_state: true
  trigger:
    - platform: mqtt
      topic: "homeassistant/switch/ac_1710256_2/state"
  action:
    - service_template: "switch.turn_{{trigger.payload | lower}}"
      entity_id: switch.ac_1710256_2

This will swith on or off the local switch based on the topic (“on” or “off”). The trigger is a change in the topic…

Another example for datetime:

- alias: mqtt_read_time_dem_che_m
  initial_state: true
  trigger:
    - platform: mqtt
      topic: "homeassistant/input_datetime/h_dem_che_m/state"
  action:
    - service: input_datetime.set_datetime
      entity_id: input_datetime.h_dem_che_m
      data_template: 
         time: "{{trigger.payload}}"

I hope this is helping you a little bit… Let me know if I misunderstood your problem…

Well, what is the point of having the anonymous setting in the config then? If I’m not to over-think the issue, wouldn’t there be a declaration somewhere in the setup or documentation that states ‘This add-on broker will not work with clients that do not use a user/pass field’?

So, why don’t I want to use the FreeBSD instance? Because I’m looking to gain leverage from the integration of MQTT messages in HA without a requirement to run external supporting infrastructure. It is clunky.

In HassOS, the mosquitto add-on allows to use anonymous connections but this is only in read mode so you cannot publish… Anyway, using Mosquitto on the same machine than HA makes sense: only one system to maintain, same uptime and maintenance windows… I am running two HA instances on two different locations but having one mosquitto server on one node centralizing all the information from the two nodes…

Thank you, @browetd.

The part that was clearly frustrating me is what appears to be an ‘undocumented feature’ that the HA instance of mosquitto (unlike the Eclipse version) does not allow clients without authentication to publish to that broker.

For others that come to this thread in the future, the solution is either:

  1. Use the HA instance of mosquitto with clients that can authenticate with user/pass or some other means, or
  2. Use an MQTT broker that is NOT the add-on in the HA store.

I’ve read so many threads here and elsewhere in which other users are clearly having exactly the same issues but they don’t know it. It would be worth having this constraint featured prominently in the docs for the add-on. I do realise that most people use a user/pass authentication and wouldn’t come across this issue at all but for those of us that do require it on occasion, it is certainly worth having that info available.

I’ll pick it up with the add-on developer.

Many thanks, my gratitude is indeed limitless! I’ve spent more than a few hours on trying to resolve this.

The mosquitto mqtt broker from version 2.0+ also defaults to need authentication setup before it will start. Recently version 2.0.6 was made the default so if one had not setup authentication mosquitto will not start. You need to at least add

allow_anonymous true
listener 1883

to the configuration file continue using mosquitto without authentication, so that it will start. Here is a link to the mosquitto documentation about Migrating from 1.x to 2.0 that explains the options.