Zigbee2mqtt - Ikea on/off switch with dimming

Here is my blueprint for the Ikea switch with dimming function

blueprint:
  name: zigbee2mqtt - Ikea on/off button with dimmer
  description: 'Control on off switch from IKEA Tradfri.'

  domain: automation
  input:
    switch:
      name: switch
      description: Ikea on/off switch
      selector:
        entity:
          domain: sensor
    light:
      name: Light
      description: The light to control
      selector:
        target:
          entity:
            domain: light

mode: restart
max_exceeded: silent
trigger:
- platform: state
  entity_id: !input 'switch'
  attribute: action
action:
- variables:
    command: '{{ trigger.to_state.state }}'
- choose:
  - conditions:
    - '{{ command == ''off'' }}'
    sequence:
    - service: light.turn_off
      target: !input 'light'
      data: {}
  - conditions:
    - '{{ command == ''on'' }}'
    sequence:
    - service: light.turn_on
      data: {}
      target: !input 'light'
  - conditions:
    - '{{ command == ''brightness_move_down'' }}'
    sequence:
    - repeat:
        until:
        - condition: state
          entity_id: !input 'switch'
          state: brightness_stop
          attribute: action
        sequence:
        - service: light.turn_on
          data:
            brightness_step_pct: -10
            transition: 1
          target: !input 'light'
        - delay: '0.5'
  - conditions:
    - '{{ command == ''brightness_move_up'' }}'
    sequence:
    - repeat:
        until:
        - condition: state
          entity_id: !input 'switch'
          state: brightness_stop
          attribute: action
        sequence:
        - service: light.turn_on
          data:
            brightness_step_pct: 10
            transition: 1
          target: !input 'light'
        - delay: '0.5'
4 Likes

I have a question that I can’t answer because I don’t own an IKEA switch or use zigbee2MQTT. It concerns the action variable.

Your automation uses the following State Trigger:

trigger:
- platform: state
  entity_id: !input 'switch'
  attribute: action    <-------- Monitors sensor's attribute

This State Trigger is monitoring the selected sensor’s action attribute (and not the sensor’s state).

However, the automation creates a variable that refers to the sensor’s state and not its action attribute:

- variables:
    command: '{{ trigger.to_state.state }}'  <---- Refers to sensor's state, not its "action" attribute

I’m curious to know why the variable doesn’t refer to the action attribute. In other words, why doesn’t it do this?

- variables:
    command: '{{ trigger.to_state.attributes.action }}' 

Is it because the sensor’s state reports the same value as its action attribute? If that’s true then why does the State Trigger use “attribute: action”?

I have tried both ways you suggested and it seems to work (by removing the “action” attribute from the state trigger and also changing the command variable). I did not think too much about that, I have used the other Ikea (Tradfri) switch blueprint which does not have a dimmer as my starting point. So maybe someone who has some deeper knowledge about the internals can give you the explanation.

I’m trying to use this and it mostly works. On/off is fine. The main issue is that the repeat doesn’t work dimming up or down. I just get one step up or down. I have to release and then hold again and it does one step up or down as appropriate.

Any ideas?

Did someone find a solution for dimming?

I gave up on this blueprint. It seemed fundamentally flawed - I don’t remember the specifics but holding down wouldn’t dim up or down as per my last comment I think was the main thing. The repeat sequence was getting interrupted.

I ended up using this one with better success with some hacky modifications using ideas from this one just for the specific switch I’m using

So it looks like this:

blueprint:
  name: '[Z2M] Generic Ikea Remote Control Shonky'
  description: Blueprint for Ikea Remote Controls (On/Off and dim)
  domain: automation
  source_url: https://gist.github.com/pavax/4ab861e102a37343fc5c39ea371bfb1a
  input:
    remote:
      name: Remote
      description: IKEA remote to use (e.g sensor.kitchen_remote_action)
      selector:
        entity:
          integration: mqtt
          domain: sensor
    lights:
      name: Lights
      description: Light(s) to control when using default light actions
      default:
        entity_id: none
      selector:
        target:
          entity:
            domain: light
mode: restart
max_exceeded: silent
trigger:
- platform: state
  entity_id: !input 'remote'
  attribute: action
variables:
  command: '{{ trigger.to_state.state }}'
condition:
- condition: template
  value_template: '{{ command != ''''}}'
action:
- choose:
  - conditions:
    - '{{ command == ''on'' }}'
    sequence:
      - service: light.turn_on
        target: !input 'lights'
        data:
          transition: 1
  - conditions:
    - '{{ command == ''off'' }}'
    sequence:
      - service: light.turn_off
        target: !input 'lights'
        data:
          transition: 1
  - conditions:
    - '{{ command == ''brightness_move_up'' }}'
    sequence:
      repeat:
        while:
        - condition: template
          value_template: '{{ repeat.index < 10 }}'
        sequence:
        - service: light.turn_on
          target: !input 'lights'
          data:
            transition: 1
            brightness_step_pct: 10
        - delay: 0.5
  - conditions:
    - '{{ command == ''brightness_move_down'' }}'
    sequence:
      repeat:
        while:
        - condition: template
          value_template: '{{ repeat.index < 10 }}'
        sequence:
        - service: light.turn_on
          target: !input 'lights'
          data:
            transition: 1
            brightness_step_pct: -10
        - delay: 0.5

Main problem is that it doesn’t stop at minimum dim level and just turns off. Also on the one Ikea ZigBee light I have, it’s quite slow to update the light but I expect that’s more the light than the script. My Wifi lights work much better at dimming.

I found the HACS add-on ControllerX, with it i got the dimming working as expected ControllerX | Create controller-based automations with ease to control your home devices and scenes.

1 Like

Hi Guys,
thanks a lot for the information and great work above.

I made with my beginner knowledge a version that is running and is able to have smooth dimming and also steps. But probably there is room for improvement.

you can find it here: Z2M Ikea Tradfri Dimmer Switch on off and dimming