The new Bayesian Binary Sensor - Any additional examples?

Great article, thanks for the work. I’m still a bit confused. I admit that I skimmed down to the examples near the bottom, but here is where I’m confused.

At the bottom you mentioned turning on the kitchen light, but the probability value looked like it was for the bedroom light (.25 vs .6), then of course the part that I just did not get was when you turned on both lights, the total was .45, both were true. I could not figure out how .45 was the output. I could not see a true or false situation that would provide that value.

Sorry, i’m sure its right in my face. I’m just missing it. I do have a sensor created for presence, and it’s working, but I admit I just guessed and threw some numbers together. I’d like to understand a bit better so I can really benefit from this powerful sensor.

Hi @ptdalen I suggest you could either clone the notebook and run yourself through all the steps, or you could just to the calculations by hand or in excel. It is just the repeated calculation of Bayes formula with different inputs, nothing too computationally complex. Let me know how you get on.
Cheers

Well after playing around with this for a while, I gotta say that I still don’t quite get it. Ha Ha. Makes me realize how much I’ve forgotten over the years.

I don’t have notebook installed to clone, and honesly I cannot even figure out how to do this in excel, and I felt like I was pretty decent at excel before this. I’ve actually read quite a bit on the Bayesian theory now, and understand enough to at least impress my friends with my very low level on understanding.

Even google returns limited resources on spreadsheets or how to set up a spreadsheet with more than two items,

I found this

http://psych.fullerton.edu/mbirnbaum/bayes/BayesCalc3.htm

Which was partially helpful. I could just play with the numbers by changing them until it works as I’d like, but I’d still really like to be able to understand why,

So using my example below

 - platform: bayesian
   prior: 0.6
   name: Paul Presence
   probability_threshold: 0.9
   observations:
   - entity_id: 'input_boolean.paul_present'
     prob_given_true: 0.9
     prob_given_false: 0.2
     platform: 'state'
     to_state: 'on'
   - entity_id: 'device_tracker.pauls_iphone'
     prob_given_true: 0.9
     prob_given_false: 0.2
     platform: 'state'
     to_state: 'home'
   - entity_id: 'device_tracker.pauls_iphone_2'
     prob_given_true: 0.9
     prob_given_false: 0.4
     platform: 'state'
     to_state: 'home'

The first sensor “input_boolean” is tied to my homekit. It usually triggers before anything else and sets the Bayesian sensor to true, My iPhone will also set the Bayesian sensor to true. Which is pretty much what I want, Problem is that if the 1st sensor stays on “False positive” when I leave, even if the others turn off, the Bayesian stays “on”. I get it, but I guess I don’t really get it.

I feel like I’m back in college when it comes to this sensor, which is both frustrating, and a bit exciting, as I slowly figure it out a little more.

Can you point me to how I could create an excel document where I could set values to 1/0 (for on/off/home/away, etc) and it would show me the end state of the Bayesian?

Thanks again for your patience

If you want access to a notebook env checkout https://notebooks.azure.com/

Thanks, managed to get your notebook imported. I greatly appreciate the amount of work that you put into this, and don’t want to take away from that, but is there any where out there where I can start with a little less detail. Something like ELI5. :slight_smile:

In the mean time I’ll continue to work on this, but it is still quite overwhelming even with the notebook.

The Wikipedia article on Bayes formula should be approachable. Do the Calcs with pen and paper :slight_smile:

1 Like

I created a command line tool to help with the configuration of the sensor @ptdalen: https://github.com/jlmcgehee21/smart_hass

An example of its usage can be found here: https://github.com/jlmcgehee21/smart_hass/blob/master/examples/Smass%20Examples.ipynb

2 Likes

I’m trying to work with the bayesian sensor and have some trouble getting the probabilities right. I would like to graph the data in a similar way like your post. Do you have an example for me how to get that graph to work? I guess I can find ways to plot the states, but the result-probability of the bayesian not so much.

i ploted the graphs with grafana:

I just now found that probability is just an attribute. That’s what I was missing. Then grafana would be a nice way to actually plot indeed. Thx!

I am trying to implement your late night sensor, but it seems it doesn’t update as there’s nothing to update it. Does it work for you? Unfortunately I am not good at templating yet, but I think a version using the Time & Date sensor platform would work and update the sensor every minute…

I used your example and others I found to create a device tracker that works using several inputs. Unfortunately right now I only have trackers that look at my phone, via Openwrt, or bluetooth tracking with our Google homes. This works well from the time we get home until bedtime and again in the morning. But between 11 and 6 say the phones are generally in sleep mode only waking occasionally. This is kind of normal to what we see.
image
I was thinking that a Bayesian sensor might be able to help smooth out the results during the night, but I am not sure how to make it work. I want to add an additional observation that says if device tracker = home sometime after 10pm and time is currently < 6am there is a high probability that I am still home.

I only use my phone as a tracker as well, but have several different device trackers. It looks like you also have several trackers. OpenWrt, and bluetooth, right? Anything else? Do both of those “sleep” at night? How long do they typically change to not_home, then back to home? You could change the value to consider_home: to a longer amount to make them more stable. If you are not opposed, I would add at least one more device tracker, a gps based one. I have an iphone, and use a router, and two different gps based trackers and tie them all into a Bayesian. I guess what I’m saying is, if the device trackers you currently use are not stable at night, I would try to improve that stability or add another device tracker that is stable, before trying to add the bayseian.

But isn’t it better to run several trackers into a bayesian to make it more stable overall? I got interested in this because of the new people object but I don’t like how it takes the latest update as fact. I want something with more logic that I can help steer like the rule I explained above or just the fact that a tracker sees me at home is important. Mine tend to error on the fact that I am home but they think I am away.

Yes, this can definitely help with a lot of things. bouncy presence sensors are definitely somewhere where this can help. But, in my opinion, if all of your current presence detectors are not stable, I feel like you are going to have a bit of a hard time with this. This also works better the more data points (presence sensors for you) that you have. Personally I think you really want at least 3 different ways to detect presence before you set up a Bayesian sensor.

So, I guess I’d start this way.

  1. How many different ways do you currently have to determine if your phone is home?
  2. Are any of these stable most of the time? Even at night?
  3. Have you tried setting the consider home to a higher value, like maybe 300 (5 minutes)? Are your current sensors both equally bouncy at night? or is one better than the other? You’ll need that info anyway when you get to the bayesian configuration.

If you dont have any stable presense sensors, you’ll need many unstable ones to get a decent bayesian.

@robbz23 checkout the new tod sensor which you can use as an input to your bayesian sensor

@robmarkcole or @ptdalen Can you guys explain to me why this isn’t working the way I think it should.

I have a bayesian sensor setup with 4 different observations that am using as a device tracker. One of those observations rarely has false positives but frequently has false negatives. I set it to prob_given_true: 0.95 and prob_given_false: 0.3. I figured a higher number for true because it is accurate when true and a higher then default for false because it is inaccurate when false.

But why doesn’t this one trigger

{  
   "observations":[  
      {  
         "prob_true":0.95,
         "prob_false":0.3
      }
   ],
   "probability":0.85,
   "probability_threshold":0.88,
   "device_class":"presence"
}

I thought that it would have a probability of .95 and not .85 in these conditions.

If its not triggering you should calculate your probabilities to check what the thresholds should be. I posted a notebook link earlier in this thread which can help you out. Cheers!

I started adding Bayesian Binary Sensors to my Hass setup and wanted something to quickly test one or more observations in Jupyter and easily create/update the sensor.

I created a Python class and Jupyter Notebook with examples that you can use to do this. Check it out: https://github.com/westenberg/hass-bayesian-sensor-notebook

1 Like

I created a simple spreadsheet to test out my bayesian sensors. I posted it under a different thread, and thought this one looked a little more appropriate. It’s at

https://community.home-assistant.io/t/bayesian-sensors-best-methods/142401?u=ksc

1 Like