The new Bayesian Binary Sensor - Any additional examples?

I’m currrently using an input_select to manually tag events (e.g. going to bed) which will hopefully allow me to determine the optimum parameters for a Bayesian_sensor for said events. Interested to see any analysis along these lines.
Cheers

Interesting, so you will use that input as a label, then analyze your data via pandas, etc?

I’m not committed enough manually label my events… at least not yet.

Thinking ahead, it would definitely be possible to build an add-on that is configured to train many classifiers (agents if we wanted to take a reinforcement learning approach) based on binary sensors that are manually triggered and corresponding entities to observe.

Yes pandas/saleranno. Am interested in collaborating on that add-on

Wow, those examples helped me a lot! I tried tinkering with the bayesian sensor for presence, but couldn’t really wrap my head around the probability numbers. Now I think I’ve done a much better job, but I guess time will tell. I guess it will come down to keeping track of how it does and then fine tuning as I go.
If it turns out well I will probably exchange my normal presence in all automations to this sensor. But then I will lose the ability to track other zones for example? Anyone has an idea of how to combine the bayesian for presence with zones? Or maybe just keep them separate? Bayesian for home/not home-automations and device_tracker for the rest?

I also just thought about making a history graph with all the entities that I’m using in the bayesian for troubleshooting purposes! Easier to see whats wrong.

My own bayesian ‘in bed bayesian sensor’ is working nicely now. I found that the ‘sun below horizon’ input wasn’t particularly useful, since in the UK at this time of year the sun is setting before 5pm.! Instead I’m using a template sensor which is ON at ‘late night’.

- platform: template
  sensors:
    late_night_sensor:
      value_template: >-
          {{ strptime("22:00", "%H%M")  < now().strftime("%H:%M")
             or now().strftime("%H:%M") < strptime("07:00", "%H%M") }}

As a couple of others have done I also have a sensor to detect when there has been no motion in the house for 5 minutes. I implemented that using an input_select and an automation:

- id: '1513346519354'
  alias: House_idle
  trigger:
  - entity_id: binary_sensor.motion_at_home
    for:
      minutes: 5
    from: 'on'
    platform: state
    to: 'off'
  action:
  - data:
      entity_id: input_boolean.house_idle
    service: input_boolean.turn_on

Finally the bayesian sensor is:

- platform: 'bayesian'
  name: 'in_bed_bayesian'
  prior: 0.25
  probability_threshold: 0.5
  observations:
    - entity_id: 'group.all_lights'
      prob_given_true: 0.4
      platform: 'state'
      to_state: 'off'
    - entity_id: 'input_boolean.house_idle'
      prob_given_true: 0.6
      platform: 'state'
      to_state: 'on'
    - entity_id: 'binary_sensor.late_night_sensor'
      prob_given_true: 0.7
      platform: 'state'
      to_state: 'on'
    - entity_id: 'switch.macbook_power'
      prob_given_true: 0.1
      platform: 'state'
      to_state: 'on'

I am using the history statistics component and a template sensor to display the number of hours I spent in bed last night.

- platform: history_stats
  name: Time in bed
  entity_id: binary_sensor.in_bed_bayesian
  state: 'on'
  type: time
  end: '{{ now() }}'
  duration:
    hours: 24

And the template sensor:

- platform: template
  sensors:
    time_in_bed_template:
      friendly_name: 'Time in bed'
      value_template: '{{states.sensor.time_in_bed.attributes.value}}'

Lastly, I wrote a notebook discussing the ideas behind the bayesian sensor, and many thanks to @jlmcgehee21 for his edits:


If theres sufficient interest, I will format this for a blog post, so let me know.
Cheers!
13 Likes

Just wanted to say thank you for the amazing tool – Used it to quickly tweak my probability inputs on my bayesian sensor setup and it was fantastic.

I’m still not 100% sure of how this works. Here is one I created, but have not done extensive testing.

- platform: bayesian
  prior: 0.6
  name: 'Paul Presence'
  probability_threshold: 0.9
  observations:
    - entity_id: 'binary_sensor.ping_paul_iphone'
      prob_given_true: 0.9
      prob_given_false: 0.1
      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.paul_iphone'
      prob_given_true: 0.9
      prob_given_false: 0.4
      platform: 'state'
      to_state: 'home'

So my question is, what has to happen for this to be ‘On’

So the ping is accurate, but often off, only works on my iPhone if the iPhone is awake. The other two are using owntracks, and the IOS home assistant app (and also asuswrt, which combines into a single sensor using some magic,( ie mac address of the device I believe)

So I guess my normal situation is that the owntracks is a little slower at proviing a home status, but quicker at an away status.

I guess I don’t understand the full math. Do you just add the prob give true if something is true, and subtract the prob given false if an item is false and he total of all numbers must match the prob threshold?

So If I came home, and HA Ios app said I was home, but others were false

ping -.1
HA +.9
Owntracks -.4
Total = .4 so switch is off

Is that basically how it works?

@ptdalen For info on how the sensor works see the notebook link I posted

1 Like

Wow, lots of words! :slight_smile:

Thanks for such a detailed description. I will definitely give this a full read tonight

Can anyone tell me what is wrong with my formatting above

I keep getting

expected , but found ‘’ for the first line

  • platform: bayesian

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: