Automation based on args

Hi, within the new ZHA component, on a press of a button, there is an event coming in, based on the args, we know the what button is pressed on the remote, like up,down,left, right

the below code is a copy/paste from other members that have the same issue/question, but i want to create another thread for this, since its actually not ZHA related
also those other members were able to resolve it finally by using NodeRed instead of automation…
but i dont want to go that road…

so below code is the event coming in

{
    "event_type": "zha_event",
    "data": {
        "unique_id": "0x1807:1:0x0005",
        "device_ieee": "00:0d:6f:ff:fe:62:50:cf",
        "command": "press",
        "args": [
            0,
            1,
            13,
            0
        ]
    },
    "origin": "LOCAL",
    "time_fired": "2019-08-19T00:32:25.590660+00:00",
    "context": {
        "id": "d344ee4083a34455aff7b04d3c66ba60",
        "parent_id": null,
        "user_id": null
    }
}

the automation is like example:

- id: '1562807168430'
  alias: Basement Remote - right arrow press
  trigger:
    platform: event
    event_type: zha_event
    event_data:
      device_ieee: '00:0d:6f:ff:fe:62:50:cf'
      command: 'press'
      args:
        - 0
        - 1
        - 13
        - 0
  condition: []
  action:
  - service: light.toggle
    data:
      entity_id: 
        - light.basement_couch_lights_level

or another example automation by another user

- alias: ikeaButton4
  trigger:
  - event_data:
      args:
      - 0
      - 1
      - 13
      - 0
      command: press
      device_ieee: 90:fd:9f:ff:fe:fb:54:50
    event_type: zha_event
    platform: event
  action:
  - data:
      entity_id: light.movie_box
    service: light.turn_off

that should work but the problem is, that it disregards the args …
so for example
left button = 0,1,13,0
right button = 1,1,13,0

so whatever button is pressed, the automation always runs, thats the issue

I tried to answer this in the other topic --> ZHA Zigbee Tested Devices...Please add your device results
Let me know if it works. I’ll try this when I’m home with mine and see if it actually works. Not sure if you need to use value_json or just value for this.

This should work – add in a condition template with the args that are required to distinguish your button presses. Perhaps there is a better way, but this worked for me.

  condition:
  - condition: template
    value_template: '{%if trigger.event.data.args==[0, 1, 13, 0]%}true{%else%}false{%endif%}'

so your automation would be:

- id: '1562807168430'
  alias: Basement Remote - right arrow press
  trigger:
    platform: event
    event_type: zha_event
    event_data:
      device_ieee: '00:0d:6f:ff:fe:62:50:cf'
      command: 'press'
  condition:
  - condition: template
    value_template: '{%if trigger.event.data.args==[0, 1, 13, 0]%}true{%else%}false{%endif%}'
  action:
  - service: light.toggle
    data:
      entity_id: 
        - light.basement_couch_lights_level

hi, thnx a lot, really appreciate it
gonna save this code for later

i am still on deconz, but have some websocket issues with it, trying to resolve first, if not, i am going to switch to native ZHA
thats why i wanted to know, if i could use everything i do now with deconz

I’m trying to do something similar and was hoping you could help. I’m using the Xiaomi vibration sensor to determine if the toilet seat lid is up or down. When the seat is up the zha_event is:

{
    "event_type": "zha_event",
    "data": {
        "unique_id": "00:15:8d:00:03:d4:b1:b5:1:0x0500",
        "device_ieee": "00:15:8d:00:03:d4:b1:b5",
        "endpoint_id": 1,
        "cluster_id": 1280,
        "command": "Tilt",
        "args": [
            {
                "degrees": 104
            }
        ]
    },
    "origin": "LOCAL",
    "time_fired": "2020-01-07T12:04:40.850613+00:00",
    "context": {
        "id": "3c99c0077bbb4c9db5ed90ca52a74ebb",
        "parent_id": null,
        "user_id": null
    }
}

When the degrees equals 104 I want the bathroom fan to turn on. Anytime it goes below that I’d like to have a 30 second delay then turn the fan off.

EDIT2 - it was bothering me, so I had no choice but to look at the template tonight… I think that trigger.event.data.args[0][“degrees”] should get the value to need to do the comparison for the trigger… edited in-line below.
EDIT - after hitting submit, I realized the template isn’t right… might need to use %if value.data.args== instead. Out of time tonight though, will look tomorrow.

@jwelvaert - I’m no expert but I’ll try to help!
I think that two separate automation are in order, one for seat up and one for seat down as it’s easiest go grab the trigger there. Probably a third would be useful as well that turns off the fan after a timeout in case someone forgets to put it back down afterwards… to combat this you can add a delay to the automation that turns it off after a timeout (15 minutes below).

- id: '000000000000'
  alias: Bathroom Fan - Toilet Seat UP
  trigger:
    platform: event
    event_type: zha_event
    event_data:
      device_ieee: '00:15:8d:00:03:d4:b1:b5'
      command: 'Tilt'
  condition:
  - condition: template
    # template to look for degrees 104 - might want to use >=90 instead of ==104
    value_template: '{%if trigger.event.data.args[0]["degrees"]==104%}true{%else%}false{%endif%}'
  action:
  - service: switch.turn_on
    data:
      entity_id: 
        - switch.bathroom_fan
  - delay: "00:15:00"
  - service: switch.turn_off
    data:
      entity_id:
        - switch.bathroom_fan

As commented above, you might need want to do degrees >= and <= rather than == or use something less, just in case there is some drift in the upright/down positions… like 100 or 90 or something like that.

- id: '000000000001'
  alias: Bathroom Fan - Toilet Seat DOWN
  trigger:
    platform: event
    event_type: zha_event
    event_data:
      device_ieee: '00:15:8d:00:03:d4:b1:b5'
      command: 'Tilt'
  condition:
  # template to look for degrees less than 90
  - condition: template
    value_template: '{%if trigger.event.data.args[0]["degrees"]<=90%}true{%else%}false{%endif%}'
  action:
  - delay: "00:00:30"
  - service: switch.turn_off
    data:
      entity_id: 
        - switch.bathroom_fan

I’m not certain if the value of degrees will be treated as a string or integer for the comparison. If a string, might need to grab the args earlier and convert with | int with a variation of the template. Hopefully this works or at least gets you on the right track :slight_smile:

Thank you. Works perfectly.

I want to use it as a trigger I ha but it give me a error missing integration

"event_type": "zha_event", 
"data": { "device_ieee": "60:a4:23:ff:fe:80:22:54", "unique_id": "60:a4:23:ff:fe:80:22:54:1:0x0006", "device_id": "d6f9d52a37b525cd785af3e92c199316", "endpoint_id": 1, 
"cluster_id": 6, 
"command": "remote_button_long_press", 
"args": [] }, 

How should it be in yamle code?