#Different actions based on trigger subtype

1 messages · Page 1 of 1 (latest)

raw fable
#

My automation triggers different actions based on a single, double or long button press. In the trigger section of my automation, this is configured using an MQTT device trigger for each subtype. Looking at the traces, this is working correctly. In the action section of my automation, I use a single 'choose' statement to decide what to do next based on the subtype. I do this as follows:

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

This seems to not be the right way, as the choose statement keeps going to the default action.

Now, looking at the 'trigger object', I actually do not see something called the 'trigger.subtype' but I do see a property called 'payload' which I guess I could be using here. I could also choose based on the id and idx fields, as both are different depending on the trigger ... triggered. None of these solution feel like they are the right thing to do here. I am after all defining 3 individual triggers with each a subtype explicitly added.
So I am wondering ... what is the right way of choosing based on trigger subtype?

plucky halo
raw fable
#

Thanks a lot for the response. Much appreciated.

#

I guess the 'subtype' of the trigger isnt something i can base further automation on. That's too bad. I'll either split it into 3 automations or I guess use the payload value.

#

I was convinced that the subtype must be accessible somewhere. Why else make defining it mandatory. Oh well.

nimble fossil
#

But what is your actual trigger? Can you show the yaml?

raw fable
# nimble fossil But what is your actual trigger? Can you show the yaml?

Sure:

trigger:
  - platform: device
    device_id: a5a3d908d2fdea501ed303a88443b730
    domain: mqtt
    type: action
    subtype: single
  - platform: device
    device_id: a5a3d908d2fdea501ed303a88443b730
    domain: mqtt
    type: action
    subtype: double
  - platform: device
    device_id: a5a3d908d2fdea501ed303a88443b730
    domain: mqtt
    type: action
    subtype: long
#

And then in the action section, I do a choose statement with 3 options and a default. As per my original post.

#

As subtype is mandatory, I assumed there must be a way to use it elsewhere. That imho would be the elegant solution.

nimble fossil
#

Yeah, device triggers are just not very flexible. Think most experienced users don't like them 😄

raw fable
#

It's the recommended way to use zigbee2mqtt inside HA, according to their documentation.

nimble fossil
#

But to do what you want. just give every trigger an id. Then you can use the trigger condition in the chooser.

nimble fossil
raw fable
#

Fair enough 😉

#

It would work like this (based on a single google search, forgive me if it's wrong):

    - platform: device
      id: 'single'
      device_id: a5a3d908d2fdea501ed303a88443b730
      domain: mqtt
      type: action
      subtype: single
#

or does it need to be globally unique, like the automation.id field?

nimble fossil
#

scope is just the automation itself.

raw fable
#

ah ok

nimble fossil
#

So you could do:

description: ""
mode: single
triggers:
  - device_id: a5a3d908d2fdea501ed303a88443b730
    domain: mqtt
    type: action
    subtype: single
    id: single
    trigger: device
  - device_id: a5a3d908d2fdea501ed303a88443b730
    domain: mqtt
    type: action
    subtype: double
    id: double
    trigger: device
  - device_id: a5a3d908d2fdea501ed303a88443b730
    domain: mqtt
    type: action
    subtype: long
    id: long
    trigger: device
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - single
        sequence: []
raw fable
#

it seems a bit superfluous to have to enter the id for each trigger, but at least its a portable and consistent way .... more elegant than using trigger.payload

nimble fossil
#

You could also do:

description: ""
mode: single
triggers:
  - device_id: a5a3d908d2fdea501ed303a88443b730
    domain: mqtt
    type: action
    subtype: single
    trigger: device
  - device_id: a5a3d908d2fdea501ed303a88443b730
    domain: mqtt
    type: action
    subtype: double
    trigger: device
  - device_id: a5a3d908d2fdea501ed303a88443b730
    domain: mqtt
    type: action
    subtype: long
    trigger: device
conditions: []
actions:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.idx == 0 }}"
        sequence: []

Which would match with the first trigger

raw fable
#

yeah i already thought of that, too

#

but then if code gets shifted around, the logic breaks

nimble fossil
#

yep

raw fable
#

I implemented the id in the automation and that is working well.

#

excellent. this is much better for my CDO than the payload solution.