I was looking for a tilt sensor to tell me if my garage door was open or closed. The sensor had to work with Home Assistant and the only one I could find that looked simple and cheap was the Aqara Vibration sensor that had a tilt/orientation feature.
Unfortunately the feature was not one of the attributes that Home Assistant showed with the device which sent me down the rabbit hole of googling. Eventually I got it figured out and this article is to share my experiences.
First of all, I am using the Conbee II usb stick to provide the zigbee hub. The Aqara Vibration sensor installed easily with that hub. The next step was to figure out how to access the tilt information. To do that I used the Home Automation Developer Tools.
In the Events section of the Developers tools I started listening to the zha_events:
I clicked the “START LISTENING” link. Then I set the Aqara Vibration sensor flat on the table, and then tilted it upright. The Developer tool captured the events and displayed them.
Event 61 fired 7:42 AM:
{
"event_type": "zha_event",
"data": {
"device_ieee": "00:15:8d:00:07:c6:3c:de",
"unique_id": "00:15:8d:00:07:c6:3c:de:1:0x0500",
"device_id": "203fb89ed504710f414cc150d3af3dda",
"endpoint_id": 1,
"cluster_id": 1280,
"command": "current_orientation",
"args": {
"rawValueX": 46,
"rawValueY": 64508,
"rawValueZ": 172,
"X": 0,
"Y": 90,
"Z": 0
},
"params": {}
},
"origin": "LOCAL",
"time_fired": "2022-05-04T14:42:45.569018+00:00",
"context": {
"id": "81b14b5bd161763702110306bcd0a4aa",
"parent_id": null,
"user_id": null
}
}
Different event commands were shown. The code above is showing the “current_orientation” command. I found it to be the most useful. The “Z” value switched between a “1” and a “0” depending on whether or not the sensor was flat or upright. With the door closed it was a value of 0, open a value of 1.
While there might be simpler ways to use this information, I decided to create a Home Assistant input_number helper to store the state. I created an automation to catch the event of the orientation changing, and that automation then calls a script, passing the Z value.
The automation code is:
- id: '1651636746523'
alias: garage door state
description: ''
trigger:
- platform: event
event_type: zha_event
event_data:
device_ieee: 00:15:8d:00:07:c6:3c:de
command: current_orientation
condition: []
action:
- data_template:
message: '{{trigger.event.data.args.Z}} '
service: script.garage_door_state
mode: single
You can see that the event_data was taken from the Developer Tools event listener. The Z value is read using the ‘{{trigger.event.data.args.Z}} ‘ template.
The final step was to write the garage_door_state script:
garage_door_state:
sequence:
- service: input_number.set_value
target:
entity_id: input_number.tilt
data_template:
value: "{{ message }}"
- service: switch.announce
data:
message: "garage door in state {{ message }}"
This code saves the Z value into the helper input_number.tilt (I originally was looking at the Tilt command instead of the current_orientation command, which is why the helper is named tilt)
Finally I added a button card to my dashboard, using the input_number tilt as the entity. When clicked the button opens or closes the garage door using a relay switch on the door.
When the door finishes opening or closing the state displayed changes from 0.0 to 1 or vice versa.
The next thing to do is change the input_number helper to an input_text helper and in the script test the Z value and change the input text to “open” or “Closed”