I’ve got 6 Rasberry Pi’s doing various jobs round the house, and recently added a DS18B20 temperature sensor to each; they log their temperatures to a local database that I can access via a web page.
I’m trying to set up MQTT publishing on each to send to the Broker running on HA, following this - Tutorial for Using Python with FairCom EDGE MQTT - example for each Pi
If I open the MQTT integration and type in the Listen to a topic “/home/#” (so /home, then a wildcard), it shows the results for the first Pi I’ve set up, but I can’t get Home Assistant to show that temperature on the Dashboard, nor show the others are being received.
Is there a step I’ve missed on the HA side?
import time
import paho.mqtt.client as mqtt
from w1thermsensor import W1ThermSensor
sensor = W1ThermSensor()
client = mqtt.Client(client_id="HA")
client.connect("192.168.1.10", 1883, 60)
while True:
temp = sensor.get_temperature(W1ThermSensor.DEGREES_F)
jsonMsg = "{%f}" % (temp)
client.publish("/home/tvroom/temp-sensor", jsonMsg)
print("published %s" %(jsonMsg))
time.sleep(10)
‘Install’ the mqtt client? Sorry for missing the step, but besides the broker, I can’t find any other mqtt integrations. I’ve seen a few articles about editing the configuration.yaml file to add mqtt (MQTT - Home Assistant), but they haven’t worked out.
I must be missing a setting here, because when I search for mqtt, the only integration that appears is the Broker, and when I click on that it says “Already configured. Only a single configuration possible.” (as the Broker is set up). Is there another integration I’m missing?
the broker runs outside of HA. Likely in another Docker container (as an add-on).
the thing that says “mqtt broker” in your screenshot above is the client in HA that listens to the broker running via the add-on.
I’m not sure why that is labelled as “broker”. When I configured mine on a test install of HA it just called itself the IP of the machine running the broker. So I’d say it’s likely you renamed it to that at some point?
So, anyway, the bottom line is that you already have the HA MQTT client running.
click on the “configure” button in the mqtt box (where it says “mosquitto broker”) and you can listen for all topics there (enter # then “start listening”) to see if your client is configured correctly. That is assuming that you have a device that periodically sends mqtt messages to the broker.
Are you running HA Container? Because I’m running HA OS - I haven’t configured any Dockers - and there’s two parts to the MQTT process; the Integration is the Broker and there’s an Add-On for the client, though it calls itself a broker:
I didn’t rename anything, and if I uninstall the Add-On and try testing the Integration, I get an error “Failed to call service mqtt/publish. Error talking to MQTT: The client is not currently connected.”
When I reinstall that add on and set the “Listen to a topic” in configuration (like I said in the question) I can see data from the first pi:
I just can’t get that to appear on the dashboard, nor can I get the other Pi’s (which are running the same script, just changing their location) to show up.
everything in HA OS runs in Docker containers - even HA itself runs in its own Docker container.
Add-ons are just other Docker containers that are configured thru the HA Supervisor interface.
If you install the Portainer add-on and open its web UI you will see all of the HA related things are there as individual containers.
that’s exactly backwards.
the integration runs inside HA and is the client.
the add-on runs outside HA and is the broker.
because it is.
the real question is that I’m not sure why the integration calls itself the broker because it’s the client.
right.
Because the add-on (broker) doesn’t exist anymore and the HA integration (client) can no longer connect to the broker (which you removed by removing the add-on).
Sorry, I missed that detail.
You may have to set up the sensor manually in yaml. look in the docs for mqtt sensor.
but I’m assuming you changed the topic they publish to as well?
You should just subscribe to all topics (#) not just home/#. That will ensure there isn’t anything wrong with the topics you think you are publishing too from the other devices.
I also suggest you install an mqtt sniffer app on another PC (I recommend MQTT Explorer) and subscribe to all topics (not just /home/# but all #) just to be sure you are seeing those messages at the broker from a different client.