blueprint:
  name: Zigbee2MQTT - IKEA Bilresa Dual Button
  description: Control actions using an IKEA Bilresa remote via Zigbee2MQTT based on MQTT topic payloads.
  domain: automation
  input:
    remote_name:
      name: Zigbee2MQTT Friendly Name
      description: The friendly name of the Bilresa remote in Zigbee2MQTT (used for the MQTT topic).
      selector:
        text: {}
    on_click:
      name: Top Button - Single Click
      default: []
      selector:
        action: {}
    on_double_click:
      name: Top Button - Double Click
      default: []
      selector:
        action: {}
    on_hold:
      name: Top Button - Long Press (Hold)
      default: []
      selector:
        action: {}
    off_click:
      name: Bottom Button - Single Click
      default: []
      selector:
        action: {}
    off_double_click:
      name: Bottom Button - Double Click
      default: []
      selector:
        action: {}
    off_hold:
      name: Bottom Button - Long Press (Hold)
      default: []
      selector:
        action: {}
    stop_hold:
      name: Button Released (Stop)
      description: Action to run when a long press is released.
      default: []
      selector:
        action: {}

trigger:
  - platform: mqtt
    topic: "{{ 'zigbee2mqtt/' ~ remote_id if '/' not in remote_id else remote_id }}"
    # We dynamically evaluate the topic in the trigger instead of matching in conditions
    # to avoid waking up the automation for unrelated MQTT traffic.
  
variables:
  remote_id: !input remote_name
  expected_topic: "{{ remote_id if '/' in remote_id else 'zigbee2mqtt/' ~ remote_id }}"

# Fallback trigger if the dynamic template topic has constraints in older versions
# Using wildcard MQTT trigger and filtering by topic in conditions for maximum compatibility:
trigger:
  - platform: mqtt
    topic: "zigbee2mqtt/#"

condition:
  - condition: template
    value_template: >-
      {{ trigger.topic == expected_topic and trigger.payload_json.action is defined }}

action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.payload_json.action == 'on' }}"
        sequence: !input on_click
      - conditions:
          - condition: template
            value_template: "{{ trigger.payload_json.action == 'on_double' }}"
        sequence: !input on_double_click
      - conditions:
          - condition: template
            value_template: "{{ trigger.payload_json.action == 'brightness_move_up' }}"
        sequence: !input on_hold
      - conditions:
          - condition: template
            value_template: "{{ trigger.payload_json.action == 'off' }}"
        sequence: !input off_click
      - conditions:
          - condition: template
            value_template: "{{ trigger.payload_json.action == 'off_double' }}"
        sequence: !input off_double_click
      - conditions:
          - condition: template
            value_template: "{{ trigger.payload_json.action == 'brightness_move_down' }}"
        sequence: !input off_hold
      - conditions:
          - condition: template
            value_template: "{{ trigger.payload_json.action == 'brightness_stop' }}"
        sequence: !input stop_hold
mode: restart