Step-by-step presence detection with bluetooth tag (e.g., GTags) and ESPresense on ESP32

Since this MQTT, subscribing and parsing topic, while it refers to ESPresense (https://espresense.com/, GitHub: ESPresense · GitHub), does not give enough detail for beginners to get this up and running easily and the ESPresense website focuses on HomeAssistant config, I’ll try to leave a few notes here that may help other beginners to get up and running if the aim is to get presence detection with bluetooth tags (like the GTags, other bluetooth devices, including mobiles, for which ESPresense has some fingerprinting features apparently, to address random MAC changes, should work similarly).

NOTE: I’m an OpenHab/MQTT newbie, so there may be way more elegant ways to do this (implementing the below requires quite a bit of clicking around). Any advice most welcome!

  • Step 1: get ESP32 or similar with ESPresense firmware up and running by following the instructions given here: Install – ESPresense – ESP32 based indoor positioning system
  • Step 2: install Mosquitto MQTT broker on OpenHab server (Raspi in my case), by selecting Optional Components/Mosquitto in openhabian-config; set adequate password and make note of this; install MQTT binding and JSONPATH transformation add on in OpenHab unless already present
  • Step 3: Set your ESPresense ESP32 to target your mosquitto by setting IP to OpenHab server IP, user ID to openhabian and password to the Mosquitto password you set via the ESPresense web UI
  • Step 4: check MQTT message stream on your Moquitto by running mosquitto_sub -v -u openhabian -P YOURMOSQUITTOPASSWORD -h localhost -p 1883 -t '#' on your OpenHab server; you should see a stream of espresence/# related MQTT messages on your console
  • Step 5: create MQTT broker thing in OpenHab
  • Step 6: Use autodetection for HomeAssistant things (should show up in your thing inbox) to get a first representation of ESPresense ESP32, which gives status, firmware, etc.
  • Step 7: create one generic MQTT thing using your MQTT broker configured in step 5 per ESP32 ESPresense device; create equipment from this thing in the appropriate room in your semantic model; you can group the other stuff there in the following steps
  • Step 7a: create status channel on this thing as ON/OFF switch type channel, with MQTT State topic “espresense/rooms/[ESP 32 ID]/status”; create status item from this channel; this seems to respond more fluidly to status changes of the ESP32 than the autodiscovered status from Step 6
  • Step 8: create one string typed MQTT “JSON complete” channel per ESP32 per bluetooth device you would like to detect, with state topic “espresense/devices/[bluetooth tag MAC]/[ESP32 ID]” as per the MQTT stream from step 4 (you can to identify your bluetooth tag MACs from the MQTT message stream by moving them around a little and looking at distance changes or whatever)
  • Step 9: from the “JSON complete” channel context, by using “create points”, create one number item per data item per tag per ESP32 that you want to use in OpenHab (I use distance, rssi, speed for each GTag on each ESP32)
  • Step 10: configure the “JSONPATH” profile for each item/channel link, inserting JSONPATH expression as appropriate, i.e., “$.distance”, “$.rssi”, “$.speed”, without quotes, of course).
  • Step 11: add metadata to item, i.e. for units (state description “%.1f m” to add meters unit to distance, “%.1f dB” for RSSI, etc.) and Expiration Timer to make sure you notice when the tag is gone (I currently use 10 s expiration).
  • Step 12: make some rules to fuse you various ESP32 distance values etc. into something you can build further logic on.

Optional/additional GTag detection using Raspi builtin Bluetooth: In addition to using the ESP32s for detecting the GTags, one can use the OpenHab Bluetooth binding and the BlueZ stack on the Raspi (if that’s what your OpenHab is running on) to cover the vicinity of the Raspi using the Raspi builtin Bluetooth hardware. After successful installation/configuration of the Bluetooth binding, just make sure to add the autodiscovered things of type “beacon”, not “generic” (sometimes takes a few tries), which then give you an RSSI channel, and add an expiration timer to the RSSI items build from the “beacon” things. Works like a charm for GTags, no need for non-standard bluetooth binding in the current version of OpenHab to get this to work, from my experience. Useful in our case so we don’t need an additional ESP32 in the basement.

7 Likes