Aquara Vibration sensor using Zha

I’m trying to make an automation where I can set the level of vibration or tilt. This information is given trough the zha _event that comes into HA

{
    "event_type": "zha_event",
    "data": {
        "device_ieee": "xx:xx:xx:xx:xx:xx:xx:xx",
        "unique_id": "xx:xx:xx:xx:xx:xx:xx:xx:1:0x0500",
        "device_id": "xxxxxxxxxxxxxxxx",
        "endpoint_id": 1,
        "cluster_id": 1280,
        "command": "Tilt",
        "args": [
            {
                "degrees": 78
            }
        ]
    },
    "origin": "LOCAL",
    "time_fired": "2021-02-23T22:54:02.578341+00:00",
    "context": {
        "id": "xxxxxxxxxxxxxxxx",
        "parent_id": null,
        "user_id": null
    }
}

or

        "command": "vibration_strength",
        "args": [
            {
                "strength": 98
            }      

I can’t figure out how to use the Degrees or Strength values. What I would need is for the automation to run if the vibration strength is above 50 or the tilt is more than 10 degrees. My current automation looks like this:

alias: Alarm - Vibration triggers
description: ''
mode: single
trigger:
  - platform: event
    event_type: zha_event
condition:
  - condition: and
    conditions:
      - condition: or
        conditions:
          - condition: template
            value_template: '{{ trigger.event.data.device_ieee == "xx:xx:xx:xx:xx:xx:xx:x1" }}'
          - condition: template
            value_template: '{{ trigger.event.data.device_ieee == "xx:xx:xx:xx:xx:xx:xx:x2" }}'
      - condition: or
        conditions:
          - condition: template
            value_template: '{{ trigger.event.data.command == "Drop" }}'
          - condition: and
            conditions:
              - condition: template
                value_template: '{{ trigger.event.data.command == "Tilt" }}'
              - condition: template
                value_template: '{{ trigger.event.data.args.degrees => "10" }}'
          - condition: and
            conditions:
              - condition: template
                value_template: '{{ trigger.event.data.command == "vibration_strength" }}'
              - condition: template
                value_template: '{{ trigger.event.data.args.strength => "50" }}'
action:
  - service: switch.turn_on
    data: {}
    entity_id: switch.wall_switch_left_158d0001dbe18c

you need to change the data to an int:

- condition: template
  value_template: '{{ trigger.event.data.args.degrees | int => 10 }}'
- condition: template
  value_template: '{{ trigger.event.data.args.strength | int => 50 }}'

I’m not sure about the rest so I’m assuming all of that is OK (I think it is tho) and the logic is sound (haven’t even looked at it) but start with the changes above and see if it works.

Hi Finity,

unfortunately that is not working. Getting this error:
Message malformed: invalid template (TemplateSyntaxError: expected token ‘end of print statement’, got ‘=’) for dictionary value @ data[‘condition’][0][‘conditions’][1][‘conditions’][1][‘conditions’][1][‘value_template’]

I can save it as

condition: template
value_template: '{{ trigger.event.data.args.degrees| int > 10 }}'

But then the automation still doesn’t work :frowning:

Well I use node red, but heres how a tilt on my aquara comes through…

So hes a mock flow I made. If degree of tilt is > 28, toggle helper1, otherwise toggle helper2.

Heres what my switch node is looking at:
image

Sorry I didn’t see the typo you had:

- condition: template
  value_template: '{{ trigger.event.data.args.degrees | int >= 10 }}'
- condition: template
  value_template: '{{ trigger.event.data.args.strength | int >= 50 }}'

as far as the automation not working I really don’t have an explanation for that. Like I said I didn’t verify your logic and I honestly don’t think I could even if I wanted to wade thru all of those nested and/or blocks since I don’t have your devices.

I think what I would do is start removing conditions and add them back one at a time and verify that the automation works as you add each one back in. Then at least you’ll know where it’s failing.

Thank you both. @nappyjim, you’re nodered output showed something the developer tools didn’t. The correct syntax is trigger.event.data.args.0.strength and trigger.event.data.args.0.degrees

Comming back to the and/or statement. It’s less difficult then it looks. I have 2 sensors that can have 3 outputs. So it’s something like this:

ZHA_Event comming from: device 1 or device 2
And
one of these

  • drop
  • vibrate + above selected strength
  • tilt + more then x degrees

The working automation looks like this:

alias: Alarm - Triggers - Vibration sensors
description: ''
trigger:
  - platform: event
    event_type: zha_event
condition:
  - condition: and
    conditions:
      - condition: or
        conditions:
          - condition: template
            value_template: '{{ trigger.event.data.device_ieee == "xx:xx:xx:xx:xx:xx:xx:x1" }}'
          - condition: template
            value_template: '{{ trigger.event.data.device_ieee == "xx:xx:xx:xx:xx:xx:xx:x2" }}
      - condition: or
        conditions:
          - condition: template
            value_template: '{{ trigger.event.data.command == "Drop" }}'
          - condition: and
            conditions:
              - condition: template
                value_template: '{{ trigger.event.data.command == "Tilt" }}'
              - condition: template
                value_template: '{{ trigger.event.data.args.0.degrees | int >= 10 }}'
          - condition: and
            conditions:
              - condition: template
                value_template: '{{ trigger.event.data.command == "vibration_strength" }}'
              - condition: template
                value_template: '{{ trigger.event.data.args.0.strength | int >= 100 }}'
action:
  - service: alarm_control_panel.alarm_trigger
    data: {}
    entity_id: alarm_control_panel.ha_alarm
mode: single

Hope this helps someone else in the future :slight_smile:

2 Likes

Thanks for posting this. Triggering on a ZHA_event - doesn’t that mean it will trig every time anything ZHA related triggers? I understand that the conditions will it prevent it from running the actions, but is this the only way to capture a drop event or orientation event?