Why can't we create custom MQTT devices?

Allow the ability to create a custom device which can contain multiple entities.

For example defining multiple entities for my smart meter’s MQTT feed results in multiple entities, however it would be great to pull these all together under a single device.

imo it can be achieved but only for entities created using mqtt discovery.
And to be honest, I gave no idea why this limitation.

btw vote on your own WTH

duplicate Miss read, changing title.

The idea is that devices can only be automatically created. I get the idea, but don’t agree with it. It would be nice to have the ability to add custom devices to any integration.

1 Like

MQTT already support this.
https://www.home-assistant.io/integrations/sensor.mqtt/#device

?

Only works through MQTT discovery and when unique_id is set

So what? Still supports it.
Exactly what OP was asking about.

if you mean add discovery entries just to create devices, then yes. Following this logic we could drop yaml for mqtt at all replacing with discovery entirely.

I believe OP meant to add ability to create device via yaml configuration. And tbh why not? there are attributes for that ready to use.

Only works for discovery not manually created :frowning:

I voted for this WTH but here’s a workaround from 123 which is just using the MQTT discovery feature but the device and entities are still “manually” created

I also voted for that - I did see the script, but couldn’t get my head around how I could fit a bunch of entities from my smart meter under a light… then gave up as too much faff.

Personally I changed all my MQTT yaml definitions by automations running at home assistant start when they changed the syntax for MQTT in yaml. Since I had to rewrite it anyway. The good way is to set the payload as a variable where you can use yaml syntax that looks a lot like classical yaml definitions, then do a loop like that:

  automation:
    - id: Parametragteleinfokitmqtt
      alias: Paramétrage Teleinfokit mqtt
      description: ''
      mode: single
      trigger:
        - platform: homeassistant
          event: start
      condition: []
      action:
        - variables:
            common:
              device:
                name: TeleInfoKit
                connections: [["mac", "a8:48:fa:cd:8d:54"]]
        - repeat:
            for_each:
              - type: sensor
                unique_id: teleinfokit_papp
                config:
                  state_topic: "teleinfokit/papp"
                  unit_of_measurement: "W"
                  name: Puissance instantanée générale
                  icon: mdi:power-plug
                  state_class: measurement
                  device_class: power
              - type: sensor
                unique_id: teleinfokit_iinst
                config:
                  state_topic: "teleinfokit/iinst"
                  unit_of_measurement: "A"
                  name: Intensité générale
                  icon: mdi:power-plug
              - type: sensor
                unique_id: teleinfokit_base
                config:
                  state_topic: "teleinfokit/base"
                  unit_of_measurement: "kWh"
                  name: Index compteur électricité RAW
                  icon: mdi:home-analytics
                  value_template: "{{ '{{' }} (value|float) / 1000.0 {{ '}}' }}"
                  state_class: total_increasing
                  device_class: energy
            sequence:
              - service: mqtt.publish
                data:
                  topic: |
                    homeassistant/{{repeat.item.type}}/{{repeat.item.unique_id}}/config
                  payload: |
                    {{ dict(
                      common.items()|list
                      + repeat.item.config.items()|list
                      + [("unique_id", repeat.item.unique_id)]
                    ) | to_json }}

You can see that in the for_each I put settings in a config entry that look like the YAML for MQTT configuration (in fact I copy-pasted most of it). I decided to move unique_id out of the config because it is used in the discovery topic but I could as well have fetched it from the config and not needing to merge it afterwards.

The good part is that you can have common configuration without repeating it, and you also can use other kind of loops to create entities programmatically, as in the following (which is part of a recurring todolist implementation).

  automation:
    - id: parametragtodolistmqtt
      alias: Paramétrage TodoList MQTT
      description: ''
      mode: single
      trigger:
        - platform: homeassistant
          event: start
      condition: []
      action:
        - repeat:
            for_each:
              - name: croquettes
                duration: 8
              - name: draps_lucas
                duration: 312
              - name: draps_matisse
                duration: 312
              - name: draps_parents
                duration: 312
              - name: eponges
                duration: 168
              - name: filtre_sable
                duration: 744
            sequence:
              - variables:
                  payload:
                    device:
                      name: Todo List
                      identifiers: todolist
                    command_topic: "TodoList/Action"
                    payload_on: "DO {{repeat.item.name}}"
                    payload_off: "UNDO {{repeat.item.name}}"
                    state_topic: "TodoList/State/{{repeat.item.name}}"
                    state_on: "DONE"
                    state_off: "TODO"
                    json_attributes_topic: |
                      TodoList/LastDone/{{repeat.item.name}}
                    json_attributes_template: "
                      {{ '{{' }}
                        { 'duration': '{{repeat.item.duration}}',
                          'last_done': value,
                          'expiry': (value|as_datetime +
                                timedelta(hours={{repeat.item.duration}})
                                ).isoformat() }
                        | tojson
                      {{ '}}' }}"
                    unique_id: "todo_list_{{repeat.item.name}}"
                    name: "todo_{{repeat.item.name}}"
              - service: mqtt.publish
                data:
                  topic: |
                    homeassistant/switch/todo_{{repeat.item.name}}/config
                  payload: |
                    {{ payload | tojson }}
2 Likes

Also note I saw here or on GitHub discussions about devices made from the UI to group entities. AFAIU nobody is against it, it is just a matter of when/how to do that. I tend to think this is a bigger scale project with multiple ramifications so don’t hope too much I guess.

Hasn’t this now been implemented? I think this WTH should be closed/completed.

And the way I read the PR, it looks like it’s always been supported but that the docs were wrong.

Either way, it works now, and the docs show it as well:

What is the implementation when working with other entities not related to MQTT? For example SNMP and templates?

I have a PDU that has switched outlets and power monitoring. I use SNMP switches to change the state and an SNMP sensor to monitor usage. All of the entities are separate in HA even through they are the same device.

Additionally if you create any template sensors it would be nice to tie them into the new or existing devices.

I have a SwitchBot Tilt on my blinds. To get the % open/closed correct you need to create a template cover. However, that template sensor is not part of the device in HA, making harder to manage that device and all related entities.

Yes there is still no implementation to add entities to non-MQTT devices. See petro’s comment earlier in this thread with links to two other WTH’s that relate more to that: