alias: Z2M Tap Dial Switch Automation
description: Zigbee2MQTT - Philips Hue Tap Dial Switch by SmartHomeGeeks.io
trigger:
  - platform: mqtt
    topic: zigbee2mqtt/Tap Dial Switch Man Cave # Change to correct value
condition:
  - condition: template
    value_template: "{{ trigger.payload_json.action != '' }}"
action:
  - variables:
      input_text_var: input_text.philips_hue_dial_switch_last_pressed # Change to correct value
      counter_var: counter.philips_hue_dial_switch_button_counter # Change to correct value
    alias: Variables for input and counter helpers
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.payload_json.action is match('^button_[1-4]_press$') }}"
        sequence:
          - if:
              - condition: template
                value_template: "{{ trigger.payload_json.action | regex_replace(find='[^1-4]', replace='') != states(input_text_var) }}"
            then:
              - service: counter.reset
                target:
                  entity_id: "{{ counter_var }}"
            alias: Prevent different buttons to be registered as double press
          - service: input_text.set_value
            data:
              value: "{{ trigger.payload_json.action | regex_replace(find='[^1-4]', replace='') }}"
            target:
              entity_id: "{{ input_text_var }}"
            alias: Update the input text helper with the last pressed button number
          - service: counter.increment
            target:
              entity_id: "{{ counter_var }}"
            alias: Increase the counter helper with 1
          - delay:
              seconds: 1
          - service: counter.reset
            target:
              entity_id: "{{ counter_var }}"
            alias: Reset the counter helper
        alias: Update Last Pressed
      - conditions:
          - condition: template
            value_template: "{{ trigger.payload_json.action is match('button_[1-4]_press_release') and states(counter_var) > '1' }}"
        sequence:
          - choose:
              - conditions:
                  - condition: template
                    value_template: "{{ trigger.payload_json.action == 'button_1_press_release' }}"
                sequence: []
                alias: Double press button 1
              - conditions:
                  - condition: template
                    value_template: "{{ trigger.payload_json.action == 'button_2_press_release' }}"
                sequence: []
                alias: Double press button 2
              - conditions:
                  - condition: template
                    value_template: "{{ trigger.payload_json.action == 'button_3_press_release' }}"
                sequence: []
                alias: Double press button 3
              - conditions:
                  - condition: template
                    value_template: "{{ trigger.payload_json.action == 'button_4_press_release' }}"
                sequence: []
                alias: Double press button 4
        alias: Double xxx2 event
    default:
      - if:
          - condition: template
            value_template: "{{ trigger.payload_json.action is match('button_[1-4]_press_release') }}"
        then:
          - delay:
              milliseconds: 250
          - if:
              - condition: template
                value_template: "{{ states(counter_var) > '1' }}"
            then:
              - stop: Double tap detected
        alias: Single xxx2 event
      - choose:
          - conditions:
              - condition: template
                value_template: "{{ states(input_text_var) == '1' and trigger.payload_json.action is match('^dial_rotate_') }}"
            sequence:
              - choose:
                  - conditions:
                      - condition: template
                        value_template: "{{ trigger.payload_json.action is match('^dial_rotate_left_') }}"
                    sequence: []
                  - conditions:
                      - condition: template
                        value_template: "{{ trigger.payload_json.action is match('^dial_rotate_right_') }}"
                    sequence: []
            alias: Dial rotate after button 1 pressed
          - conditions:
              - condition: template
                value_template: "{{ states(input_text_var) == '2' and trigger.payload_json.action is match('^dial_rotate_') }}"
            sequence:
              - choose:
                  - conditions:
                      - condition: template
                        value_template: "{{ trigger.payload_json.action is match('^dial_rotate_left_') }}"
                    sequence: []
                  - conditions:
                      - condition: template
                        value_template: "{{ trigger.payload_json.action is match('^dial_rotate_right_') }}"
                    sequence: []
            alias: Dial rotate after button 2 pressed
          - conditions:
              - condition: template
                value_template: "{{ states(input_text_var) == '3' and trigger.payload_json.action is match('^dial_rotate_') }}"
            sequence:
              - choose:
                  - conditions:
                      - condition: template
                        value_template: "{{ trigger.payload_json.action is match('^dial_rotate_left_') }}"
                    sequence: []
                  - conditions:
                      - condition: template
                        value_template: "{{ trigger.payload_json.action is match('^dial_rotate_right_') }}"
                    sequence: []
            alias: Dial rotate after button 3 pressed
          - conditions:
              - condition: template
                value_template: "{{ states(input_text_var) == '4' and trigger.payload_json.action is match('^dial_rotate_') }}"
            sequence:
              - choose:
                  - conditions:
                      - condition: template
                        value_template: "{{ trigger.payload_json.action is match('^dial_rotate_left_') }}"
                    sequence: []
                  - conditions:
                      - condition: template
                        value_template: "{{ trigger.payload_json.action is match('^dial_rotate_right_') }}"
                    sequence: []
            alias: Dial rotate after button 4 pressed
          - conditions:
              - condition: template
                value_template: "{{ trigger.payload_json.action == 'button_1_press_release' }}"
            sequence: []
            alias: Button 1 pressed
          - conditions:
              - condition: template
                value_template: "{{ trigger.payload_json.action == 'button_1_hold' }}"
            sequence: []
            alias: Button 1 hold
          - conditions:
              - condition: template
                value_template: "{{ trigger.payload_json.action == 'button_2_press_release' }}"
            sequence: []
            alias: Button 2 pressed
          - conditions:
              - condition: template
                value_template: "{{ trigger.payload_json.action == 'button_2_hold' }}"
            sequence: []
            alias: Button 2 hold
          - conditions:
              - condition: template
                value_template: "{{ trigger.payload_json.action == 'button_3_press_release' }}"
            sequence: []
            alias: Button 3 pressed
          - conditions:
              - condition: template
                value_template: "{{ trigger.payload_json.action == 'button_3_hold' }}"
            sequence: []
            alias: Button 3 hold
          - conditions:
              - condition: template
                value_template: "{{ trigger.payload_json.action == 'button_4_press_release' }}"
            sequence: []
            alias: Button 4 pressed
          - conditions:
              - condition: template
                value_template: "{{ trigger.payload_json.action == 'button_4_hold' }}"
            sequence: []
            alias: Button 4 hold
mode: parallel
max: 10
max_exceeded: silent
