Long Button Press in automation > Turn off all lights

I have a 433MHz remote with 10 buttons. Long presses don’t result in prolonged “On” states; instead, the result is On/Off/On/Off… and my ESPHome yaml features delayed_off: 500ms, so I presume the button would have five “On” states in three seconds.

I want Button 2 to turn off the lights in my bedroom, but I want a long press to turn off every light in the house. Let’s say a long press is three seconds. I think this would call for two parallel sequences when Button 2 is pressed. One sequence to turn off the lights in my room and a parallel sequence to wait for five “On” states within three seconds and then activate the “All Off” scene.

Here is a relevant excerpt of my automation so far:

alias: 433MHz Buttons
description: ""
trigger:

 - platform: state
    entity_id:
      - binary_sensor.remote_433mhz_1_button_2
    id: Button 2
    to: "on"


action:
  - choose:
        - conditions:
          - condition: trigger
            id: Button 2
        sequence:
          - parallel:
              - service: light.turn_off
                data:
                  transition: 2
                target:
                  entity_id:
                    - light.master_bedroom_dimmer
                    - light.master_bedroom_lamp_right_side
                    - light.master_bedroom_lamp_left_side

              - service: 
                  (...if 5 On states within 3 seconds --> Activate Scene: "All Off")

 
Any assistance would be appreciated.

Or, perhaps there is a way to create a template binary sensor that turns On when Button 2 is on 5 times in rapid succession?

Solved. I am using the below YAML for Button 2 and I’ve added an option in my automation that waits for the input button’s trigger ID.

  - platform: remote_receiver
    name: "433MHz Remote 1 Button 2"
    rc_switch_raw:
      code: '(redacted)'
      protocol: 6
    filters:
      - delayed_off: 500ms
    on_multi_click:
    - timing:
        - ON for at most 1s
        - OFF for at most 0.75s
        - ON for at most 1s
        - OFF for at most 0.75s
        - ON for at most 1s
      then:
        - homeassistant.service:
            service: input_button.press
            data:
              entity_id: input_button.remote_433mhz_button_2_long_press
    - timing:
        - ON for at least 2.5s
      then:
        - homeassistant.service:
            service: input_button.press
            data:
              entity_id: input_button.remote_433mhz_button_2_long_press

Sometimes the long press on the remote does translate to the binary sensor staying on for a long press, so I added the 2.5s timing as well.