I’m only 1 week into provisioning HomeAssistant onto a pi4, and want to use another raspberry pi which has some sensors hanging off of it, to send data to my system, and over MQTT seems the logical way. So I installed Mosquitto Addon.
I have a tiny python script that authenticates correctly to my Broker, but the topics I send are never appearing. When I use the Config dialog “listen”, to debug if things work, it’s not getting any data. My debug log shows my Python is talking to HA, but the “listen” topic does not fire. If I use the publish to send the topic locally that works.
time=“2024-01-19T21:02:59Z” level=debug msg=“Superuser check with backend HTTP”
time=“2024-01-19T21:02:59Z” level=debug msg=“http request approved for mqtt_client1”
time=“2024-01-19T21:02:59Z” level=debug msg=“superuser mqtt_client1 acl authenticated with backend HTTP”
time=“2024-01-19T21:02:59Z” level=debug msg=“setting acl cache (granted = true) for mqtt_client1”
time=“2024-01-19T21:02:59Z” level=debug msg=“to auth record: [97 99 108 45 109 113 116 116 95 99 108 105 101 110 116 49 45 47 104 111 109 101 97 115 115 105 115 116 97 110 116 47 100 97 116 97 45 49 115 51 101 48 55 74 83 97 110 73 83 80 115 104 120 87 80 72 97 83 76 45 49 218 57 163 238 94 107 75 13 50 85 191 239 149 96 24 144 175 216 7 9]\n”
time=“2024-01-19T21:02:59Z” level=debug msg=“Acl is true for user mqtt_client1”
2024-01-19 21:02:59: Sending PUBCOMP to 1s3e07JSanISPshxWPHaSL (m49)
2024-01-19 21:02:59: Received PUBREC from 1s3e07JSanISPshxWPHaSL (Mid: 9)
2024-01-19 21:02:59: Sending PUBREL to 1s3e07JSanISPshxWPHaSL (m9)
2024-01-19 21:02:59: Received PUBCOMP from 1s3e07JSanISPshxWPHaSL (Mid: 9, RC:0)
2024-01-19 21:03:22: Received PINGREQ from 1s3e07JSanISPshxWPHaSL
2024-01-19 21:03:22: Sending PINGRESP to 1s3e07JSanISPshxWPHaSL
My Python Publish script is literally down to 22 lines:
#!/usr/bin/env python3
import time
import paho.mqtt.client as mqttmqtt_username = “mqtt_client1”
mqtt_password = “mqtt_password”
mqtt_host = “192.168.2.108”
mqtt_port = 1883def on_connect(client, userdata, flags, rc):
print("Connected to MQTT broker with result code " + str(rc))while True: client.publish(topic="/homeassistant/sensor/power", payload="OFF") print("Published") time.sleep(5)
if name == “main”:
client = mqtt.Client()
client.username_pw_set(mqtt_username, mqtt_password)
client.on_connect = on_connect
client.connect(mqtt_host, mqtt_port)
client.loop_forever()
When I use the commandline sample utitlity to send a message it is recieved by Broker , example this works:
./mosquitto_pub -h 192.168.2.108 -p 1883 -P “mqtt_password” -u “mqtt_client1” -t “/homeassistant/pub” -m “ON”
I notice that topics are prefixed with a / and some people do not prefix with /, not sure but doing either of these had no difference not does the QOS flag values I’m missing something obvious but since I’m only a day into mqtt I have missed whatever it is here. Clues?