Add entity to mqtt device

Hey folks,

I have my laptop reporting power to HA via MQTT (iot-link) and have a device in the UI with the various sensors listed. I am also using the command functionality in iot-link to suspend the laptop from automations. I have done this by creating an mqtt button in my config that send the appropriate topic.

All of this works fine.

However, what I would like to do is add the button entity to the device so that they both appear in the UI together under the same device with button press listed under controls.

Is there anyway to link them together?

Thanks

J

A lot of searching has led me to this: Manually add MQTT Entities - #7 by 123

But I have to admit, I am struggling. Can anyone link to any other guides on how to publish MQTT discovery messages to add an extra entity to a device?

It’s done exactly like I explained in the linked topic. In order for the discovery message to add an entity (i e. a button entity) to an existing device, the discovery message must contain the same device parameters as the device.

  1. Go to Settings > Devices & Services > Devices
  2. Click on the device you want to add a button to.
  3. Take note of the device’s parameters displayed in the upper left corner of the first card.

Example:

Here’s one of my devices that was created via MQTT Discovery.

That’s the values you will need to use in the discovery message’s device portion.

Thanks @123 thats what I have been following

I think I am heading in the right direction. Here is what I have got.

The laptop has mqtt discovery topics for several sensors. Here is an example:

Published to: homeassistant/sensor/iotlink/t510_power_batterylevel/config
Payload:

{
  "availability_topic": "iotlink/workgroup/t510/lwt",
  "state_topic": "iotlink/workgroup/t510/windows-monitor/stats/battery/level",
  "name": "T510 Battery Level",
  "unique_id": "t510_power_batterylevel",
  "device_class": "battery",
  "payload_available": "ON",
  "payload_not_available": "OFF",
  "device": {
    "identifiers": [
      "T510_Power"
    ],
    "name": "T510 Power",
    "model": "WORKGROUP",
    "manufacturer": "IOTLink 2.2.2.0"
  }
}

So I have created the following:

Published to: homeassistant/button/iotlink/t510_suspend/config
Payload:

{
  "name": "T510 Suspend",
  "availability_topic": "iotlink/workgroup/t510/lwt",
  "unique_id": "t510_suspend",
  "command_topic": "iotlink/workgroup/t510/commands/suspend",
  "entity_category": "controls",
  "device": {
     "identifiers": [
       "T510_Power"
     ],
     "name": "T510 Power",
     "model": "WORKGROUP",
     "manufacturer": "IOTLink 2.2.2.0"
  }
}

Which I thought might work but doesnt!

Is the button created but isn’t part of the device or is it simply not created at all?

Are there any related errors in the Log?

I cant find the button the entities list in the UI

I have the following error in the log which I need to read a few time to try and digest

Exception in async_discover when dispatching 'mqtt_discovery_new_button_mqtt': ({'availability_topic': 'iotlink/workgroup/t510/lwt', 'name': 'T510 Suspend', 'unique_id': 't510_suspend', 'command_topic': 'iotlink/workgroup/t510/commands/suspend', 'entity_category': 'controls', 'device': {'identifiers': ['T510_Power'], 'name': 'T510 Power', 'model': 'WORKGROUP', 'manufacturer': 'IOTLink 2.2.2.0'}, 'platform': 'mqtt'},) Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/components/mqtt/mixins.py", line 334, in async_discover config: DiscoveryInfoType = discovery_schema(discovery_payload) File "/usr/local/lib/python3.10/site-packages/voluptuous/schema_builder.py", line 272, in __call__ return self._compiled([], data) File "/usr/local/lib/python3.10/site-packages/voluptuous/schema_builder.py", line 595, in validate_dict return base_validate(path, iteritems(data), out) File "/usr/local/lib/python3.10/site-packages/voluptuous/schema_builder.py", line 433, in validate_mapping raise er.MultipleInvalid(errors) voluptuous.error.MultipleInvalid: expected EntityCategory or one of 'config', 'diagnostic' for dictionary value @ data['entity_category']

Fixed it!
I removed

 "entity_category": "controls",

It now appears but doesnt allow me to press :frowning:

Edited to add: Removing the availability topic has fixed the problem. It now appears, I can press it and it works!

The final solution was to publish:

{
  "name": "T510 Suspend",
  "unique_id": "t510_suspend",
  "command_topic": "iotlink/workgroup/t510/commands/suspend",
  "device": {
     "identifiers": [
       "T510_Power"
     ],
     "name": "T510 Power",
     "model": "WORKGROUP",
     "manufacturer": "IOTLink 2.2.2.0"
  }
}

It’s odd that you had to remove the availability topic to make it work given that it’s the same topic used by the other IOTLink entities.

I suppose its possible I was just impatient and hadn’t waited for it to receive anything on that topic in the few seconds I was looking at it :slight_smile:

I may add it back and see what happens

Check if the payload that is published to the availability topic is a retained message. If it is then Home Assistant receives immediately upon connecting to the MQTT broker. If it isn’t then, yes, you would have to wait until it’s published.