Is there a decision tree feature in automations?

I have a wall switch that can send several messages (single press, double press, long press, …).

I would like to bind to each of these messages specific actions.

Is there a way to keep them all in one automation, where I would have a decision tree (or a multi condition similar to the “switch” command in JavaScript or Go), or do I have to make a separate automation for each message?

(I am using pyscript for that, but was curious if there was a built in solution, I could not find anything along these lines)

choose :

1 Like

As per what @francisp has linked to, here is my example using the single, double and long presses of a Xiaomi button.

automation:
  - alias: 'Ensuite Remote Button'
    trigger:
      - platform: event
        event_type: xiaomi_aqara.click
        event_data:
          entity_id: binary_sensor.switch_158d00016bf679
          click_type: single
      - platform: event
        event_type: xiaomi_aqara.click
        event_data:
          entity_id: binary_sensor.switch_158d00016bf679
          click_type: double
      - platform: event
        event_type: xiaomi_aqara.click
        event_data:
          entity_id: binary_sensor.switch_158d00016bf679
          click_type: long_click_press
    action:
      choose:
        - conditions:
          - condition: template
            value_template: "{{  trigger.event.data.click_type == 'single'  }}"
          sequence:
            - service: automation.turn_off
              entity_id: automation.ensuite_lights_auto
            - service: switch.turn_off
              entity_id: switch.adaptive_lighting_ensuite
            - service: light.turn_on
              entity_id: light.ensuite_downlight
              data:
                white: 255
            - service: light.turn_on
              data:
                entity_id: light.ensuite_led_strip
                effect: solid
                rgbw_color: [0,0,0,255]
        - conditions:
          - condition: template
            value_template: "{{  trigger.event.data.click_type == 'double'  }}"
          sequence:
            - service: automation.turn_off
              entity_id: automation.ensuite_lights_auto
            - service: switch.turn_off
              entity_id: switch.adaptive_lighting_ensuite
            - service: light.turn_off
              entity_id: light.ensuite_lights
        - conditions:
          - condition: template
            value_template: "{{  trigger.event.data.click_type == 'long_click_press'  }}"
          sequence:
            - service: automation.turn_on
              entity_id: automation.ensuite_lights_auto
            - service: switch.turn_on
              entity_id: switch.adaptive_lighting_ensuite
            - service: light.turn_on
              data_template:
                entity_id: light.ensuite_lights
                brightness_pct: "{{ state_attr('switch.adaptive_lighting_ensuite', 'brightness_pct') | int }}"
                rgb_color: "{{ state_attr('switch.adaptive_lighting_ensuite', 'rgb_color') }}"
                white_value: 0

That automation can actually be simplified even more using Trigger ID’s but I just haven’t updated it yet.

2 Likes