Using ESPHome + bluetooth detection as an "alarm"

I have an ESP32 device which I would like to use as a ‘people detection’ mechanism, but unlike ‘presence detection’ where you know the unique BLE signature of an individual device, I want to take action on ANY BLE signature.

Let me lay out the scenario. I have a cottage that is ‘remote’. It is unlikely that any bluetooth devices will be near the cottage if no one from my family is there. I would like to use the ESP32 / ESPHome to detect ANY bluetooth signature, and raise an alert. Bonus points, if it can be aware of known devices and do something different.

Another scenario I think might be useful for others: Sure you might have 100’s of bluetooth devices in range in your neighbourhood - but it would be weird to see a brand new signature arrive in the middle of the night? (well, maybe if you have a new paper delivery person?) Still, it may be interesting to get alerted if odd bluetooth discovery events occur. This alert might cause your external video cameras to record, or lighting to turn on.

[Yes, I did search - but maybe I don’t know the right search terms? Pointers to similar problem solutions are welcomed]

I don’t know if this is a good approach, but you could set the logger level to (say) VERY_VERBOSE and then do some stuff with the device details using on_message, such as publish the details to a text sensor. Maybe you can add conditions for known devices if you can parse the mac etc…

Maybe you can do similar stuff with on_ble_advertise?

I use an automation I found on here to notify when a new device_tracker is “created” by the unifi network integration, maybe you can adapt this to the BLE integration ?

- alias: New network device registered
  id: New device registered
  mode: queued
  trigger:
    platform: event
    event_type: entity_registry_updated
    event_data:
      action: create
  condition:
    condition: and
    conditions:
      - condition: template
        value_template: "{{ trigger.event.data.entity_id.split('.')[0] == 'device_tracker' }}"
      - condition: template
        value_template: "{{ not ((trigger.event.data.entity_id.split('.')[1].startswith('unifi_') and trigger.event.data.entity_id.split('.')[1].endswith('default')) or (trigger.event.data.entity_id.split('.')[1].startswith('unifi_default'))) }}"
  action:
    - delay: "00:01:00"
    - variables:
        entity: >
          {{trigger.event.data.entity_id}}
    - service: system_log.write
      data:
        message: >
          New device registered: {{state_attr(entity,'friendly_name')}} - {{state_attr(entity,'entity_id')}}
        level: info
        logger: homeassistant.components.device_tracker
    - service: persistent_notification.create
      data:
        title: >
          New device registered: {{state_attr(entity,'friendly_name')}}
        message: >
          Entity: {{entity}}
          Host: {{state_attr(entity,'host_name')}}
          IP: {{state_attr(entity,'ip')}}
          MAC: {{state_attr(entity,'mac')}}

I dont think changing it to very_verbose will make a difference or help here. Changing the log level doesnt effect the BT scan. Very_verbose just queries devices found in a scan for more information about the devices it finds. What he wants can simply be achieved by creating an automation that does a BT scan on a time interval and if a device is found in range, that triggers some automation that notifies him someone is in the area. I suspect some filtering will be needed incase he has BT devices being found and maybe a notification is only sent if the same device is found in consecutive scans.

I know mine picks up random cars that drive down my street. Id be getting alerts all damn day if i didnt filter stuff like that.

Thank you for the replies so far. Very helpful starting points.

Well, in my particular ‘cottage’ use case - the road is quite far from where the sensor would be, I doubt bluetooth has that much range. If someone drives down my long driveway, I would want to know. So maybe your ‘annoying’ drive by scenario is actually exactly what I want. I’m not suggesting this alarm me and wake me up - but a notification that the event happened may be interesting (or it could turn on a light, or a camera)

Thanks also @Holdestmade for your example automation - I’m still learning about automations (this speaks to the strength of HA being great even without a lot of automations… I just need to soak in the docs for a bit to get caught up). Having a motivating task is the best way to learn stuff for me.

Possibly not. I couldn’t recall if some device types like RANDOM and unsupported ones only show up with more verbose logging. Having a log of the device details could be useful for tracking repeat lurkers. Plus you could potentially do some crude proximity tracking with the rssi.

Which particular component(s) are you suggesting are the best fit here? The ble tracker scans continuously by default so you don’t necessarily need to manage the scan intervals yourself (especially if you are prioritising low latency detection).

Yes, you’re right it does continuously scan, i was just pointing out that the scans can be controlled. The documentation gives an example how to set up notifications if a device is discovered. That sounds like exactly what hes after. If a device is found during a scan log it or send a notification or just log it anc send a notification if a device appears for a significant time indicating its more than someone who went down the wrong road and it requires action.

This makes me think of the paxcounter project

You don’t need the LoRa part but the counting part could be interesting in your case. At least to get some inspiration.

Now, smartphones advertise a random address unless you have the key to decode it; you will see new addresses popping up very frequently.

I think it is an exciting approach for a remote location with no traffic. Be careful of your Bluetooth devices with random MAC in the cottage.
MQTT explorer can be interesting in this kind of analysis as it persists the discovered MAC address while turned ON.

1 Like

I use monitor for presence detection. I use this because AFAIK esp can’t resolve random macs. It runs on a pi zero w. In verbose mode, it will publish all macs to the log.

The way it is set up though, it doesn’t publish this info to mqtt. Which is how it communicates with HA. IDK but you may be able to use a wild card for the known address file.

I am also looking for something like this. Could be useful to be alerted when an Axon (police body-cam) signal is nearby or could automate home cameras to start recording. Would only need to have a condition to the automation so only the MAC OUI prefix of these devices which are 00-58-28 and 00-C0-D4 are detected.

Is this picked up from an automation you have? how is this setup?

The good thing about monitor is it can do regular bluetooth too and not just ble (esp32 limit).

Not sure how many lurkers would have just ble advertising (maybe on their phone?). More likely to catch them with Bluetooth I’d have thought?

Couldn’t recall if it was easy with monitor to push alerts for any detected device. It’s a pretty crazy script.

I think the ideal solution would be a combination on sensors. BT alone would most likely be hit and miss but BT + PIR/motion or other presence detection methods would probably be better.

I think I found something that can do this. This article combines Kismet with Home Assistant and looks like that software works for both wifi and bluetooth monitoring/sniffing.

Thats prett interesting. Thanks for sharing that.
Id be curious if you try it and how it does.

Yea, went through setting it up and all that, got kismet running on a NUC and works beautifully however, it appears the project has been abandoned or no updates since roughly 2019 because of Updated API issues Kismet wifi monitor device_tracker component - Share your Projects! / Custom Integrations - Home Assistant Community (home-assistant.io)
I am still interested in getting this revived, im not very good with python but willing to learn or hoping I can find someone good with python willing to help.

Hi all,

I’ve managed to get the Kismet added as a sensor to HA to get what I want without the python code. Just modified the device alert settings on the Kismet from kismet_alerts.conf, here can specify MAC or OUI of devices with “devicefound” alert. Then added the following to configuration.yaml in HA.

sensor:

  • platform: rest
    json_attributes:
    • kismet.alert.header
    • kismet.alert.source_mac
      method: GET
      name: “Kismet_Sensor”
      resource_template: “http://[kismet_address]:[port]/alerts/all_alerts.json”
      username: [censored]
      password: [censored]
      authentication: basic
      scan_interval: 60
      value_template: ‘{{ value_json[0][“kismet.alert.header”],value_json[0][“kismet.alert.source_mac”] }}’

Now I can create HA automations to my liking using the sensor and json attribute changes. Thanks all for your guidance along this journey!