MQTT Auto Configuration

Hi,

I’m trying to use auto configuration to integrate a lock into homeassistant via mqtt auto discovery protocol:
Config:

{"name":"Nuki",
"unique_id":"lock-c56828-lock",
"~":"lock-c56828/lock",
"command_topic":"~/cmd",
"state_topic":"~/state",
"payload_lock":"lock",
"payload_unlock":"unlock",
"payload_open":"open",
"state_locked":"locked",
"state_unlocked":"unlocked"}

Published on:

homeassistant/lock/lock-c56828-lock/config

It does work well and I execute the commands and the state also gets updated. However, I would like to add also the battery soc as a sensor. The only way possible I found so far was to add it as a new sensor which will become it’s own entity. Is it possible to collect multiple entities in one device or somehow make this value accessible in the context of the lock?

So far I had to group all the entitities manually which is annoying if a device provides a lot of different sensors.

Unfortunately the mqtt auto discovery documentation is written in a very confusing way but after a few tries I think I managed to kinda get it right. However, I cannot solve the multi sensor issue mentioned above.

You need to create a device, and add all sensors to the same device.

example:

2 sensors, one device:

     - service: mqtt.publish
        data:
          retain: true
          topic: homeassistant/sensor/xiron_3201/temperature/config
          payload: '{"unit_of_measurement":"°C","device_class":"temperature","value_template":"{{ value_json.TEMP }}","state_class": "measurement","state_topic":"rflink/Xiron-3201","name":"eetkamer_temperature","unique_id":"eetkamer_temperature","device":{"identifiers":["xiron_3201"],"name":"xiron_3201","model":"Digoo temp & humidity sensor","manufacturer":"Digoo"}}'
      - service: mqtt.publish
        data:
          retain: true
          topic: homeassistant/sensor/xiron_3201/humidity/config
          payload: '{"unit_of_measurement":"%","device_class":"humidity","value_template":"{{ value_json.HUM }}","state_class": "measurement","state_topic":"rflink/Xiron-3201","name":"eetkamer_humidity","unique_id":"eetkamer_humidity","device":{"identifiers":["xiron_3201"],"name":"xiron_3201","model":"Digoo temp & humidity sensor","manufacturer":"Digoo"}}'

But how do I do that using autoconfiguation? Is that implicitly done by the topic? Or is it because they point to the same state topic and I have to merge the data into to the state? Or is it because of the payload configuration? Is there some more documentation that explains this a bit better?

I’m using this to make my devices autoconfig themselves in homeassistant, so I dont need to write extra configuration files.

Because this part is the same for both sensors

they belong to the same device.

Ok, thank you, so I guess the device identifier must be unique, if i would have have more devices of the same type.

Thank you I will try that

Hi Francis,

thank you very much, I managed to get it working this way.

What I find a bit confusing is that every entity would have to post the same device information causing quite a bit of redundancy ,e.g. configuration url, name, etc. Also what happens if 2 devices provide the same identifiers entry but provide different configuration url/name, etc?

Is there a best practice for this?

If they have a different configuration url, I think you better create a different device.

Makes sense, but I tried to understand the concept. It feels like the dependency is reversed somehow.

one device could have multiple entities, but currently it’s like an entity has a device but to work around that we give it a unique identifier so we can merge all the devices from the entities to one which has the same identifier.

More then 3 years ago, when I started with HA, there were no devices, only entities. When they added devices, they modified MQTT auto-discovery to create devices, but as you say, from the bottom up. You add a device to the MQTT entity, and if you want more then one MQTT entity to the same device, you have to add each MQTT entity to the same device.