WTH why can't I use mqtt device triggers in a blueprint?

It’s almost 2024 and still no solution in sight that is not “hacky”…

1 Like

discovery_id in mqtt device trigger would be awesome.

I have this selector:

remote:
  name: Remote
  description: IKEA Tradfri Dimmer remote to use
  selector:
    device:
      integration: mqtt
      manufacturer: IKEA
      model: TRADFRI ON/OFF switch (E1743)`

and this trigger:

trigger:
  - platform: device
    domain: mqtt
    device_id: 5488c0aad0cd6cb3abf95102cd6de90f
    type: action
    subtype: "on"
    discovery_id: 0x3425b4fffe8127d5 action_on

EDIT1:

I was able to get discovery_id via template, but device triggers don’t support templates :confused:

{% set entities_list = device_entities('5488c0aad0cd6cb3abf95102cd6de90f') %}
{% set discovery_id = entities_list[0].split('.')[1].split('_')[0] %}
{{ discovery_id }} action_on

EDIT 2:

blueprint:
  name: Ikea Tradfri Dimmer as Shutter Remote (Z2M)
  description: |
    Control a shutter with an Ikea Tradfri Dimmer over Zigbee2MQTT

    Press on I button will open the shutter, it the shutter is opening it will stop.
    Press on 0 button will close the shutter, it the shutter is closing it will stop.
  domain: automation
  input:
    remote:
      name: Remote
      description: IKEA Tradfri Dimmer remote to use
      selector:
        device:
          integration: mqtt
          manufacturer: IKEA
          model: TRADFRI ON/OFF switch (E1743)
    cover:
      name: Shutter
      description: The shutter to control
      selector:
        target:
          entity:
            domain: cover
            
variables:
  input_remote: !input remote
  input_cover: !input cover
  input_discovery_id: "{{ device_entities(input_remote)[0].split('.')[1].split('_')[0] }} action_off"
            
trigger:
  - platform: device
    domain: mqtt
    device_id: !input remote
    type: action
    subtype: "off"
    discovery_id: 0x3425b4fffe8127d5 action_off
    
action:
  - service: persistent_notification.create
    data:
      message: "{{input_discovery_id}}"
mode: restart

I was able to calculate the discovery_id inside a variable, but I’m unable to use it in trigger.

discovery_id does not support variables at the moment, you are better off using MQTT triggers instead.

are there other places that support variables? I’ll try to look at the code, maybe this could be added.

Yes, there are many fields that accept variables, however that one is not one of them. It could be easily added if someone wants to add it.

There is a suggested fix in this issue mqtt device trigger -> discovery_id does not work with trigger_variables? · Issue #106412 · home-assistant/core · GitHub but I’m just going with mqtt triggers instead as it seems easier, the only downside is that you can’t use the Device picker in your blueprint.

That’s the reason for this WTH - to be able to select a device.
Thank you for linking the issue.

When I use your suggested template it doesn’t return a hex value in discovery_id.

On pairing a device, Zigbee2MQTT identifies it by its IEEE address (a hex value that becomes part of the discovery_id in an MQTT Device Trigger). However, the user is allowed to rename it.

For example, I renamed a device (IKEA E1812) to Kitchen Button and instructed it to share that name with Home Assistant. As a result, the device was represented in Home Assistant by these three entities:

sensor.kitchen_button_battery
sensor.kitchen_button_action
update.kitchen_button

Those are the three entities that are reported by the device_entities function. There’s no IEEE address in their entity_id so when I use your template, which attempts to parse the address out of the entity_id, it produces this:

kitchen action

Unfortunately, that’s not what an MQTT Device Trigger expects for discovery_id.

I rummaged through the hidden core.device_registry file and found the device’s IEEE address in its identifiers key.

        "identifiers": [
          [
            "mqtt",
            "zigbee2mqtt_0x7deadbeefdeadbeef"
          ]
        ],

The device_attr function can report the value of identifiers but it’s a set containing a tuple.

{('mqtt', 'zigbee2mqtt_0x7deadbeefdeadbeef')}

Therefore a bit templating is needed to extract the IEEE address.

{{ (device_attr('d4bacfb73175d44ad4fdc88cbc3676b1', 'identifiers')|list)[0][1].split('_')[-1] }}

When I use the Automation Editor to create an MQTT Device Trigger for sensor.kitchen_button_action, it promptly creates a discovery_id containing the device’s IEEE address. I assume it’s getting it from core.device_registry.

It would be handy if there was a Jinja2 function that could get it by simply supplying a device_id (it would simplify the template). For example, device_discovery_id or perhaps device_ieee_addr.

With the changes from Add templating support for mqtt device automation triggers by jbouwh · Pull Request #107830 · home-assistant/core · GitHub and blueprint from Add templating support for mqtt device automation triggers by jbouwh · Pull Request #107830 · home-assistant/core · GitHub everything works in my system.

I have IKEA TRADFRI added via Zigbee2MQTT, the sensor for the action is called sensor.0x3425b4fffe8127d5_action, but I fully agree that this may not work for everyone and if there is a place where we can pick IEEE address, or better a helper for that, then it would make life much easier.

One thing to notice is that device_attr is not supported in limited templates (Templating - Home Assistant) so this may not work in
trigger_variables.

An alternative approch is Simplify MQTT device triggers by jbouwh · Pull Request #108309 · home-assistant/core · GitHub. This will remove the need to add discovery_id.

Question on related note: Why can’t MQTT entity triggers be event entities, since we have event entities now?

From HA Core 2024.2.0, the discovery_id attribute is not needed any longer. From now on device, type and subtype will be used to identify the device trigger. This makes it possible to make a blueprint without work around.

3 Likes