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.

let me tell you, manually publishing a discovery message just to add a new entity seems quite laborious to me (this is a message to the mantainers not you).
what if I had to add 100 MQTT entities? wouldn’t it be more straightforward to have a dedicated yaml syntax instead of publishing a specially crafted message for each device?

1 Like

2 years later …

Check the mqtt documentation, it is already possible to add a entity to an existing device in yaml

let me address your sarcasm, 2 years is nothing for a platform which requires hundreds of configurations and man hours to setup.
I setup my cameras as follows just 2 years ago:

  - platform: mqtt
    command_topic: "cameras_socket"
    state_topic: "cameras_socket/status"
    name: Videocameras
    payload_on: "on"
    payload_off: "off"
    optimistic: true

and now 2 years later (which feels like 2 days considering how many other devices I have set up), I realize the entity is not appearing anymore in home assistant.
And I should re-read the manual because no upgrade procedure has been setup for those who originally started using home assistant some years ago and didn’t keep reading the forum every day.

what is even more odd is that searching google for:

https://www.google.com/search?client=firefox-b-d&q=add+a+entity+to+an+existing+device+in+yaml

returns a question originally asked in 2021: Assign entity to a device

My two cents of feedback: let’s try to keep configuration options as they are and change them gradually in the span of years while keeping an upgrade guide somewhere to reference in the forum: “check the documentation” for people who already read it years ago and need to re-read it completely because everything has been changed is not an answer.

Not sarcasm, HA is an evolving platform, looking at posts that are 2 years old does often not result in the best information

I don’t know if it’s me but I keep finding forum answers which are at least 1 or 2 years old, and they are not slighly off but completely irrelevant to the current release.
Imagine if any StackOverflow answer were irrelevant just after a year because the software has completely changed and you have to read the manual again.

The fact the home assistant manual is not browsable by version doesn’t help.

Again, just my 2 cents