This guide explains how to create automations in Home Assistant to save electricity by automatically turning on or off lights based on a Bluetooth motion and ambient light sensor. The guide uses a low cost ($12 USD) Bluetooth Low Energy (BLE) motion and ambient light sensor with a low cost ($5 USD) Bluetooth LE gateway built using a ESP32 microcontroller running ESPHome software to connect with Home Assistant.
Want to avoid stumbling in the dark looking for the light switch? If you answered yes, then this guide will help you create a smart home that automatically turns on lights in a room at night when people are in the room and automatically turns off lights after people leave. Home Assistant can be configured to change device state (e.g. turn on light) based on status of motion sensor and illuminance (ambient light) sensors.
With the Qingping motion & ambient light sensor (model CGPR1), you can place the sensor in any room. There's no wiring needed becauses it's battery powered and has Bluetooth Low Energy wireless connectivity.
Prerequisites
- Home Assistant already installed on Raspberry Pi, single board computer or PC. See Setup Home Assistant on a thin client guide as an example.
- Completed setup and configuration of Home Assistant integration for your smart Wi-Fi plug, light bulb, or light switch.
- ESPHome configured as Bluetooth Low Energy device scanner running on a ESP32 microcontroller.
Note: This guide assumes you've completed the Home Assistant room temperature and humidity with Bluetooth temperature sensor and ESPHome guide which explains how to configure ESPHome to passively monitor Blueooth LE sensors and connect ESPHome with Home Assistant.
Hardware required
- Qingping motion & ambient light sensor (model CGPR1)
- ESP32 microcontroller
- PC with Bluetooth for initial sensor pairing
- Smart Wi-Fi plug, light bulb, or light switch compatible with Home Assistant (ex: Philips Hue, Belkin WeMo, TP-Link Kasa)
Software required
Tools required
- No special tools needed, nothing to take apart or assemble.
Qingping motion & ambient light sensor (model CGPR1) is a small battery powered sensor with Bluetooth wireless connectivity.
- Includes a PIR motion sensor that uses infrared sensor to detect movement of people and pets within 2 to 7 meters range.
- Includes a ambient light (illuminance) sensor
- Comes with adhesive wall mount base with adjustable angle that sensor magnetically attaches to
- Powered by 2 replaceable CR2450 coin cell batteries
- Has integrated Bluetooth Low Energy (BLE) wireless connectivity that will be used to integrate this sensor with Home Assistant
- Can be used with Home Assistant without pairing with Mijia smart home app
- Powered by Telink TLSR825x series ultra low power microcontroller designed for Internet of Things (IoT)
Building this is separated into 4 phases
- Obtain the CGPR1 sensor device's bind key and Bluetooth MAC address
- Update ESPHome configuration to detect Bluetooth sensor
- Configure Home Assistant integration for the motion and illuminance sensors from ESPHome
- Configure Home Assistant automation to turn on and off turn on a light, switch, scene, script or group based on motion and illuminance sensors
Before proceeding, review the Prerequisites section to check that you have Home Assistant already running with a smart light bulb, smart plug, or smart switch, and ESPHome is already running to monitor Bluetooth LE devices.
You'll need to provide the Mi bind key and Bluetooth MAC address in the ESPHome configuration in order to integrate the device into Home Assistant. The CGPR1 motion sensor device broadcasts data in an encrypted format designed to work with Xioami Mijia smarthome ecoysystem, but the format has been decoded to work with Home Assistant if you obtain the device Mi bind key.
- On the back of the motion sensor device, rotate the plastic battery cap to remove it, exposing the CR2450 coin cell battery.
- Enable pairing mode by pressing blue button for 3 seconds until the LED light flashes.
- On a PC with Bluetooth, use Google Chrome, Microsoft Edge or Opera Browser. Go to CGPR1 Utils page
- In the CGPR1 Utils page, select
Connect
button. The browser should open a popup with visible Bluetooth devices. Select theQingping Motion & Light
device. - After connection is successful, select the
Mi Activation
button. TheMi Bind Key
is then displayed (ex:ff1ae526b23b4aebeadcaaad86f59055
) - Save the
Mi Bind Key
. - Now that you have obtained the bind key, the next step is to configure ESPHome to obtain the Bluetooth MAC address of the sensor device by following the instructions in "Step 2: Update ESPHome configuration to detect Bluetooth thermometer" of Home Assistant room temperature and humidity with Bluetooth temperature sensor and ESPHome guide. Proceed to the next step after you've obtained the Bluetooth MAC address (ex:
A1:B1:C1:D1:E1:F1
) of the motion sensor.
Now that you have obtained the bind key and Bluetooth MAC address for your ,motion sensor device, the next step is configuring ESPHome software on your ESP32 microcontroller to listen to motion and ambient light Bluetooth data broadcasted by your sensor device, and then re-trasnmit that data through your Wi-Fi network to Home Assistant.
- Within the ESPHome Dashboard, select "Edit" button to edit the ESPHome .yaml configuration. Use my sample ESPHome configuration.yaml as a reference and add the following to the .yaml config. Update
mac_address
with the MAC address you determined in the previous step.
esp32_ble_tracker:
binary_sensor:
- platform: xiaomi_cgpr1
name: "M1 Motion detector" # CGPR1 Motion & Light Sensor
mac_address: A1:B1:C1:D1:E1:F1 # Device MAC address
bindkey: "5f60b65c144f3c6b8fdae819fca6f614"
idle_time:
name: "M1 Idle Time"
battery_level:
name: "M1 Battery Level"
illuminance:
name: "M1 Illuminance"
# Auto daily reset
time:
- platform: homeassistant
on_time:
- seconds: 0
minutes: 0
hours: 4
days_of_week: MON-SUN
then:
- switch.toggle: restart_switch
switch:
- platform: restart
name: "ESP-BLE-MOTION restart"
id: restart_switch
- Select Save, then select Install. Your ESP32 should have successfully connected to your Wi-Fi network and should be detected as online in ESPHome Dashboard at this point, so Wirelessly is the easiest option. If your ESP32 is not detected as online in ESPHome Dashboard, then double check you've configured the correct Wi-Fi SSID and password in your configuration.yaml file.
After configuring ESP32 to detect motion and ambient light data broadcasted by your Bluetooth LE sensor and re-transmit the data as a Home Assistant binary sensor, the next step is to enable ESPHome integration so that ESPHome dashboard displays temperature and humidity data.
Follow the instructions in "Step 3: Configure Home Assistant integration for sensors in the Bluetooth thermometer" in Home Assistant room temperature and humidity with Bluetooth temperature sensor and ESPHome guide. Proceed to the next step after Home Assistant Overview dashboard displays new motion detector and illuminance sensors.
Step 4: Configure Home Assistant automation to turn on and off lights based on motion and illuminance sensors
Now that motion and ambient light sensor data is integrated into Home Assistant, the last steps is to automate Home Assistant to trigger an action to turn on a light, switch, scene, script or group based on motion and illuminance.
-
Select the button below to import the blueprint into your Home Assistant.
OR in Home Assistant, go to Configuration and then Blueprints. Click on the blue Import Blueprint button in the bottom right and enter the blueprint URL.
https://github.com/jturn08/smarthome/blob/main/esphome/blueprints/turn-on-switch-with-motion.yaml
-
In Home Assistant, go to Configuration and then Automations & Scenes. Select the blue "+ Create Automation" button in the lower right corner.
-
Select the "Motion-activated entity" blueprint
-
Enter the automation name (ex: Motion activated stair light switch)
-
Select the motion sensor and illuminance sensor names (ex: M1 Motion detector and M1 Illuminance) that were defined in your ESPHome configuration .yaml file.
-
Select the target light or switch entity. If you need to control multiple entities, create a new scene that includes all the entities you want to control and select the scene in the automation.
-
If desired, adjust the motion idle time before lights are turned off and the max illuminance before lights are not turned on.
Use this automation to save electricity and reduce your home's energy footprint by automatically turning off lights when there's no motion detected in an area after some time. This automation works even if lights were manually turned on.
-
Select the button below to import the blueprint into your Home Assistant.
OR in Home Assistant, go to Configuration and then Blueprints. Click on the blue Import Blueprint button in the bottom right and enter the blueprint URL.
https://github.com/jturn08/smarthome/blob/main/esphome/blueprints/turn-off-entity-if-no-motion.yaml
-
In Home Assistant, go to Configuration and then Automations & Scenes. Select the blue "+ Create Automation" button in the lower right corner.
-
Select the "Motion-deactivated entity" blueprint
-
Enter the automation name (ex: Auto off stair lights)
-
Select the motion sensor (ex: M1 Motion detector) that was defined in your ESPHome configuration .yaml file.
-
Select the target light or switch entity to turn off.
-
If desired, adjust the motion idle time before lights are turned off. Note the sensor reports a max idle time of 30 minutes even if there's been no motion for longer than 30 minutes.
Congratulations! Your lights can now automatically turn on at night when people are in the room and automatically turns off lights after people leave. With the low cost of the Bluetooth LE motion sensors, you can buy a few and place them in each area of your home to control different rooms. A single ESP32 microcontroller can detect data broadcast from multiple Bluetooth thermometers.
For more information about automations, see