Reacting to a single long-press event until a long-press-release event comes in

Hi,

I am using DeCONZ and a few Busch-Jaeger ZigBee switches. For the “dim up” button the switch emits the following events (like many other ZigBee switches):

  • 2001: HOLD
  • 2002: SHORT_RELEASED
  • 2003: LONG_RELEASED

So I’d like to smoothly dim the lights up while the button is kept pressed. The tricky thing is that when pressing the button, only one single 2001 event is fired and when releasing the button one 2003 event is fired. What’s the best way to create an automation which dims up a light on 2001 until 2003 is reached?

My current solution consists of one script and two automations. The script dims up the light in 10% steps with a couple of delays in between the steps. The script is started by an automation which is triggered on 2001. Another automation which is triggered by 2003 stops the script. These are the automations:

- initial_state: True
  alias: Kitchen Ambient Dim Up Start
  trigger:
    - platform: event
      event_type: deconz_event
      event_data:
        id: sw_kitchen_1
        # Long_PRESS event
        event: 2001
  condition:
    - condition: state
      entity_id: light.kitchen_ambient
      state: 'on'
  action:
    - service: script.increase_brightness
      data:
        light: light.kitchen_ambient

- initial_state: True
  alias: Kitchen Ambient Dim Up Stop
  trigger:
     - platform: event
       event_type: deconz_event
       event_data:
         id: sw_kitchen_1
         # Long_PRESS release
         event: 2003
  action:
    - service: script.turn_off
      data:
        entity_id: script.increase_brightness

This is working but it is super bulky. The script itself is reusable for other lights, as it accepts the light as an argument. But I’d still like to do this with one single automation? I mean this is a pretty common task (run something until an event comes in), so I guess there must be a cleaner approach.

Right now I need 4 automations for changing the brightness of one light :slight_smile:

You could combine those two automations into one something like this:

- initial_state: True
  alias: Kitchen Ambient Dim Up
  trigger:
    - platform: event
      event_type: deconz_event
  condition:
    - condition: state
      entity_id: light.kitchen_ambient
      state: 'on'
  action:
    # 2001 = Long_PRESS, 2003 = Long_RELEASE
    - choose:
        # id: sw_kitchen_1
        - conditions:
            - condition: template
              value_template: '{{ trigger.event.data.id == "sw_kitchen_1" }}'
          sequence:
            - choose:
                # event: Long_PRESS hold
                - conditions:
                    - condition: template
                      value_template: '{{ trigger.event.data.event == "2001" }}'
                  sequence:
                    - service: script.increase_brightness
                      data:
                        light: light.kitchen_ambient

                # event: Long_PRESS release
                - conditions:
                    - condition: template
                      value_template: '{{ trigger.event.data.args.value == "2003" }}'
                  sequence:
                    - service: script.turn_off
                      data:
                        entity_id: script.increase_brightness

I have very similar automation for my Xiaomi buttons on ZHA. But you may have to tinker to get the trigger data right.

Hi. Thanks for the suggestion. This is what I was looking for. :+1: It’s actually quite simple but apparently I was not creative enough to come up with a solution like this :slight_smile:

1 Like

I found an even simper solution which is made possible by Home Assistant’s recent automation mode feature. This gives me dim-up and dim-down all within one automation:

- initial_state: true
  alias: Kitchen Ambient Dimmer
  mode: restart
  trigger:
  - platform: event
    event_type: deconz_event
    event_data:
      id: sw_kitchen_1
  condition:
    - condition: state
      entity_id: light.kitchen_ambient
      state: 'on'
  action:
    - choose:
        - conditions:
            - condition: template
              value_template: '{{ trigger.event.data.event == 4001 }}'
          sequence:
            - service: script.increase_brightness
              data:
                 light: light.kitchen_ambient
        - conditions:
            - condition: template
              value_template: '{{ trigger.event.data.event == 3001 }}'
          sequence:
            - service: script.decrease_brightness
              data:
                 light: light.kitchen_ambient

Note the mode=restart, which causes a running “Kitchen Ambient Dimmer” automation to stop on any other incoming event from that switch. So I don’t need to handle the PRESS_RELEASE events any more, it’s enough to catch them in the trigger (catchall for the switch). This is super awesome!

3 Likes

@direx I know I’m 10 months late to the party, but any chance you could share your increase_brightness and decrease_brightness scripts? I’m in the exact same boat as you and this would immediately solve my problem. Thanks!

2 Likes

same here, can anyone share the script?

I am not using the script approach any more as there is an even more elegant way to do this with deCONZ:

  • On button press you simply call light.turn_on with a long transition (such as 5 seconds)
  • On button release you stop the currently running transition by issuing a {"bri_inc": 0} request to the deCONZ API.

This way dimming is smooth as butter!

If you are using Busch-Jaeger ZigBee switches I made a ready-to-use blueprint:

(The relevant part for you is after the comment Handle DIM STOP events (release button after long-press event)).

1 Like

thanks I was able to do it myself, as I dont deCONZI