Dimmer for Aqara buttons (1st gen)

Dim lights by long pressing a button. The entity must be ‘on’ during hold.

This is my first blueprint so I am happy for feedback. It has currently only been tested with Aqara buttons (1st gen). Please report any other switches that work.

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

I noticed that setting a very short transition_step_length does not work. Maybe it is my system that cannot handle it…

blueprint:
  name: Single button dim switch
  description: Switch a light on/off by pressing a button.
    Dim it up/down (depending on it's current state) by holding the same button.
  domain: automation
  input:
    button:
      name: Push button
      description:
        Entity representing a physical push button (which restores its position
        when released).
      selector:
        entity:
          domain: binary_sensor
    light:
      name: Light
      selector:
        entity:
          domain: light
    brightness_threshold:
      name: Brightness threshold
      description: If the light brightness is above this value,
        the light is going to dim down when holding the button.
      default: "130"
      selector:
        number:
          min: "0"
          max: "255"
    brightness_step_pct:
      name: Brightness step
      description: Percentage brightness increase/decrease in each transition step.
      default: "7"
      selector:
        number:
          min: "1"
          max: "100"
          unit_of_measurement: "%"
    transition_step_length:
      name: Transition step length
      description: Length of each transition step.
      default: "0.3"
      selector:
        number:
          min: "0"
          max: "5"
          step: "0.01"
          unit_of_measurement: s

mode: restart

trigger:
  - platform: state
    entity_id: !input button
    to: "on"

action:
  - variables:
      brightness_step_pct_positive: !input brightness_step_pct
      brightness_step_pct_negative: "{{ brightness_step_pct_positive|int * -1 }}"
      light: !input light
      max_brightness: 255
      min_mireds: "{{ state_attr(light, 'min_mireds') }}"
      max_mireds: "{{ state_attr(light, 'max_mireds') }}"
      initial_mireds: "{{ state_attr(light, 'color_temp') | float(666) }}"
      initial_brightness: "{{ state_attr(light, 'brightness') | float(0.0) }}"
  - choose:
      - conditions:
          # when brightness level is high
          - condition: numeric_state
            entity_id: !input light
            attribute: brightness
            above: !input brightness_threshold
        sequence:
          # dim down
          - repeat:
              while:
                - condition: state
                  entity_id: !input button
                  state: "on"
              sequence:
                - service: light.turn_on
                  data:
                    transition: !input transition_step_length
                    brightness_step_pct: "{{ brightness_step_pct_negative }}"
                    color_temp: "{{ max_mireds - ((max_mireds - initial_mireds) * (state_attr(light, 'brightness') / initial_brightness)) | int }}"
                  entity_id: !input light
                - delay: !input transition_step_length
    default:
      # otherwise, dim up
      - repeat:
          while:
            - condition: state
              entity_id: !input button
              state: "on"
          sequence:
            - service: light.turn_on
              data:
                transition: !input transition_step_length
                brightness_step_pct: "{{ brightness_step_pct_positive }}"
                color_temp: "{{ initial_mireds - ((initial_mireds - min_mireds) * ((state_attr(light, 'brightness') | float(0.0) - initial_brightness) / (max_brightness - initial_brightness))) | int }}"
              entity_id: !input light
            - delay: !input transition_step_length

Credits: 💡 Single button on/off/dim switch