I had an epiphany! I was making this code more complex that I needed to. By using a counter entity, and assuming I am unlikely to be taping multiple ZHA buttons within a half second of each other, I got rid of the complex input_text field.
Here is my improved code for my three remotes!
automation:
alias: ZHA Button Click
id: zha_button_click
initial_state: true
mode: restart # Force restart to pickup double click
max_exceeded: silent
trigger:
platform: event
event_type: zha_event
action:
# Increment the counter then delay 0.5 second to allow for a second click to cancel the second action.
- service: counter.increment
entity_id: counter.zha_button_click
- delay:
seconds: 0.5
# Store the click count and reset the counter.
- variables:
click_count: '{{ states("counter.zha_button_click") }}'
- service: counter.reset
entity_id: counter.zha_button_click
# Retrieve the remaining values needed to determine which action was requested.
- variables:
remotes:
# Ikea
"ec:1b:bd:ff:fe:23:9c:ee": "remote_family_room"
# Xiaomi
"00:15:8d:00:01:2d:d7:c3": "button_printer_power"
"00:15:8d:00:02:83:e2:b6": "remote_bedroom_double_rocker"
manufacturers:
"ec:1b:bd:ff": "ikea"
"00:15:8d:00": "xiaomi"
device_ieee: '{{ trigger.event.data.device_ieee }}'
manufacturer: '{{ manufacturers[device_ieee[0:11]] }}'
remote: '{{ remotes[device_ieee] }}'
click_count: |
{% if manufacturer == "xiaomi" %}
{%- set count = trigger.event.data.args.value %}
{%- if count == False or count is not number %}
{{- 0 }}
{%- else %}
{{ count|int }}
{%- endif %}
{% else %}
{{ click_count }}
{%- endif %}
# Removed '(manufacturer == "xiaomi" and click_count == 0) to make printer button work.'
command: |
{% set command = trigger.event.data.command %}
{%- if (manufacturer == "hue" and command == "step")
or (manufacturer == "ikea" and command == "checkin")
or remote|trim == "" %}
{%- set command = "ignore" %}
{%- endif %}
{{- command }}
command_modifier: |
{% if manufacturer == "ikea" %}
{{ trigger.event.data.args[0] }}
{%- endif %}
# Only take action for commands other than "ignore".
- choose:
- conditions: '{{ command != "ignore" }}'
sequence:
# Take the requested action!
- service: script.zha_button_actions
data:
click_count: '{{ click_count }}'
command: '{{ command }}'
command_modifier: '{{ command_modifier }}'
manufacturer: '{{ manufacturer }}'
remote: '{{ remote }}'
script:
zha_button_actions:
sequence:
- choose:
# Printer Power Button
# * Xiaomi Mijia Wireless Switch (round)
- conditions: '{{ remote == "button_printer_power" }}'
sequence:
- service: switch.toggle
entity_id: switch.printers
# Family Room 5 button remote
# * IKEA Tradfri Remote Control
- conditions: '{{ remote == "remote_family_room" }}'
sequence:
- choose:
# Button: Power (middle)
- conditions:
- condition: template
value_template: '{{ command == "toggle" }}'
sequence:
- choose:
# Single Click
- conditions: '{{ click_count == 1 }}'
sequence:
# Toggle the fan.
- service: fan.toggle
entity_id: fan.family_room
# Double click
- conditions: '{{ click_count == 2 }}'
sequence:
- service: switch.toggle
entity_id: switch.tv_family_room
# Triple click
- conditions: '{{ click_count == 3 }}'
sequence:
- choose:
# The door is locked
- conditions: '{{ states("lock.front_door")|lower|trim == "locked" }}'
sequence:
- service: lock.unlock
entity_id: lock.front_door
- service: input_boolean.turn_on
entity_id: input_boolean.leave_unlocked
# The door is unlocked
default:
- service: input_boolean.turn_off
entity_id: input_boolean.leave_unlocked
- service: lock.lock
entity_id: lock.front_door
# Quadruple Click
- conditions: '{{ click_count == 4 }}'
sequence:
- service: input_boolean.toggle
entity_id: input_boolean.guest_mode
# Brightness Up (top)
- conditions: '{{ command == "step_with_on_off" }}'
sequence:
- choose:
# Single Click
- conditions: '{{ click_count == 1 }}'
sequence:
- service: light.turn_on
data:
entity_id: light.family_room
brightness_pct: 100
# Double click
- conditions: '{{ click_count == 2 }}'
sequence:
- service: light.turn_on
data:
entity_id: light.table_lamp
brightness_pct: 100
# Brightness down (bottom)
- conditions: '{{ command == "step" }}'
sequence:
- choose:
# Single Click
- conditions: '{{ click_count == 1 }}'
sequence:
- service: light.turn_off
entity_id:
- light.family_room
- light.table_lamp
# Double click
- conditions: '{{ click_count == 2}}'
sequence:
- service: light.turn_off
entity_id: light.table_lamp
###############################
# SINGLE CLICK / HOLD BUTTONS #
###############################
# Single Click: Arrow left (right)
- conditions: '{{ command == "press" and command_modifier == 256 }}'
sequence:
- service: light.turn_on
data:
entity_id: light.family_room_floor_lamp
brightness_pct: 100
# Single Click: Arrow right (left)
- conditions: '{{ command == "press" and command_modifier == 257 }}'
sequence:
- service: light.turn_off
entity_id: light.family_room_floor_lamp
# Hold: Brightness Up (top)
- conditions: '{{ command == "move" }}'
sequence:
- service: light.turn_on
data:
entity_id:
- light.family_room
- light.table_lamp
brightness_step_pct: "-20"
# Hold: Brightness down (bottom)
- conditions: '{{ command == "move_with_on_off" }}'
sequence:
- service: light.turn_on
data:
entity_id:
- light.family_room
- light.table_lamp
brightness_step_pct: "20"
# Hold: Arrow left (left)
- conditions: '{{ command == "hold" and command_modifier == 3329 }}'
sequence:
- service: light.turn_on
data:
entity_id: light.family_room_floor_lamp
brightness_step_pct: "-20"
# Hold: Arrow right (right)
- conditions: '{{ command == "hold" and command_modifier == 3328 }}'
sequence:
- service: light.turn_on
data:
entity_id: light.family_room_floor_lamp
brightness_step_pct: "20"
# Bedroom Double Switch
# * Xiaomi Double Rockers, Battery Powered
- conditions: '{{ remote == "remote_bedroom_double_rocker" }}'
sequence:
- choose:
# Single Click: Left
- conditions: '{{ command == "left_single" }}'
sequence:
- service: light.toggle
entity_id: light.bedroom
# Single Click: Right
- conditions: '{{ command == "right_single" }}'
sequence:
- service: light.toggle
entity_id: light.brian
# Single Click: Both
- conditions: '{{ command == "both_single" }}'
sequence:
- service: light.toggle
entity_id: light.glowforge
# Double Click: Left
- conditions: '{{ command == "left_double" }}'
sequence:
- service: switch.toggle
entity_id: switch.sound_machine
# Double Click: Right
- conditions: '{{ command == "right_double" }}'
sequence:
- service: switch.toggle
entity_id: switch.bedroom_fan
# Double Click: Both
- conditions: '{{ command == "both_double" }}'
sequence:
- service: switch.toggle
entity_id:
- switch.sound_machine
- switch.bedroom_fan