Estimated reading time: 13 minutes

Imagine always knowing exactly who is in which room of your home - and all automatically. With Home Assistant, an ESP32 and BLE (Bluetooth Low Energy) tracking, you can implement precise room presence detection. In this blog article, I explain step by step how you can do this with the ESPHome extension and the Format BLE Tracker realizes. You will learn how to ESPHome install, set up your ESP32, activate the BLE transmitter in the Home Assistant app and then merge everything into one big one in Home Assistant.

For detailed instructions and an explanation of all configuration options, I recommend the YouTube video linked below. In this video, every single setting is explained in detail so that you receive comprehensive instructions for the exact setup. You can find any code from the video in this article so that you can use it directly without having to type it in.

What is Home Assistant?

Home Assistant is an open source platform for home automation that aims to connect and centrally control all devices in the home. It is designed to provide a unified user interface and simple, flexible control for a variety of smart home devices from different ecosystems. Here are some points in favor of Home Assistant:

  1. CompatibilityHome Assistant supports thousands of devices and services from various manufacturers, including lighting, air conditioning, heating, cameras, weather sensors, voice assistants and more.
  2. Local controlUnlike many commercial smart home platforms that rely on cloud services, Home Assistant runs locally on your network. This means increased privacy and reliability, as your smart home devices work even without an active internet connection.
  3. Automation and scenesOne of the main features of Home Assistant is the ability to create complex automations and scenarios. For example, you can have lights switch on automatically when it gets dark or turn on the heating when the temperature falls below a certain level.
  4. Simple user interfaceHome Assistant offers a user-friendly interface that makes it easy to monitor and control devices. This interface is fully customizable.
  5. Integration with voice assistantsHome Assistant can be integrated with voice assistants such as Amazon Alexa or Google Assistant to enable voice-controlled control of your smart home devices.
  6. Open source and community-drivenSince Home Assistant is open source, it is supported by an active community that is constantly developing new integrations and improving existing ones.
  7. Data protection and securityAs a locally running platform, Home Assistant has the advantage that the data remains within its own network. This protects privacy and increases security compared to cloud-based solutions.

In summary, Home Assistant is a powerful and flexible solution for those who want to design their own smart home and control their devices centrally, regardless of the manufacturer or ecosystem.

What is ESPHome / ESP32?

ESPHome is an open source platform that allows you to easily integrate microcontrollers such as the ESP32 or ESP8266 into your smart home. With ESPHome, you can program these devices to use them as sensors, actuators or control units. You write configuration files in YAML, and ESPHome takes care of creating the firmware and flashing it to the device. ESPHome offers seamless integration with Home Assistant, so you can easily control and automate your devices via the smart home system.

The ESP32 is a powerful microcontroller with integrated Wi-Fi and Bluetooth that is particularly popular in the IoT (Internet of Things) sector. It is often used to create smart devices that can be wirelessly integrated into a network. With its small size, versatile functions and cost-effective availability, the ESP32 is ideal for projects such as room presence detection or smart lighting. With ESPHome, you can easily program the ESP32 and integrate it into your smart home.

Requirements

  • Home Assistant installed
  • MQTT installed and set up
  • ESP32 device with Bluetooth support available (ideally ESP32-S3)
  • WLAN 2.4GHz network available everywhere
  • Android smartphone with Home Assistant app or BLE beacon
ESP32 devices. The M5NanoC6 does not (yet) work for this purpose as it is too new!
Last updated on 15. October 2024 at 18:20 - Image source: Amazon Affiliate Program. All statements without guarantee.

ESPHome installation: Two simple options

Before you start with the ESP32, you must install ESPHome. You have two simple options to install ESPHome either as Addon in Home Assistant or as Docker Container to install.

Option 1: Install ESPHome as an add-on in Home Assistant

  1. Open Home Assistant:
    Log in to your Home Assistant dashboard.
  2. Go to Add-ons:
    Click on Supervisor and then select Add-on store.
  3. Search for and install ESPHome:
    Search for "ESPHome" and click on Install. As soon as the installation is complete, click on Start.
  4. Open ESPHome Dashboard:
    After starting, you can open the ESPHome Dashboard via the Home Assistant menu and get started.

Option 2: Install ESPHome as a Docker container

If you want to install ESPHome as a Docker container, proceed as follows:

1. prepare Docker:
Make sure that Docker is running on your system.

2. start the Docker ESPHome container:
Open a terminal and create there e.g. with sudo nano docker-compose-esphome-yml a new file with the following content:

services:
  esphome:
    container_name: "esphome"
    image: "ghcr.io/esphome/esphome:stable"
    environment:
      - "ESPHOME_DATA_DIR=/data"
      - "TZ=Europe/Berlin"
      - "ESPHOME_DASHBOARD_USE_PING=false"
    expose:
      - "6052/tcp"
    network_mode: "host"
    restart: "unless-stopped"
    volumes:
      - "/dev/shm/esphome-cache:/cache"
      - "/mnt/cache/appdata/smart-home/esphome-data:/data"
      - "/mnt/cache/appdata/smart-home/esphome:/config"

At the end, don't forget to start the container (adjust the path to your path):

sudo docker compose -f /mnt/cache/appdata/compose/docker-compose-esphome.yml up -d

3. ESPHome Dashboard:
Access the dashboard by entering the IP of your server in the browser with the port 6052 call, e.g. http://192.168.178.100:6052.

Now ESPHome is ready and you can set up your ESP32.

Set up your ESP32 with ESPHome

Once ESPHome is running, you can set up your ESP32 as a BLE tracker for room presence detection. The annoying thing is that the initial setup can only be done via the web protocol https:// can be made. Accordingly, you have to go to the website https://web.esphome.io/ change.

Connect the device to the computer via USB:
Connect the ESP32 to your computer via USB. There are also other options, but this is by far the easiest and simplest.

Add new device:
Go to the ESPHome dashboard from https://web.esphome.io/ and click on Connect. Then select the device there. It will say something about USB Serial.

Enter name and WLAN access data:
The ESP32 is now connected. Now click on Prepare for first use and give your ESP32 a name and enter the WLAN data that the ESP32 should use later.

Flash firmware to ESP32:
Connect the ESP32 via USB to your computer or the device on which Home Assistant is running. Click on Create and then select Install. Follow the instructions to flash the firmware to the ESP32.

Make ESP32 available in ESPHome:
As soon as the firmware is installed, the ESP32 connects to your WLAN and is available in the ESPHome dashboard. Your ESP32 is now ready for use.

Change the settings of the ESP32:
We now need to make a few settings to really turn our ESP32 into a room presence detector.

mqtt:
  broker: 
  username: 
  password: 
  discovery: true

# For the external build:
external_components:
  source: github://formatBCE/ESP32_BLE_presense@main

# System time configuration - required for accurate timestamps, used during integration:
time:
  - platform: homeassistant
    id: homeassistant_time

# Initialization:
esp32_ble_presense:
  area: "" # Replace this with your room name
  time_id: homeassistant_time

Finally, we need to comment out or delete part of the existing configuration:

# Sets up Bluetooth LE (Only on ESP32) to allow the user
# to provision wifi credentials to the device.
esp32_improv:
  authorizer: none

If you do not do this, you will receive an error message that says error: expected type-specifier.

Now we have to install the firmware again and the ESP32 is successfully set up. The device has its own small user interface running on its IP address when it is connected to the WLAN, for example http://192.168.178.78.

Install Format BLE Tracker

Now that your ESP32 is set up, we can use the Format BLE Tracker install. This tool is perfect for detecting BLE signals and determining the presence of devices in the room.

What is the Format BLE Tracker?

The Format BLE Tracker is an extension that turns your ESP32 into a BLE tracker. It scans for Bluetooth signals from devices such as smartphones or BLE beacons and detects whether someone is in the room. This makes it easy to perform accurate presence detection without additional sensors.

Installation of HACS and integration of format BLE tracker in Home Assistant

Step 1: Installing HACS in Home Assistant

Home Assistant Community Store (HACS) is a custom add-on for Home Assistant that provides access to a variety of custom integrations and plugins developed by the community. How to install HACS:

  1. Check requirements: Make sure that your Home Assistant is ideally up to date.
  2. Download Execute script: Open a terminal for the Docker container version or the core version.
    • Docker: Dial into the container with: docker exec -it homeassistant bash or (similar)
    • Core: Switch to the user who is running Home Assistant
    • Execute the following command for both variants:
      wget -O - https://get.hacs.xyz | bash -
  3. Restarting the Home Assistant: After the files have been copied, restart Home Assistant.
  4. Integrate HACS into Home AssistantGo to "Configuration" > "Integrations" > "Add" in the Home Assistant interface and search for HACS. Add it and enter your GitHub token if prompted.

For the supervised version, please refer to the Documentation of HACS itself to read about the installation.

Step 2: Add format BLE tracker integration via HACS

Open your Home Assistant instance and open a repository inside the Home Assistant Community Store.

Once HACS is installed, you can simply add Format BLE trackers:

  1. Open HACS: Go to HACS in the Home Assistant interface.
  2. Search for integrationClick on "Integrations" and click on the three dots at the top right and then "Custom repositories".
  3. Add repositoryNow copy the GitHub repository https://github.com/formatBCE/Format-BLE-Tracker in the repository field and enter "Integration" as the category.
  4. Install format BLE trackerFind Format-BLE-Tracker in the list and click on "Install". This will add the integration to your Home Assistant.
  5. Restarting the Home Assistant: Start Home Assistant again to apply the changes.
  6. Configure format BLE trackerAfter restarting, you can set up and customize Format BLE Tracker via "Configuration" > "Integrations" in Home Assistant.

Conclusion

With these steps, you have successfully installed HACS in your Home Assistant and added the Format BLE tracker integration. This powerful combination opens up a world of additional features and customizations that have been and are being developed by the Home Assistant community. More blog posts and videos on useful integrations to follow. If you don't want to miss out, subscribe to my YouTube channel!

Activate BLE transmitter in the Home Assistant app

For the ESP32 to recognize your smartphone as a BLE signal, you must set the BLE transmitter in the Home Assistant Companion app. Unfortunately, this option only seems to be available for Android smartphones. The iPhone, on the other hand, is left empty-handed.

  1. Open the Home Assistant Companion App:
    Launch the Home Assistant Companion app on your Android smartphone.
  2. Go to the settings:
    Open the app settings and select the item Companion app and then tap on Sensors Manage.
  3. Activate BLE transmitter:
    Search for the BLE transmitter and activate it. Make sure that the option "Only transfer WLAN SSID to the home network" is switched on and set the "Transmitter power" on Lowinstead of very low by default. Your smartphone now regularly sends BLE signals that the ESP32 can receive.
    Make a note of the UUID at the bottom, as we will need this in the next step.

Set up BLE Tracker in Home Assistant

Under Integrations, add the integration Format BLE Tracker and select in the popup "Add New Beacon". We must now enter the previously noted UUID and enter a name of our choice for the sensor.

HVVENTY NRF51822 Bluetooth Beacon Tag Eddystone Ibeacon Ble Proximity Locator Beacon
Price not available Buy now on Amazon* Price incl. VAT, Excl. shipping
Gravity: BLE Sensor Beacon Pack (5 PCS)
Price: € 32.90 Buy now on Amazon* Price incl. VAT, Excl. shipping
Last updated on 15. October 2024 at 18:20 - Image source: Amazon Affiliate Program. All statements without guarantee.

Note:

  • For dumb beacons (e.g. Tile), you can find the MAC address in the official app or use any Bluetooth tracker app on your phone to delete it;
  • For smart beacons (e.g. Android phone with Home Assistant Companion application and activated BLE tracker), you must copy the Proximity UUID, as the MAC address of the device is not publicly accessible.
  • Minimum RSSI: If the signal strength is below the value you set in this slider, it will be ignored by the ESP32 tracker node. I have an ESP32 tracker node in most rooms and my rooms are not that big, and I found that setting it to -90 works best for me. You have to play with it to find out what works best in your environment.

The integration creates a device with three entities per beacon:

  • Device tracker entity for the device. Displays the Home status for this device if the device is visible to at least one of the tracking nodes, or the Away status.
  • Sensor with the currently nearest node name for this device (basically the current room name).
  • Input slider for setting the data expiry period (from 1 minute to 10 minutes). This affects the time from the last visibility event until the away mode is switched on. Use larger values if you frequently switch from Home to Away and back. By default, this value is set to 2 minutes.

Now you will quickly see that your new device shows you the room name that you have stored in the firmware of the ESP32 device in ESPHome. Based on this, you can now configure even more automation than was previously possible!

YouTube video implementation

https://youtu.be/WLuRdUHhgW8

Conclusion

With Home Assistant, ESPHome and an ESP32, you can create precise room presence detection. The BLE Tracker format makes it possible to capture BLE signals and trigger specific automations when someone is in a room. By activating the BLE transmitter on your smartphone, you can ensure that your home always knows where you are. Try it out and make your smart home even smarter!

The display of the products was implemented with the affiliate-toolkit plugin.

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

en_US