Dim/Brighten light on a single button long press?

I was able to piece together a dimming function with an Aqara Zigbee mini switch.

Every time the button is held, the light will either dim or brighten step-by-step until you release the button. Each release of the long press will cycle the dim or brighten mode. This will leave the single and double click operations available for other automations.

What you will need:

  • Input Select (Dropdown) Helper - <location> Long Press Mode with options turn up and turn down
  • Input Boolean Helper - <location> Dimming Script Enable

Setup a script like this:

alias: <location> Dimming Switch
sequence:
  - choose:
      - conditions:
          - condition: state
            entity_id: input_select.<location>_long_press_mode
            state: Turn Up
        sequence:
          - repeat:
              sequence:
                - service: light.turn_on
                  data:
                    brightness_step_pct: 3  ! Customize !
                  target:
                    entity_id: <light entity>
                - delay:
                    hours: 0
                    minutes: 0
                    seconds: 0
                    milliseconds: 250 ! Customize !
              while:
                - condition: state
                  entity_id: input_boolean.office_dimming_script_enable
                  state: "on"
      - conditions:
          - condition: state
            entity_id: input_select.<location>_long_press_mode
            state: Turn Down
        sequence:
          - repeat:
              sequence:
                - service: light.turn_on
                  data:
                    brightness_step_pct: -3 ! customize !
                  target:
                    entity_id: <light entity>
                - delay:
                    hours: 0
                    minutes: 0
                    seconds: 0
                    milliseconds: 250 ! customize !
              while:
                - condition: state
                  entity_id: input_boolean.<location>_dimming_script_enable
                  state: "on"
mode: single

And your automation for the single event button will have two triggers. One is the long press. The other is the release of the button. It is important that the event is a zigbee event (no reason why z2mqtt shouldn’t work too).

alias: <location> Long Press Automation
description: ""
trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_id: 789cd ... bcee ! change to your device !
condition: []
action:
  - choose:
      - conditions:
          - "{{ trigger.event.data.command == 'hold' }}"
        sequence:
          - service: input_boolean.turn_on
            data: {}
            target:
              entity_id: input_boolean.<location>_dimming_script_enable
          - service: script.<location>_dimming_switch
            data: {}
      - conditions:
          - "{{ trigger.event.data.command == 'release' }}"
        sequence:
          - service: input_boolean.turn_off
            data: {}
            target:
              entity_id: input_boolean.<location>_dimming_script_enable
          - service: input_select.select_next
            data:
              cycle: true
            target:
              entity_id: input_select.<office>_long_press_mode
mode: restart
3 Likes

Was able to adjust this for my use case. Thanks for posting in detail, you really helped me out!