New to zigbee, need some help dimming lights

Hello all
I am fairly new to Zigbee (but very used to Home Assistant) and today, I tried to dim lights with an IKEA 2 button.
I thought I could easily just create an automation on brightness_move_down and then add a reapeat until brightness_stop is received and decrease the brightness in the loop.
But I struggle hard as I cannot find brightness_stop as a condition.
Would this anyway be the correct way? And also, I don’t have experimental_event_entities currently enabled. Should I enable this? Is this the way in the future that will be used?
Many thanks for some help.

What I currently could do is to decrease the brightness on a single long-press:

alias: Brightness Down
description: ""
triggers:
  - domain: mqtt
    device_id: c0cb760ccc5e7ee927fca6feec0d198b
    type: action
    subtype: brightness_move_down
    trigger: device
conditions: []
actions:
  - device_id: 6cf2173debf0caae9aa4ac1e106d3ef2
    domain: light
    entity_id: 9a6e1d8f0915145510bbbc9b3d9adf86
    type: brightness_decrease
mode: single

This is what I use, see if that can be modified to your use.

alias: Lys - dimming stue
description: ""
mode: restart
triggers:
  - domain: mqtt
    device_id: b3bc085725c505a4957b675cffcc6dcf
    type: action
    subtype: brightness_move_up_l2
    discovery_id: 0xccccccfffe6443d1 action_brightness_move_up_l2
    id: dim-up-l2
    trigger: device
  - domain: mqtt
    device_id: b3bc085725c505a4957b675cffcc6dcf
    type: action
    subtype: brightness_move_down_l2
    discovery_id: 0xccccccfffe6443d1 action_brightness_move_down_l2
    id: dim-down-l2
    trigger: device
  - domain: mqtt
    device_id: b3bc085725c505a4957b675cffcc6dcf
    type: action
    subtype: brightness_stop_l2
    discovery_id: 0xccccccfffe6443d1 action_brightness_stop_l2
    id: stop-l2
    trigger: device
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id: dim-up-l2
        sequence:
          - repeat:
              while:
                - condition: trigger
                  id: dim-up-l2
              sequence:
                - data:
                    brightness_step: 5
                  target:
                    entity_id: light.dimmer_tak_stue
                  action: light.turn_on
                - delay:
                    hours: 0
                    minutes: 0
                    seconds: 0
                    milliseconds: 200
      - conditions:
          - condition: trigger
            id: dim-down-l2
        sequence:
          - repeat:
              while:
                - condition: trigger
                  id: dim-down-l2
              sequence:
                - data:
                    brightness_step: -5
                  target:
                    entity_id: light.dimmer_tak_stue
                  action: light.turn_on
                - delay:
                    hours: 0
                    minutes: 0
                    seconds: 0
                    milliseconds: 200

Assuming all your lights are Zigbee, here is a working example with an IKEA 2 button E1743 controlling light the way you want it.

This is with Home Assistant experimental event entities enabled and using that.

alias: Bedroom Desk Switch
description: Note. Bedroom desk lamp is a single light and not group
triggers:
  - trigger: state
    entity_id: event.bedroom_desk_switch_action
    not_from: unavailable
actions:
  - choose:
      - conditions:
          - "{{trigger.to_state.attributes.event_type == 'on'}}"
        sequence:
          - entity_id:
              - light.bedroom_desk
            action: light.turn_on
      - conditions:
          - "{{trigger.to_state.attributes.event_type == 'off'}}"
        sequence:
          - data:
              entity_id:
                - light.bedroom_desk
            action: light.turn_off
      - conditions:
          - condition: template
            value_template: "{{trigger.to_state.attributes.event_type == 'brightness_move_up'}}"
        sequence:
          - action: mqtt.publish
            metadata: {}
            data:
              topic: zigbee2mqtt/Bedroom Desk/set
              payload: "{\"brightness_move\": 50 }"
      - conditions:
          - condition: template
            value_template: >-
              {{trigger.to_state.attributes.event_type ==
              'brightness_move_down'}}
        sequence:
          - action: mqtt.publish
            metadata: {}
            data:
              topic: zigbee2mqtt/Bedroom Desk/set
              payload: "{\"brightness_move\": -50 }"
      - conditions:
          - condition: template
            value_template: "{{trigger.to_state.attributes.event_type == 'brightness_stop'}}"
        sequence:
          - action: mqtt.publish
            metadata: {}
            data:
              topic: zigbee2mqtt/Bedroom Desk/set
              payload: "{\"brightness_move\": 0 }"
initial_state: "on"
mode: queued
max_exceeded: silent

This is awesome! Works out of the box and I don’t even need the experimental events activated. Pretty clever with the brightness_stop and restart mode, didn’t get how this worked at first but it works great. Maybe one small question: you gate the triggers an id, I guess this is needed. Are those ID globally unique or just unique per automation?

Here is my working code:

alias: 2-Button Brightness
description: ""
mode: restart
triggers:
  - domain: mqtt
    device_id: c0cb760ccc5e7ee927fca6feec0d198b
    type: action
    subtype: brightness_move_up
    id: dim-up-l2
    trigger: device
  - domain: mqtt
    device_id: c0cb760ccc5e7ee927fca6feec0d198b
    type: action
    subtype: brightness_move_down
    id: dim-down-l2
    trigger: device
  - domain: mqtt
    device_id: c0cb760ccc5e7ee927fca6feec0d198b
    type: action
    subtype: brightness_stop
    id: stop-l2
    trigger: device
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id: dim-up-l2
        sequence:
          - repeat:
              while:
                - condition: trigger
                  id: dim-up-l2
              sequence:
                - data:
                    brightness_step: 5
                  target:
                    entity_id: light.9a6e1d8f0915145510bbbc9b3d9adf86
                  action: light.turn_on
                - delay:
                    hours: 0
                    minutes: 0
                    seconds: 0
                    milliseconds: 200
      - conditions:
          - condition: trigger
            id: dim-down-l2
        sequence:
          - repeat:
              while:
                - condition: trigger
                  id: dim-down-l2
              sequence:
                - data:
                    brightness_step: -5
                  target:
                    entity_id: light.9a6e1d8f0915145510bbbc9b3d9adf86
                  action: light.turn_on
                - delay:
                    hours: 0
                    minutes: 0
                    seconds: 0
                    milliseconds: 200

Sending a message 5 times per second on a zigbee network can flood the network. It may work. But if you also control many lights this way, you may notice that not all the lights react and get out of sync. It is better for the network to send one message to start dimming up or down and one to stop. That is also how the Philips Hue hub does it

I believe they are unique per automation, but I haven’t looked into it really :slight_smile: Glad you got it working!

Scope for trigger id is within the automation.

2 Likes

I get your point. In my case it is not that critical as it is with manual buttons, so there won’t be 100 buttons pressed at the same time I hope :slight_smile:
I actually also tried a combination of both versions by just switch the light.turn_on action to the mqtt.publish action with works but the one issue I have with it is that when dimming down and it somewhen reaches 0, the light is still on very dim. If I use the light.turn_on it seems that HA has some internal logic that when the brightness is 0%, it will turn off the light. That seems to be missing in Zigbee2MQTT.

Here is this approach:

alias: 2 Button Brightness MQTT
description: ""
triggers:
  - domain: mqtt
    device_id: c0cb760ccc5e7ee927fca6feec0d198b
    type: action
    subtype: brightness_move_up
    id: dim-up-l2
    trigger: device
  - domain: mqtt
    device_id: c0cb760ccc5e7ee927fca6feec0d198b
    type: action
    subtype: brightness_move_down
    id: dim-down-l2
    trigger: device
  - domain: mqtt
    device_id: c0cb760ccc5e7ee927fca6feec0d198b
    type: action
    subtype: brightness_stop
    id: stop-l2
    trigger: device
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id: dim-up-l2
        sequence:
          - data:
              topic: zigbee2mqtt/0x588e81fffe405ee2/set
              payload: "{\"brightness_move\": 5 }"
            action: mqtt.publish
      - conditions:
          - condition: trigger
            id: dim-down-l2
        sequence:
          - data:
              topic: zigbee2mqtt/0x588e81fffe405ee2/set
              payload: "{\"brightness_move\": -5 }"
            action: mqtt.publish
      - conditions:
          - condition: trigger
            id: stop-l2
        sequence:
          - data:
              topic: zigbee2mqtt/0x588e81fffe405ee2/set
              payload: "{\"brightness_move\": 0 }"
            action: mqtt.publish
mode: restart

It is not missing in Zigbee

The way the dimming works in my example is that you give the light the message to dim up or down 50 steps (from 1 to 255) per second until it either reach upper or lower limit or you send the stop message (move 0)

If someone wants to lower the light to the lowest it will be really annoying if you have to accurately time your release to avoid turning it off. To turn off you short press. Or press an Off button if it is a 4 button.

The real risk of poor performance is if you have this scenario.

You have a living room with 8 lights. (like me).

You put the 8 lights in a Home Assistant group and not in a zigbee group

And then you use an automation that sends step up or down 5 times per second.

This will make Home Assistant send 8 messages every 200 ms to each of your lights which is 40 zigbee messages per second. Zigbee is not a fast network and it has a lot of retries and rerouting and all your sensors keep on chatting also. The result many have reported is that when they dim up and down the some lights misses steps so you end up having everything out of sync.

If you have your lights in a zigbee group - HA sends one message 5 times per second to 1 group. That is 5 messages per second. Better and probably works for most.

There is another benefit of Zigbee groups. If a light looses connection to the coordinator but is still alive, it will most often still react to the group commands so it turns on and off and dimms with the rest even if it is officially lost to Home Assistant. That means your family will only notice if they try to turn the individual bulb on and off. Always use Zigbee groups. Only group lights in HA if they are wifi. If you have a mix of zigbee and wifi lights and you want to control them as a group, then make a zigbee group first. And then in HA you group the Zigbee group with the Wifi lights. The wifi lights will not know the brightness_move commands so then you have to iterate in a loop stepping up and down.