Mqtt connection trouble

You haven’t answered my question yet.

Sorry, that I am asking stupid questions, but it seems I still do not understand the way how Home Assistant does work with the mosquitto broker.
The broker config looks like:
image

And the most recent log after a new start:

Does this answer your questions?

What I don’t see in the broker’s log is evidence of Home Assistant connecting to the broker (unless it’s using the same account as your python script, namely mqtt-user).

  1. Go to Settings > Devices & Services > Integrations
  2. Click the MQTT integration
  3. Click Configure
  4. Click Re-Configure MQTT

A form will appear displaying several things including the Username and Password that Home Assistant uses to connect to the broker.

The Username and Password in that form will be the same as one of the User accounts listed in Settings > People > Users. What is the User account that Home Assistant has been configured to use to connect to the broker?

In my case, I created several User accounts for connecting to the MQTT broker. One is for use by Home Assistant. Another is for Tasmota devices and another for when I use MQTT Explorer.

In your case, mqtt-user appears to be the User account used by MQTT Explorer and your python script. What is the User account that Home Assistant has been configured to use to connect to the broker? Is it also mqtt-user?

I do not find mosquitto in Settings>Devices&Services>Integrations

I just find it in Add-ons (the version 6.4.0)

Yes, the mqtt-user is a Home Assistant user account.

Solution:
The isse was that I did not have the integration! After creating it manually everything runs fine.

Thank you very much for your support!

There’s the cause of your problem. You overlooked to install the MQTT integration so Home Assistant has no way of communicating with the MQTT broker. It also explains why there’s no evidence in the broker’s log of Home Assistant attempting to connect.

  1. Go to Settings > People > Users and create two new User accounts that will be used for connecting to the broker. Create one for use by Home Assistant (I suggest HA_MQTT) and another for use by other things like your python script and MQTT Explorer (I suggest USER_MQTT). Remember the passwords you assign to each account.

  2. Install the MQTT integration and configure it to use the User account and password you created for use by Home Assistant (HA_MQTT).

Don’t do it like that. Put the broker’s configuration for logins back to the way it was then restart the broker add-on. Follow the instructions I posted above.


NOTE

If you follow my instructions, all user accounts that can connect to Home Assistant and the MQTT broker will be centralized for easy management. In addition, the broker’s log will clearly show who is connected because separate accounts are used by Home Assistant and other MQTT clients.

You’re welcome!

Please consider marking my post above with the Solution tag (as opposed to your own post). It’s the custom of the community to assign the Solution tag to the first or best post that explains the cause of the problem and provides a solution.

By setting the Solution tag it will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

I find now the entities in Home Assistant, but I do not get valid data.

From python the format is a simple string (e.g. 25.50).

Do I need to format the incoming data in Home Assistant to be able to show it on the dashboard?

What do you get?

image

On the dashboard it shows unknown.

I did adjust the entity as following:
image

Is there a need for a template to modify the incoming data?

unknown means sensor.temperatur_wohnzimmer has not received any data.

Earlier, you said “everything runs fine”. How did you confirm it?

I thougt that it’s fine if I see the entities in Home Assistant and the data in the mqtt explorer. But that was the wrong assumption.

I really appreciate that you take the time to answer my questions! Thanks a lot!

Yes, that’s about two-thirds of what needs to work. The last third is to get Home Assistant to connect to the broker and receive data. I don’t think that part is working yet.

If you followed my instructions to create a User account named HA_MQTT and then use it to configure the MQTT integration, you should see an entry in the broker’s log showing that HA_MQTT is connected. If the broker doesn’t contain it then it means Home Assistant isn’t connected to the broker and cannot receive data that your python script is publishing.

It seems that the OP is unaware that the Broker is a separate program that can run on any computer. It has nothing to do with Home Assistant, but a broker program is available as an add-on to run alongside your Home Assistant. (As is MQTT Explorer. It is a separate program that can run on your PC, but is available as a convenience as an add-on to Home Assistant).

What I haven’t seen is an MQTT sensor defined in your configuration.yaml.
Read this.

I did create a separate user in Home Assistant, but it had the same name. Now I created the HA_MQTT user you recommended to identify the different users in the broker.
After a re-start of mosquitto I see the HA_MQTT connecting once to the broker:

2024-01-02 14:29:25: New connection from 127.0.0.1:50592 on port 1883.
2024-01-02 14:29:25: Client disconnected due to protocol error.
2024-01-02 14:29:25: New connection from 192.168.178.60:38249 on port 1883.
2024-01-02 14:29:25: New client connected from 192.168.178.60:38249 as 2zNaxYYC2m9j2XJ1jvu1BA (p2, c1, k60, u’HA_MQTT’).
2024-01-02 14:29:25: New connection from 192.168.178.60:36366 on port 1883.
2024-01-02 14:29:25: New client connected from 192.168.178.60:36366 as auto-7C4208F2-63A1-CC47-6291-8B41B298F6EE (p2, c1, k60, u’mqtt-user’).
2024-01-02 14:29:25: New connection from 172.30.32.1:41134 on port 1883.
2024-01-02 14:29:25: New client connected from 172.30.32.1:41134 as mqtt-explorer-fdd707f6 (p2, c1, k60, u’mqtt-user’).

image

after few minutes:
image

I still do not get any data in Home Assistant.

@stevemann:
The sensor configuration in the yaml file is shown at the very beginning of this chat and I do see the entities now in Home Assistant. What I am missing is data of the entities.

That looks good. My only question would be why the IP address for HA_MQTT doesn’t start with 172 but the one for MQTT Explorer does (In my case it’s the other way around. HA_MQTT is from a docker container so its address starts with 172 and MQTT Explorer is on a separate computer whose IP address starts with 192). Anyway, let’s skip that for now (it’s probably irrelevant) and proceed with this simple test:

  1. Go to Settings > Devices & Services > Integrations
  2. Click the MQTT integration
  3. Click Configure
  4. Under ‘Listen to a topic’, enter junk in ‘Topic to Subscribe to’ (not /junk but junk)
  5. Click ‘Start Listening’ and leave the web page open.
  6. Go to MQTT Explorer and publish any simple payload (like hello) to the topic junk (not /junk but junk)
  7. Look on the ‘Listen to a topic’ web page for the payload you just published.
  8. If it’s not there then there’s still a communication problem between Home Assistant and the broker.
  9. If it is there, then it implies there may be a configuration problem with your MQTT sensors.

Good idea!
So I published to the topic “junk” 123 from the mqtt explorer and the Home Assistant mqtt integration received:

image

But if I listen to the topic /home/pi_zero/temp1 I do not receive anything

Drop the leading “/”:
home/pi_zero/temp1

You are right. Now I get the data in the mqtt integration:

image

But still not in the dashboard.
The yaml file looks like:

image

Now all that MQTT stuff just happens in the background nd you can write automations using the entity:
sensor.Temperatur_Wohnzilnme

Note, since entity names are case-sensitive, I avoid any upper-case just to not confuse myself.

It received 123 so that confirms Home Assistant is connected to the broker.

Then that means what I wrote here:

Look at what your script contains. According to what you posted, there’s no leading slash in the topic.

mqtt_topic = "home/pi_zero"

The inclusion or absence of a leading slash in an MQTT topic is significant and changes the entire topic.

According to what you posted above, your sensors are configured to subscribe to home/pi_zero not /home/pi_zero.

    - name: "Temperatur Wohnzimmer"
      state_topic: "home/pi_zero/temp1

If they are not receiving payloads then it means your script and sensors aren’t using the same topic. One is using a topic with a leading slash and the other one isn’t.

So which one is using the wrong topic?