Xiaomi Automations

Xiaomi Automations

Here you will find automations that I use at home assistant in combination with xiaomi devices.

Motion Sensor (1st gen)

Xiaomi motion sensor to turn on /off the WC lights automatically.

This automation turns on the light when someone enters the WC, as long as it detects someone in the WC the light remains on. Also, if someone turns on the light from the classic switch but does not enter the WC, then it turn off after a while.

automation.yaml

- alias: Light WC Turn On/Off
  trigger:
  - entity_id: light.wc_light
    for: 0:05:00
    from: 'off'
    platform: state
    to: 'on'
  - entity_id: binary_sensor.motion_sensor_xxxxxxxxxx
    for: 0:05:00
    platform: state
    to: 'off'
  - entity_id: binary_sensor.motion_sensor_xxxxxxxxxx
    platform: state
    to: 'on'
  action:
  - data: {}
    entity_id: light.xxxxxxx
    service_template: >
      {% if is_state('binary_sensor.motion_sensor_x','on' ) %}  
        homeassistant.turn_on
      {% else %} 
        homeassistant.turn_off
      {% endif %}
  initial_state: 'on'

Button (1st gen)

I use a Xiaomi switch with three functions that controls some lights that children usually turn on and off. With one click one light comes on, with two clicks another light comes on and if it is pressed then the above lights turn off.

automation.yaml

- alias: Switch Single Click
  trigger:
  - event_data:
      click_type: single
      entity_id: binary_sensor.switch_xxxxxxxxxx
    event_type: xiaomi_aqara.click
    platform: event
  action:
  - entity_id: light.xxxxxxxx
    service: light.toggle
- alias: Switch Double Click
  trigger:
  - event_data:
      click_type: double
      entity_id: binary_sensor.switch_xxxxxxxxxx
    event_type: xiaomi_aqara.click
    platform: event
  action:
  - entity_id: light.xxxxxxxx
    service: light.toggle
- alias: Switch Long Click
  trigger:
  - event_data:
      click_type: long_click_press
      entity_id: binary_sensor.switch_xxxxxxxxx
    event_type: xiaomi_aqara.click
    platform: event
  action:
  - entity_id: light.xxxxxxxx
    service: light.turn_off
  - data: {}
    entity_id: light.xxxxxxxx
    service: light.turn_off

Another application with a second switch that I have is to manage the boiler. I have installed a power relay and a sonoff which I turn on/off with the xiaomi switch, which I have installed outside the electrical panel where the classic switch is located.

So I’ve told my wife if she needs to turn on the boiler to press the new xiaomi switch, that way if it’s forgotten open then it will shut down after a while or when the water in the boiler reaches some degrees.

automation.yaml

- alias: Switch Boiler Double Click
  trigger:
  - event_data:
      click_type: double
      entity_id: binary_sensor.switch_xxxxxxxxx
    event_type: xiaomi_aqara.click
    platform: event
  condition: []
  action:
  - data: {}
    entity_id: switch.xxxxxxxxx
    service: switch.toggle

Gateway

Xiaomi gateway has integrated a colorful ring with light. Which I use as an indicator light for some problems or to show me the alarm status. When the alarm is armed at night then the light turns red, when the alarm is disarmed then the light is green.

automation.yaml

- alias: Turn on Red Light Alarm Armed
  trigger:
  - entity_id: alarm_control_panel.xxxxxxxxx
    platform: state
    to: armed_night
  condition:
  - condition: and
    conditions:
    - after: '20:00:00'
      before: '10:00:00'
      condition: time
  action:
  - data:
      brightness: 255
      rgb_color:
      - 255
      - 0
      - 0
    entity_id: light.gateway_light_xxxxxxxxxx
    service: light.turn_on
- alias: Turn on Green Light Alarm Disarmed
  trigger:
  - platform: state
    entity_id: alarm_control_panel.xxxxxxx
    to: disarmed
  condition:
    condition: and
    conditions:
    - condition: time
      after: '20:00:00'
      before: '10:00:00'
  action:
    service: light.turn_on
    entity_id: light.gateway_light_xxxxxxxxxxx
    data:
      rgb_color:
      - 0
      - 255
      - 0
      brightness: 255

The Xiaomi gateway also has a loudspeaker and some pre-installed melodies that I use when the alarm triggered.

automation.yaml

- alias: On Alarm
  trigger:
  - entity_id: alarm_control_panel.xxxxxxxxx
    platform: state
    to: triggered
  action:
  - data:
      gw_mac: xxxxxxxxxxx #<- add your mac address
      ringtone_id: 1
      ringtone_vol: 50
    service: xiaomi_aqara.play_ringtone

Leave a Reply

Your email address will not be published.