Background:
I have the Mosquitto broker add-on. I have the MQTT integration. I already use system_sensors/src/sensors.py at bd9ba5c66d156200865a0b78ed2be6f2f28e451d · Sennevds/system_sensors · GitHub to send data to Home Assistant, and that works really well.
But I can’t get Home Assistant to detect my sensors.
I have copied code from EclipseMonitor/src/eclipse.py at e297bc673b752b13b942dbb39d48070c58c9a6dd · someyob/EclipseMonitor · GitHub and Temp-Sensor/main.py at 196abd9059e0eee7b678ff020da2dea4d682ecf4 · zeit0dn1/Temp-Sensor · GitHub
I have read the docs: MQTT - Home Assistant
In the MQTT integration settings you can listen to a topic and I see my messages coming through. But I do not see an additional device appear, and I do not see the temperature sensor entity appear.
Does anyone have any working (micro)python code that they can share?! I’m at my wits end.
More details:
I’m using a Raspberry Pi Pico 2W, with umqtt.simple as the MQTTClient. Connecting to WiFi is fine. Connecting to the broker is fine.
Here’s some code copied from others for basic discovery (I’ve tried both types of “data”):
config = b'pico2w/sensor/dummy/config'
data = b'{"unique_id":"' + b'"dummy"' + b'","name":"dummy", "device_class":"temperature", "state_class":"measurement", "unit_of_measurement":"°C", "state_topic":"pico2w/sensor/dummy/state", "icon": "thermometer", "value_template":"{{ value_json.temperature}}" }'
data = b'{"unique_id":"' + b'"dummy"' + b'","name":"dummy", "device_class":"temperature", "state_class":"measurement", "unit_of_measurement":"°C", "state_topic":"pico2w/sensor/dummy/state"}'
client.publish(config,data,1)
and based on the documentation I’ve also tried this full fat version:
data = b'''{
"device": {
"ids": "123456",
"name": "dummy",
"mf": "Raspberry Pi",
"mdl": "Pico2W",
"sw": "1.0",
"sn": "micropython",
"hw": "2W"
},
"origin": {
"name": "Pico2W",
"sw": "1.0"
},
"components": {
"dummy2": {
"p": "sensor",
"device_class": "temperature",
"unit_of_measurement": "°C",
"value_template": "{{ value_json.temperature}}",
"unique_id": "dummy2",
}
},
"state_topic":"dummy/state",
"qos": 0,
}
and then when it comes to sending the data, these are my attempts:
pub = b'pico2w/sensor/dummy/state'
temp_str = str(12.34)
# send number directly
state = temp_str
client.publish(pub, state)
# send number as JSON without quotes
state = b'{"temperature": ' + temp_str + '}'
client.publish(pub, state)
# send number as JSON with quotes
state = b'{"temperature": "' + temp_str + '"}'
client.publish(pub, state)
Listening to “pico2w/sensor/dummy/state” from the MQTT configuration page it actually picks up the messages… but the devices/entities in Home Assistant remain blank:
Message 884 received on pico2w/sensor/dummy/state at 17:16:
{
"temperature": "12.34"
}
QoS: 0 - Retain: false
Message 883 received on pico2w/sensor/dummy/state at 17:16:
{
"temperature": 12.34
}
QoS: 0 - Retain: false
Message 882 received on pico2w/sensor/dummy/state at 17:16:
12.34
QoS: 0 - Retain: false
Message 881 received on pico2w/sensor/dummy/state at 17:16:
{
"device": {
"ids": "123456",
"name": "dummy",
"mf": "Raspberry Pi",
"mdl": "Pico2W",
"sw": "1.0",
"sn": "micropython",
"hw": "2W"
},
"origin": {
"name": "Pico2W",
"sw": "1.0"
},
"components": {
"dummy2": {
"p": "sensor",
"device_class": "temperature",
"unit_of_measurement": "°C",
"value_template": "{{ value_json.temperature}}",
"unique_id": "dummy2",
}
},
"state_topic":"dummy/state",
"qos": 0,
}
and finally there is nothing very interesting in the Broker logs:
2025-01-10 17:40:57: New client connected from 192.168.0.201:49894 as pico2w (p2, c1, k300, u'mqtt').
2025-01-10 17:40:57: Client pico2w disconnected.
Am I missing something basic here? Please help if you can.