How to use Device triggers in blueprint?

Hi I’m trying to create a blueprint for a device that sends events from zigbee2mqtt but the discovery_id gives me some troubles…

I have the following blueprint

blueprint:
  name: 'Zigbee2MQTT - Hue Tap Dail Switch'
  description: |
    Control lights with a Philips Hue Tap Switch. Use the four buttons
    to control up to four light(s) with an on/off toggle. The dial can be used to
    dim the most recently used light.
    source https://community.home-assistant.io/t/philips-hue-tap-dial-switch-zha/446636/114
  domain: automation
  input:
    button_sensor:
      name: Philips Hue Tap dial switch sensor entity
      description: Sensor for Philips Hue Tap dial switch to use
      selector:
        device:
          multiple: false
          integration: mqtt
          manufacturer: Philips
          model: 'Hue Tap dial switch (8719514440937/8719514440999)'
    button_sensor_discovery_id:
      name: discovery_id
      description: 'The first part of the discovery_id "0x001788010d7d3cac"'
      selector:
        text:
mode: restart
max_exceeded: silent
variables:
  button_sensor_discovery_id: !input button_sensor_discovery_id
  action_button_1_press_release: "{{ button_sensor_discovery_id }} action_button_1_press_release"
trigger:
  - platform: device
    domain: mqtt
    device_id: !input button_sensor
    type: action
    subtype: button_1_press_release
    discovery_id: "{{ action_button_1_press_release }}"
action:
  - service: notify.mobile_app_pixel_6_pro
    data:
      message: "action_button_1_press_release: {{action_button_1_press_release}}"

Creating it works like a charm and triggering it manually does indeed work, but when pressing the button nothing happens.

If I hardcode the discovery_id value then it works.
So my guess is that discovery_id not support templates/variables?

What changes would be needed in HA to make this work? (I’m willing to make a pull request to make this happen if someone can give me some pointers)

If there is some way to use a variable in discovery_id that I have missed then please enlighten me :slight_smile:

Edit:
I have also tried to move my variables into trigger_variables but that didn’t help.

blueprint:
  name: 'Zigbee2MQTT - Hue Tap Dail Switch'
  description: |
    Control lights with a Philips Hue Tap Switch. Use the four buttons
    to control up to four light(s) with an on/off toggle. The dial can be used to
    dim the most recently used light.
    source https://community.home-assistant.io/t/philips-hue-tap-dial-switch-zha/446636/114
  domain: automation
  input:
    button_sensor:
      name: Philips Hue Tap dial switch sensor entity
      description: Sensor for Philips Hue Tap dial switch to use
      selector:
        device:
          multiple: false
          integration: mqtt
          manufacturer: Philips
          model: 'Hue Tap dial switch (8719514440937/8719514440999)'
    button_sensor_discovery_id:
      name: discovery_id
      description: 'The first part of the discovery_id "0x001788010d7d3cac"'
      selector:
        text:
mode: restart
max_exceeded: silent
trigger_variables:
  button_sensor_discovery_id: !input button_sensor_discovery_id
  action_button_1_press_release: "{{ button_sensor_discovery_id }} action_button_1_press_release"
trigger:
  - platform: device
    domain: mqtt
    device_id: !input button_sensor
    type: action
    subtype: button_1_press_release
    discovery_id: "{{ action_button_1_press_release }}"
action:
  - service: notify.mobile_app_pixel_6_pro
    data:
      message: "{{action_button_1_press_release}}"

Personally I hate device triggers and find them pretty much unusable. However I can try to help you.

Your first one could never work because variables are not rendered until after the trigger. So your instincts to use trigger_variables is a good one.

Your next problem is this:

You need to put this into developer - templates and edit it until what you get is what you are trying to get. I would guess that is not giving you the action_button_1_press_release that you want it to, and that is why it isn’t working.

I personally would change this to trigger more generally, IE on every trigger of the device and then in the condition or action statement sort out what the trigger event was and act on it there. In condition statements you have the full set of template functions to work with. In the trigger you only have a few.
But that is a personal choice.

Edit 2 days later:
Why and how to avoid device_ids in automations and scripts - Community Guides - Home Assistant Community.

Plus I found out device triggers and templates are not a thing.

1 Like

I’m not 100% sure but I think the value of action_button_1_press_release is correct, when I trigger it manually it looks correct.

The step details have the following

params:
  domain: notify
  service: mobile_app_pixel_6_pro
  service_data:
    message: action_button_1_press_release: 0x001788010d7d3cac action_button_1_press_release
  target: {}
running_script: false

And if I setup the trigger manually in a automation I get

  - platform: device
    domain: mqtt
    device_id: 159f1b5a4d1e1a848b14e72e0698ccf7
    type: action
    subtype: button_1_press_release
    discovery_id: 0x001788010d7d3cac action_button_1_press_release

Edit:
But just to be sure I tried the following template in the template editor

{% set button_sensor_discovery_id = '0x001788010d7d3cac' %}
{{ button_sensor_discovery_id }} action_button_1_press_release

And it outputs 0x001788010d7d3cac action_button_1_press_release
which is correct?

Edit 2: I tried to hardcode the trigger variable to "0x001788010d7d3cac action_button_1_press_release" and that also fails, so I’m 99% sure its not the value in the variable that is the issue.

I have a few blueprints where I use this trigger:

trigger_variables:
  base_topic: !input base_topic
  controller: !input controller
trigger:
- platform: mqtt
  topic: '{{ base_topic ~ ''/'' ~ controller }}'

I use this in the input part of the blueprint

  input:
    controller:
      name: Z2M Controller Name
      description: The name of the device, remember device name must have no space or - _
      default: ''
    base_topic:
      name: Z2M Base mqtt topic
      description: The base topic z2m
      default: zigbee2mqtt

However, I better like to use “state”. Seem easier.

1 Like

I ended up creating a bug, as it just seems like discovery_id won’t accept trigger_variables.

I went with the mqtt trigger option instead haven’t tested it yet tho https://github.com/AnderssonPeter/HomeAssistantBlueprints/blob/760487baae0f5e5d933ff896e63a0ead322aefb0/HueTapDailSwitch.yaml