How to trigger/condition based on zha_event listener?

My zigbee Aqara sensor sends X,Y and Z values only as events.
How do I run an automation based on value X exceeding 20 degrees?
The event data is this:

{
    "event_type": "zha_event",
    "data": {
        "device_ieee": "00:15:8d:00:07:26:f2:8a",
        "unique_id": "00:15:8d:00:07:26:f2:8a:1:0x0500",
        "device_id": "8899867fa9497c17e19116c2bcbbbe10",
        "endpoint_id": 1,
        "cluster_id": 1280,
        "command": "current_orientation",
        "args": [
            {
                "rawValueX": 19,
                "rawValueY": 26,
                "rawValueZ": 1299,
                "X": 1,
                "Y": 1,
                "Z": 89
            }
        ]
    },
    "origin": "LOCAL",
    "time_fired": "2022-02-27T19:24:39.848073+00:00",
    "context": {
        "id": "ee9acca333f1f2aaf5d6b06efb4dd7f1",
        "parent_id": null,
        "user_id": null
    }
}

Using an event trigger, you’ll get the data as trigger.event.data.args[0].X in templates

Thanks koying-
I tried this code, is it the same thing? doesn’t seem to work.

platform: event
event_type: motion angle
event_data:
  event_type: zha_event
  data:
    args:
      X: 90
context: {}
id: motion-zigbee

I’m taking this from this how-to, but it helped me figure out how to use my tilt sensor based on zha events. I set mine up a little different but it helped me figure it out even with being a newbie.

alias: Set Garage State
description: ''
trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_ieee: '28:6d:97:00:01:0a:a5:7a'
condition:
  - condition: template
    value_template: '{{ trigger.event.data.args.attribute_name == "z_axis" }}'
action:
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ trigger.event.data.args.value > 200 }}'
        sequence:
          - service: input_boolean.turn_on
            data: {}
            entity_id: input_boolean.garage_door_open
    default:
      - service: input_boolean.turn_off
        data: {}
        entity_id: input_boolean.garage_door_open
mode: queued
max: 20

So this condition should work? What does the 0 in “args[0].X” mean?
Thanks koying!

condition: template
value_template: '{{ trigger.event.data.args[0].X > 10 }}'

Something like that, at least. I cannot test, obviously.
args[0] is because the args are an array of a single element, at least in your example.

mjeffer,
I’m curious why you can’t combine the two conditions into the first condition.
In other words, why not:

condition:
  - condition: template
    value_template: '{{ trigger.event.data.args.z-axis < 200 }}'

Is it because you first have to assign the value to “z_axis” before you can perform an operation on it?

The first condition makes it so it only triggers on a z_axis event. The one I have sends an event for each axis. Looking at your event data, your sensor reports a bit differently so it doesn’t look like you’d need that to filter events. The second one is a condition to set the state based on the z_axis value.

I’ve duplicated your yaml but still can’t get it to trigger-- from my event data above, I assume I’m correctly assigning attribute_name to “X”?

alias: zigbee motion6
description: ''
trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_id: 8899867fa9497c17e19116c2bcbbbe10
condition:
  - condition: template
    value_template: '{{ trigger.event.data.args.attribute_name == "X" }}'
action:
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ trigger.event.data.args.value > 1 }}'
        sequence:
          - service: notify.gmail
            data:
              message: zigbee6
    default: []
mode: single

1 Like

you’ll need to use trigger.event.data.args.X.value. It’s based on the output of your message data.

Try it like this.

alias: zigbee motion6
description: ''
trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_id: '8899867fa9497c17e19116c2bcbbbe10'
action:
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ trigger.event.data.args.X.value > 1 }}'
        sequence:
          - service: notify.gmail
            data:
              message: zigbee6
    default: []
mode: single

Tried it, no joy…argh!
I see everything in zha_event correctly, just can’t act on it.

alias: zigbee motion6
description: ''
trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_id: 8899867fa9497c17e19116c2bcbbbe10
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ trigger.event.data.args.X.value > 1 }}'
        sequence:
          - service: notify.gmail
            data:
              message: zigbee6
    default: []
mode: single

Ok, just noticed, as mentioned above it’s an array so try using trigger.event.data.args[0].X, the sensor I have reports a bit differently so was mixing up with how mine did it.

It’s definitely

value_template: '{{ trigger.event.data.args[0].X > 1 }}

I tested it.

Obviously, if the actual X value is 1, as in your OP, nothing will happen.
Also, keep in ming you have automation traces to debug this stuff.

You both are just terrific, thank you.
So in trace here is the error:
Error: In ‘template’ condition: UndefinedError: ‘int object’ has no attribute ‘value’
for this config:

id: '1646152742284'
alias: zigbee motion2
description: ''
trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_id: 8899867fa9497c17e19116c2bcbbbe10
condition:
  - condition: template
    value_template: '{{ trigger.event.data.args[0].X.value > 3 }}'
action:
  - service: notify.gmail
    data:
      message: zigbee motion 2
mode: single

Changed variables:

trigger:
  id: '0'
  idx: '0'
  platform: event
  event:
    event_type: zha_event
    data:
      device_ieee: 00:15:8d:00:07:26:f2:8a
      unique_id: 00:15:8d:00:07:26:f2:8a:1:0x0500
      device_id: 8899867fa9497c17e19116c2bcbbbe10
      endpoint_id: 1
      cluster_id: 1280
      command: current_orientation
      args:
        - rawValueX: 65533
          rawValueY: 13
          rawValueZ: 1286
          X: 89
          'Y': 0
          Z: 1
    origin: LOCAL
    time_fired: '2022-03-01T18:37:01.018113+00:00'
    context:
      id: 159ad57d725a1330bf11dbf4630e1cbb
      parent_id: null
      user_id: null
  description: event 'zha_event'

D’oh! I’m an idiot; I deleted the “.value” and it WORKS!
I REALLY appreciate you two helping this newb (but I suspect what I’m doing isn’t routine stuff).
You are both officers and gentlemen.

for what it’s worth, this is what I found that worked for my Aqara vibration senor to read “current_oreintatation”. Tried a few variations based on this thread but this is what works for me.

alias: zigbee door open
description: ""
trigger:
  - platform: event
    event_type: zha_event
    event_data:
      device_id: 503ad514d8fe108e00b0c06db1d89f6a
    context: {}
condition:
  - condition: template
    value_template: "{{ trigger.event.data.args.Y < 1 }}"
action:
  - service: notify.push_over
    data:
      message: door is open
mode: single

“0” is when the sensor is face down (garage door open)
“1” is when the sensor is on it’s edge with the button on top (garage door closed).

thanks all

Although this thread is becoming old, the subject discussed is still highly relevant. So I thought I’d contribute with my own solution to this problem:
image

The key here is to define the event command as a variable for later use:

variables:
  command: "{{ trigger.event.data.command }}"
enabled: true

Now it’s possible to create conditional testing on the actual value returned in the command event. In my example I’m testing for the command to be either ‘on’ or ‘off’ string values:

 - condition: template
    value_template: "{{ command == 'on' }}"

Then I create a similar test for ‘off’ and I use an A/B test to halt execution if both tests are false.
This works well for me. If the command fired is either ‘on’ or ‘off’ then if the kitchen light is on, I turn it off, and vice versa.
Thus I managed to solve the problem caused by the fact that the Namron switch sends alternating ‘on’ and ‘off’ commands every time the button is pressed, but it has no knowledge of whether the lights are actually on or off, so the command wording itself has no real meaning. In addition, to confuse even more, the Namron switch also sends several other commands at random intervals. With the above method, all commands except ‘on’ and ‘off’ are ignored.

1 Like