If you are generating the config messages manually then you may as well define them in the configuration.yaml. Though according to the documentation the device section only works via MQTT discovery - not from the config file.
Otherwise you either need to publish the config with retain, or if you have an active element publishing them then subscribe to the HA birth message and republish the config when HA goes online.
Similarly with the sensor state - they need to be retain or republished after HA has had a chance to process the config, be it from the configuration.yaml or the republish. If the state is being published regularly then that might be good enough.
I’ve used both approaches, with my current preference being the republish - otherwise everything ends up being retain which is poor MQTT form, AIUI.
Since the Christmas break I working on my Home assistant project and have to admid, regarding the documentation, some additional information might be helpful. I intend to document my learning as input for improvement once I have finished.
My sensors provide data every 60 seconds, so there is no need to use the retain option.
My issue is to create persistant sensor configurations. I’m able to create them manual (via integrations / configure option) but they are gone after a reboot. So, I try to define the sensors in configuration file.
Unfortunately, the documentation is ambiguous. Sometimes I see
# Example configuration.yaml entry
mqtt:
sensor:
- state_topic: "home/bedroom/temperature"
Alternatively
sensor:
- platform: mqtt
name: "Water temperatuur zone 1"
unique_id: "Sensors-12_zone-1"
object_id: "Sensors-12_zone-1"
icon: "mdi:thermometer-lines"
state_topic: "Sensors-12/Sensor01/Zone-1"
My intend is to configure my 12 sensors and the device
Any example would be great.
The MQTT config format changed in 2022.12. Refer to the breaking changes MQTT in the 2022.12 release notes.
The current doc for MQTT sensor is correct - that corresponding to the first form. The alternate is the old form, which is no longer supported as of 2022.12. Have you tried the first form and it didn’t work for you?
e.g.
mqtt:
sensor:
- name: "Water temperatuur zone 1"
unique_id: "Sensors-12_zone-1"
object_id: "Sensors-12_zone-1"
icon: "mdi:thermometer-lines"
state_topic: "Sensors-12/Sensor01/Zone-1"
Thanks for the tip, a game changer! This code is working. (If you are not sloppy with using capitals. So there is a difference between Mqtt and mqtt. Same for Sensor and sensor. my mistake)
mqtt:
sensor:
- name: "Water temperatuur zone 1"
state_topic: "Sensors-12/Sensor01/Zone-1"
unique_id: "Sensors-12_zone-1"
object_id: "Sensors-12_zone-1"
icon: "mdi:thermometer-lines"
unit_of_measurement: "°C"
device: {
name: "Sensors-12",
manufacturer: "Maarten",
model: "Sensors-12",
identifiers: "Sensors-12"
}
- name: "Water temperatuur zone 2"
......
Disadvantage is my configuration file gets big. Will look for an ‘include’ option.
Not an answer… Just a thought.
I have switched all of my MQTT devices to use the ESPHome API. I wasn’t having any real problems with MQTT, but using the API just seemed a whole lot easier. Also, making all of my ESPHome devices speak the same language has greatly reduced the complexity of maintenance.
Hi, Also trying to get my head how to publish MQTT data and device being autodiscovered by HA.
Using the code below, can see the data being published via MQTT explorer. But device isn’t getting integrated or recognised by HA. Have other off the shelf tasmota firmware sensors on the network that automatically are added to my HA.
Question is, how does HA autodiscover a MQTT device?
Have re-used some of the code from this thread to form the publish data using micropython as follows:
from umqtt.simple import MQTTClient
payload={
"name": "Water temperature zone 1",
"unique_id": "Sensors-12_zone-1",
"object_id": "Sensors-12_zone-1",
"icon": "mdi:thermometer-lines",
"state_topic": "Sensors-12/Sensor01/Zone-1",
"unit_of_measurement": "°C",
"device": {
"name": "Sensors-12",
"identifiers": "Sensors-12"
}
}
def main():
client = MQTTClient('config','192.168.0.111')
client.connect()
print("Connected to {}".format(CONFIG['broker']))
while True:
client.publish('{}/{}'.format('homeassistant/sensor/Water_temperature_zone_1',
CONFIG['client_id']),
bytes(str(payload), 'utf-8'))
time.sleep(5)
Ensure the MQTT integration is installed and connected to your MQTT broker. This integration automatically discovers entities and devices via MQTT Discovery.
If you publish a discovery payload (JSON containing specific field names, like what you have created above) to a discovery topic (specific topic path starting withhomeassistant and similar to what you created above), Home Assistant will create the entity (and optional device) described in the discovery payload. It’s important to publish the payload as a retained message (otherwise when you restart the broker, the published discovery payload is purged from the broker).
NOTE
Unless there’s a good reason for your MQTT topics to include uppercase letters, I suggest you use lowercase letters exclusively (it doesn’t hurt legibility).
Getting close (I think), can at least see a discovery error in HA as follows:
2023-08-28 00:55:08.817 WARNING (MainThread) [homeassistant.components.mqtt.discovery] Unable to parse JSON esp32: ‘{‘name’: ‘water temperature zone 1’}’
Tried a “few” variations of the code, but here’s a simplified version of it:
payload={"name": "water temperature zone 1"}
client = MQTTClient('config','192.168.0.111')
client.connect()
client.publish('{}/{}'.format('homeassistant/sensor','esp32/config'),bytes(str(payload), 'utf-8'))
Using your version still returns the error.
Not sure if this could be an issue, but comparing the published data in MQTT explorer with other devices, noticed Tasmota devices publish using double quotation as opposed to mine are single quotations.
2023-08-28 10:17:09.634 WARNING (MainThread) [homeassistant.components.mqtt.discovery] Unable to parse JSON esp32: ‘{‘name’: ‘Water_temperature_zone_1’, ‘state_topic’: ‘Sensors-12/Sensor01/Zone-1’, ‘unit_of_measurement’: ‘\xb0C’, ‘object_id’: ‘sensors_12_zone_1’, ‘icon’: ‘mdi:thermometer-lines’, ‘device’: {‘name’: ‘Sensors-12’, ‘identifiers’: [‘sensors_12’]}, ‘unique_id’: ‘sensors_12_zone_1’}’
Only change to your payload is changes spaces to underscore
Here’s a screenshot of MQTT Explorer displaying a switch’s discovery payload. I use a (Home Assistant) script to create various types of entities (switch, binary_sensor, cover, climate, etc). It shows that the JSON payload employs double-quotes.
You’re saying the same view for you shows single-quotes in the payload?
EDIT
I’m no python expert but I get the impression, from the example below, that you need to convert the python dictionary into a JSON string using json.dumps()
really just haven’t got my head around with the formatting structure of MQTT
All I’m attempting below is to publish Water_temperature_zone_1 with a value of 1.
Have tried all sorcery, with no luck againg.
To create an MQTT Sensor via MQTT Discovery, you publish a Discovery payload to the discovery topic. You succeeded in doing that when you published the payload in your previous post to homeassistant/sensor/esp32/config. The resulting entity is sensor.sensors_1.
You have configured sensor.sensors_1 to receive payloads via the topic Sensors-1/Sensor01/Zone-1. The physical device represented by sensor.sensors_1 must publish its temperature value to Sensors-1/Sensor01/Zone-1.