Home Assistant + Aqara Vibration Sensor Question (No vibration within an hour then shut down PC)

Hi,
I’ve created two automation, however, the sensor has only 3 events as vibrate, freefall and tilt, there is no OFF state, how do I achieve to Startup the PC when vibration is detected + shut down the PC when no vibration is detected for 30 mins? if the vibration is detected within countdown minutes, these 30 minutes will reset and count down again.

  1. Startup the PC by Vibration Sensor (it worked perfectly)
alias: Startup the PC by Vibration Sensor
description: ''
trigger:
  - platform: event
    event_type: xiaomi_aqara.movement
    event_data:
      entity_id: binary_sensor.study_room_vibration
      movement_type: vibrate
condition: []
action:
  - service: wake_on_lan.send_magic_packet
    data:
      mac: AA:BB:CC:DD:EE:FF
mode: single
  1. Shutdown the PC by Vibration Sensor (It will not work as the sensor has only 3 events as vibrate, freefall and tilt), anyone has an idea how to achieve it?
alias: Shutdown the PC by Vibration Sensor
description: ''
trigger:
  - platform: event
    event_type: xiaomi_aqara.movement
    event_data:
      entity_id: binary_sensor.study_room_vibration
      movement_type: ????
condition: []
action:
  - service: hassio.addon_stdin
    data:
      addon: core_rpc_shutdown
      input: MyPC
mode: single

Thanks!

Maybe you can try creating Event Binary Sensor-

template:
  - trigger:
      - platform: event
        event_type: xiaomi_aqara.movement
        event_data:
          entity_id: binary_sensor.study_room_vibration
          movement_type: vibrate
    binary_sensor:
      - name: Vibration Detected
        auto_off: 5
        state: "on"

This binary_sensor will turn off after 5 seconds. But, vibration is detected later on, it should turn on again.

trigger:
  - platform: state
    entity_id: binary_sensor.vibration_detected
    to: 'off'
    for: '00:30:00'
condition: []
action:
  - service: hassio.addon_stdin
    data:
      addon: core_rpc_shutdown
      input: MyPC

This automation will turn off your PC after the binary_sensor.vibration_detected stays in off position for at least 30 minutes.

Thanks ardysusilo,

I copied the yaml coding and it seems the syntax is incorrect.

error: Message malformed: extra keys not allowed @ data[‘template’]

Did you copy it to your configuration.yaml? If the previous code fails, try this instead-

template:
  - trigger:
      - platform: event
        event_type: xiaomi_aqara.movement
        event_data:
          entity_id: binary_sensor.study_room_vibration
          movement_type: vibrate
    binary_sensor:
      - name: Vibration Detected
        auto_off: 5
        state: "true"

Make sure you only have 1 template: in your configuration.yaml. If you want to make more template sensor, the format should look like-

template:
  - trigger:
      - platform: event
        event_type: xiaomi_aqara.movement
        event_data:
          entity_id: binary_sensor.study_room_vibration
          movement_type: vibrate
    binary_sensor:
      - name: Vibration Detected
        auto_off: 5
        state: "true"

  - sensor:
      - name: "Average temperature"
        unit_of_measurement: "°C"
        state: >-
          {{ }}

  - sensor:
      - name: "Average temperature"
        unit_of_measurement: "°C"
        state: >-
          {{ }}

  - binary_sensor:
      - name: "Has Unavailable States"
        state: "{{ states.sensor | selectattr('state', 'in', ['unavailable', 'unknown', 'none']) | list | count }}"

  - binary_sensor:
      - name: "Has Unavailable States"
        state: "{{ states.sensor | selectattr('state', 'in', ['unavailable', 'unknown', 'none']) | list | count }}"

Edit: I made a mistake in state: "Detected". It should be state: "true".

This is according to the documentation which states “The sensor is on if the template evaluates as true , yes , on , enable or a positive number. Any other value will render it as off.”

Thanks ardysusilo,

can I use the state as “on” instead of “true”? so it will match the automation script which is
to: ‘off’

on and off make sense to me :slight_smile:

Yes, you should be able to use on according to the documentation.

Have you tried the above template sensor and automation?

Hi ardysusilo,

Yes, I have tried but somehow it doesn’t work.


configuration.yaml

template:
  trigger:
    platform: event
    event_type: xiaomi_aqara.movement
    event_data:
      entity_id: binary_sensor.study_room_vibration
      movement_type: vibrate
  binary_sensor:
    - name: binary_sensor.study_room_vibration_detected
      auto_off: 5
      state: "on"

Automation 1

alias: Start Up PC by Vibration Sensor
description: ''
trigger:
  - platform: event
    event_type: xiaomi_aqara.movement
    event_data:
      entity_id: binary_sensor.study_room_vibration
      movement_type: vibrate
condition: []
action:
  - service: wake_on_lan.send_magic_packet
    data:
      mac: AA:BB:CC:DD:EE:FF
mode: single

Automation 2

alias: Shutdown PC by Vibration Sensor
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.study_room_vibration
    to: 'off'
    for: '00:01:00'
condition: []
action:
  - service: hassio.addon_stdin
    data:
      addon: core_rpc_shutdown
      input: MyPC
mode: single

1 Like


template:
  - trigger:
      - platform: event
        event_type: xiaomi_aqara.movement
        event_data:
          entity_id: binary_sensor.study_room_vibration
          movement_type: vibrate
    binary_sensor:
      - name: Vibration Detected
        auto_off: 5
        state: "on"

Please use the above YAML code. After that, restart HA. Do you have a new binary_sensor entity? It should be named binary_sensor.vibration_detected.

If yes, can you try to vibrate your binary_sensor.study_room_vibration to see if binary_sensor.vibration_detected turns ON?

Yes, it does turns on.
image

Good! Then use this for your 2nd automation to turn OFF your PC after 30 minutes-

trigger:
  - platform: state
    entity_id: binary_sensor.vibration_detected
    to: 'off'
    for: '00:30:00'
condition: []
action:
  - service: hassio.addon_stdin
    data:
      addon: core_rpc_shutdown
      input: MyPC
1 Like

It works like a charm.

I will have some test and monitor the use case, thanks a Million ardysusilo :slight_smile:

1 Like

Hi, what device are you using? I have the Aqara vibration sensor but I don’t think it supports movement_type in its event data unless I’m mistaken. Could you share the model number of your device? Thanks!