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

@Shivy011 @tobias1337 @JoeP97 @karlc28 @Robson31

do you want to try the below blueprint and let me know how it goes? if it works I’ll update the main blueprint

i think this import button works, make sure it has the new mqtt topic option when you use it:
Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

# 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:
          entity:
            domain: sensor
            device_class: battery
    controller_topic:
      name: HUE Smart Button Device (Zigbee2MQTT)
      description: The MQTT topic to use for the automation (only fill if using Zigbee2MQTT)
      default: ""
      selector:
        text:
    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) Enable button multi press events
      description: Choose whether or not to enable 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:
    # inputs for enabling double press events
    button_multi_press_always_fire_first_release:
      name: (Optional) Always fire first short button release
      description: Choose whether or not to always fire the first short button release action.
      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_multi_press_always_fire_first_release: !input button_multi_press_always_fire_first_release
  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, on_press, press]
  button_short_release: [short_release, on_short_release, release]
  button_long_repeat: [repeat, on_hold, hold]
  button_long_release: [long_release, on_long_release]
  # build data to send within a controller event
  controller_topic: !input controller_topic
  controller_device: !input controller_device
  controller_id: >-
    {% if controller_topic|length > 1 %}
    {{ controller_topic }}
    {% else %}
    {{controller_device}}
    {% endif %}
mode: restart
max_exceeded: silent
trigger:
  - platform: event
    event_type:
      - hue_event
      - zha_event
    event_data:
      device_id: !input controller_device
  - platform: mqtt
    topic: !input controller_topic
    payload: press
  - platform: mqtt
    topic: !input controller_topic
    payload: release
  - platform: mqtt
    topic: !input controller_topic
    payload: hold
condition:
  - condition: and
    conditions:
      # check that the button event is not empty
      - >-
        {%- set trigger_action -%}
        {% if "payload" in trigger %}
        {{ trigger.payload }}
        {% elif "type" in trigger.event.data %}
        {{ trigger.event.data.type }}
        {% else %}
        {{ trigger.event.data.command }}
        {% endif %}
        {%- 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: >-
        {% if "payload" in trigger %}
        {{ trigger.payload }}
        {% elif "type" in trigger.event.data %}
        {{ trigger.event.data.type }}
        {% else %}
        {{ trigger.event.data.command }}
        {% endif %}
      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 }}'
                      # Optionally skip delay to always fire the first release
                      - choose:
                          - conditions: "{{ button_multi_press_always_fire_first_release }}"
                            sequence: []
                        default:
                          # 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

using the new zigbee2mqtt version above make sure you use the /action topic

if you look in topias’s post here there are two topics, use the topic that ends in /action

Hi there,

I’m quite new to home assistant so apologies for the hastle, however I’m having trouble setting up your blueprint:

I have a hue smart button (the single button that comes with the starter kit) and want to not onyl trigger my livingroom lights (hue bulbs) but also toggle some smart plugs at the same time.

First of all, I dont understand the required “helper - last controller event” part. What do I do here?
Additionally, I cant seem to figure out how to dim the light via long-press events.

Finally, do I have to do anything else once I have filled out this form? Do I need to set up anything elsewhere in my home-assistant, or does this blueprint cover it all?.

Do you have any tips on this?

I created a text helper, but not sure if I need to configure it differently as I still cant get things to work. Any tips?

can you show the rest of your config and what you wanna do?

Works like a charm for me.
Thank you very much !!

1 Like

I got the simple press event worked using the button over zigbee2mqtt. But the multiple press event does not get triggert even with the related toggle enabled. Any idea which configuration may be wrong?

Can you share some more info? Unfortunately I don’t have Z2M so I did it by eye :slight_smile:

Can you show the MQTT events you see while pressing the button, and if there are any automation traces for the secondary presses or if it didn’t trigger at all

Also just to confirm you’re using my new mqtt version above and not the one at the top of the thread

Hey Roger,
sure…
I’m using your latest version.
Furthermore, I have to correct me… the double press is recognized, but a single press not anymore if multi-press is enabled.

This one are the events from a single press:

And this one from the double press:

Here you can see the trace from the single press (left one) and the double press (right one)

If you need something else, I will upload it asap.
Thank you for your help :slight_smile:

Do you see any other traces for a single press? Maybe some cancelled ones?

If you know how to edit the blueprint code try changing the helper_debounce_delay: 10 line to maybe 0 and see what happens?

If not I’ll upload a version where you can change it as a setting

For me, you don’t have to upload a new version :wink:
I tried the 0 delay setting but with no effect.

You are right. There were some more traces. For one single press, I got three traces in the given sequence:

I played with the setting (UI) and my current workaround is to enable the “Always fire first short button release” action.

I think I can see the problem, the mqtt action topic sends additional payloads of “off” and “on” which seems to be some sort of toggler…

Is that a setting in z2m that maybe you can disable?

If not I’ll poke around in the blueprint and see if I can filter the trigger while keeping it working for all integrations

I updated the blueprint with different triggers Philips HUE Smart Button - 5 presses - #61 by TheHolyRoger

Do you want to see if that works better without the workaround?

Hi there,

Unfortunately I’m getting a “unsupported url” message when trying to import your blueprint.

Are you sure the import button is working ? other blueprints are importing without a problem.

Awesome, now it works flawless!
Thank you so much for your script and your help :smiley:

I got the unsupported message too. So I just copied the code and add it manually as yaml script under the /blueprints/automation folder

Hm maybe it doesn’t like it not being the first post. I’ll update the main post tomorrow with the mqtt version since @EagleDev says it works :slight_smile:

I checked the url, you need to remove the ?username= at the end of it. The current button displays your username @TheHolyRoger .

Once removed, it imports without any problem ! :slight_smile:

Still, I’m sorry but I must be dumb. I don’t get how to get this to work.

I select a button, and I select short press (first one of the list) → toggle → and I select a z2m group I previously created.

If I head to Z2M, I can see the button works in the logs (I see the on off events)

But when I save the automation and press the button, the event is not triggered at all. It’s like of this button was not pressed. What am I doing wrong ?

here is the automation yaml:

alias: "Interrupteur : Chambre Raph et Cel"
description: ""
use_blueprint:
  path: TheHolyRoger/philips-hue-smart-button-5-presses.yaml
  input:
    helper_last_controller_event: input_text.dimmer_switches_events
    action_button_short_release:
      - service: light.toggle
        data: {}
        target:
          device_id: f66322ddc89251787f6b1fb19b18f1f9
    controller_device: cc24f0a084209f0e2b766438128636a9

I use the exact same button in an automation created from scratch, where I tell it to light the same Z2M group on an on and off event, with no problems. :confused:

like this :

alias: "Chambre Raph et Cel : Allumage"
description: ""
trigger:
  - platform: device
    domain: mqtt
    device_id: cc24f0a084209f0e2b766438128636a9
    type: action
    subtype: "on"
    discovery_id: 0x0017880108937253 action_on
condition: []
action:
  - service: light.turn_on
    data: {}
    target:
      area_id: chambre_raph_et_cel
mode: single