Best Implementation Strategy for Bayesian Binary Sensor

Cool - looking forward to seeing this in Home Assistant!

THis is super cool…love it. I am just thinking that presence detection could really benefit from this sensor. Instead of relying only on phone or router based device_trackers, we can use Bayesian sensor to include other sensors to provide posterior probability of occupancy.

Looking forward to playing with this.

My PR finally got merged! Should be in the 0.53 release!

In the mean time, consider installing the dev branch to give it a try:

$ pip3 install --upgrade git+git://github.com/home-assistant/home-assistant.git@dev
3 Likes

This is really cool and I love the idea. The complicated thing is actually developing probability for events so that one can supply good values for the bayesian binary sensor. At the moment it seems that selecting the values seems to be more about good guessing, which I presume provides a relatively accurate solution, but doesn’t account for changing patterns.

@jlmcgehee21 would you be willing to provide your method for settings these values?

1 Like

@jlmcgehee21 thanks for that sensor! Like @keatontaylor I’m really curious about how to “professionally guess” these probabilities as well.
Would you mind explaining it in the documentation in some more detail?

Hey all, check out this post (including my response in the Github issue) and let me know if you still have more questions: Bayesian binary sensor does not seem to work

1 Like

In addition to thinking about probabilities in the way I described in the previous answer, it should be fairly straightforward to actually pull your history data and process it via pandas or a similar tool in Jupyter Notebooks to determine the true probabilities. If I ever have time or a need to do this myself, I will document the procedure and be sure to share with everyone.

1 Like

Bayesian sensor may be difficult to understand for people who do not have a statistics background. Here’s an example of sensor that I am using for presence detection:

  - platform: 'bayesian'
    prior: 0.75
    name: 'Rashmi Home'
    probability_threshold: 0.95
    observations:
      - entity_id: 'device_tracker.pi_rashmiphone'
        prob_given_true: 0.8
        prob_given_false: 0.2
        platform: 'state'
        to_state: 'home'
      - entity_id: 'device_tracker.rashmiappiphone'
        prob_given_true: 0.99
        prob_given_false: 0.10
        platform: 'state'
        to_state: 'home'
      - entity_id: 'device_tracker.rashmisiphone'
        prob_given_true: 0.99
        prob_given_false: 0.4
        platform: 'state'
        to_state: 'home'
      - entity_id: 'device_tracker.sonu_sonu'
        prob_given_true: 1
        platform: 'state'
        to_state: 'home'

The way it works is as follows. The information that I am interested is whether Rashmi is home or not (which is unobserved). The Bayesian sensor allows me to infer the probability of whether Rashmi is home or not using the four trackers (that have varying degrees of accuracy). For example, device_tracker.sonu_sonu is quite accurate, so I can infer with probability of 1 if device_tracker.sonu_sonu is home. On the other hand, device_tracker.rashmisiphone is less accurate. Hence, even when device_tracker.rashmisiphone is not_home, I have assigned a 40% probability that she is home. You can use the probability values according to the accuracy of these individual sensors.

Bayes’ rule allows you to combine the probabilities of these individual sensors to obtain the probability of the underlying sensor (rashmi_home, in this case). Finally, the binary_sensor.rashmi_home will be assigned on if the probability is greater than the probability_threshold otherwise it will be off.

Hope this helps.

13 Likes

@jlmcgehee21 Now that you have multiple observations of the same entity merged, here are couple of things that would be really useful.

  1. Allowing for support. For instance, in the above example, if the device_tracker.rashmisiphone is not_home for 10 minutes, the probability that she is not home is quite high, so I would like to reduce prob_given_false to a lower value.
  2. Allowing device_class support.
2 Likes

There’s been a lot of added comments now, but with respect to “good guessing … doesn’t account for changing patterns”. This is true in a sense, but becomes less true with the more observations you add to the sensor. Consider adding as many sensors as you can that may influence the likelihood of the event you desire to track (sun, weather, etc.). Even if an observation only influences the probability by a small amount, multiple observations can stack up to create a very robust sensor.

1 Like

@arsaboo, I know this may be cumbersome, but you could create a separate sensor to track the time, then add that as an observation to the Bayesian Binary sensor. I don’t want to go about re-inventing the automation platform and all the other sensor options, but I do want to make sure this provides necessary functionality. Hopefully a good balance can be found.

1 Like

@jlmcgehee21 Any chance you can add value_template support to the Bayesian sensor? That will be super helpful.

I have started on the analysis in this thread. Developing a few tools to put the HA data into a pandas data frame.

I just had a kid in October, am working full time, and have two exams in the next 3 weeks for my online MS in CS from GaTech. Maybe I’ll get around to it at the end of the semester :wink: .

2 Likes

Good luck with everything. Are you in ATL too?

Not too far away @arsaboo, I’m in Chattanooga, TN. I make it down to ATL for MLConf and a few other things from time to time.

Could you share the platforms and the associated accuracy? Would be a nice starting point. Tx.

Everything is available in my repo. Let me know if you have any questions.

1 Like

Hi, just wondering if you ever got around or are planning to to add a value_template sensor? I’ve been experimenting with this sensor for the past few days and think it shows promise especially for presence, but I notice a lot of limitations unless I’m approaching it all wrong. Most of these can be circumvented by creating separate sensors but it would be nice to support some common use cases imo and not make things too messy (it quickly adds up).

I’ve been looking at your code to add it myself, but I’m unsure how to properly listen to events for the entities used within a template. I assume that supporting templates itself doesn’t require any coding, that in essence you just create invisible entities?

A few other ideas I had:

  • set a deactivation threshold. I imagine it as a sort of potential barrier (I’m a physicist sorry :P), once a sensor is triggered it is harder to ‘de-trigger’. I’ve tested a sort of recursive pattern where I use the on state of the bayesian sensor itself as one of the sensors, but that seems like a hack to me (it does work though). It’s easy enough to implement, but I’m unsure if it makes sense since I’m not too familiar with the Bayesian approach.
  • scaling probabilities. Say I have a time sensor, it gives me the time since a motion sensor was last triggered. To me it makes sense that the probability scales with some function of that time. It would be nice if we could provide an upper and lower limit and a ramping function (for instance: linear, quadratic, logit). So instead of the hard border for a numeric state more of a fuzzy border. Probably makes most sense for ‘negative’ sensors.
  • A ‘for’ field that specifies an amount of time a certain state is true. A simpler version of the template, and mentioned in this thread. I am unsure how to best implement this. Can you just start a timer on a state change to a callback like in appdaemon and cancel that callback in case the state changes back again?

Let me know what you think. I haven’t supplied any code or HASS so far, but I assume I would just fork the github and supply a pull-request? In general I assume it will be harder though to change an existing platform, is there some process I would have to go through?