Detect double press on RF remote

Hi everybody. I would like some help with a mini project.
I have an RF remote connected to a sonoff bridge with original firmware.
The remote has 2 buttons A and B.
I can detect each button press in home assistant as STATE CHANGE based on last_triggered time.
However the integration doesn’t detect double press or hold for instance.
I am trying to simulate a double press of a button with an automation.
When I press for instance button A it triggers an automation that waits for another trigger (button A again) and have 2 possible actions based on that.
However the second A button press never gets detected and I don’t know why.
If I do wait for trigger button B for example this gets detected and I can have an action fire off if B is pressed right after A.
Can u take a look at this yaml?
Thanks

alias: "RF AUTOMATION NOT WORKING "
description: ""
trigger:
  - platform: state
    entity_id:
      - button.1001588a20_2
condition: []
action:
  - wait_for_trigger:
      - platform: state
        entity_id:
          - button.1001588a20_2
        id: A
    timeout:
      hours: 0
      minutes: 0
      seconds: 2
      milliseconds: 0
  - if:
      - condition: template
        value_template: "{{wait.trigger.id == \"A\"}}"
    then:
      - service: switch.turn_off
        data: {}
        target:
          entity_id: switch.prise_pc_l3
    else:
      - service: switch.turn_on
        data: {}
        target:
          entity_id: switch.prise_pc_l3
mode: single

I seem to remember this being a bug.

Even though you are waiting in the automation for the second A button press and the automation mode is single, the second A button trigger stops the automation.

I’ll see if I can find the issue that was opened about it.

EDIT: here it is:

It was poorly explained and they let it go stale so it was never fixed.

I suggest you open a new issue and highlight the fact that the second time the trigger occurs, instead of being processed by the wait_for_trigger the automation stops and that this occurs when the wait_for_trigger matches one of the automation triggers.

Do not just point to this forum topic. Put all the information in the new issue, including your relevant system log entries (there will be one about the automation stopping). You could even link to the stale issue.

I think you should use a device as trigger in your wait and not a state.
This could be a way to go:

alias: TST - Double click
description: ""
trigger:
  - device_id: 4f83861da1da08c366b6f1b3fb81c727
    domain: deconz
    platform: device
    type: remote_button_short_press
    subtype: turn_on
condition: []
action:
  - wait_for_trigger:
      - device_id: 4f83861da1da08c366b6f1b3fb81c727
        domain: deconz
        platform: device
        type: remote_button_short_press
        subtype: turn_on
    timeout:
      hours: 0
      minutes: 0
      seconds: 1
      milliseconds: 0
  - if:
      - condition: template
        value_template: |-
          {{
            not wait.trigger
          }}
    then:
      - service: persistent_notification.create
        data:
          message: Wait completed, no double click
          notification_id: tst02
    else:
      - service: persistent_notification.create
        data:
          message: Double click
          notification_id: tst02
mode: single

Tested it with a spare IKEA switch before posting it. Don’t know if there is any difference using an RF remote. Next, you should of course specify your own device. Another approach could be using wait.remaining instead of wait.trigger.

Of course you can do anything you want with the wait.trigger in the else condition. I guess a choose will work to, as long as the first one is the not wait.trigger template condition.

1 Like

If true, that’s the first time device triggers have ever proven useful.

The state based trigger/wait_for_trigger bug still needs reporting and fixing.

I am not allowed to upload MP4, so I used a (poor) web converter to quickly convert it to GIF with this low quality GIF as a result, but it does show the PoC :slight_smile: :arrow_heading_down:

Doubleclick

1 Like

Thanks for the replies guys.
for tom_I
I don’t see the same behaviour as in the bug report.
He seems to talk about the automation giving him an error message and stopping. For me I can see only the trace from the first button press which always evaluates the template to false (wait.trigger is null).
for Recte
I have to try with a ZigBee button and see if your solution works. The problem is the only way I know to detect the RF button press is state change. There is no device trigger for it.

Because the automation stops when the second button press occurs?

Hmmm, then well. I don’t know the details of the bug, but it’s worth a try using the state trigger in the wait.
Just remember, if the wait times out, there is no wait.trigger, so your IF will likely fail and the else will probably not be reached. I even think the automation “crashes” if you try to use the wait.trigger while it’s not there.

You can simulate this by adding a persistent notification ahead of the if/then in the PoC.

service: persistent_notification.create
        data:
          message: "{{ wait.trigger }}" 
          notification_id: tst01

So I’d always start with the not wait.trigger to catch the scenario where there is no second press.

Try changing your condition:

  - if:
      - condition: template
        value_template: '{{wait.trigger.id == "A" }}'

I think the automation fails when there is no second press, see my changed post above.

Work’a’round might be to just free that thing and flash your own *ware :tada:

You can then easily detect long/double/etc. presses directly on the bridge (in esphome) and even trigger the actions directly. It should make things much easier. :raised_hands:

Just a little example from a esphome node yaml: :point_down:

binary_sensor:
  - platform: remote_receiver
    name: "Little Blue Button"
    rc_switch_raw:
      code: '011001111010101010101010'
      protocol: 1
    on_click:
    - min_length: 0ms
      max_length: 1000ms
      then:
        - homeassistant.service:
            service: light.toggle
            data:
              entity_id: light.....
    - min_length: 1000ms
      max_length: 5000ms
      then:
        - homeassistant.service:
            service: fan.toggle
            data:
              entity_id: fan.....
    on_double_click:
      min_length: 50ms
      max_length: 350ms
      then:
        - homeassistant.service:
            service: switch.toggle
            data:
              entity_id: switch.....

tom_I
There is a single trace, and it’s probably the first button press, I need to log the exact trigger time.
I don’t know if it’s intended behaviour but seems logical
If the second button press in quick succession it should not trigger another instance of the automation.
This is the trace:

Triggered manually at 27 August 2023 at 16:05:30
Wait for 1 trigger
2 seconds later
If: then action executed
Call a service ‘Logbook: Log’ on
Custom (button.1001588a20_2) Single press detected
Finished at 27 August 2023 at 16:05:32 (runtime: 2.01 seconds)

And it’s actually possible to use the device trigger, I missed it before. However it’s the same result. The second press never gets detected

Can you press the download trace (Top right) and paste the full trace here please?

@orange-assistant

I didn’t know that was possible. Seems really powerful. However it’s a bit too much trouble to flash esp home on the sonoff bridge. With the physical modifications and all. Have to see what the advantages of the tasmota firmware are. Do I know if it allows multi press detection?

@tom_l

I extended the wait period to 10 seconds to see if the integration has a minimum delay between notifying of two consecutive presses. Still the second press doesn’t get detected.
Here’s the full trace (2 seconds delay, button pressed 2 times)

{
  "trace": {
    "last_step": "action/1/then/0",
    "run_id": "9a3e831e4ddac2224fba2a8ce8761ed8",
    "state": "stopped",
    "script_execution": "finished",
    "timestamp": {
      "start": "2023-08-27T14:05:30.510326+00:00",
      "finish": "2023-08-27T14:05:32.520501+00:00"
    },
    "domain": "automation",
    "item_id": "1693128603375",
    "trigger": null,
    "trace": {
      "trigger": [
        {
          "path": "trigger",
          "timestamp": "2023-08-27T14:05:30.510424+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "automation.rf_automation_not_working",
              "state": "on",
              "attributes": {
                "id": "1693128603375",
                "last_triggered": "2023-08-27T10:37:21.852362+00:00",
                "mode": "single",
                "current": 0,
                "friendly_name": "RF AUTOMATION NOT WORKING "
              },
              "last_changed": "2023-08-27T10:41:47.407067+00:00",
              "last_updated": "2023-08-27T10:41:47.407067+00:00",
              "context": {
                "id": "01H8V9BRWFP6CJM5H3MMMZWKTB",
                "parent_id": null,
                "user_id": null
              }
            },
            "trigger": {
              "platform": null
            }
          }
        }
      ],
      "action/0": [
        {
          "path": "action/0",
          "timestamp": "2023-08-27T14:05:30.512868+00:00",
          "changed_variables": {
            "context": {
              "id": "01H8VN0SGE8TR2M1SFRWE8GAY5",
              "parent_id": "01H8VN0SGDTS4HP7YHE30FC2AH",
              "user_id": null
            }
          },
          "result": {
            "wait": {
              "remaining": 0,
              "trigger": null
            }
          }
        }
      ],
      "action/1": [
        {
          "path": "action/1",
          "timestamp": "2023-08-27T14:05:32.514800+00:00",
          "changed_variables": {
            "wait": {
              "remaining": 0,
              "trigger": null
            }
          },
          "result": {
            "choice": "then"
          }
        }
      ],
      "action/1/if": [
        {
          "path": "action/1/if",
          "timestamp": "2023-08-27T14:05:32.515059+00:00",
          "result": {
            "result": true
          }
        }
      ],
      "action/1/if/condition/0": [
        {
          "path": "action/1/if/condition/0",
          "timestamp": "2023-08-27T14:05:32.515094+00:00",
          "result": {
            "result": true,
            "entities": []
          }
        }
      ],
      "action/1/then/0": [
        {
          "path": "action/1/then/0",
          "timestamp": "2023-08-27T14:05:32.515827+00:00",
          "result": {
            "params": {
              "domain": "logbook",
              "service": "log",
              "service_data": {
                "message": "Single press detected",
                "entity_id": "button.1001588a20_2",
                "name": "Custom"
              },
              "target": {}
            },
            "running_script": false
          }
        }
      ]
    },
    "config": {
      "id": "1693128603375",
      "alias": "RF AUTOMATION NOT WORKING ",
      "description": "",
      "trigger": [
        {
          "platform": "device",
          "device_id": "48728c2ea4ff424ff576c549008cdc57",
          "domain": "button",
          "entity_id": "c9ca1e9a73903255d61eee8497d3ee7f",
          "type": "pressed"
        }
      ],
      "condition": [],
      "action": [
        {
          "wait_for_trigger": [
            {
              "platform": "device",
              "device_id": "48728c2ea4ff424ff576c549008cdc57",
              "domain": "button",
              "entity_id": "c9ca1e9a73903255d61eee8497d3ee7f",
              "type": "pressed",
              "id": "A"
            }
          ],
          "timeout": {
            "hours": 0,
            "minutes": 0,
            "seconds": 2,
            "milliseconds": 0
          }
        },
        {
          "if": [
            {
              "condition": "template",
              "value_template": "{{\n  not wait.trigger\n}}"
            }
          ],
          "then": [
            {
              "service": "logbook.log",
              "data": {
                "message": "Single press detected",
                "entity_id": "button.1001588a20_2",
                "name": "Custom"
              }
            }
          ],
          "else": [
            {
              "service": "logbook.log",
              "data": {
                "message": "Double press detected "
              }
            }
          ]
        }
      ],
      "mode": "single"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01H8VN0SGE8TR2M1SFRWE8GAY5",
      "parent_id": "01H8VN0SGDTS4HP7YHE30FC2AH",
      "user_id": null
    }
  },
  "logbookEntries": [
    {
      "name": "RF AUTOMATION NOT WORKING ",
      "message": "triggered",
      "source": null,
      "entity_id": "automation.rf_automation_not_working",
      "context_id": "01H8VN0SGE8TR2M1SFRWE8GAY5",
      "when": 1693145130.510497,
      "domain": "automation"
    },
    {
      "when": 1693145132.516183,
      "name": "Custom",
      "message": "Single press detected",
      "domain": "button",
      "entity_id": "button.1001588a20_2",
      "context_event_type": "automation_triggered",
      "context_domain": "automation",
      "context_name": "RF AUTOMATION NOT WORKING ",
      "context_message": "triggered",
      "context_entity_id": "automation.rf_automation_not_working"
    }
  ]
}

Well “flashing” was yesterday - today what’s left is to “install”. Easiest way is typical to browse to web.esphome.io and install directly to the esp attached via usb (for the rf bridge a usb-serial adapter is needed for that task) from the browser. Physical modifications are not necessary when used with the component (efm8bb1) linked.

depends what that is - maybe something simple like that it my last post? :thinking:

or something little more advanced like multi_click which allows custom sequences :point_down:

binary_sensor:
  - platform: gpio
    # ...
    on_multi_click:
    - timing:
        - ON for at most 1s
        - OFF for at most 1s
        - ON for 0.5s to 1s
        - OFF for at least 0.2s
      then:
        - logger.log: "Double-Clicked"

I have same issue
After programming a key (rf) then it look like pressing twice
Did the flash fixed the issue ?