Presence Detection Part 1: Home Assistant & Bayesian Probability

ConfigHome AssistantSmart HomeUseful Sensor

Written by:

One of the most useful things to track for home automation is whether anyone is home or not. If you want the lights to turn off when no one is home, the vacuum robot to run when you’re at work, or the heat to come on before you arrive home on a cold night you need to reliably be able to tell if the house is occupied.

How can we track the state of something that is not directly observable? We can’t plug ourselves directly into the internet (…yet). After trying several approaches to monitoring presence, I’ve come up with a method that is very near 100% reliable.

STEP ONE:  Multiple Device Trackers

Take a look through the list of device tracking platforms supported by Home Assistant, there are quite a few. In my experience, no one platform will always be correct. What works best is to add multiple trackers for each person, and then use the Bayesian sensor to look at all of them and determine if the person being tracked is home or not.

I am tracking myself using:

  • ping – My phone has a static IP on my wifi, so a simple ping tells whether I’m connected or not
  • Owntracks – This has been the most reliable GPS platform for me. It also supports bluetooth iBeacons.
  • Hass iOS – The Home Assistant iOS app does location tracking as well as iBeacons.
  • Tile – The Tile app tracks my phone and a beacon attached to my keys.
  • Node-Red – A custom GPS tracker using Node-Red, see blog post

Multiple Trackers

I do recommend adding some cheap iBeacons to your smart home hardware to increase the reliability of presence detection. An iBeacon is a simple Bluetooth transmitter that broadcasts it’s name to the world. When the Owntracks or Home Assistant apps see the beacon, it forces an update. This ensures your comings and goings are detected immediately.

STEP TWO: Bayesian Binary Sensor

The Bayesian sensor is really cool, even if I don’t fully understand the math behind it. It allows us to guess whether something is true or not based on multiple observations. In this case, we are going to guess the probability of whether I am home based on the states of multiple device trackers in Home Assistant. My YAML looks like this:

- platform: bayesian
  prior: 0.5
  name: 'Brad Presence'
  probability_threshold: 0.9
    - entity_id: 'device_tracker.brad_ping'
      prob_given_true: 0.9
      prob_given_false: 0.1
      platform: 'state'
      to_state: 'home'
    - entity_id: 'device_tracker.brad_ios'
      prob_given_true: 0.9
      prob_given_false: 0.2
      platform: 'state'
      to_state: 'home'
    - entity_id: 'device_tracker.brad_owntracks'
      prob_given_true: 0.9
      prob_given_false: 0.4
      platform: 'state'
      to_state: 'home'
    - entity_id: 'device_tracker.brad_nodered'
      prob_given_true: 0.8
      prob_given_false: 0.2
      platform: 'state'
      to_state: 'home'
    - entity_id: 'device_tracker.tile_keys'
      prob_given_true: 0.8
      prob_given_false: 0.4
      platform: 'state'
      to_state: 'home'

RELATED >>  Useful Sensor: Cardinal Wind Direction for Home Assistant

Let’s break it down:

  • “prior” Indicates the probability that this is true in a day – let’s say I’m home half the time, or 0.5
  • “probability_threshold” How sure should it be to guess that I’m home? By setting the bar high at 90%, we are requiring more than one platform to be ‘home’ before it will flip to on.

Now we list our observations for each device_tracker entity:

  • “prob_given_true” If I’m home, how likely is this state true? In other words, how reliable is that platform at marking me home?
  • “prob_given_false” How likely is this platform to be wrong – for it to be ‘home’ when I’m not home?

I have to be physically present to be on the wifi for the ‘ping’ platform, so that one is the highest probability. Owntracks and the iOS app are pretty accurate, but also occasionally wrong, so we’ll weight their prob_given_false higher.

This may require some tinkering with the numbers in response to your individual setup, but the goal is to trigger the Bayesian to ‘on’ when more than one platform is ‘home’.

Observations and the calculated probability


Let’s convert this bayesian_sensor into a device_tracker in my next post, and create a new and better device_tracker to use as an additional data source.

Improving Presence Detection with Node-Red

6 Replies to “Presence Detection Part 1: Home Assistant & Bayesian Probability”

  1. […] my previous post about presence detection, I showed how you can combine multiple device trackers into one highly accurate Bayesian […]

  2. Fred says:

    How has your this impacted battery life. Having so many location apps seems like it would be likely to drain quickly?

    • brad says:

      Haven’t really noticed a major effect on battery performance, I’m on a iPhone 7s. I have Owntracks set to only make updates if it sees an iBeacon or detects significant movement. The Tile app & Home Assistant apps aren’t updating all that often and I would be using them anyways.

      I think using iBeacons helps, it forces updates when you enter/exit a zone and then you can turn all these apps to not update that often if they have an option for it.

  3. Sandro says:

    Could you please add more on how do you track with the Tile bluetooth key finder?
    I have one of those and I’ll like to make it working with Home Assistant.

  4. […] to do on the HASS presence detection front. I’d specifically like to try a combination of Bayesian Sensors and the “Not so Binary” […]

Leave a Reply

Your email address will not be published. Required fields are marked *