Mastering Room-Level Presence Detection with ESPresense and Home Assistant

0
0
  • #Home_Assistant
  • #ESPresense
  • #BLE
  • #Presence_Detection
  • #MQTT
  • #Smart_Home
  • #ESPHome
7m read

The Quest for Accurate Room-Level Presence

In the realm of smart homes, automation based on who is where is the holy grail. Turning on lights as you enter a room, pausing music when you leave, adjusting temperature based on occupancy – these require more than just knowing if someone is home. You need room-level presence. While motion sensors are useful, they only detect activity, not persistent presence. Device tracking often relies on Wi-Fi or GPS, useful for home/away status but lacking the granularity for specific rooms.

Enter ESPresense, a powerful and flexible solution built for precisely this purpose. ESPresense is an open-source project that leverages inexpensive ESP32 or ESP8266 microcontrollers to create a network of Bluetooth Low Energy (BLE) scanners. These scanners detect nearby BLE devices (like phones, smartwatches, or dedicated BLE tags) and report their proximity and signal strength to an MQTT broker. Home Assistant can then subscribe to these MQTT topics, process the data, and determine which room a device is likely in, enabling truly context-aware automations.

What is ESPresense?

At its core, ESPresense is specialized firmware for ESP boards that acts as a BLE scanner. It continuously listens for BLE advertisements, identifies known devices by their MAC address or specific advertisement data, and calculates an estimated distance based on the Received Signal Strength Indicator (RSSI) and a calibrated power measurement (TX power). This information (device ID, scanner ID, distance, RSSI, speed, etc.) is published via MQTT topics, typically in JSON format.

The beauty of ESPresense lies in its distributed nature. You deploy multiple ESPresense nodes throughout your home. As a device moves, different nodes will report its presence and distance. Home Assistant, receiving data from all nodes, can then infer the device's location, often determining which node is closest and assigning the device to the corresponding room.

Prerequisites

Before you dive into setting up ESPresense, make sure you have the following:

  • Home Assistant: An operational Home Assistant instance.
  • MQTT Broker: An MQTT broker accessible by Home Assistant and your ESP boards. The Mosquitto broker add-on for Home Assistant is a popular and easy-to-set-up option.
  • ESPRobot Boards: One or more compatible ESP32 or ESP8266 boards. ESP32 boards are generally recommended for their superior BLE performance. Examples include ESP32-DevKitC, M5Stack Atom Lite, or Lolin D32.
  • Power Supply: A reliable power source for each ESP board (e.g., USB power adapters).
  • BLE Devices to Track: Devices you want to track (e.g., your smartphone, an Apple AirTag, a Tile tag, a fitness tracker). You'll need to identify their BLE MAC address or other unique identifier.
  • Network Connectivity: Stable Wi-Fi coverage for all ESP boards to reach your MQTT broker.

Setting Up ESPresense Nodes

Setting up the ESPresense firmware on your boards is straightforward, thanks to the web-based installer:

  1. Connect the Board: Plug your ESP32/ESP8266 board into your computer via USB.
  2. Open the Web Installer: Navigate to the ESPresense web installer in a Chromium-based browser (like Chrome, Edge, Brave).
  3. Select Firmware: Choose the latest stable version of the ESPresense firmware. Make sure to select the correct chip type (ESP32 or ESP8266).
  4. Connect: Click the "Connect" button and select the serial port corresponding to your connected board.
  5. Install ESPresense: Follow the prompts to install the firmware. This will erase the flash and write the new firmware.
  6. Configure Wi-Fi & MQTT: Once installation is complete, the board will typically create a Wi-Fi access point (look for a network named like !$0$!). Connect to this network. A configuration portal should open automatically. If not, open a browser and go to !$1$!.
    • Enter your home Wi-Fi SSID and password.
    • Enter your MQTT broker details: Hostname/IP, Port (usually 1883), and credentials if authentication is enabled.
    • Optionally, set a unique name for this node (e.g., !$2$!, !$3$!). This name will be part of the MQTT topic.
  7. Save and Reboot: Save the configuration. The board will reboot and attempt to connect to your Wi-Fi and MQTT broker.
  8. Verify MQTT Connection: Use an MQTT client tool or Home Assistant's MQTT integration diagnostics to verify that the node is publishing messages to topics like !$4$! and !$5$!.
  9. Repeat: Repeat these steps for each ESPresense node you want to deploy.

Integrating with Home Assistant

The integration process is largely automatic thanks to Home Assistant's MQTT Discovery:

  1. Enable MQTT Discovery: Ensure MQTT discovery is enabled in your Home Assistant !$6$! file under the !$7$! integration. If you used the Mosquitto add-on, this is often enabled by default.
  2. Configure MQTT Integration: Add or configure the MQTT integration in Home Assistant (Settings > Devices & Services > Integrations > Add Integration > MQTT). Point it to your MQTT broker.
  3. ESPresense Device Discovery: Once your ESPresense nodes are publishing via MQTT, Home Assistant should automatically discover the nodes themselves (as MQTT devices) and any BLE devices they detect.
  4. Identify Tracked Devices: When Home Assistant discovers a BLE device via ESPresense, it typically creates a set of entities for that device, including a sensor showing the distance reported by each scanner node and a device tracker entity based on the nearest scanner. The entity ID will be based on the BLE device's identifier. You might see entities like !$8$! or !$9$!.
  5. Rename Entities (Optional but Recommended): Rename the discovered entities to be more user-friendly within Home Assistant (e.g., !$10$!).

Device Integration Tips

Getting specific devices to reliably track can sometimes be tricky:

  • Finding BLE Identifiers: The most common identifier is the BLE MAC address. For phones, this can be dynamic due to privacy features (see below). For dedicated tags like Tile or AirTag, the identifier is usually stable but might be a manufacturer-specific ID or a resolved public address rather than the private MAC. ESPresense logs (viewable via the web interface or MQTT messages) are invaluable for identifying these IDs when a device is near a scanner.
  • Smartphones (iOS & Android):
    • Privacy MAC Addresses: Modern phones use rotating, private MAC addresses when scanning or advertising, making simple MAC tracking difficult.
    • iOS: The most reliable method for iPhones/iPads is often to track the unique identifier broadcast by the device when it's actively scanning for AirTags or other Find My devices. Keep Bluetooth enabled. Some users report success tracking the iBeacon advertisement broadcast when Bluetooth sharing is enabled in settings (though this isn't guaranteed).
    • Android: Similar to iOS, Android often uses randomized MACs. Some success is reported by tracking the device when Bluetooth Scanning is enabled (distinct from just having Bluetooth on). Relying on the companion app's own location reporting combined with ESPresense can be a robust strategy.
  • BLE Tags (AirTag, Tile, etc.): These often broadcast stable identifiers. AirTags broadcast an identifier that changes infrequently and ESPresense has specific support to handle this. Tile tags broadcast a stable MAC or ID.
  • Other Devices: Smartwatches, fitness trackers, etc., may or may not broadcast a detectable BLE advertisement consistently. Check the ESPresense logs to see if they appear and with what identifier.
  • Calibration: The distance estimate is based on RSSI. You can improve accuracy by calibrating the TX power for your specific board and device combination, though for simple nearest-node detection, this is often not strictly necessary.

Best Practices for a Reliable Smart Home

To build a dependable presence detection system with ESPresense:

  • Strategic Node Placement: Place nodes in key areas or entrances/exits of rooms you want to track. Avoid placing them behind metal objects or inside cabinets, which can block BLE signals. Consider placing nodes in hallways to help distinguish between adjacent rooms.
  • Multiple Nodes: Deploying several nodes provides redundancy and improves accuracy. A device moving between rooms will likely be picked up by multiple scanners, and Home Assistant can use the "nearest" logic effectively.
  • Utilize the !$11$! Attribute: Instead of just relying on the default device tracker (which often just reports the name of the nearest scanner), create template sensors or automations that look at the !$12$! attribute reported by each scanner for a given device. This gives you more granular data to work with.
  • Create a "Combined" Presence Sensor: For each person or important device, create a template sensor that evaluates the distances reported by all relevant scanners and determines the likely room. For example:
!$13$!
  • Implement Detection Thresholds: Don't rely on a distance of exactly 0. Use thresholds (e.g., < 3 meters or < 5 meters) based on your node placement and calibration. Consider signal strength vs. estimated distance, as RSSI is often a more direct measure of signal power.
  • Debounce and Delays in Automations: Presence detection can sometimes be jumpy. When using presence changes to trigger automations (like lights), add delays (e.g., "turn lights on if person detected in room for 10 seconds") to prevent lights flickering as the signal fluctuates.
  • Combine with Other Sensors: Enhance reliability by combining ESPresense data with other sensors. For example, only turn on the light if ESPresense detects someone *and* a motion sensor detects motion *and* the light level is low.
  • Monitor Node Status: Use the status topics published by ESPresense nodes via MQTT (e.g., !$14$! which reports !$15$! or !$16$!) to ensure your scanners are running correctly. Create Home Assistant alerts if a node goes offline.
  • Secure MQTT: Always use authentication for your MQTT broker and ideally TLS/SSL encryption, especially if it's accessible outside your local network.
  • Power Reliability: Ensure nodes have stable power. A brief power flicker can take a node offline, impacting presence detection for that zone.
  • Keep Firmware Updated: Periodically check for and install updates for the ESPresense firmware to benefit from bug fixes and new features/device support.

Advanced Applications

Once you have reliable room-level presence, the possibilities for sophisticated automations are vast:

  • Contextual Lighting: Adjust brightness, color, or even scenes based on who is in the room and the time of day.
  • HVAC Control: Only heat or cool rooms that are currently occupied.
  • Media Control: Automatically pause or play music/video when entering or leaving a room (requires media player integration).
  • Security: Trigger alerts if a specific device (like a child's BLE tag) is detected in an unexpected area at certain times.
  • Notifications: Send reminders based on location (e.g., "Don't forget your keys!" if your key tag is detected near the door but you leave the house).

Conclusion

ESPresense, combined with Home Assistant and MQTT, provides an accessible yet powerful platform for achieving accurate and reliable room-level presence detection. While setting up tracking for specific devices like smartphones might require some troubleshooting due to privacy features, the potential for creating truly responsive and intelligent smart home automations makes the effort worthwhile. By strategically placing nodes, utilizing the data effectively in Home Assistant templates, and following best practices for reliability, you can unlock a new dimension of control and convenience in your smart home.

Written by:

NGC 224

Author bio:

There are no comments yet
loading...