Mqtt connection trouble

Hello,
I am running the following python script on a raspi and try to integrate it in HA without success:

import time
import Adafruit_DHT as dht
import board
import adafruit_sht4x 
import paho.mqtt.client as mqtt

Sensor1 = dht.DHT22
Pin = 4
Sensor2 = adafruit_sht4x.SHT4x(board.I2C())
broker_address = "192.168.178.60"
mqtt_topic = "home/pi_zero"
QOS = 1 
username = "mqtt-user"
password = "xxxx"
#Client Instanz erstellen
client = mqtt.Client()

def on_connect(client, userdata, flags, rc):
    print("Verbunden mit MQTT Broker mit Code: " + str(rc))
    client.subscribe(topic)

def publish_message(Temp1, Phi1, Temp2, Phi2):
    client.publish(mqtt_topic + "/temp1", str(Temp1), QOS)
    client.publish(mqtt_topic + "/phi1", str(Phi1), QOS)
    client.publish(mqtt_topic + "/temp2", str(Temp2)), QOS
    client.publish(mqtt_topic + "/phi2", str(Phi2), QOS)

client.on_connect = on_connect
client.username_pw_set(username, password)
client.connect(broker_address, 1883, 60)

while True:
    try:
        Phi1, Temp1 = dht.read_retry(Sensor1, Pin)
    except Exception as e:
        print('Fehler Sensor1: ', e)
    try:
        Temp2, Phi2 = Sensor2.measurements
    except Exception as e:
        print('Fehler Sensor2: ', e)
   
    publish_message(Temp1, Phi1, Temp2, Phi2)

    time.sleep(900)

The yaml integration looks like:

mqtt:
  sensor:
    - name: "Temperatur Wohnzimmer"
      state_topic: "home/pi_zero/temp1"
      unit_of_measurement: "°C"
    - name: "rel. Feuchte Wohnzimmer"
      state_topic: "home/pi_zero/phi1"
      unit_of_measurement: "%"
    - name: "Temperatur Keller"
      state_topic: "home/pi_zero/temp2"
      unit_of_measurement: "°C"
    - name: "rel. Feuchte Keller"
      state_topic: "home/pi_zero/phi2"
      unit_of_measurement: "%"

In the mosquitto protocoll I see the following:
2023-12-30 22:23:23: New connection from 192.168.178.58:47579 on port 1883.
2023-12-30 22:23:23: New client connected from 192.168.178.58:47579 as auto-C9BEFD6D-38C3-3A73-704D-D46300696119 (p2, c1, k60, u’mqtt-user’).

Can anybody identify my mistake?
Thanks a lot!
J

I believe that’s just to print to the console. Publishing is handled by the publish_message function which relies on client.publish (which is part of paho.mqtt.client).

The broker’s log shows that the python script is connected via the mqtt-user account. My question to Seppl is:

Does the log also show Home Assistant connected to the broker (using the user account name you originally assigned it)?

1 Like

Oops. Yeah ignore my post. Actually I’ve deleted it to avoid confusion.

I am not sure if I understand your question. I thougt that Home Assistant is the broker for MQTT. Do I need to set up a broker on the raspi?

No.

  • Home Assistant is an MQTT client and connects to the MQTT broker.

  • Your python script is an MQTT client and connects to the MQTT broker.

  • The MQTT broker is the MQTT Mosquitto Add-on you installed (or whatever other means you used to install the Mosquitto broker).

Every MQTT client must connect to the broker, using an account name and password (unless you configured the broker to accept anonymous logins but that’s not recommended).

That shows the account mqtt-user has connected to the Mosquitto broker. That’s the account used by your python script. What is the account that Home Assistant is configured to use and is there a connection entry in the broker’s log with that account name?

Because if Home Assistant isn’t connecting to the broker then it isn’t subscribed to any MQTT topics and isn’t receiving any MQTT payloads.

Thanks for your help to understand the MQTT integration! Yes, you are right that mosquitto is the broker I installed already.

In the meantime I did install the MQTT explorer and can identify the incoming data from the python script. So this works fine:

image

However, I am still not able to create an entity out of the incoming data. I thougt that I just need to define it in the configuration.yaml file as shown above. But this does not work. Any further ideas?

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.