Philips HUE Smart Button - 5 press actions + hold-to-dim

Here is my Blueprint to replicate the original Philips HUE App Smart Button functionality.

The Philips HUE Smart Button in the native HUE app allows for a cycling 5-press + hold configuration, but this isn’t supported inside Home Assistant natively. This blueprint adds that functionality for your HASS configured Smart Buttons :slight_smile:

There’s lots of advanced options but the main ones are at the top.

Be sure to create a text helper with at least a 500 character limit.

For Zigbee2MQTT you’ll need to use the topic ending in /action

Important!
You must have “Advanced” mode enabled in your HASS user profile to configure text helper limits.

Credit to @epmatt for indirect blueprint lessons.

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

Source on github

8 Likes

Hi @TheHolyRoger, I have imported this blueprint but when try to make automation I don’t see device.or entities. Is there somethin im doing wrong?

Can you show me the device/entity you’re trying to control?

The helper controller will be a new helper text input you create in config > devices > helpers

This is the switch:

and this is the lamp:

I think you want this one instead

My blue print is for the hue smart button which is one SINGLE button, but using my blueprint you can cycle through actions with up to 5 pushes of the hue smart button (like hue app)

I’m having 3 hue switch where with HA, I can take control of all buttons with 4 differents way available.

For my perspective, it’s very difficult to people using the button to remember the configuration. So I keep simple short press and nothing more for the moment

I have a mixture (got 10-20 buttons i think), some are simple, some have complex conditions e.g. checking which lights are on etc, but I wanted to replicate the 5 scene-cycle functionality of the HUE app which I couldn’t do without this blueprint

I am trying to tweak your blueprint to allow the hue button after it was moved from the Hue Hub to Zigbee2MQTT but I am having issues. When I setup the automation, it says no supported devices. Despite that I changed the blueprint file to look for the model and integration based off MQTT.

Edit: I was able to find the device by changing it to use manufacturer instead of integration. Though the trigger is not working so I will play around with that more. I had tried changing the trigger to use the state_changed event on but it did not seem to work.

# Blueprint metadata
blueprint:
  name: Controller - Philips Hue Smart Button - Zigbee2MQTT Edition
  description: |
    # Controller - Philips Hue Smart button with multiple presses
  domain: automation
  input:
    controller_device:
      name: HUE Smart Button Device
      description: The button device to use for the automation.
      default: ''
      selector:
        device:
          manufacturer: Philips
          model: Hue smart button (8718699693985)
    helper_last_controller_event:
      name: (Required) Helper - Last Controller Event
      description: Input Text used to store the last event fired by the controller. You will need to manually create a text input entity for this, please read the blueprint Additional Notes for more info.
      default: ''
      selector:
        entity:
          domain: input_text
    # inputs for custom actions
    action_button_short_release:
      name: (Optional) Short button press release.
      description: Action to run on short button press release.
      default: []
      selector:
        action:
    action_button_double_press:
      name: (Optional) Button double press
      description: Action to run on the release of a double button press.
      default: []
      selector:
        action:
    action_button_triple_press:
      name: (Optional) Button triple press
      description: Action to run on the release of a triple button press.
      default: []
      selector:
        action:
    action_button_quadruple_press:
      name: (Optional) Button quadruple press
      description: Action to run on the release of a quadruple button press.
      default: []
      selector:
        action:
    action_button_quintuple_press:
      name: (Optional) Button quintuple press
      description: Action to run on the release of a quintuple button press.
      default: []
      selector:
        action:
    action_button_long_repeat:
      name: (Optional) Button long hold
      description: Action to run on long button hold - this is fired every second the button is held down.
      default: []
      selector:
        action:
    action_button_long_release:
      name: (Optional) Long button press release
      description: Action to run on button release after long press.
      default: []
      selector:
        action:
    action_button_initial_press:
      name: (Optional) Button initial press
      description: Action to run on initial short button press. This is run on every single button press.
      default: []
      selector:
        action:
    # inputs for looping custom actions on long button press events until the corresponding release event is received
    button_long_repeat_loop:
      name: (Optional) Button long press - loop actions
      description: Loop the button action until the button is released (it will be called every second without this).
      default: false
      selector:
        boolean:
    button_long_repeat_max_loop_repeats:
      name: (Optional) Button long press - Maximum loop repeats
      description: >-
        Maximum number of repeats for the custom action, when looping is enabled.
        Use it as a safety limit to prevent an endless loop in case the corresponding stop event is not received.
      default: 500
      selector:
        number:
          min: 1
          max: 5000
          mode: slider
          step: 1
    # inputs for enabling double press events
    button_multi_press_enabled:
      name: (Optional) Expose button multi press events
      description: Choose whether or not to expose the virtual multi press events for the button. Turn this on if you are providing actions for the button double/triple/quadruple/quintuple press events.
      default: false
      selector:
        boolean:
    # helpers used to properly recognize the remote button events
    helper_multi_press_delay:
      name: (Optional) Helper - Multi Press delay
      description: Max delay between button presses for multi-press events. Provide a value only if you are using a multi-press action. Increase this value if you notice that the multi-press action is not triggered properly.
      default: 1200
      selector:
        number:
          min: 1000
          max: 5000
          unit_of_measurement: milliseconds
          mode: box
          step: 10
# Automation schema
variables:
  # convert input tags to variables, to be used in templates
  button_multi_press_enabled: !input button_multi_press_enabled
  button_long_repeat_loop: !input button_long_repeat_loop
  button_long_repeat_max_loop_repeats: !input button_long_repeat_max_loop_repeats
  helper_last_controller_event: !input helper_last_controller_event
  helper_multi_press_delay: !input helper_multi_press_delay
  helper_debounce_delay: 10
  # adjusted debounce delay so that the resulting multi press delay is exactly as specified by the user when running the action, taking also account of debouncing
  # make sure it never goes below the minimum multi press delay
  adjusted_multi_press_delay: '{{ [helper_multi_press_delay - helper_debounce_delay, 100] | max }}'
  # mapping between actions and integrations
  button_initial_press: [initial_press]
  button_short_release: [short_release]
  button_long_repeat: [repeat]
  button_long_release: [long_release]
  # build data to send within a controller event
  controller_device: !input controller_device
  controller_id: '{{controller_device}}'
mode: restart
max_exceeded: silent
trigger:
  - platform: event
    event_type:
      - state_changed
    event_data:
      device_id: !input controller_device
condition:
  - condition: and
    conditions:
      # check that the button event is not empty
      - >-
        {%- set trigger_action -%}
        {{ trigger.event.data.type }}
        {%- endset -%}
        {{ trigger_action not in ["","None"] }}
action:
  # debouncing - when automation is triggered multiple times, the last automation run is the one which completes execution, due to mode restart
  # therefore previous runs must wait for the debounce delay before executing any other action
  # if the delay expires and the automation is still running it means it's the last run and execution can continue
  - delay:
      milliseconds: '{{ helper_debounce_delay }}'
  # extract button event from the trigger
  # provide a single string value to check against
  - variables:
      trigger_action: >-
        {{ trigger.event.data.type }}
      trigger_delta: '{{ (as_timestamp(now()) - as_timestamp((states(helper_last_controller_event) | from_json).last_triggered if helper_last_controller_event is not none and (states(helper_last_controller_event) | regex_match("^\{(\".*\": \".*\"(, )?)*\}$")) else "1970-01-01 00:00:00")) * 1000 }}'
  # choose the sequence to run based on the received button event
  - choose:
      - conditions: '{{ trigger_action | string in button_initial_press }}'
        sequence:
          # fire the event
          - event: ahb_controller_event
            event_data:
              controller: '{{ controller_id }}'
              action: button_initial_press
          # run the custom action
          - choose:
              - conditions: []
                sequence: !input action_button_initial_press
      - conditions: '{{ trigger_action | string in button_short_release }}'
        sequence:
          - choose:
              # if multi press events are enabled
              - conditions: '{{ button_multi_press_enabled }}'
                sequence:
                  - choose:
                      # if previous event was a short press
                      - conditions: '{{ trigger_action | string in states(helper_last_controller_event) and trigger_delta | int <= helper_multi_press_delay | int }}'
                        sequence:
                          # store the double press event in the last controller event helper
                          - service: input_text.set_value
                            data:
                              entity_id: !input helper_last_controller_event
                              value: '{{ {"trigger_action":"double_press","last_triggered":now() | string} | to_json }}'
                          # run the double press action
                          # fire the event
                          - event: ahb_controller_event
                            event_data:
                              controller: '{{ controller_id }}'
                              action: button_double_press
                          # run the custom action
                          - choose:
                              - conditions: []
                                sequence: !input action_button_double_press
                      - conditions: '{{ "double_press" in states(helper_last_controller_event) and trigger_delta | int <= helper_multi_press_delay | int }}'
                        sequence:
                          # store the triple press event in the last controller event helper
                          - service: input_text.set_value
                            data:
                              entity_id: !input helper_last_controller_event
                              value: '{{ {"trigger_action":"triple_press","last_triggered":now() | string} | to_json }}'
                          # run the triple press action
                          # fire the event
                          - event: ahb_controller_event
                            event_data:
                              controller: '{{ controller_id }}'
                              action: button_triple_press
                          # run the custom action
                          - choose:
                              - conditions: []
                                sequence: !input action_button_triple_press
                      - conditions: '{{ "triple_press" in states(helper_last_controller_event) and trigger_delta | int <= helper_multi_press_delay | int }}'
                        sequence:
                          # store the quadruple press event in the last controller event helper
                          - service: input_text.set_value
                            data:
                              entity_id: !input helper_last_controller_event
                              value: '{{ {"trigger_action":"quadruple_press","last_triggered":now() | string} | to_json }}'

> Blockquote

# run the quadruple press action
                          # fire the event
                          - event: ahb_controller_event
                            event_data:
                              controller: '{{ controller_id }}'
                              action: button_quadruple_press
                          # run the custom action
                          - choose:
                              - conditions: []
                                sequence: !input action_button_quadruple_press
                      - conditions: '{{ "quadruple_press" in states(helper_last_controller_event) and trigger_delta | int <= helper_multi_press_delay | int }}'
                        sequence:
                          # store the quintuple press event in the last controller event helper
                          - service: input_text.set_value
                            data:
                              entity_id: !input helper_last_controller_event
                              value: '{{ {"trigger_action":"quintuple_press","last_triggered":now() | string} | to_json }}'
                          # run the quintuple press action
                          # fire the event
                          - event: ahb_controller_event
                            event_data:
                              controller: '{{ controller_id }}'
                              action: button_quintuple_press
                          # run the custom action
                          - choose:
                              - conditions: []
                                sequence: !input action_button_quintuple_press
                    # previous event was not a short press
                    default:
                      # update helper
                      - service: input_text.set_value
                        data:
                          entity_id: !input helper_last_controller_event
                          value: '{{ {"trigger_action":trigger_action,"last_triggered":now()|string} | to_json }}'
                      # wait for the multi press event to occur, within the provided delay
                      # if the second press is received, automation is restarted
                      - delay:
                          milliseconds: '{{ adjusted_multi_press_delay }}'
                      # if delay expires, no second press was received, therefore run the short press action
                      # run the short press action
                      # fire the event
                      - event: ahb_controller_event
                        event_data:
                          controller: '{{ controller_id }}'
                          action: button_short_release
                      # run the custom action
                      - choose:
                          - conditions: []
                            sequence: !input action_button_short_release
            # if multi press event is disabled run the action for the single short press
            default:
              # update helper
              - service: input_text.set_value
                data:
                  entity_id: !input helper_last_controller_event
                  value: '{{ {"trigger_action":trigger_action,"last_triggered":now()|string} | to_json }}'
              # fire the event
              - event: ahb_controller_event
                event_data:
                  controller: '{{ controller_id }}'
                  action: button_short_release
              # run the custom action
              - choose:
                  - conditions: []
                    sequence: !input action_button_short_release
      - conditions: '{{ trigger_action | string in button_long_repeat }}'
        sequence:
          # update helper
          - service: input_text.set_value
            data:
              entity_id: !input helper_last_controller_event
              value: '{{ {"trigger_action":trigger_action,"last_triggered":now()|string} | to_json }}'
          # fire the event only once before looping the action
          - event: ahb_controller_event
            event_data:
              controller: '{{ controller_id }}'
              action: button_long_repeat
          - choose:
              # if looping is enabled, loop the action for a finite number of iterations
              - conditions: '{{ button_long_repeat_loop }}'
                sequence:
                  - repeat:
                      while: '{{ repeat.index < button_long_repeat_max_loop_repeats | int }}'
                      sequence: !input action_button_long_repeat
            # if looping is not enabled run the custom action only once
            default: !input action_button_long_repeat
      - conditions: '{{ trigger_action | string in button_long_release }}'
        sequence:
          # update helper
          - service: input_text.set_value
            data:
              entity_id: !input helper_last_controller_event
              value: '{{ {"trigger_action":trigger_action,"last_triggered":now()|string} | to_json }}'
          # fire the event
          - event: ahb_controller_event
            event_data:
              controller: '{{ controller_id }}'
              action: button_long_release
          # run the custom action
          - choose:
              - conditions: []
                sequence: !input action_button_long_release

@FrayDabson Did you make any further progress?

I’m trying to do the same thing and adapt this for ZHA (via Sonoff Zigbee 3.0 dongle). I could only get the device to appear by changing it to a manufacturer instead of integration (after seeing your post). Via ZHA the manufacturer appears as ‘Signify Netherlands B.V.’, but otherwise seeing the same behavious as you.

The trigger doesn’t fire and the text helper isn’t populated. If I just create standard automation to toggle the light based on a short button press, that does work fine but I’d like to recover the ability to dim/brighten on long press that I had with the native Hue hub and app.

Any help appreciated.

Sorry guys unfortunately I only use the HUE integration so I’m not too sure how to help your triggers, I know zha etc do have different triggers/event names to the native hue integration and I’m not sure what they are.

It’s the event names for down/release etc that are different and will need changing

Thanks for taking the time to reply. I’m quite new to home assistant, is there an easy way to view the trigger/event names within the UI? i.e. press the button, check some sort of event log and obtain the right syntax?

If you go to developer tools → events you should see your integration as an active listener - copy and paste the listener tag into the “listen to events” box and then give your button a couple of presses to see what’s fired, paste it here if you like and I’ll tell you what to change

Thanks for the kind offer. Below is the event log after I pressed it twice about 1 second apart. ie. single press, wait, single press.

Event 3 fired 21:07:
{
    "event_type": "zha_event",
    "data": {
        "device_ieee": "00:17:88:01:08:02:bb:fd",
        "unique_id": "00:17:88:01:08:02:bb:fd:1:0xfc00",
        "device_id": "7ebb7de993f333624841d47c6e1cc0a2",
        "endpoint_id": 1,
        "cluster_id": 64512,
        "command": "on_press",
        "args": {
            "button": "on",
            "press_type": "press",
            "command_id": 0,
            "args": [
                1,
                3145728,
                0,
                33,
                0,
                0
            ]
        },
        "params": {}
    },
    "origin": "LOCAL",
    "time_fired": "2022-07-19T20:07:31.682294+00:00",
    "context": {
        "id": "01G8C159K2162FFF7PG29HP15B",
        "parent_id": null,
        "user_id": null
    }
}
Event 2 fired 21:07:
{
    "event_type": "zha_event",
    "data": {
        "device_ieee": "00:17:88:01:08:02:bb:fd",
        "unique_id": "00:17:88:01:08:02:bb:fd:1:0xfc00",
        "device_id": "7ebb7de993f333624841d47c6e1cc0a2",
        "endpoint_id": 1,
        "cluster_id": 64512,
        "command": "on_short_release",
        "args": {
            "button": "on",
            "press_type": "short_release",
            "command_id": 0,
            "args": [
                1,
                3145728,
                2,
                33,
                1,
                0
            ]
        },
        "params": {}
    },
    "origin": "LOCAL",
    "time_fired": "2022-07-19T20:07:31.503765+00:00",
    "context": {
        "id": "01G8C159DFPW63W9RQ685Q3WVH",
        "parent_id": null,
        "user_id": null
    }
}
Event 1 fired 21:07:
{
    "event_type": "zha_event",
    "data": {
        "device_ieee": "00:17:88:01:08:02:bb:fd",
        "unique_id": "00:17:88:01:08:02:bb:fd:1:0xfc00",
        "device_id": "7ebb7de993f333624841d47c6e1cc0a2",
        "endpoint_id": 1,
        "cluster_id": 64512,
        "command": "on_press",
        "args": {
            "button": "on",
            "press_type": "press",
            "command_id": 0,
            "args": [
                1,
                3145728,
                0,
                33,
                0,
                0
            ]
        },
        "params": {}
    },
    "origin": "LOCAL",
    "time_fired": "2022-07-19T20:07:28.841711+00:00",
    "context": {
        "id": "01G8C156T9WH67HV0P7Y0FX4BK",
        "parent_id": null,
        "user_id": null
    }
}
Event 0 fired 21:07:
{
    "event_type": "zha_event",
    "data": {
        "device_ieee": "00:17:88:01:08:02:bb:fd",
        "unique_id": "00:17:88:01:08:02:bb:fd:1:0xfc00",
        "device_id": "7ebb7de993f333624841d47c6e1cc0a2",
        "endpoint_id": 1,
        "cluster_id": 64512,
        "command": "on_short_release",
        "args": {
            "button": "on",
            "press_type": "short_release",
            "command_id": 0,
            "args": [
                1,
                3145728,
                2,
                33,
                1,
                0
            ]
        },
        "params": {}
    },
    "origin": "LOCAL",
    "time_fired": "2022-07-19T20:07:28.662361+00:00",
    "context": {
        "id": "01G8C156MP0CG7A1NQNTRD8QH4",
        "parent_id": null,
        "user_id": null
    }
}

This is completely untested I’m afraid but below might work:

# Blueprint metadata
blueprint:
  name: Controller - Philips Hue Smart Button
  description: |
    # Controller - Philips Hue Smart button with multiple presses
  domain: automation
  input:
    controller_device:
      name: HUE Smart Button Device
      description: The button device to use for the automation.
      default: ''
      selector:
        device:
          model: Hue smart button (8718699693985)
    helper_last_controller_event:
      name: (Required) Helper - Last Controller Event
      description: Input Text used to store the last event fired by the controller. You will need to manually create a text input entity for this, please read the blueprint Additional Notes for more info.
      default: ''
      selector:
        entity:
          domain: input_text
    # inputs for custom actions
    action_button_short_release:
      name: (Optional) Short button press release.
      description: Action to run on short button press release.
      default: []
      selector:
        action:
    action_button_double_press:
      name: (Optional) Button double press
      description: Action to run on the release of a double button press.
      default: []
      selector:
        action:
    action_button_triple_press:
      name: (Optional) Button triple press
      description: Action to run on the release of a triple button press.
      default: []
      selector:
        action:
    action_button_quadruple_press:
      name: (Optional) Button quadruple press
      description: Action to run on the release of a quadruple button press.
      default: []
      selector:
        action:
    action_button_quintuple_press:
      name: (Optional) Button quintuple press
      description: Action to run on the release of a quintuple button press.
      default: []
      selector:
        action:
    action_button_long_repeat:
      name: (Optional) Button long hold
      description: Action to run on long button hold - this is fired every second the button is held down.
      default: []
      selector:
        action:
    action_button_long_release:
      name: (Optional) Long button press release
      description: Action to run on button release after long press.
      default: []
      selector:
        action:
    action_button_initial_press:
      name: (Optional) Button initial press
      description: Action to run on initial short button press. This is run on every single button press.
      default: []
      selector:
        action:
    # inputs for looping custom actions on long button press events until the corresponding release event is received
    button_long_repeat_loop:
      name: (Optional) Button long press - loop actions
      description: Loop the button action until the button is released (it will be called every second without this).
      default: false
      selector:
        boolean:
    button_long_repeat_max_loop_repeats:
      name: (Optional) Button long press - Maximum loop repeats
      description: >-
        Maximum number of repeats for the custom action, when looping is enabled.
        Use it as a safety limit to prevent an endless loop in case the corresponding stop event is not received.
      default: 500
      selector:
        number:
          min: 1
          max: 5000
          mode: slider
          step: 1
    # inputs for enabling double press events
    button_multi_press_enabled:
      name: (Optional) Expose button multi press events
      description: Choose whether or not to expose the virtual multi press events for the button. Turn this on if you are providing actions for the button double/triple/quadruple/quintuple press events.
      default: false
      selector:
        boolean:
    # helpers used to properly recognize the remote button events
    helper_multi_press_delay:
      name: (Optional) Helper - Multi Press delay
      description: Max delay between button presses for multi-press events. Provide a value only if you are using a multi-press action. Increase this value if you notice that the multi-press action is not triggered properly.
      default: 1200
      selector:
        number:
          min: 1000
          max: 5000
          unit_of_measurement: milliseconds
          mode: box
          step: 10
# Automation schema
variables:
  # convert input tags to variables, to be used in templates
  button_multi_press_enabled: !input button_multi_press_enabled
  button_long_repeat_loop: !input button_long_repeat_loop
  button_long_repeat_max_loop_repeats: !input button_long_repeat_max_loop_repeats
  helper_last_controller_event: !input helper_last_controller_event
  helper_multi_press_delay: !input helper_multi_press_delay
  helper_debounce_delay: 10
  # adjusted debounce delay so that the resulting multi press delay is exactly as specified by the user when running the action, taking also account of debouncing
  # make sure it never goes below the minimum multi press delay
  adjusted_multi_press_delay: '{{ [helper_multi_press_delay - helper_debounce_delay, 100] | max }}'
  # mapping between actions and integrations
  button_initial_press: [on_press]
  button_short_release: [on_short_release]
  button_long_repeat: [on_repeat]
  button_long_release: [on_long_release]
  # build data to send within a controller event
  controller_device: !input controller_device
  controller_id: '{{controller_device}}'
mode: restart
max_exceeded: silent
trigger:
  - platform: event
    event_type:
      - zha_event
    event_data:
      device_id: !input controller_device
condition:
  - condition: and
    conditions:
      # check that the button event is not empty
      - >-
        {%- set trigger_action -%}
        {{ trigger.event.data.command }}
        {%- endset -%}
        {{ trigger_action not in ["","None"] }}
action:
  # debouncing - when automation is triggered multiple times, the last automation run is the one which completes execution, due to mode restart
  # therefore previous runs must wait for the debounce delay before executing any other action
  # if the delay expires and the automation is still running it means it's the last run and execution can continue
  - delay:
      milliseconds: '{{ helper_debounce_delay }}'
  # extract button event from the trigger
  # provide a single string value to check against
  - variables:
      trigger_action: >-
        {{ trigger.event.data.command }}
      trigger_delta: '{{ (as_timestamp(now()) - as_timestamp((states(helper_last_controller_event) | from_json).last_triggered if helper_last_controller_event is not none and (states(helper_last_controller_event) | regex_match("^\{(\".*\": \".*\"(, )?)*\}$")) else "1970-01-01 00:00:00")) * 1000 }}'
  # choose the sequence to run based on the received button event
  - choose:
      - conditions: '{{ trigger_action | string in button_initial_press }}'
        sequence:
          # fire the event
          - event: ahb_controller_event
            event_data:
              controller: '{{ controller_id }}'
              action: button_initial_press
          # run the custom action
          - choose:
              - conditions: []
                sequence: !input action_button_initial_press
      - conditions: '{{ trigger_action | string in button_short_release }}'
        sequence:
          - choose:
              # if multi press events are enabled
              - conditions: '{{ button_multi_press_enabled }}'
                sequence:
                  - choose:
                      # if previous event was a short press
                      - conditions: '{{ trigger_action | string in states(helper_last_controller_event) and trigger_delta | int <= helper_multi_press_delay | int }}'
                        sequence:
                          # store the double press event in the last controller event helper
                          - service: input_text.set_value
                            data:
                              entity_id: !input helper_last_controller_event
                              value: '{{ {"trigger_action":"double_press","last_triggered":now() | string} | to_json }}'
                          # run the double press action
                          # fire the event
                          - event: ahb_controller_event
                            event_data:
                              controller: '{{ controller_id }}'
                              action: button_double_press
                          # run the custom action
                          - choose:
                              - conditions: []
                                sequence: !input action_button_double_press
                      - conditions: '{{ "double_press" in states(helper_last_controller_event) and trigger_delta | int <= helper_multi_press_delay | int }}'
                        sequence:
                          # store the triple press event in the last controller event helper
                          - service: input_text.set_value
                            data:
                              entity_id: !input helper_last_controller_event
                              value: '{{ {"trigger_action":"triple_press","last_triggered":now() | string} | to_json }}'
                          # run the triple press action
                          # fire the event
                          - event: ahb_controller_event
                            event_data:
                              controller: '{{ controller_id }}'
                              action: button_triple_press
                          # run the custom action
                          - choose:
                              - conditions: []
                                sequence: !input action_button_triple_press
                      - conditions: '{{ "triple_press" in states(helper_last_controller_event) and trigger_delta | int <= helper_multi_press_delay | int }}'
                        sequence:
                          # store the quadruple press event in the last controller event helper
                          - service: input_text.set_value
                            data:
                              entity_id: !input helper_last_controller_event
                              value: '{{ {"trigger_action":"quadruple_press","last_triggered":now() | string} | to_json }}'
                          # run the quadruple press action
                          # fire the event
                          - event: ahb_controller_event
                            event_data:
                              controller: '{{ controller_id }}'
                              action: button_quadruple_press
                          # run the custom action
                          - choose:
                              - conditions: []
                                sequence: !input action_button_quadruple_press
                      - conditions: '{{ "quadruple_press" in states(helper_last_controller_event) and trigger_delta | int <= helper_multi_press_delay | int }}'
                        sequence:
                          # store the quintuple press event in the last controller event helper
                          - service: input_text.set_value
                            data:
                              entity_id: !input helper_last_controller_event
                              value: '{{ {"trigger_action":"quintuple_press","last_triggered":now() | string} | to_json }}'
                          # run the quintuple press action
                          # fire the event
                          - event: ahb_controller_event
                            event_data:
                              controller: '{{ controller_id }}'
                              action: button_quintuple_press
                          # run the custom action
                          - choose:
                              - conditions: []
                                sequence: !input action_button_quintuple_press
                    # previous event was not a short press
                    default:
                      # update helper
                      - service: input_text.set_value
                        data:
                          entity_id: !input helper_last_controller_event
                          value: '{{ {"trigger_action":trigger_action,"last_triggered":now()|string} | to_json }}'
                      # wait for the multi press event to occur, within the provided delay
                      # if the second press is received, automation is restarted
                      - delay:
                          milliseconds: '{{ adjusted_multi_press_delay }}'
                      # if delay expires, no second press was received, therefore run the short press action
                      # run the short press action
                      # fire the event
                      - event: ahb_controller_event
                        event_data:
                          controller: '{{ controller_id }}'
                          action: button_short_release
                      # run the custom action
                      - choose:
                          - conditions: []
                            sequence: !input action_button_short_release
            # if multi press event is disabled run the action for the single short press
            default:
              # update helper
              - service: input_text.set_value
                data:
                  entity_id: !input helper_last_controller_event
                  value: '{{ {"trigger_action":trigger_action,"last_triggered":now()|string} | to_json }}'
              # fire the event
              - event: ahb_controller_event
                event_data:
                  controller: '{{ controller_id }}'
                  action: button_short_release
              # run the custom action
              - choose:
                  - conditions: []
                    sequence: !input action_button_short_release
      - conditions: '{{ trigger_action | string in button_long_repeat }}'
        sequence:
          # update helper
          - service: input_text.set_value
            data:
              entity_id: !input helper_last_controller_event
              value: '{{ {"trigger_action":trigger_action,"last_triggered":now()|string} | to_json }}'
          # fire the event only once before looping the action
          - event: ahb_controller_event
            event_data:
              controller: '{{ controller_id }}'
              action: button_long_repeat
          - choose:
              # if looping is enabled, loop the action for a finite number of iterations
              - conditions: '{{ button_long_repeat_loop }}'
                sequence:
                  - repeat:
                      while: '{{ repeat.index < button_long_repeat_max_loop_repeats | int }}'
                      sequence: !input action_button_long_repeat
            # if looping is not enabled run the custom action only once
            default: !input action_button_long_repeat
      - conditions: '{{ trigger_action | string in button_long_release }}'
        sequence:
          # update helper
          - service: input_text.set_value
            data:
              entity_id: !input helper_last_controller_event
              value: '{{ {"trigger_action":trigger_action,"last_triggered":now()|string} | to_json }}'
          # fire the event
          - event: ahb_controller_event
            event_data:
              controller: '{{ controller_id }}'
              action: button_long_release
          # run the custom action
          - choose:
              - conditions: []
                sequence: !input action_button_long_release

What I changed:

  input:
    controller_device:
      name: HUE Smart Button Device
      description: The button device to use for the automation.
      default: ''
      selector:
        device:
          model: Hue smart button (8718699693985)

This is based on @FrayDabson above using zigbee2mqtt so the device model might be different in ZHA

condition:
  - condition: and
    conditions:
      # check that the button event is not empty
      - >-
        {%- set trigger_action -%}
        {{ trigger.event.data.command }}
        {%- endset -%}
        {{ trigger_action not in ["","None"] }}
# ....SNIP
action:
# ....SNIP
  - variables:
      trigger_action: >-
        {{ trigger.event.data.command }}

HUE integration uses data.type, for ZHA it looks like data.command so I changed that

variables:
  # ....SNIP
  button_initial_press: [on_press]
  button_short_release: [on_short_release]
  button_long_repeat: [on_repeat]
  button_long_release: [on_long_release]

I’m guessing the “long” commands here, also not sure the initial press is handled the same in ZHA as in HUE

1 Like

You’re a hero! Thank you so much! Now got it working in ZHA.

I also had to change:

  button_long_repeat: [on_repeat]

to

  button_long_repeat: [on_hold]

but otherwise I think it’s worked exactly as you intended (not done a huge amount of testing mind).

Here’s the full ZHA version of the automation with the fix for long press for anyone that needs it:

# Blueprint metadata
blueprint:
  name: Controller - Philips Hue Smart Button ZHA edition
  description: |
    # Controller - Philips Hue Smart button with multiple presses ZHA edition
  domain: automation
  input:
    controller_device:
      name: HUE Smart Button Device
      description: The button device to use for the automation.
      default: ""
      selector:
        device:
          model: Hue smart button (8718699693985)
    helper_last_controller_event:
      name: (Required) Helper - Last Controller Event
      description: Input Text used to store the last event fired by the controller. You will need to manually create a text input entity for this, please read the blueprint Additional Notes for more info.
      default: ""
      selector:
        entity:
          domain: input_text
    # inputs for custom actions
    action_button_short_release:
      name: (Optional) Short button press release.
      description: Action to run on short button press release.
      default: []
      selector:
        action:
    action_button_double_press:
      name: (Optional) Button double press
      description: Action to run on the release of a double button press.
      default: []
      selector:
        action:
    action_button_triple_press:
      name: (Optional) Button triple press
      description: Action to run on the release of a triple button press.
      default: []
      selector:
        action:
    action_button_quadruple_press:
      name: (Optional) Button quadruple press
      description: Action to run on the release of a quadruple button press.
      default: []
      selector:
        action:
    action_button_quintuple_press:
      name: (Optional) Button quintuple press
      description: Action to run on the release of a quintuple button press.
      default: []
      selector:
        action:
    action_button_long_repeat:
      name: (Optional) Button long hold
      description: Action to run on long button hold - this is fired every second the button is held down.
      default: []
      selector:
        action:
    action_button_long_release:
      name: (Optional) Long button press release
      description: Action to run on button release after long press.
      default: []
      selector:
        action:
    action_button_initial_press:
      name: (Optional) Button initial press
      description: Action to run on initial short button press. This is run on every single button press.
      default: []
      selector:
        action:
    # inputs for looping custom actions on long button press events until the corresponding release event is received
    button_long_repeat_loop:
      name: (Optional) Button long press - loop actions
      description: Loop the button action until the button is released (it will be called every second without this).
      default: false
      selector:
        boolean:
    button_long_repeat_max_loop_repeats:
      name: (Optional) Button long press - Maximum loop repeats
      description: >-
        Maximum number of repeats for the custom action, when looping is enabled.
        Use it as a safety limit to prevent an endless loop in case the corresponding stop event is not received.
      default: 500
      selector:
        number:
          min: 1
          max: 5000
          mode: slider
          step: 1
    # inputs for enabling double press events
    button_multi_press_enabled:
      name: (Optional) Expose button multi press events
      description: Choose whether or not to expose the virtual multi press events for the button. Turn this on if you are providing actions for the button double/triple/quadruple/quintuple press events.
      default: false
      selector:
        boolean:
    # helpers used to properly recognize the remote button events
    helper_multi_press_delay:
      name: (Optional) Helper - Multi Press delay
      description: Max delay between button presses for multi-press events. Provide a value only if you are using a multi-press action. Increase this value if you notice that the multi-press action is not triggered properly.
      default: 1200
      selector:
        number:
          min: 1000
          max: 5000
          unit_of_measurement: milliseconds
          mode: box
          step: 10
# Automation schema
variables:
  # convert input tags to variables, to be used in templates
  button_multi_press_enabled: !input button_multi_press_enabled
  button_long_repeat_loop: !input button_long_repeat_loop
  button_long_repeat_max_loop_repeats: !input button_long_repeat_max_loop_repeats
  helper_last_controller_event: !input helper_last_controller_event
  helper_multi_press_delay: !input helper_multi_press_delay
  helper_debounce_delay: 10
  # adjusted debounce delay so that the resulting multi press delay is exactly as specified by the user when running the action, taking also account of debouncing
  # make sure it never goes below the minimum multi press delay
  adjusted_multi_press_delay: "{{ [helper_multi_press_delay - helper_debounce_delay, 100] | max }}"
  # mapping between actions and integrations
  button_initial_press: [on_press]
  button_short_release: [on_short_release]
  button_long_repeat: [on_hold]
  button_long_release: [on_long_release]
  # build data to send within a controller event
  controller_device: !input controller_device
  controller_id: "{{controller_device}}"
mode: restart
max_exceeded: silent
trigger:
  - platform: event
    event_type:
      - zha_event
    event_data:
      device_id: !input controller_device
condition:
  - condition: and
    conditions:
      # check that the button event is not empty
      - >-
        {%- set trigger_action -%}
        {{ trigger.event.data.command }}
        {%- endset -%}
        {{ trigger_action not in ["","None"] }}
action:
  # debouncing - when automation is triggered multiple times, the last automation run is the one which completes execution, due to mode restart
  # therefore previous runs must wait for the debounce delay before executing any other action
  # if the delay expires and the automation is still running it means it's the last run and execution can continue
  - delay:
      milliseconds: "{{ helper_debounce_delay }}"
  # extract button event from the trigger
  # provide a single string value to check against
  - variables:
      trigger_action: >-
        {{ trigger.event.data.command }}
      trigger_delta: '{{ (as_timestamp(now()) - as_timestamp((states(helper_last_controller_event) | from_json).last_triggered if helper_last_controller_event is not none and (states(helper_last_controller_event) | regex_match("^\{(\".*\": \".*\"(, )?)*\}$")) else "1970-01-01 00:00:00")) * 1000 }}'
  # choose the sequence to run based on the received button event
  - choose:
      - conditions: "{{ trigger_action | string in button_initial_press }}"
        sequence:
          # fire the event
          - event: ahb_controller_event
            event_data:
              controller: "{{ controller_id }}"
              action: button_initial_press
          # run the custom action
          - choose:
              - conditions: []
                sequence: !input action_button_initial_press
      - conditions: "{{ trigger_action | string in button_short_release }}"
        sequence:
          - choose:
              # if multi press events are enabled
              - conditions: "{{ button_multi_press_enabled }}"
                sequence:
                  - choose:
                      # if previous event was a short press
                      - conditions: "{{ trigger_action | string in states(helper_last_controller_event) and trigger_delta | int <= helper_multi_press_delay | int }}"
                        sequence:
                          # store the double press event in the last controller event helper
                          - service: input_text.set_value
                            data:
                              entity_id: !input helper_last_controller_event
                              value: '{{ {"trigger_action":"double_press","last_triggered":now() | string} | to_json }}'
                          # run the double press action
                          # fire the event
                          - event: ahb_controller_event
                            event_data:
                              controller: "{{ controller_id }}"
                              action: button_double_press
                          # run the custom action
                          - choose:
                              - conditions: []
                                sequence: !input action_button_double_press
                      - conditions: '{{ "double_press" in states(helper_last_controller_event) and trigger_delta | int <= helper_multi_press_delay | int }}'
                        sequence:
                          # store the triple press event in the last controller event helper
                          - service: input_text.set_value
                            data:
                              entity_id: !input helper_last_controller_event
                              value: '{{ {"trigger_action":"triple_press","last_triggered":now() | string} | to_json }}'
                          # run the triple press action
                          # fire the event
                          - event: ahb_controller_event
                            event_data:
                              controller: "{{ controller_id }}"
                              action: button_triple_press
                          # run the custom action
                          - choose:
                              - conditions: []
                                sequence: !input action_button_triple_press
                      - conditions: '{{ "triple_press" in states(helper_last_controller_event) and trigger_delta | int <= helper_multi_press_delay | int }}'
                        sequence:
                          # store the quadruple press event in the last controller event helper
                          - service: input_text.set_value
                            data:
                              entity_id: !input helper_last_controller_event
                              value: '{{ {"trigger_action":"quadruple_press","last_triggered":now() | string} | to_json }}'
                          # run the quadruple press action
                          # fire the event
                          - event: ahb_controller_event
                            event_data:
                              controller: "{{ controller_id }}"
                              action: button_quadruple_press
                          # run the custom action
                          - choose:
                              - conditions: []
                                sequence: !input action_button_quadruple_press
                      - conditions: '{{ "quadruple_press" in states(helper_last_controller_event) and trigger_delta | int <= helper_multi_press_delay | int }}'
                        sequence:
                          # store the quintuple press event in the last controller event helper
                          - service: input_text.set_value
                            data:
                              entity_id: !input helper_last_controller_event
                              value: '{{ {"trigger_action":"quintuple_press","last_triggered":now() | string} | to_json }}'
                          # run the quintuple press action
                          # fire the event
                          - event: ahb_controller_event
                            event_data:
                              controller: "{{ controller_id }}"
                              action: button_quintuple_press
                          # run the custom action
                          - choose:
                              - conditions: []
                                sequence: !input action_button_quintuple_press
                    # previous event was not a short press
                    default:
                      # update helper
                      - service: input_text.set_value
                        data:
                          entity_id: !input helper_last_controller_event
                          value: '{{ {"trigger_action":trigger_action,"last_triggered":now()|string} | to_json }}'
                      # wait for the multi press event to occur, within the provided delay
                      # if the second press is received, automation is restarted
                      - delay:
                          milliseconds: "{{ adjusted_multi_press_delay }}"
                      # if delay expires, no second press was received, therefore run the short press action
                      # run the short press action
                      # fire the event
                      - event: ahb_controller_event
                        event_data:
                          controller: "{{ controller_id }}"
                          action: button_short_release
                      # run the custom action
                      - choose:
                          - conditions: []
                            sequence: !input action_button_short_release
            # if multi press event is disabled run the action for the single short press
            default:
              # update helper
              - service: input_text.set_value
                data:
                  entity_id: !input helper_last_controller_event
                  value: '{{ {"trigger_action":trigger_action,"last_triggered":now()|string} | to_json }}'
              # fire the event
              - event: ahb_controller_event
                event_data:
                  controller: "{{ controller_id }}"
                  action: button_short_release
              # run the custom action
              - choose:
                  - conditions: []
                    sequence: !input action_button_short_release
      - conditions: "{{ trigger_action | string in button_long_repeat }}"
        sequence:
          # update helper
          - service: input_text.set_value
            data:
              entity_id: !input helper_last_controller_event
              value: '{{ {"trigger_action":trigger_action,"last_triggered":now()|string} | to_json }}'
          # fire the event only once before looping the action
          - event: ahb_controller_event
            event_data:
              controller: "{{ controller_id }}"
              action: button_long_repeat
          - choose:
              # if looping is enabled, loop the action for a finite number of iterations
              - conditions: "{{ button_long_repeat_loop }}"
                sequence:
                  - repeat:
                      while: "{{ repeat.index < button_long_repeat_max_loop_repeats | int }}"
                      sequence: !input action_button_long_repeat
            # if looping is not enabled run the custom action only once
            default: !input action_button_long_repeat
      - conditions: "{{ trigger_action | string in button_long_release }}"
        sequence:
          # update helper
          - service: input_text.set_value
            data:
              entity_id: !input helper_last_controller_event
              value: '{{ {"trigger_action":trigger_action,"last_triggered":now()|string} | to_json }}'
          # fire the event
          - event: ahb_controller_event
            event_data:
              controller: "{{ controller_id }}"
              action: button_long_release
          # run the custom action
          - choose:
              - conditions: []
                sequence: !input action_button_long_release
1 Like

Nice well done :slight_smile: I might tinker with a new version of the blueprint at some point which works with both

I’m having the same issue that when using the blueprint I can’t find the device but when I use the revised one I can but I’m using zigbee2mqtt so the actions won’t work could someone please work out what needs changed to get a working blueprint for me please

Can you follow the same steps above to monitor the event for it then share the results here?

Sorry to but in, having the same problem with Zigbee2MQTT. This is one click of the switch. Thanks for the help.

Just let me know if you need anything else.

event_type: state_changed
data:
  entity_id: sensor.switch_2_front_bedroom_action
  old_state:
    entity_id: sensor.switch_2_front_bedroom_action
    state: "off"
    attributes:
      action: ""
      battery: 31.5
      last_seen: "2022-08-06T22:27:31+01:00"
      linkquality: 65
      update:
        state: null
      update_available: null
      icon: mdi:gesture-double-tap
      friendly_name: Switch 2 - Front Bedroom action
    last_changed: "2022-08-06T21:27:31.081626+00:00"
    last_updated: "2022-08-06T21:27:31.105160+00:00"
    context:
      id: 01G9TGWPH1QV0YE6509GQ6KHR4
      parent_id: null
      user_id: null
  new_state:
    entity_id: sensor.switch_2_front_bedroom_action
    state: ""
    attributes:
      action: ""
      battery: 31.5
      last_seen: "2022-08-06T22:27:31+01:00"
      linkquality: 65
      update:
        state: null
      update_available: null
      icon: mdi:gesture-double-tap
      friendly_name: Switch 2 - Front Bedroom action
    last_changed: "2022-08-06T21:27:31.107983+00:00"
    last_updated: "2022-08-06T21:27:31.107983+00:00"
    context:
      id: 01G9TGWPH3GXR3DX99WPEE3DEM
      parent_id: null
      user_id: null
origin: LOCAL
time_fired: "2022-08-06T21:27:31.107983+00:00"
context:
  id: 01G9TGWPH3GXR3DX99WPEE3DEM
  parent_id: null
  user_id: null