Automation template with device Trigger and subtype

I use Z2M with aqara buttons. I want one automation that on a single click opens a cover, on a double click closes the cover. I would like to use the chosse construct in the action sequence.

alias: WG Taster Markise
description: single zu double auf
trigger:
  - platform: device
    domain: mqtt
    device_id: a08cc1f305a243c986772bf5e00632ec
    type: action
    subtype: single
    discovery_id: 0x00158d00020595fa action_single
  - platform: device
    domain: mqtt
    device_id: a08cc1f305a243c986772bf5e00632ec
    type: action
    subtype: double
    discovery_id: 0x00158d00020595fa action_double
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ trigger.event.data.action == "double" }}'
        sequence:
          - service: cover.open_cover
            target:
              device_id:
                - b8af905540d44f46a51bf35b6235283c
      - conditions:
          - condition: template
            value_template: '{{ trigger.event.data.action == "single" }}'
        sequence:
          - service: cover.close_cover
            target:
              device_id:
                - b8af905540d44f46a51bf35b6235283c
    default: []
mode: single

i didn’t find any hints on using the trigger variable in templates. At least any hints how to solve my probleme, or even better how to examin the trigger variable in automations.

Trigger templating is covered here:

Unfortunately there is no information on device triggers. I would suggest you use an mqtt trigger instead.

1 Like

Thanx to @tom_i

During event debugging i found that there is a state with the click-type. So my solution is now:

alias: WG Taster Markise
trigger:
  - platform: state
    entity_id: sensor.taster_wg_click
    to: single
  - platform: state
    entity_id: sensor.taster_wg_click
    to: double
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ trigger.to_state.state == "double" }}'
        sequence:
          - service: cover.open_cover
            target:
              device_id:
                - b8af905540d44f46a51bf35b6235283c
      - conditions:
          - condition: template
            value_template: '{{ trigger.to_state.state == "single" }}'
        sequence:
          - service: cover.close_cover
            target:
              device_id:
                - b8af905540d44f46a51bf35b6235283c
    default: []
mode: single

But there is a question still open. How to debug the trigger variable?

With the knowledge of every attribute in it, it would increase the possibilties within templates. The mentioned doc-page helped a bit.

There’s an automation debugging tool in the next release. If you join the beta program you’ll be able to give feedback on its use and request the trigger states be added. Or wait for the next stable release and create a feature request then.

One of the main reasons to avoid using them if one intends to modify the resulting YAML code. They were designed to be managed by the Automation Editor (where their many options and unwieldy GUIDs are hidden from human eyes).


As for the trigger variable, it’s thoroughly documented in the link tom_I posted. The only mystery is usually the Event Trigger because events can have all sorts of data. To learn what a particular event contains, use Developer Tools > Events > Listen to events. It will reveal an event’s contents.

2 Likes

The state mentioned in @lubeda’s comment is deprecated and will not be updated when homeassistant: legacy_triggers is not enabled in Z2M.

From what I can see the documentation for device triggers is simply wrong, there does not seem to be an trigger.event object.

I created an automation like this:

trigger:
  - platform: device
    domain: mqtt
    device_id: ...
    type: action
    subtype: single
    discovery_id: ... action_single
action:
  - event: foo
    event_data:
      t: "{{ trigger|pprint }}"

Then I listened to the foo event in the Developer Tools and got this:

event_type: foo
data:
  t:
    alias: null
    description: mqtt topic zigbee2mqtt/butt/action
    id: "0"
    idx: "0"
    payload: single
    platform: mqtt
    qos: 0
    topic: zigbee2mqtt/butt/action
...

By the way, don’t ask me how the output of pprint ended up as YAML, I would have expected a string.

So this tells us that the condition we need is:

action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.payload == 'single' }}"
2 Likes