Best Implementation Strategy for Bayesian Binary Sensor

Hello all,

I am currently starting the process of building a “Bayesian Binary Sensor” which will allow an intelligent “sensor fusion” of many other sensors within hass. This will allow the user to sense much more complex events than he/she can with a single sensor or simple template-based deterministic state machines combining sensors.

Example use case:
I would like to activate a specific scene automatically when I am in bed, though I cannot sense directly when I am in bed. However, I know that if it is after 10pm and before 7am there is a 70% chance I am in bed, if the lights are on in the bedroom, there is a 10% chance I am in bed, etc. So, knowing my habits, I can configure the various probabilities and a probability threshold for triggering, then the Bayesian Binary Sensor can use Bayesian inference to decide whether I am in bed or not based on the observations it makes from the various sensors I have configured.

My main question is this:
The sensor will need to keep track of many other sensors in order to record observations and update its probability. The easy method seems to be to poll the other sensors at a regular interval, but it would be great to leverage the automation platform and receive observations based on triggers. So… Which method should I start with, and for that method, do you have any implementation tips?

PS: I am a data scientist and do this Bayesian stuff every day, so I’m very confident it will work.

edit: It seems like many of the things I am unsure about have been implemented for the Template Binary Sensor. I am currently exploring that implementation.

9 Likes

This is a really cool idea! Are you planning on adding this directly to HASS? In which case you would have access to any and all state changes as they occur.

If not, AppDaemon would be a great platform to code or even just prototype this on - since this is so interesting I’d be very happy to help you with the AppDaemon piece if you decide to go down that route.

With AppDaemon you are able to subscribe to specific state changes so this would be a very good fit.

1 Like

I feel like it should be built in as core HASS functionality, but I was totally unaware of AppDaemon, honestly. Was currently looking at from homeassistant.helpers.event import track_state_change as used in the “Binary Trend” sensor, I think that may be the path to a good implementation for me.

I am relatively unfamiliar with the HASS codebase, but am pretty comfortable with async python3. Mainly hoping for some specific guidance on what tooling already exists for subscribing to events and updating a “parent” sensor’s state based on those events.

1 Like

I’ll let others answer those questions as I am not an expert in core development - the AppDaemon approach would be a lot quicker to realize as a prototype though, so the offer to help stands if you want to go that route.

I do agree though that ultimately this makes the most sense as a built in binary sensor just as you planned.

I’d like to interject here and also suggest that the AppDaemon route would not only be the easiest but also likely the best for implementation of something like this. Less time spent with issues around trying to fit it into the core and more time spent on refining the processes.

I can admit a small bias as I do a lot of testing for @aimc and did a little work on HADashboard as well. But I think you’ll find AD to be quite an extensible toolkit for tapping into HA and for processes that interact with it.

Just submitted a pull-request in the main repo: https://github.com/home-assistant/home-assistant/pull/8810

Hopefully it can get into the next release! Feel free to comment/criticize on GitHub!

5 Likes

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.