Combining multiple actions and triggers into one automation

Hi all,

I’m having a hard time figuring out how to combine multiple triggers and actions into one automation.

I’m using Hue wall switches to activate my lights. They’re all added to Deconz. The Hue wall switches have 4 buttons, but each button generates multiple “event codes” based on short press, long press etc.

At this moment I’m having 4 automations for one Hue wall switch. One to turn on the lights, one to turn off the lights, one to dim the lights and one to brighten the lights. If I want to utilize the long press functions I need 8 automations per wall switch. That’s a lot, considering I’m having like 10 switches in my house.

I believe it’s possible to pass the event_data code from the deconz_event to an if-else action. I can use different if-else actions to decide which service to call. Can you help me figuring this out?

description: ""
mode: single
trigger:
  - platform: event
    event_type: deconz_event
    event_data:
      id: slaapkamer_switch
      event: << Make this a variable >>
condition: []
action:
  - if:
      - condition: template
        value_template: Variable == '1000'
    then:
      - service: light.turn_on
        data: {}
  - if:
      - condition: template
        value_template: Variable == '2000'
    then:
      - service: light.turn_off
        data: {}

What I want to do is make a variable within the trigger from the event.event_data.event object, and use that to decide which action to activate. This way I can reduce my automations to one automation per switch instead of multiple.

Hue switch button codes:
1000 - On short press
1003 - On long press
2000 - Dim + short press
2003 - Dim + long press
3000 - Dim - short press
3003 - Dim - long press
4000 - Off short press
4003 - Off long press

1 Like

You can probably do it something like this:

description: ""
mode: single
trigger:
  - platform: event
    event_type: deconz_event
    event_data:
      id: slaapkamer_switch
# removed event to make it trigger on all events
condition: []
action:
  - if:
      - condition: template
        value_template: "{{ trigger.event.data.event_data.event == '1000' }}"
    then:
      - service: light.turn_on
        data: {}
  - if:
      - condition: template
        value_template: "{{ trigger.event.data.event_data.event == '2000' }}"
    then:
      - service: light.turn_off
        data: {}

You can use this but will need to update the button codes deCONZ - Philips Hue Dimmer Switch - Custom Actions

I’d go for a choose in the automation even thou you can do it with if of course
The choose syntax is close to the if you did but is more readable in your case, where there is plenty values to check for:

actions: 
  - choose:
      - conditions: "{{ trigger.event.data.event_data.event == '1000' }}"
        sequence:
          - service: light.turn_on
            data: {}
      - conditions: "{{ trigger.event.data.event_data.event == '1003' }}"
        sequence:[]
      - conditions: "{{ trigger.event.data.event_data.event == '2000' }}"
        sequence:
          - service: light.turn_off
            data: {}
      - conditions: "{{ trigger.event.data.event_data.event == '2003' }}"
        sequence:[]
      - conditions: "{{ trigger.event.data.event_data.event == '3000' }}"
        sequence:[]
      - conditions: "{{ trigger.event.data.event_data.event == '3003' }}"
        sequence:[]
      - conditions: "{{ trigger.event.data.event_data.event == '4000' }}"
        sequence:[]
      - conditions: "{{ trigger.event.data.event_data.event == '4003' }}"
        sequence:[]

But do you know that there is a blueprint for your switch?

You guys are all great, thanks for the suggestions! I’m going to experiment with the choose instead of if/else to see if I can get it working. The blueprint looks promising too!

So, I tried to use this method to switch the lights, but it didn’t work. The trigger works as I’m seeing the green “triggered” message appear when I press a button. The condition template doesn’t seem to work though.

When I take a look at the YAML of the blueprint that @Olivier1974 mentioned, they are using the following template:

trigger.event.data.event

As you can see I tried both:

choose:
  - conditions:
      - condition: template
        value_template: "{{ trigger.event.data.event == '1000' }}"
    sequence:
      - service: light.turn_on
        data:
          brightness_pct: 50
        target:
          entity_id: light.slaapkamer_lampplafond
  - conditions:
      - condition: template
        value_template: "{{ trigger.event.data.event_data.event == '1003' }}"
    sequence:
      - service: light.turn_on
        data:
          brightness_pct: 50
        target:
          entity_id:
            - light.slaapkamer_lampplafond

Both didn’t work. Is there any way to troubleshoot to find the cause?

Use
trigger.event.data.event
not
trigger.event.data.event_data.event

I tried that in the first choose-condition, but that doesn’t work either.

Check the automation’s trace.

I quote:

conditions also accepts a shorthand notation of a template condition. For example:

automation:
  - trigger:
      - platform: state
        entity_id: input_select.home_mode
    action:
      - choose:
          - conditions: >
              {{ trigger.to_state.state == 'Home' and
                 is_state('binary_sensor.all_clear', 'on') }}
            sequence:
              - service: script.arrive_home
                data:
                  ok: true

That is the reason why I did not use the conditions: + condition: template form.
As Taras said, you should have a look at the trace of your automation, see what is wrong.

Try this to short the amount of triggers
I don’t know if it will work because I didn’t test it.
It’s just an idea
Variable “type” should contain: short or long and “command” should contain: on, dim+, dim-, off

description: ""
mode: single
trigger:
  - platform: event
    event_type: deconz_event
    event_data:
      id: slaapkamer_switch

condition:
  - condition: template
    value_template: "{{ trigger.event.data.event in [1000, 2000, 3000, 4000] }}"
  - condition: template
    value_template: "{{ trigger.event.data.event in [1003, 2003, 3003, 4003] }}"

action:
  - variables:
      subtype: "{{ trigger.event.data.event [1:] }}"
      command: "{{ {1:'on', 2:'dim +', 3:'dim', 4:'off'}[subtype] }}"
      type: "{{ 'short' if trigger.event.data.event [:1] == 0 else 'long' }}"

  [...]

This is what the trace gives:

image

No option taken at the choose-function. Could it be the fact that the ‘1003’ shouldn’t be between ‘’ because it’s no string?

Is there a more precise debug information somewhere?
Usually there is something that shows what the trigger was when it triggered

The screenshot confirms that the received data failed to match and of the conditions. It may indeed be received as an integer value instead of a string. Therefore change the template to:

"{{ trigger.event.data.event == 1000 }}"

If that fails to work then we need to see more information from the trace. The screenshot you posted only provides the most basic information about what happened. You can download the trace as a text file and then post it here where we can examine it for you.

I indeed needed to remove the ’ '. Without those, the choose-function works like a charm.

Thanks a lot!

1 Like

I used Group - Home Assistant very simple to setup