[presence] Reliable, Multi-User, Distributed Bluetooth Occupancy/Presence Detection

For those interested, I’m using Node-Red for my automations within HomeAssistant. Here’s how I’ve done it with a native device_tracker object.

  1. Node Red listens on the MQTT and updates another MQTT topic when Confidence is >= 10.
  2. Home Assistant is configured to use MQTT as a device tracker.

Gist: https://gist.github.com/carpenike/70d42db80c8dc34a5bae5b46eced3f2a

HomeAssistant Config:

platform: mqtt
devices:
  ryan_phone_bt: 'device_tracker/ryan_phone_bt'
  stefanie_phone_bt: 'device_tracker/stefanie_phone_bt'

Couple notes…

  1. I only have one PIZ and it’s been working well. Adding more you’d probably want to have multiple inputs feeding through a similar max/min.
  2. I’ve combined multiple device trackers with a presence drop down:

Using https://philhawthorne.com/making-home-assistants-presence-detection-not-so-binary/ as a guide, i have the following for multiple device_trackers.

platform: template
sensors: 
  ryan_status:
    value_template: '{{ states.input_select.ryan_status_dropdown.state }}'
    friendly_name: 'Ryan'
  stefanie_status:
    value_template: '{{ states.input_select.stefanie_status_dropdown.state }}'
    friendly_name: 'Stefanie'
  ryan_presence:
    friendly_name: 'Ryan Presence'
    value_template: >-
      {%- if is_state('device_tracker.ryan_phone_bt', 'home') %}
        {{ states.device_tracker.ryan_phone_bt.state }}
      {%- elif is_state('device_tracker.ryan_phone_wifi', 'home') %}
        {{ states.device_tracker.ryan_phone_wifi.state }}
      {% else %}
        {{ states.device_tracker.ryan_phone_zanzito_ryan_phone_zanzito.state }} 
      {%- endif %}
2 Likes

Hi! I use this without a problem even if I am a newbi.
I have one question.
I use three PiW. The third is setup at our gate. We have a very big plot, about 8500 sqm so when someone connects to this third one the other two is not in reach. Would it be possible to make an automation using only this third sensor. F.ex starting the lights on the driveway after sun is down. Just an example

Regards

1 Like

Anyone fancy posting their working config for presence detection cos I’m now getting in a bit of a loop.
I’ll post what I have below though basically I believe I have all working correctly using motion until I need to restart the HA.

Once I do this the config seems not to reset & I have to reboot both rasp pi w’s to get the ‘Jaz BT’ to show ‘home’ when we are at home at the time of the HA restart.

Cheers

configuration.yaml

  - platform: mqtt
    state_topic: 'location/owner/garage/C0:A5:XX:XX:60:XX'
    value_template: '{{ value_json.confidence }}'
    unit_of_measurement: '%'
    name: 'Jaz Garage'

  - platform: mqtt
    state_topic: 'location/owner/bedroom/C0:A5:XX:XX:60:XX'
    value_template: '{{ value_json.confidence }}'
    unit_of_measurement: '%'
    name: 'Jaz First Floor'

  - platform: mqtt
    state_topic: 'location/jaz_bt'
    name: 'Jaz BT'
    icon: mdi:bluetooth

  - platform: min_max
    name: "Jaz Home Occupancy Confidence"
    type: mean
    round_digits: 0
    entity_ids:
      - sensor.jaz_garage
      - sensor.jaz_first_floor

device_tracker:
  - platform: mqtt
    devices:
      jazs_8: 'location/jaz_bt'

automation.yaml

   - alias: Jaz Home
     hide_entity: true
     trigger:
      - platform: numeric_state
        entity_id: sensor.jaz_home_occupancy_confidence
        above: 10
     action:
      - service: homeassistant.turn_on
        data:
          entity_id: input_boolean.jaz_occupancy
      - service: mqtt.publish
        data:
          topic: 'location/jaz_bt'
          payload: 'home'

   - alias: jaz Away
     hide_entity: true
     trigger:
      - platform: numeric_state
        entity_id: sensor.jaz_home_occupancy_confidence
        below: 10
     action:
      - service: homeassistant.turn_off
        data:
          entity_id: input_boolean.jaz_occupancy
      - service: mqtt.publish
        data:
          topic: 'location/jaz_bt'
          payload: 'not_home'

   - alias: hass_startup_jaz_set_occupancy
     hide_entity: true
     trigger:
      - platform: homeassistant
        event: start
     condition:
      condition: and
      conditions:
        - condition: numeric_state
          entity_id: sensor.jaz_home_occupancy_confidence
          above: 10
     action:
       - service: input_boolean.turn_on
         entity_id: input_boolean.jaz_occupancy
1 Like

Hi @LarsAC
Giving a try of the presence script on an intel NUC (ubuntu VM)
I have installed all the packages in the presence.sh docs but I get an error

Scan result: [Invalid device: No such device]

I’m struggling with bluetooth configuration, but no results…

Did you succeed setting up the bluetooth on the NUC?

Why BLT has to work on the NUC? Or you plan to use that as a sensor?

Solved, bluetooth interface working on the NUC, just had to share it to the VM.

I will use the integrated bluetooth interface with the presence script to detect the BT devices. If coverage is limited I’ll add a rpi zero w as suggested by the author.

1 Like

Glad you got it working!

1 Like

Now I’m experimenting with the reliability of the bluetooth detector, and trying to integrate with my existing home/away automations. Will report results soon :sunglasses:

1 Like

Great work on this :slight_smile:
I have a android mobile phone that this woks very well for, but I also have a Nut BTLE tag that this doesn’t work at all for as it always comes up with confidence 0, does anyone have any suggestions or is it not supposed to work with tags?

presence/owner/keith/xx:xx:xx:1B:6D:28 { version : 0.1.481, confidence : 0, name : Nut, timestamp : Wed Aug 15 2018 11:40:58 GMT+0100 (BST), manufacturer : Unknown , type : Known Static MAC ,“type”:“Known Static MAC” }

BTW I also have some MiFlora plant monitors that come up 100% and some LED BTLE lights that come up 100%

I have the same problem with a nut mini. Always 0.

The device should report as a beacon. Enable the -b flag, and remove the MAC address from known_static_addresses. You’ll see the beacon address in the logs.

I see this in the logs for my Nut…

0.1.481 07:52:53 pm [CMD-RAND] xx:xx:xx:xx:6D:28 ADV_IND RAND_NUM: 2 (x10)

Is the beacon flag enabled? If so, you’ll also see the iBeacon advertisements.

The above is what I see in monitor with the -b option, also this…

0.1.481 10:59:36 pm onds PUBL_NUM: 3
0.1.481 10:59:40 pm [CMD-RAND] xx:xx:xx:xx:6D:28 ADV_IND RAND_NUM: 3
0.1.481 10:59:40 pm onds PUBL_NUM: 3
0.1.481 10:59:43 pm [CMD-RAND] xx:xx:xx:xx:6D:28 ADV_IND RAND_NUM: 3
0.1.481 10:59:43 pm onds PUBL_NUM: 3
0.1.481 10:59:46 pm [CMD-RAND] xx:xx:xx:xx:6D:28 ADV_IND RAND_NUM: 3
0.1.481 10:59:46 pm onds PUBL_NUM: 3
0.1.481 10:59:50 pm [CMD-RAND] xx:xx:xx:xx:6D:28 ADV_IND RAND_NUM: 3
0.1.481 10:59:50 pm onds PUBL_NUM: 3
0.1.481 10:59:53 pm [CMD-RAND] xx:xx:xx:xx:6D:28 ADV_IND RAND_NUM: 3
0.1.481 10:59:53 pm onds PUBL_NUM: 3
0.1.481 10:59:56 pm [CMD-RAND] xx:xx:xx:xx:6D:28 ADV_IND RAND_NUM: 3
0.1.481 10:59:56 pm onds PUBL_NUM: 3
0.1.481 10:59:59 pm [CMD-RAND] xx:xx:xx:xx:6D:28 ADV_IND RAND_NUM: 3
0.1.481 11:00:00 pm onds PUBL_NUM: 3

This likely means that the Nut BTLE tag is not conforming to the iBeacon specification. I’ll see if I can add support for devices like this.

1 Like

Andrew, i’m running precense on one PI Zero right now, with plans on using 6 to cover house plus garage. But with just one running, my wife complained that her BT keyboard was acting strange and those problems dissapeared when I shutdown the Pi.

To make things worse, her office is just next to the entry door to the house, so cant avoid covering that area for precense detection.

How would you solve that issue if you had the same prob?

1 Like

Hello @andrewjfreyer,

New fan of your script thanks to @PianSom.

Please a quick question:

Is it possible for one to estimate the distance of the Bluetooth presence system, from the Bluetooth device based on the confidence?

If it is, could you kindly share some data on your finding? I know it depends on several things like walls in between and type of Bluetooth device, but I just need any little data you might have worked out already if you had considered it before.

Thanks and regards

Hey @Odianosen25 - I’m sure you don’'t want to read what is nearly 500 posts now, but Andrew did mention up here that he has not had very much luck at all with this.

Hey @andrewjfreyer (and any Appdaemon users) - if you haven’t been watching it, you might like to have a look over in the Appdaemon section here at some wonderful coding that @Odianosen25 has just put together for monitor

1 Like

Oh thanks bro, yeah you right, wasn’t really in the mode of reading via the entire threads :smile:.

I was looking at doing some triangulation, but even at that, i think it I will still look at it later to I could at least allow for possible location of which floor a device is.

Like interfacing it with Snips and asking “where is my phone?”. It could give a possible location based on the different readings, even if its not so accurate.

Well a project for another time.

Many thanks again @PianSom, much appreciated :facepunch:t5:

1 Like

I’d make the switch to the monitor script. With low interval values, the presence script has the tendency to interfere with 2.4 GHz devices, including Bluetooth and Wi-Fi.