ZHA - IKEA Tradfri Shortcut button debounced

Explanation

Only use this blueprint if the advanced one doesn’t work for you:
zha-ikea-tradfri-shortcut-button
This automation blueprint is for the Ikea shortcut button. This only works if u use ZHA integration to connect the shortcut button. You can add any action to the button for a short press or a long press.

Just copy the url of this post and add it to your blueprints.

Blueprint Code

blueprint:
  name: ZHA - IKEA Tradfri Shortcut button (debounced)
  description: Control anything with the ikea shortcut button (debounced)
  domain: automation
  input:
    shortcut_button:
      name: Device
      description: Select an IKEA shortcut button
      selector:
        device:
          integration: zha
          manufacturer: IKEA of Sweden
          model: TRADFRI SHORTCUT Button
    button_short:
      name: Single click
      description: "Choose action(s) when short pressed:"
      default: []
      selector:
        action: {}
    button_long:
      name: Long click
      description: "Choose action(s) when long pressed:"
      default: []
      selector:
        action: {}
mode: single
max_exceeded: silent
trigger:
- platform: event
  event_type: zha_event
  event_data:
    device_id: !input 'shortcut_button'
action:
- variables:
    command: '{{ trigger.event.data.command }}'
    cluster_id: '{{ trigger.event.data.cluster_id }}'
    endpoint_id: '{{ trigger.event.data.endpoint_id }}'
    args: '{{ trigger.event.data.args }}'
- choose:

  # single click
  - conditions:
    - '{{ endpoint_id == 1 }}'
    - '{{ cluster_id == 6 }}'
    - '{{ command == ''on'' }}'
    sequence:
    - choose:
      default: !input 'button_short'
    # delay for debounce, or long press while debounce
    - wait_for_trigger:
      - platform: event
        event_type: zha_event
        event_data:
          device_id: !input 'shortcut_button'
          endpoint_id: 1
          cluster_id: 8
          command: move_with_on_off
      timeout:
        milliseconds: 750
      continue_on_timeout: false
    - choose:
      default: !input 'button_long'

  # long click
  - conditions:
    - '{{ endpoint_id == 1 }}'
    - '{{ cluster_id == 8 }}'
    - '{{ command == ''move_with_on_off'' }}'
    sequence: !input 'button_long'

Changelog

  • 2021-01-08: First initial release.
3 Likes

Thank you for this!

One question: Why do all buttons that use this blueprint get triggered once every hour?

[Knapp 2] has been triggered by event 'zha_event'

The “trigger” doesn’t cause activation of the set actions.

1 Like

I’m just noticing this myself. After some debugging, it looks like the device sends a periodic “checkin” event that triggers the automation.

You think there’s anything one can do to stop it?

But isn’t it weird that it doesn’t actually trigger the automation actions?

It actually does trigger the actions for me. The lights turn themselves on every hour. I just realized the blueprint I’m using is a different one than in this post. I’m using the one here: ZHA - IKEA Tradfri On/Off Switch

Hm, ok. Interesting that both blueprints have this problem. Is it something that started appearing recently for you?

Wait maybe not. LOL. It looks like Ikea motion sensors send the same hourly checkin events as well, and those were triggering my lights, not the button automation.

The checkin events from the button do trigger my blueprint automation every hour, but I haven’t watched long enough to see if that actually turns my lights on.

I’m still new to all this, but from what I’ve read so far, these blueprints create automations that are triggered by events, and any event from the Ikea button will trigger the automation, including those ‘checkin’ events.

trigger:
- platform: event
  event_type: zha_event
  event_data:
    device_id: !input 'remote'

Once the automation is triggered, the event’s data is evaluated against conditions that determine what actions should happen. The ‘checkin’ command doesn’t match any conditions in the automation, so it just gets ignored and nothing should happen.

It would be great if we could filter out those specific events so the automation doesn’t even get triggered, but I haven’t had luck editing the blueprint.

1 Like

Thanks for this blueprint. I was previously tearing my hair out with my automation running 4-5 times per button press.

Thank you for this explanation! Makes sense. I’d also like to find a way to get rid of these events, it’s harder to track actual usage of the buttons with these periodic events.

I’m still getting multiple triggers of my automations in one second with this blueprint. If I want to increase the delay of the debounce do I just change this part?

      timeout:
        milliseconds: 750

So I’ve tried the original blueprint and this one and I’m not getting any response. I’ve even checked zha_events in developer tools and I get nothing. I’ve removed and paired the button and it is seen in HA but it doesn’t show any kind of trigger. Any thoughts.

1 Like

I had this issue too. I tried again today, removing the device from the zha list, then re-adding it. This time it works and I can trigger events. Unfortunately I am seeing the multiple events per press this automation aims to solve, but at least I can get short and long press.

Actually I made a small change to the blueprint to allow a third “hidden” action. The code looks for a long press after a short press already, but currently just triggers the long action. I modified it to have a unique action for short then long press.

Try replacing the trigger section with this:

trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_id: !input "shortcut_button"
      cluster_id: 6
  - platform: event
    event_type: zha_event
    event_data:
      device_id: !input "shortcut_button"
      cluster_id: 8

That way, the checkin commands (which have cluster_id: 32) won’t trigger automations built from this blueprint. A similar fix (adjusting cluster_id to account for other possible commands) could be applied to blueprints for the other IKEA buttons like this and this.

1 Like

It seems the blueprint no longer works as of the latest updates, not sure if the updates themselves had anything to do.

Executed: July 31, 2022 at 9:32:36 PM

Error: UndefinedError: ‘dict object’ has no attribute ‘event’

variables:
command: ‘{{ trigger.event.data.command }}’
cluster_id: ‘{{ trigger.event.data.cluster_id }}’
endpoint_id: ‘{{ trigger.event.data.endpoint_id }}’
args: ‘{{ trigger.event.data.args }}’

Whenever I press the button it does recognize it and tries to run it, but it seems that there’s no event in the trigger?

How did you get this shortcut button to work with ZHA? For me I can only see the battery level, but button presses do not trigger any zha_event.

1 Like