@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:
# 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