How do I add entities to devices so they appear together?

I’m trying to use the newish Flic SDK integration to automatically share my Flic buttons to Home Assistant via MQTT. So far, so good.

I’ve got it creating the action and battery entities, but they won’t link to the Device that’s supposed to contain it. I am using the exact same Device syntax in the JSON for both entities, isn’t that all that’s supposed to be required?

For topic “homeassistant/sensor/BA33-A04439/action/config”:

{"device":{"name":"One","identifiers":["BA33-A04439"],"manufacturer":"Flic","model":"Button"},"name":"One Flic Button","state_topic":"homeassistant/flic/BA33-A04439/action"}

For topic “homeassistant/sensor/BA33-A04439/battery/config”:

{"device":{"name":"One","identifiers":["BA33-A04439"],"manufacturer":"Flic","model":"Button"},"name":"One Flic Button Battery Level","state_topic":"homeassistant/flic/BA33-A04439/battery","device_class":"battery"}

But in Home Assistant, it creates the Device with no entities and two entities that are not connected?

What am I doing wrong?

(Source in flic2hass/main.js at 9be3e0a6851a186797ff6f2470ff82f3e4de7325 · id628/flic2hass · GitHub)

Nobody has any suggestions?

Is this a bug?

Your discovery topic doesn’t seem match the documented format.

<discovery_prefix>/<component>/[<node_id>/]<object_id>/config

Best practice for entities with a unique_id is to set <object_id> to unique_id and omit the <node_id> .

The more I look at this, the more confused i become. However it may be useful if I post the discovery messages that HA ha received for one of my entities. These are generated by zigbee2mqtt, and serve as an illustration. I’ll post the discovery messages for two entities that are both in the same device.

  • Topic: homeassistant/sensor/0x04cd15fffe35c3f6/battery/config
{
  "availability": [
    {
      "topic": "zigbee2mqtt/bridge/state"
    }
  ],
  "device": {
    "identifiers": [
      "zigbee2mqtt_0x04cd15fffe35c3f6"
    ],
    "manufacturer": "TuYa",
    "model": "Vibration sensor (TS0210)",
    "name": "Rat Trap One"
  },
  "device_class": "battery",
  "enabled_by_default": true,
  "entity_category": "diagnostic",
  "json_attributes_topic": "zigbee2mqtt/Rat Trap One",
  "name": "Rat Trap One battery",
  "state_class": "measurement",
  "state_topic": "zigbee2mqtt/Rat Trap One",
  "unique_id": "0x04cd15fffe35c3f6_battery_zigbee2mqtt",
  "unit_of_measurement": "%",
  "value_template": "{{ value_json.battery }}",
  "platform": "mqtt"
}
  • Topic: homeassistant/binary_sensor/0x04cd15fffe35c3f6/vibration/config
{
  "availability": [
    {
      "topic": "zigbee2mqtt/bridge/state"
    }
  ],
  "device": {
    "identifiers": [
      "zigbee2mqtt_0x04cd15fffe35c3f6"
    ],
    "manufacturer": "TuYa",
    "model": "Vibration sensor (TS0210)",
    "name": "Rat Trap One"
  },
  "device_class": "vibration",
  "json_attributes_topic": "zigbee2mqtt/Rat Trap One",
  "name": "Rat Trap One vibration",
  "payload_off": false,
  "payload_on": true,
  "state_topic": "zigbee2mqtt/Rat Trap One",
  "unique_id": "0x04cd15fffe35c3f6_vibration_zigbee2mqtt",
  "value_template": "{{ value_json.vibration }}",
  "platform": "mqtt"
}

I hope that helps.

I appreciate the help! It seems the problem was my unique_id, I’ve got it working now.

1 Like