As is well documentation in these forums, the vibration sensor created in the Xiaomi Gateway integration does not have a state that is functional. No matter how the device is handled , the state never changes. It does not match the Xiaomi Mi home app which show each state change.
The only attribute that is useful for triggering automatons or setting templates is last_action which will state the last action the device detected vibrate,tilt or free_fall
However the last_action state never resets , so we don’t know when the action stops , or if the same action was repeated later.
To solve this problem and match what is shown on the MI Home app . I have created 3 binary sensors for vibration, tilt and free_fall which will trigger on when movement is detected and off when the action does not repeat within preset time. This preset time is handled by a timer entity. The timer entity is maintained by a very simple automation.
Timer entity
Change the time to increase or decrease sensitivity
xiaomi_move:
duration: '00:03:00'
Automation
Change binary_sensor.vibration_158d00031ce0c7 to match the name of your Xiaomi Vibration Sensor created by the Xiaomi integration
alias: xiaomi movement
description: Turn on timer when vibration, tilt or free fall detected
trigger:
- platform: event
event_type: xiaomi_aqara.movement
event_data:
entity_id: binary_sensor.vibration_158d00031ce0c7
action:
- service: timer.start
data_template:
entity_id: timer.xiaomi_move
Binary Sensors
Change binary_sensor.vibration_158d00031ce0c7 to match the name of your Xiaomi Vibration Sensor created by the Xiaomi integration
- platform: template
sensors:
vibration:
friendly_name: "Vibration"
value_template: >
{{state_attr("binary_sensor.vibration_158d00031ce0c7","last_action") == "vibrate"
and states("timer.xiaomi_move") == "active"
}}
tilt:
friendly_name: "tilt"
value_template: >
{{state_attr("binary_sensor.vibration_158d00031ce0c7","last_action") == "tilt"
and states("timer.xiaomi_move") == "active"
}}
free_fall:
friendly_name: "free fall"
value_template: >
{{state_attr("binary_sensor.vibration_158d00031ce0c7","last_action") == "free_fall"
and states("timer.xiaomi_move") == "active"
}}
So when the automation is triggered by the xiaomi_aqara.movement event ( the device is moved) , it simply restarts the timer.
The binary sensors are set true when the timer is running and if they match the last_action of the Xiaomi Vibration Sensor. This way the binary sensor can monitor when the action is set and when it stops.
You can now use these binary sensors to simply trigger automatons and templates on state change.