Mqtt autodiscovery function - in a third party project

Hi There,

sorry, if this might not be the right place for my topic, but I don’t think it does fit in “configuration” or somewhere else… ?!

Currently, I am working on a python program, which should provide some hardware information to an MQTT Broker.
I want to use HA’s AutoDiscovery functionallity - and during my tests I came accross an issue I don’t really understand at the moment (and therefore, I can’t solve it)…

Short explanation:
I have some devices that report different sensors.

For example:
Device 1 (CPU):
Sensor 1: Voltage
Sensor 2: Current
Sensor 3Chip-Temperature

Connected to Device 1 is a humidity sensordevice (1.1) which does provide the humidity (one sensor entitiy)
Also connected to Device 1 is a barometer (device 1.2), which does provide two sensors - air pressure and altitude (two sensor entities)

Right now, it seems that my program does deliver all information to the MQTT Broker - just as expected,
But the autodiscovery does only find one sensor for Device 1

Connected to: Master Brick  6m9VR4
INFO:root:[14/11/2022 18:17:05:886425] - connected to Master Brick - Identity(uid='6m9VR4', connected_uid='0', position='0', hardware_version=(2, 0, 0), firmware_version=(2, 5, 1), device_identifier=13)

DEBUG (availability) Topic: homeassistant/sensor/Tinkerforge/Stack-Voltage_6m9VR4/availability
DEBUG (availability) Payload: online
INFO:root:[14/11/2022 18:17:05:898392] - Published message id: 1

DEBUG (configuration) Topic: homeassistant/sensor/Tinkerforge/Stack-Voltage_6m9VR4/config
DEBUG (configuration) Payload: {"availability_topic":"homeassistant/sensor/Tinkerforge/Stack-Voltage_6m9VR4/availability","icon":"mdi:sine-wave","unique_id":"6m9VR4","unit_of_measurement":"V","device":{"identifiers":"6m9VR4","manufacturer":"tinkerforge.com","model":"Master Brick","name":"Master Brick (6m9VR4)","sw_version":"2.5.1","hw_version":"2.0.0"},"name":"Stack-Voltage (6m9VR4)","state_topic":"homeassistant/sensor/Tinkerforge/Stack-Voltage_6m9VR4/state"}
INFO:root:[14/11/2022 18:17:06:004556] - Published message id: 2

DEBUG (state) Topic: homeassistant/sensor/Tinkerforge/Stack-Voltage_6m9VR4/state
DEBUG (state) Payload: 0
INFO:root:[14/11/2022 18:17:06:108268] - Published message id: 3

DEBUG (availability) Topic: homeassistant/sensor/Tinkerforge/Stack-Current_6m9VR4/availability
DEBUG (availability) Payload: online
INFO:root:[14/11/2022 18:17:06:209988] - Published message id: 4

DEBUG (configuration) Topic: homeassistant/sensor/Tinkerforge/Stack-Current_6m9VR4/config
DEBUG (configuration) Payload: {"availability_topic":"homeassistant/sensor/Tinkerforge/Stack-Current_6m9VR4/availability","icon":"mdi:current-dc","unique_id":"6m9VR4","unit_of_measurement":"A","device":{"identifiers":"6m9VR4","manufacturer":"tinkerforge.com","model":"Master Brick","name":"Master Brick (6m9VR4)","sw_version":"2.5.1","hw_version":"2.0.0"},"name":"Stack-Current (6m9VR4)","state_topic":"homeassistant/sensor/Tinkerforge/Stack-Current_6m9VR4/state"}
INFO:root:[14/11/2022 18:17:06:316277] - Published message id: 5

DEBUG (state) Topic: homeassistant/sensor/Tinkerforge/Stack-Current_6m9VR4/state
DEBUG (state) Payload: 0
INFO:root:[14/11/2022 18:17:06:418001] - Published message id: 6

DEBUG (availability) Topic: homeassistant/sensor/Tinkerforge/Chip-Temperature_6m9VR4/availability
DEBUG (availability) Payload: online
INFO:root:[14/11/2022 18:17:06:525877] - Published message id: 7

DEBUG (configuration) Topic: homeassistant/sensor/Tinkerforge/Chip-Temperature_6m9VR4/config
DEBUG (configuration) Payload: {"availability_topic":"homeassistant/sensor/Tinkerforge/Chip-Temperature_6m9VR4/availability","icon":"mdi:thermometer","unique_id":"6m9VR4","unit_of_measurement":"°C","device":{"identifiers":"6m9VR4","manufacturer":"tinkerforge.com","model":"Master Brick","name":"Master Brick (6m9VR4)","sw_version":"2.5.1","hw_version":"2.0.0"},"name":"Chip-Temperature (6m9VR4)","state_topic":"homeassistant/sensor/Tinkerforge/Chip-Temperature_6m9VR4/state"}
INFO:root:[14/11/2022 18:17:06:633625] - Published message id: 8

DEBUG (state) Topic: homeassistant/sensor/Tinkerforge/Chip-Temperature_6m9VR4/state
DEBUG (state) Payload: 439
INFO:root:[14/11/2022 18:17:06:738455] - Published message id: 9
-----------------------------
Connected to: Humidity Bricklet  eGQ
INFO:root:[14/11/2022 18:17:06:855319] - connected to Humidity Bricklet - Identity(uid='eGQ', connected_uid='6m9VR4', position='a', hardware_version=(1, 1, 0), firmware_version=(2, 0, 2), device_identifier=27)

DEBUG (availability) Topic: homeassistant/sensor/Tinkerforge/Humidity_eGQ/availability
DEBUG (availability) Payload: online
INFO:root:[14/11/2022 18:17:06:864292] - Published message id: 10

DEBUG (config) Topic: homeassistant/sensor/Tinkerforge/Humidity_eGQ/config
DEBUG (config) Payload: {"availability_topic":"homeassistant/sensor/Tinkerforge/Humidity_eGQ/availability","icon":"mdi:water-percent","unique_id":"6m9VR4-eGQ","unit_of_measurement":"%","device":{"identifiers":"eGQ","manufacturer":"tinkerforge.com","model":"Humidity Bricklet","name":"Humidity Bricklet (6m9VR4-eGQ)","sw_version":"2.0.2","hw_version":"1.1.0"},"name":"Humidity (eGQ)","state_topic":"homeassistant/sensor/Tinkerforge/Humidity_eGQ/state"}    
INFO:root:[14/11/2022 18:17:06:970865] - Published message id: 11

DEBUG (state) Topic: homeassistant/sensor/Tinkerforge/Humidity_eGQ/state
DEBUG (state) Payload: 487
INFO:root:[14/11/2022 18:17:07:073581] - Published message id: 12

so all topics with their payload are submitted to my mqtt broker …

The AutoDiscovery does correctly recognize all devices

But for the “Master Brick” devices (both) - only one Sensor Entity is available:
grafik

also, all states are “unavailable”

    "device": {
      "id": "6ec6bc788703fb4f16b7879c0b055733",
      "name": "Humidity Bricklet (6m9VR4-eGQ)",
      "name_by_user": null,
      "disabled": false,
      "disabled_by": null,
      "entities": [
        {
          "device_class": null,
          "disabled_by": null,
          "disabled": false,
          "entity_category": null,
          "entity_id": "sensor.humidity_egq",
          "icon": null,
          "original_device_class": null,
          "original_icon": "mdi:water-percent",
          "state": {
            "entity_id": "sensor.humidity_egq",
            "state": "unavailable",
            "attributes": {
              "unit_of_measurement": "%",
              "icon": "mdi:water-percent",
              "friendly_name": "Humidity (eGQ)"
            },
            "last_changed": "2022-11-14T17:17:06.999680+00:00",
            "last_updated": "2022-11-14T17:17:06.999680+00:00"
          },
          "unit_of_measurement": "%"
        }
      ]
    },
    "mqtt_debug_info": {
      "entities": [
        {
          "entity_id": "sensor.humidity_egq",
          "subscriptions": [
            {
              "topic": "homeassistant/sensor/Tinkerforge/Humidity_eGQ/availability",
              "messages": []
            },
            {
              "topic": "homeassistant/sensor/Tinkerforge/Humidity_eGQ/state",
              "messages": [
                {
                  "payload": "487",
                  "qos": 0,
                  "retain": 0,
                  "time": "2022-11-14T17:17:07.096417+00:00",
                  "topic": "homeassistant/sensor/Tinkerforge/Humidity_eGQ/state"
                }
              ]
            }
          ],
          "discovery_data": {
            "topic": "homeassistant/sensor/Tinkerforge/Humidity_eGQ/config",
            "payload": {
              "availability_topic": "homeassistant/sensor/Tinkerforge/Humidity_eGQ/availability",
              "icon": "mdi:water-percent",
              "unique_id": "6m9VR4-eGQ",
              "unit_of_measurement": "%",
              "device": {
                "identifiers": "eGQ",
                "manufacturer": "tinkerforge.com",
                "model": "Humidity Bricklet",
                "name": "Humidity Bricklet (6m9VR4-eGQ)",
                "sw_version": "2.0.2",
                "hw_version": "1.1.0"
              },
              "name": "Humidity (eGQ)",
              "state_topic": "homeassistant/sensor/Tinkerforge/Humidity_eGQ/state",
              "platform": "mqtt"
            }
          },
          "transmitted": []

can someone maybe help me and point out, where my error might be?

thanks :slight_smile:

Sorry I only glanced through your post, but I find that the mqtt “device” data has to be identical for all the entities that are part of the same device.

Hi :slight_smile:
That’s the case as far as I can see :slight_smile:

{
 "availability_topic":"homeassistant/sensor/tinker/availability",
 "icon":"mdi:sine-wave",
 "unique_id":"6m9VR4-6qCxXx",
 "unit_of_measurement":"V",
 "device_class":"voltage",
 "device":
 {
   "identifiers":"6qCxXx",
   "manufacturer":"tinkerforge.com",
   "model":"Master Brick",
   "name":"Master Brick (6m9VR4-6qCxXx)",
   "sw_version":"2.5.1",
   "hw_version":"1.0.0"
 },
 "name":"Stack Voltage (6qCxXx)",
 "state_topic":"homeassistant/sensor/tinker/Stack Voltage (6qCxXx)/state"
}
{
  "availability_topic":"homeassistant/sensor/tinker/availability",
  "icon":"mdi:current-dc",
  "unique_id":"6m9VR4-6qCxXx",
  "unit_of_measurement":"A",
  "device_class":"current",
  "device":
  {
    "identifiers":"6qCxXx",
    "manufacturer":"tinkerforge.com",
    "model":"Master Brick",
    "name":"Master Brick (6m9VR4-6qCxXx)",
    "sw_version":"2.5.1",
    "hw_version":"1.0.0"
  },
  "name":"Stack Current (6qCxXx)",
  "state_topic":"homeassistant/sensor/tinker/Stack Current (6qCxXx)/state"
}
{
  "availability_topic":"homeassistant/sensor/tinker/availability",
  "icon":"mdi:thermometer",
  "unique_id":"6m9VR4-6qCxXx",
  "unit_of_measurement":"°C",
  "device_class":"temperature",
  "device":
  {
    "identifiers":"6qCxXx",
    "manufacturer":"tinkerforge.com",
    "model":"Master Brick",
    "name":"Master Brick (6m9VR4-6qCxXx)",
    "sw_version":"2.5.1",
    "hw_version":"1.0.0"
  },
  "name":"Chip Temperature (6qCxXx)",
  "state_topic":"homeassistant/sensor/tinker/Chip Temperature (6qCxXx)/state"
}

But from this device, only the “Stack Voltage (6qCxXx)” will be created - and the state does stay unavailable…

One other thing to look at is the discovery topic. The docs have a layout for how to construct the discovery topic and one is the object id:
Best practice for entities with a unique_id is to set <object_id> to unique_id and omit the <node_id>

For example, if the entity’s unique id is say unique_id: 12521526, then the discovery topic would look something like: homeassistant/sensor/12521526/config . This is how I have done it.

I think, I got it solved.
The reason why the sensors were always “unavailable” was related to the availability topic.
It seems that it is not enough to send this message once.
Instead I had to send it with each state change in order to get it displayed correctly.

After this change, the sensor values became available in HA.
also, some other, smaller changes to the Unique IDs in the topic have been made…

Thanks for the help :slight_smile: