ESPHome and IR Sensor - How to detect a sequence

Hi all, I am using ESPHome with an IRReceiver sensor. All is working fine and my remote are perfectly detected by ESPHome.
But I would like ESPHome to be able to detect a sequence of keys pressed and then launch a dedicated action.

  • Let’s say when I press “1” on my remote and nothing more within x ms → We detect “1” pressed and launch an action associated to “1”
  • But if I press “1” on my remote, then press “2” with x ms → We detect “12” and we launch an associated specific action associated to “12”

Any idea on how to perform this? Is there a way to somewhat detect a keypress sequence?

Thanks for your help.

Self answer. Here is how I handled it.

I first create a global to store the keysequence before actually publishing it

globals:
  - id: on_going_command
    type: std::string
    restore_value: no
    initial_value: ""

Then I created a “text sensor” that will received the sequence:

text_sensor: 
    - platform: template
      name: remote_command
      id: remote_command
      icon: "mdi:remote-tv"
      internal: False

Then I created a script that I will trigger each time a button is pressed on the remote.
It is important to set the mode to “restart” so that the script is retriggered on each button press.
At the end of the script, the key sequence will be published:

#
# Script to wait for the next remote press
#
script:
  - id: command_timeout
    mode: restart
    then:
      - delay: 1s
      - lambda: |- 
          id(remote_command).publish_state(id(on_going_command));
      - lambda: |-
          id(on_going_command)="";

And finally, for each button press, I created an action that will concatenate the key pressed to the key sequence and also launch the script. So that 1s after the press of the button, the sequence will be published to the sensor.

  - platform: remote_receiver
    name: "Btn_0"
    nec:
      address: 0x0E86
      command: 0xFF00

    on_press:
      - script.execute: command_timeout
      - lambda: |- 
          id(on_going_command)=id(on_going_command)+"0";


  - platform: remote_receiver
    name: "Btn_1"
    nec:
      address: 0x0E86
      command: 0xFE01   

    on_press:
      - script.execute: command_timeout
      - lambda: |- 
          id(on_going_command)=id(on_going_command)+"1"; 

In the end, everything is fine, when I press “123” in sequence with my remote, text_sensor recognizes “123”.

1 Like

Nice one. Good on you for sharing. I’ve been thinking about doing something similar for some time.