IKEA E1743 Tradfri on/off switch (ZHA) with smoother dimming

image

  • Use a IKEA Tradfri on/off remote to turn on/off or dim a light
  • Light dimming should be smoother compared to other blueprints
  • Works with ZHA

So I tried the one from Awesome HA Blueprints but thought the dimming experience wasn’t that smooth. Then I tried ControllerX which did make things way more smooth, however I really like having all my config in one place and rely less on external tools.

So as a fun proof-of-concept I created this blueprint that uses the transition feature instead of multiple calls to increase/decrease the brightness. In short:

  1. When you start holding the button down a transition towards full/zero brightness starts
  2. When you release the button it cancels the transition by sending a guesstimate of what the current brightness should be, based on how long you held the button down and how fast you want the dimming to be

To my surprise it works fairly well and so far my dimming experience has been smooth

Enough talking, you can get the blueprint from GitHub Gists or down below:

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

blueprint:
  name: IKEA E1743 Trådfri on/off switch (ZHA)
  description: Dim or turn on/off a light using the IKEA Trådfri E1743 switch remote. Works with ZHA.
  author: oscarb
  source_url: https://gist.github.com/oscarb/1e295b636d27559aa6c4d45c9ccd24ae
  domain: automation

  input:
    light:
      name: Light 
      description: Entiy ID of light to control 
      selector:
        entity: 
          filter: 
            domain: light
          multiple: false
    remote: 
      name: Trådfri on/off switch 
      description: The IKEA Trådfri on/off switch remote to use 
      selector: 
        device:
          filter: 
            integration: zha
            manufacturer: IKEA of Sweden
            model: TRADFRI on/off switch
          multiple: false
    fade_speed:
      name: Fade speed
      description: The speed used for dimming the light )brightness steps per second)
      default: 80
      selector:
        number:
          min: 1
          max: 255
          unit_of_measurement: brightness steps/second
    default_brightness:
      name: Default brightness
      description: The brightness to set when turning on the light
      default: 128
      selector:
        number:
          min: 1
          max: 255

mode: restart  
max_exceeded: silent

variables:
  input_light: !input light
  fade_speed: !input fade_speed

trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_id: !input remote
action:
  - choose:
    - conditions: "{{ trigger.event.data.command == 'on' }}"
      sequence:
        - service: light.turn_on
          entity_id: !input light
          data:
            brightness: !input default_brightness
    - conditions: "{{ trigger.event.data.command == 'off' }}"
      sequence:
        - service: light.turn_off
          entity_id: !input light
    - conditions: "{{ trigger.event.data.command in ['move', 'move_with_on_off'] }}"
      sequence:
        - alias: Set up variables
          variables:
            brightness_from: "{{ state_attr(input_light, 'brightness') | int(0) }}"
            brightness_target: "{{ 0 if trigger.event.data.command == 'move' else 255 }}"
            brightness_delta: "{{ (brightness_target - brightness_from) | abs }}"
            brightness_direction: "{{ -1 if trigger.event.data.command == 'move' else 1 }}"
            transition_time: "{{ brightness_delta / fade_speed }}"
        - service: light.turn_on
          entity_id: !input light
          data:
            transition: "{{ transition_time }}"
            brightness: "{{ brightness_target }}"
        - wait_for_trigger:
          - platform: event
            event_type: zha_event
            event_data:
              device_id: !input remote
              command: stop_with_on_off
          timeout: "{{ transition_time }}"
          continue_on_timeout: false
        - alias: Estimate current brightness
          variables:
            brightness_current: > 
              {{ brightness_from + 
                  brightness_direction * fade_speed * (wait.trigger.event.time_fired - trigger.event.time_fired).total_seconds() 
              }}
        - service: light.turn_on
          entity_id: !input light
          data:
            brightness: "{{ brightness_current }}"
1 Like

Are you using the newest fork?
lsismeiro/awesome-ha-blueprints: A curated collection of automation blueprints for Home Assistant. (github.com)

This fixed some dimming issues for me :slight_smile:

The parts responsible for handling a long button up/down press haven’t changed in 3 years. It’s a loop that sends commands continuously.

oscarb’s blueprint uses a different technique than sending a continuous stream of commands.

This looks interesting. Thanks for sharing.
Do you think it in practice it would be possible to adapt to use with multiple lights?
Just poking around your code - it looks like you’re using the brightness of the input light to calculate the final brightness call - would that be possible with more than one light as input?

The script would need to be updated to do different guesstimates for different lights given that they could start off at different brightness levels.

However, I haven’t tested this myself yet but I think you could simply use the blueprint multiple times for different lights but with the same remote as input, like this:

- id: '10000'
  alias: IKEA E1743 Trådfri on/off switch - Bedroom Light 1
  use_blueprint:
    path: oscarb/ikea_tradfri_e1743_light.yaml
    input:
      light: light.bedroom_light_1
      remote: 1a898747b9d814c5d3b5b904ac8a9a60
- id: '10001'
  alias: IKEA E1743 Trådfri on/off switch - Bedroom Light 2
  use_blueprint:
    path: oscarb/ikea_tradfri_e1743_light.yaml
    input:
      light: light.bedroom_light_2
      remote: 1a898747b9d814c5d3b5b904ac8a9a60

Sure, it’s gonna get messy if you have a lot of lights but maybe YAML anchors and such could help you reduce repetitive code, if this approach now works that is :slight_smile:

1 Like

Thanks for the response!
Currently use custom actions to have the single switch control multiple lights and it is a little convoluted but I’d love to incorporate your dimming method somehow!
I hope to try it out once I have some free time.

Experimenting some and have come across a strange behaviour - on first dimming event it works quite smoothly. But subsequent dimming events cause brightness to jump up or down, or it will dim smoothly and then rebound on the stop event.
If I monitor the state of the targeted light entity I can see it reports both brightness and on/off incorrectly after dimming.
Example:
switch on - light turns on to default brightness and reports state correctly.
long press dim up - light dims smoothly to ~n brightness, but now reports state as 254 regardless if observed brightness level,
long press dim down - light remains unchanged, but state reports as off.
long press dim up - light smoothly dims up from observed state but on button release rebounds down to the brightness it presumably would be estimating had it started at the reported ‘off’ state.

Just wondering can anyone else recreate this bug or is it perhaps unique to my setup? Ive tried with a few different lights.