How to turn off the light automatically when you go out using esp32
Everytime you go out and you forget to turn off the light !
Don’t worry, you are not alone in this situation. We are all LAZY. In this article, we will make it simple and easy to make some economy on your electric bill, but we will also contribute to save the planet.
Requirements
Before starting the tutorial, you should have these materials.
Aliexpress:
ESP32-DevKitC board : Buy here from aliexpress
Tuya wifi bulb : Buy here from aliexpress
Xiaomi mi band : Buy here from aliexpress
Finding the Mi band MAC adress
if you know already your band bluetooth MAC adress, you can skip this part.
In your terminal run the command and find the name of your band in the output:
$ sudo hcitool lescan
Or
$ sudo hcitool lescan | grep Band
lescan
means : low energy scan
ESPhome configuration
If you are not familiar with esphome [check this article](/how-to-monitor-your-home-temperature-with-esp32-and-xiaomi-mijia-using-esphome/.
Run ESPhome on your computer, and prepare the esp32 configuration:
- Create a config file
smartwatch.yaml
under config directory. - Add the following configuration to the file.
substitutions:
# Modify variables based on your settings
hostname: "livingroom"
esphome:
name: $hostname
platform: ESP32
board: esp32dev
wifi:
ssid: wifi_ssid
password: wifi_password
fast_connect: True
# Enable logging
logger:
# Enable Home Assistant API
api:
reboot_timeout: 0s
web_server:
port: 80
ota:
time:
- platform: homeassistant
id: homeassistant_time
# Enable Bluetooth scanning for this ESP32
esp32_ble_tracker:
# Example configuration entry
binary_sensor:
- platform: ble_presence
mac_address: XX:XX:XX:XX:XX:XX
id: mibandpresence
name: "Presence MiBand EntryESP"
Check the binary_sensor ble_presence for more information Now you can flush your esp32 with the configuration above.
If you find any problem during this tutorial: Please leave a commment and I will reply very soon!
Configuring smartbulb
If your smart bulb is not configured yet, you can refer to this video to see how to setup it
Home assistant automation
Once your esp32 is configured, connected to the internet and to home assistant, let’s create the automation.
Indeed, 2 automations are required :
- When going out, turn off the light
- When coming to the house, turn on the light
Turn off when I leave home automation
Do the following :
Go to home assistant http://localhost:8123
the left menu :
Configuration
Automations
+
button in the bottom rightChoose a name :
Turn off when I leave home
Then choose a person (your username) and the name of your bulb.
When a new page appears, scroll down to
Triggers
- Trigger type :
Device
- Device : your esp32 name
livingroom
. - Trigger :
Presence MiBand EntryESP turned off
- Trigger type :
Scroll down to
Actions
:- Action type :
Device
- Device :
Smart Bulb
- Action :
Turn off smart bulb
- Action type :
Turn off when I leave home automation
Do the following :
Go to home assistant http://localhost:8123
the left menu :
Configuration
Automations
+
button in the bottom rightChoose a name :
Turn off when I leave home
Then choose a person (your username) and the name of your bulb.
When a new page appears, scroll down to
Triggers
- Trigger type :
Device
- Device : your esp32 name
livingroom
. - Trigger :
Presence MiBand EntryESP turned off
- Trigger type :
Scroll down to
Actions
:- Action type :
Device
- Device :
Smart Bulb
- Action :
Turn off smart bulb
- Action type :
If you like this tutorial, please give me support by subscribing to my Youtube channel my youtube channel
I hope this article was useful, please write a comment if you succeed and share it with your friends!