How can I combine to action subtypes in one automation?

I have a TS0041 Switch and would like to turn a device on or off, depend if the switch has been single tabbed or double tabbed. Right now, I did this by writing two automations (see below). How can I combine these two automations in one statement? What is the correct syntax for checking the subtype (single or double) of an action with an if-statement?

description: ""
trigger:
  - platform: device
    domain: mqtt
    device_id: c1e721fa460e4f5809fd156c2f8c3c50
    type: action
    subtype: single
    discovery_id: 0x70ac08fffe4f10a5 action_single
condition: []
action:
  - type: turn_on
    device_id: 5712cf4d05e1da4f9fa120b61ffcfaec
    entity_id: 757ef5cba285af0cf93554e142743cc7
    domain: switch
mode: single
description: ""
trigger:
  - platform: device
    domain: mqtt
    device_id: c1e721fa460e4f5809fd156c2f8c3c50
    type: action
    subtype: double
    discovery_id: 0x70ac08fffe4f10a5 action_double
condition: []
action:
  - type: turn_off
    device_id: 5712cf4d05e1da4f9fa120b61ffcfaec
    entity_id: 757ef5cba285af0cf93554e142743cc7
    domain: switch
mode: single

Here’s one way to do it, using trigger.id and a templated service call.

alias: example
description: ""
trigger:
  - platform: device
    domain: mqtt
    device_id: c1e721fa460e4f5809fd156c2f8c3c50
    type: action
    subtype: single
    discovery_id: 0x70ac08fffe4f10a5 action_single
    id: 'on'
  - platform: device
    domain: mqtt
    device_id: c1e721fa460e4f5809fd156c2f8c3c50
    type: action
    subtype: double
    discovery_id: 0x70ac08fffe4f10a5 action_double
    id: 'off'
condition: []
action:
  - service: 'switch.turn_{{ trigger.id }}'
    target:
      entity_id: 757ef5cba285af0cf93554e142743cc7
mode: single
1 Like