Dim/Brightness on button hold (Ikea Tradfri)

Just got a new Ikea tradfri bulb which has brightness dimming, and I’m controlling it with a Xiaomi button which has a hold / release state exposed

I’d like to have it dim down to zero, then brighten back up while the button is held (i.e. adjust brightness while held and stop once released)

I can think of a few hacky ways to do this but they don’t feel quite right (i.e. jumping the brightness in steps) - sure someone has a better way?

Notes: Using Zigbee2MQTT. Don’t mind setting the automation in Home Assistant or NodeRed, either is fine

Will this work for you?

1 Like

Hmm… perhaps - let me take a closer look and revert - it has the switch I want to use, the default action isn’t what i want but looks like it might have the steps I need in the light hook… will experiment!

I use a technique that doesn’t change brightness in incremental steps. It uses the transition option so it requires the light to support transition natively (your IKEA lamp qualifies).

If you’re interested, I can share the automation. It’s designed for use with Zigbee2MQTT 2.X and an IKEA E1743 device (but can probably be modified to use the button-events of your Aqara device).

1 Like

Yeah, if you don’t mind sharing that would be great :slight_smile:

In the following example, the automation monitors event.foo_dimmer_action for button events and controls the operation of light.example.

In order to use this automation, your Zigbee2MQTT must have the following option enabled (Settings → Home Assistant integration).
image

Foo Dimmer
alias: Foo Dimmer
id: foo_dimmer_abc123
mode: single  
max_exceeded: silent

variables:
  interval: 1
  input_light: light.example
  fade_speed: 60
  fade_off: 3
  default_brightness: 128

triggers:
  - trigger: state
    entity_id: event.foo_dimmer_action
    not_from: 'unavailable'

conditions: []

actions:
  - variables:
      event: "{{ trigger.to_state.attributes.event_type }}"
  - choose:
      - conditions: "{{ event == 'on' }}"
        sequence:
          - wait_for_trigger:
              - platform: state
                entity_id: event.foo_dimmer_action
                not_from: 'unavailable'
            timeout: '{{ interval }}'
            
          - action: light.turn_on
            target:
              entity_id: '{{ input_light }}'
            data:
              brightness: "{{ 255 if wait.completed and wait.trigger.to_state.attributes.event_type == 'on' else default_brightness }}"


      - conditions: "{{ event == 'off' }}"
        sequence:
          - wait_for_trigger:
              - platform: state
                entity_id: event.foo_dimmer_action
                not_from: 'unavailable'
            timeout: '{{ interval }}'
          - action: light.turn_off
            target:
              entity_id: '{{ input_light }}'
            data:
              transition: "{{ 0 if wait.completed and wait.trigger.to_state.attributes.event_type == 'off' else fade_off }}"


      - conditions: "{{ event in ['brightness_move_down', 'brightness_move_up'] }}"
        sequence:
          - variables:
              brightness_from: "{{ state_attr(input_light, 'brightness') | int(0)}}"
              brightness_target: "{{ iif(event == 'brightness_move_down', 5, 255) }}"
              brightness_delta: "{{ (brightness_from - brightness_target) | abs }}"
              transition_time: "{{ brightness_delta / fade_speed }}"
              fade_speed: "{{ fade_speed * iif(event == 'brightness_move_down', -1, 1) }}"

          - action: light.turn_on
            target:
              entity_id: '{{ input_light }}'
            data:
              transition: '{{ transition_time }}'
              brightness: '{{ brightness_target }}'

          - wait_for_trigger:
              - trigger: state
                entity_id: event.foo_dimmer_action
                not_from: 'unavailable'
            timeout: '{{ transition_time }}'
            continue_on_timeout: false

          - if: "{{ wait.trigger.to_state.attributes.event_type == 'brightness_stop' }}"
            then:
              - action: light.turn_on
                target:
                  entity_id: '{{ input_light }}'
                data:
                  brightness: >
                    {{ (brightness_from + fade_speed * (wait.trigger.to_state.last_changed - trigger.to_state.last_changed).total_seconds()) | round(0) }}

    default: []
1 Like