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

I’ve very excited to give this a go, I do however have a couple of questions.

I currently have my Pi Zero W running DietPi and setup for MiFlora integration, do you envisage any issues installing Presence on DietPi as oppose to Jessie Lite? Also DietPi has its own ‘software library’ where it lists Mosquitto as an option. I’m again hoping this will be work as opposed to installing another version of Mosquitto.

Thoughts?

I haven’t used that distro, so I’m not sure. I would say that this does require full and constant access to the BT hardware, so using with miflora is probably not going to be successful.

Oh that’s a shame. Still with the cost of a Pi Zero W what they are I can afford to have a couple around the house, just wanted to limit the amount of things that were plugged in.

Very like this project, but after using, found that no days, the home network can not be used all, restart the network can, back off zero W, the next few days the network is very normal! It’s very strange!

Yes, Bluetooth interferes with many 2.4GHz networks, especially broadcast from routers that do not have robust interference mitigation. Try the monitor script beta referenced in this thread, I think you’ll find that interference is dramatically reduced.

Yeah, it’s unfortunate. But, that is the nature of a bluetooth sensor - both miflora and presence and monitor need to have access to the BT hardware in order to promptly report changes. They can all coexist, but reporting delays for each would be substantial.

Ok I took the plunge and setup the system on a spare pi 3 until I get hold of another Pi Zero W. All installed fine until I ran sudo service bluetooth status then I receive the follow error:

Aug 07 19:24:16 raspberrypi systemd[1]: Starting Bluetooth service...
Aug 07 19:24:16 raspberrypi bluetoothd[451]: Bluetooth daemon 5.43
Aug 07 19:24:16 raspberrypi systemd[1]: Started Bluetooth service.
Aug 07 19:24:16 raspberrypi bluetoothd[451]: Starting SDP server
Aug 07 19:24:16 raspberrypi bluetoothd[451]: Bluetooth management interface 1.14 initialized
Aug 07 19:24:16 raspberrypi bluetoothd[451]: Failed to obtain handles for "Service Changed" characteristic
Aug 07 19:24:16 raspberrypi bluetoothd[451]: Sap driver initialization failed.
Aug 07 19:24:16 raspberrypi bluetoothd[451]: sap-server: Operation not permitted (1)

Completed the setup and not seeing anything in mqttfx so I’m guessing the bluetooth error is the issue. I didn’t install optional: BLUEZ UPGRADE as mentioned as this came after the status check.

Any ideas how to fix?

UPDATE: I see from another post on here that this error can be ignored, just need to work out why I’m not seeing anything published to MQTTfx.

@apt Hi! Are you running presence or monitor?

Presence. It seems to be working, I don’t think I was giving it long enough to report via MQTT. How often should I see an MQTT payload sent? Is it only when theres a definite change in state?

One of my devices it reporting quite often (every 30 seconds) the other has shown twice and each time it says ‘unknown device’ and then doesn’t show again. rebooted pi and device, still the same.

1 Like

The behavior of the script depends on the command line options at launch. Also, the distance separating your Bluetooth devices from the server running the script. If your second device is much farther away, or has a weaker Bluetooth signal, or has a low battery, the present script may determine that it is away more frequently than other Bluetooth devices in your home.

Or see who visit the missis when you are out :rofl:

I have it running on my DietPi ZW and works great. But that’s the only thing other than mosquitto on it.

quick edit: I’m doing nothing fancy with it. Just gathering two points, my RPi 3B on DietPi and my RPi ZW on DietPi, and making sure both are reading 100% based on the simple example on the GitHub Wiki.

2 Likes

I think I have this working fine now, still need to do some testing.

I’m a little confused with the guest tracking however, if I have to add a MAC address for guest how does this differ from owner?

Functionally, not much difference. Guest scanning is just de-prioritized from owner scanning and guests are either present or not - there’s not a sliding scale of ‘confidence’ for the guests.

Be warned, I’m highly likely to remove guest scanning in an upcoming update. Per a user survey up this thread, not many people use it.

Thanks for the clarification.

1 Like

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