Dynamic (Template) Values for Prior and probabilities in Bayesian platform

I have a pretty in-depth bayesian setup, I also use the history_stats platform to calculate the probabilities of things actually happening based on the numerous sensors i have around the house.


I would like to be able to define the bayesian prior and probabilities via a template as below:

- platform: bayesian
  name: Area - Test Occupied
  prior: "{{ states('sensor.stats_study_occupied')| float / 100 }}"
  device_class: occupancy
  probability_threshold: 0.8
  observations:
    - platform: "state"
      entity_id: "group.area_study_motion"
      prob_given_true: "{{ states('sensor.stats_study_motion')| float / 100 }}"
      to_state: "on"

This would be calculated upon started up and perhaps have a service call to re-calculate as I understand that there may be a performance impact with constant recalculation of a lot of these definitions

Can you share details?

What you you want to know? My setup? I will be publishing it on GitHub with docs in the next couple weeks

Yes … Im very interested in how it works

Okay, i’ve gotten half way writing it tonight, i’ll post it tomorrow hopefully, will also highlight my request for this feature too

Here you go! My Config and a writeup of whats what
https://github.com/hankanman/Home-Assistant

thx man … Im going to grab interested parts to my setup =) … Just recently finished my my hardware setup (wall panel and HA server) … but I still need improve automations and UI parts

Hey, I came to the forum to post this suggestion but you’ve already written it out better than I would have. Nice idea about using it to dynamically track priors BTW.

I don’t think this would need to be triggered as a one off: looking through the current Bayesian sensor code (core/binary_sensor.py at 8bf6aba1cf44ee841de063755c935ea78040f399 · home-assistant/core · GitHub) I think the additional templates could be registered just like the current templates are. It won’t slot straight in, but I might be able to give it a try if there’s interest in this change.

Hey Charles, if you have the know how to implement I would be appricative. I haven’t found the time to figure it out myself yet. Naturally happy to help in anyway testing etc

Actually having another think about it the ideal implementation wouldn’t require the history stats at all.

Instead have a config option for the entity you wish to base you probability on. Then the integration would invoke the history stats code to get the probability, saves a lot of manually creating a bunch of history stats sensors and adding to the database. So it would be more like:

- platform: bayesian
  name: Area - Test Occupied
  prior: 0.8 #<- Optional config if you wanted to configure it manually and prevent breaking changes
  prior_entity: binary_sensor.test_occupied #<- the entity provided here will have its stats calculated from the database on scan interval and automatically provide the prior value (no template actually needed)
  scan_interval: 7 #<- In day perhaps? how often to update the prior
  device_class: occupancy
  probability_threshold: 0.8
  observations:
    - platform: "state"
      entity_id: "group.area_test_motion"
      prob_given_true: 0.4 #<- Optional manual config remains to prevent breaking changes
      prob_given_true_entity: "group.area_test_motion" #<- Works similar to the sensor prior providing a calculated probability given history of the sensor state based on history_days
      history_days: 30 #<- Number of days in the past to now over which the total % of time that the "to_state" was met 
      to_state: "on"

I hope this makes sense. All the logic is in my head, so happy to figure out the implementation with people.

My goal with this is there is little to no maintenance once it’s set up. Home Assistant just knows, and always does. Massively simplifies automations, don’t need a million conditions, just “Is someone there?”

Did anyone figure this out? How to use history_stats or any other way to use template sensors to set/update priors and probabilities?