Mastering Room-Level Presence: Integrating ESPresense with Home Assistant

0
0
  • #home_assistant
  • #espresence
  • #presence_detection
  • #bluetooth
  • #esp32
  • #automation
Represent Mastering Room-Level Presence: Integrating ESPresense with Home Assistant article
6m read

Introduction: Why Precise Presence Matters

Generic presence detection in Home Assistant, like tracking via router, phone apps, or ping, is great for knowing if someone is home. But what if you want your lights to turn on only when you enter a specific room? Or have music follow you around the house? This requires a more granular level of presence tracking – room-level presence.

ESPresense is an open-source project that turns inexpensive ESP32 microcontrollers into powerful Bluetooth Low Energy (BLE) scanners. By strategically placing these scanners around your home and tracking the signal strength (RSSI) of BLE-broadcasting devices (like your phone, smartwatches, or dedicated BLE tags), ESPresense can determine which room a device is likely in. This information is published via MQTT, which Home Assistant can easily consume, opening up a world of highly localized automation possibilities.

How ESPresense Works

The core idea behind ESPresense is trilateration (or multilateration, more accurately). Each ESP32 running ESPresense constantly scans for BLE advertisements from configured devices. When it detects a known device, it measures the Received Signal Strength Indicator (RSSI) and publishes this, along with its own identity, to an MQTT topic. Home Assistant, using the ESPresense integration, subscribes to these topics. By comparing the RSSI reported by multiple ESPresense nodes for the same device, Home Assistant can estimate which node (and thus which room) the device is closest to.

Prerequisites

  • One or more ESP32 boards (e.g., ESP32-DevKitC, ESP32-WROOM).
  • Devices that broadcast BLE signals you want to track (smartphones, smartwatches, Tile/AirTag/Nut tags, Mi Bands, etc.).
  • An MQTT broker accessible by both your ESP32s and Home Assistant (Mosquitto addon is common).
  • A running Home Assistant instance.
  • HACS (Home Assistant Community Store) installed for easy integration management.
  • Basic understanding of flashing firmware to ESP boards and MQTT concepts.

Step 1: Flashing ESPresense Firmware

The easiest way to get ESPresense onto your ESP32s is using the ESPresense web flasher tool. This requires Google Chrome or Edge and a USB cable.

  1. Connect your ESP32 board to your computer via USB.
  2. Go to the ESPresense Web Installer.
  3. Select your board type if prompted (usually defaults correctly).
  4. Click 'Connect' and choose the correct USB port.
  5. Select the latest stable firmware version.
  6. Choose installation options. For a standard setup, 'Erase device' is recommended for a clean start.
  7. Click 'Install ESPresense'.
  8. Once flashing is complete, the board will restart.

Step 2: Initial Configuration (Wi-Fi & MQTT)

After flashing, the ESP32 will create a Wi-Fi access point (usually named like ESPresense-XXXX). Connect to this network from your computer or phone. A configuration portal should automatically pop up (if not, navigate to 192.168.4.1).

  1. Enter your home Wi-Fi SSID and password.
  2. Enter your MQTT broker details: Broker address (IP or hostname), port (default 1883, 8883 for SSL), username, and password.
  3. Optionally, set a unique node name for this specific ESP32 (e.g., espresence_living_room). This is highly recommended, especially if you have multiple nodes.
  4. Save the configuration. The ESP32 will restart and attempt to connect to your Wi-Fi and MQTT broker.

Repeat Steps 1 and 2 for every ESP32 you want to deploy. Place them strategically in different rooms or areas where you need precise presence tracking.

Step 3: Home Assistant Integration via HACS

The ESPresense integration makes consuming the MQTT data easy.

  1. Open Home Assistant.
  2. Go to HACS -> Integrations.
  3. Click the '+' button in the bottom right.
  4. Search for "ESPresense" and select it.
  5. Click 'Download' and choose the latest version.
  6. Restart Home Assistant (Settings -> System -> Restart).
  7. After restarting, go to Settings -> Devices & Services -> Integrations.
  8. Click '+ Add Integration'.
  9. Search for "ESPresense" and select it.
  10. The integration will ask for your MQTT broker details. These *must match* the details you used when configuring the ESP32s.
  11. Submit the configuration. The integration should connect to your MQTT broker and start listening for ESPresense messages.

Step 4: Identifying and Adding Devices

Once the Home Assistant integration is running, it will automatically discover any devices reported by your ESPresense nodes. Go to Settings -> Devices & Services -> Devices. You should see devices reported by ESPresense, identified by their BLE MAC address or UUID.

These devices will show up with a name like BLE [mac_address] or iBeacon [uuid]. To make them useful, you need to give them friendly names.

  1. Click on an unknown device listed under the ESPresense integration.
  2. Identify the device based on its MAC address/UUID and when/where it appeared. You can use tools like a BLE scanner app on your phone to find MAC addresses of nearby devices. For phones, the MAC address often changes due to privacy features; using a static BLE tag is often more reliable.
  3. Once identified (e.g., this is John's phone), click on the device page in Home Assistant.
  4. Click the pencil icon to rename the device (e.g., John's Phone).
  5. Each tracked device will have state attributes indicating its estimated distance to each ESPresense node and which node it's currently 'in'. The 'best' attribute indicates the node with the strongest signal (closest). The 'room' attribute is derived from the 'best' attribute and the name you gave the node.

Alternatively, you can add devices via the ESPresense web UI for each node (by connecting to its IP address) or via MQTT messages published to espresence/settings/devices. This method gives you more control over parameters like transmission power calibration.

Tips for Reliability and Accuracy

  • Node Placement: Place nodes strategically in rooms you want to track. Avoid placing them behind large metal objects or near microwaves, which can interfere with BLE signals. Corner placements can sometimes improve coverage.
  • Multiple Nodes: For reliable room detection, you need at least two nodes that can 'hear' a device, preferably more. Overlapping coverage is key for the triangulation logic to work effectively.
  • Calibration: Different BLE devices broadcast at different power levels. ESPresense allows calibration per device to improve distance estimates. You can find calibration instructions in the ESPresense documentation.
  • Device Consistency: Be aware that some devices (especially phones) use MAC address randomization. For reliable tracking, a dedicated, non-randomizing BLE tag (like an iBeacon or specific model) is often superior.
  • Power Supply: Ensure your ESP32s have stable power supplies. Brownouts can cause reboots and temporary loss of tracking.
  • MQTT Stability: A stable MQTT broker is crucial as ESPresense relies entirely on it for communication with Home Assistant.

Using Presence Data in Automations

The real power comes from using the best and room attributes of the tracked devices in your automations.

Example Automation: Turn on light when entering a room

automation:
  - alias: Turn on living room light when John enters
    trigger:
      - platform: state
        entity_id: device_tracker.johns_phone
        attribute: room
        to: 'living_room'
    condition:
      - condition: state
        entity_id: light.living_room_light
        state: 'off'
      - condition: sun
        before: sunset # Only trigger after dark
        after: sunrise
    action:
      - service: light.turn_on
        entity_id: light.living_room_light
        data:
          brightness_pct: 80

You can build more complex logic, like turning off lights when a room becomes empty, adjusting media playback based on who is in the room, or triggering alerts if a specific tag (on a valuable item or pet) leaves a defined area.

Best Practices for Managing Your ESPresense Setup

  • Unique Node Names: Always give each ESP32 a unique, descriptive name (e.g., hallway_scanner, kitchen_scanner). This makes identifying their location easy in Home Assistant and MQTT logs.
  • MQTT Topic Structure: Understand the MQTT topics ESPresense uses (default base topic is espresence). This helps with debugging using an MQTT explorer.
  • Keep Firmware Updated: Periodically check the ESPresense GitHub repository or website for firmware updates. Updates often include bug fixes, performance improvements, and support for new BLE devices. The web flasher makes updates relatively painless.
  • Document Device IDs: Keep a list of the MAC addresses or UUIDs of the devices you are tracking and their friendly names. This is invaluable for troubleshooting.
  • Monitor Performance: Keep an eye on the ESP32 uptime and connectivity via Home Assistant device pages or your MQTT explorer. Ensure they stay connected and are reporting data consistently.

Conclusion

Integrating ESPresense with Home Assistant provides a robust and highly flexible solution for achieving precise, room-level presence detection. While it requires a bit more setup than simpler methods, the benefits in terms of automation possibilities are significant. By strategically deploying ESP32 nodes, understanding how to identify and track your BLE devices, and following best practices for placement and configuration, you can build a powerful presence-aware smart home ecosystem that reacts intelligently to your movement within the house.

Avatar picture of NGC 224
Written by:

NGC 224

Author bio:

There are no comments yet
loading...