esphome icon indicating copy to clipboard operation
esphome copied to clipboard

Added support for MQTT Room Presence

Open wjtje opened this issue 3 years ago • 6 comments

What does this implement/fix?

This implements a new component that track BLE devices and send the distance using MQTT to Home Assistant for the MQTT Room Presence sensor.

Types of changes

  • [ ] Bugfix (non-breaking change which fixes an issue)
  • [x] New feature (non-breaking change which adds functionality)
  • [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • [ ] Other

Related issue or feature (if applicable): https://github.com/esphome/feature-requests/issues/1412

Pull request in esphome-docs with documentation (if applicable): esphome/esphome-docs#2257

Test Environment

  • [x] ESP32
  • [ ] ESP32 IDF
  • [ ] ESP8266

Example entry for config.yaml:

# Example config.yaml
esp32_ble_tracker:
  scan_parameters:
    duration: 30s

sensor:
  - platform: ble_rssi
    ibeacon_uuid: '11223344-5566-7788-99aa-bbccddeeff00'
    id: my_own_tracker

mqtt_room:
  room: Living Room
  trackers:
    - device_id: my_own_tracker
      name: My own tracker

Checklist:

  • [x] The code change is tested and works locally.
  • [x] Tests have been added to verify that the new code works (under tests/ folder).

If user exposed functionality or configuration variables are added/changed:

wjtje avatar Sep 01 '22 14:09 wjtje

I also wouldn't close the linked issue when this is merged, the big draw of the ESPresense project is 1. filtering so that distance isn't extremely noisy and 2. fingerprinting apple devices that don't have set uuids. This is a great PR but it fixes a different, though related, problem.

rpatel3001 avatar Sep 07 '22 20:09 rpatel3001

I'm currently working on getting the fingerprinting working and tx power. But I won't be able to test the fingerprinting for Apple devices because I don't own any.

wjtje avatar Sep 09 '22 09:09 wjtje

I have changed how trackers are detected so there are some changes in the config.yaml

# Example config.yaml
esp32_ble_tracker:
  scan_parameters:
    duration: 30s

mqtt_room:
  room: Living Room

wjtje avatar Sep 09 '22 15:09 wjtje

I'm currently working on getting the fingerprinting working and tx power. But I won't be able to test the fingerprinting for Apple devices because I don't own any.

Awesome! Is this sensor compatible with the ESPHome built in sensor filtering capabilities? If so, that would probably suffice, instead of adding the one euro filter or doing the median in this component.

rpatel3001 avatar Sep 09 '22 20:09 rpatel3001

I'm currently working on getting the fingerprinting working and tx power. But I won't be able to test the fingerprinting for Apple devices because I don't own any.

Awesome! Is this sensor compatible with the ESPHome built in sensor filtering capabilities? If so, that would probably suffice, instead of adding the one euro filter or doing the median in this component.

Currently not, but that is a thing I want to implement soon.

wjtje avatar Sep 09 '22 20:09 wjtje

I have added support for sensor filters and custom signal power. The filters will also be used in the calculation that is send using MQTT Room Presence.

mqtt_room:
  room: Living Room
  state_topic: esphome/rooms # Optional
  trackers:
    - device_id: ibeacon:11223344-5566-7788-99aa-bbccddeeff00-1-100
      name: Personal Tracker # Optional
      signal_power: -72 # Optional
      rssi: # Optional
        name: Tracker RSSI
      distance: # Optional
        name: Distance RSSI

wjtje avatar Sep 10 '22 17:09 wjtje

Coincidentally I’m working on something similar here https://github.com/mvgijssel/setup/pull/128, where I’m creating a ble distance component based on ESPresence logic.

The default Bluetooth stack of ESPHome (Bluedroid) which is used by esp32_ble_tracker currently does not support tracking Apple devices using irks, therefore I’m using the NimBLE stack instead.

If we manage to get NimBLE support in ESPHome then integration with this component means all the same functionality related to device tracking from ESPresence would be available to everyone!

mvgijssel avatar Nov 06 '22 11:11 mvgijssel

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days. Thank you for your contributions.

github-actions[bot] avatar Feb 05 '23 00:02 github-actions[bot]

@wjtje @jesserockz what's needed to get this merged? There was some good work done that would be nice to have in esphome.

rpatel3001 avatar Feb 05 '23 00:02 rpatel3001

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days. Thank you for your contributions.

github-actions[bot] avatar May 07 '23 00:05 github-actions[bot]

@wjtje @jesserockz bumping again

rpatel3001 avatar May 07 '23 01:05 rpatel3001

@mvgijssel may have some input here as well since they were working on something similar.

Would love to have this integrated into esphome.

MRobi1 avatar May 07 '23 01:05 MRobi1

This seems to be crashing a lot here:

[16:12:51][D][mqtt_room:096]: device '11:22:33:44:55:66': fetching manufacturer data
[16:12:51][D][mqtt_room:125]: Found device with id: 'ibeacon:92A41396-87FA-A499-214E-EAD9B75C7650-64052-27623'
[16:12:51]
[16:12:51]Stack smashing protect failure!
[16:12:51]
[16:12:51]
[16:12:51]abort() was called at PC 0x400f9c5b on core 1
[16:12:51]
[16:12:51]
[16:12:51]Backtrace:0x40083c59:0x3ffcd4b00x40093529:0x3ffcd4d0 0x40098aed:0x3ffcd4f0 0x400f9c5b:0x3ffcd570 0x400e2f45:0x3ffcd590 0x400e2f81:0x3ffcd660 0x400d9de5:0x3ffcd680 0x401fd8ad:0x3ffcd790 0x401fd961:0x3ffcd7b0 0x400e7d96:0x3ffcd7d0 0x400ea90a:0x3ffcd800 0x400f0f52:0x3ffcd8e0 
WARNING Found stack trace! Trying to decode it
WARNING Decoded 0x40083c59: panic_abort at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_system/panic.c:402
WARNING Decoded 0x40093529: esp_system_abort at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_system/esp_system.c:128
WARNING Decoded 0x40098aed: abort at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/newlib/abort.c:46
WARNING Decoded 0x400f9c5b: __stack_chk_fail at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_system/stack_check.c:36
WARNING Decoded 0x400e2f45: esphome::mqtt_room::MqttRoom::parse_device(esphome::esp32_ble_tracker::ESPBTDevice const&) at /config/.esphome/build/m5atom-desktop/src/esphome/components/mqtt_room/mqtt_room.cpp:138
WARNING Decoded 0x400e2f81: non-virtual thunk to esphome::mqtt_room::MqttRoom::parse_device(esphome::esp32_ble_tracker::ESPBTDevice const&)
WARNING Decoded 0x400d9de5: esphome::esp32_ble_tracker::ESP32BLETracker::loop() at /config/.esphome/build/m5atom-desktop/src/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp:127 (discriminator 2)
WARNING Decoded 0x401fd8ad: esphome::Component::call_loop() at /config/.esphome/build/m5atom-desktop/src/esphome/core/component.cpp:76
WARNING Decoded 0x401fd961: esphome::Component::call() at /config/.esphome/build/m5atom-desktop/src/esphome/core/component.cpp:98
WARNING Decoded 0x400e7d96: esphome::Application::setup() at /config/.esphome/build/m5atom-desktop/src/esphome/core/application.cpp:52 (discriminator 2)
WARNING Decoded 0x400ea90a: setup() at /config/.esphome/build/m5atom-desktop/src/main.cpp:393
WARNING Decoded 0x400f0f52: loopTask(void*) at /config/.esphome/platformio/packages/framework-arduinoespressif32/cores/esp32/main.cpp:42

Sometimes it finds a few more devices, but crashes in the end. Any suggestions how to debug this?

vrutkovs avatar Jun 21 '23 16:06 vrutkovs

I have changed to format device name function, hopefully this is better. It seemed like I allocated to much room for the new string.

wjtje avatar Jun 21 '23 19:06 wjtje

I don't think it was device name, I still get crashes. Checking if some device is non-compliant with the spec (?) and requires bigger buffers.

UPD: bumping buffer for ibeacon: string to 128 chars seems to help

vrutkovs avatar Jun 22 '23 10:06 vrutkovs

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days. Thank you for your contributions.

github-actions[bot] avatar Sep 21 '23 00:09 github-actions[bot]

Still current.

codyc1515 avatar Sep 29 '23 00:09 codyc1515