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?”

1 Like

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

Did you figure out how to implement this @hankanman?

I have managed to achieve this to an extent by generating the yaml in appdaemon periodically and setting the value for prior based on the last 7 days activity weighted to consider the same day last week to be a more pertinent number for prior

Currently I use altering an input number (for threshold) as a trigger to tell appdaemon to regenerate

It’s improved Bayes accuracy, however, as the Bayes sensor has recently had some love from the developers, I would love to see that little extra push to have both prior and threshold be fed from another sensor/input

Is there a feature request to add templates for to hard coded variables in Bayesian config. I would vote for this if it’s already been suggest or can submit it if it has not yet been requested. Having the ability to build a test platform and modify values would greatly demystify a really useful tool. A while back there was a post that had an Excel spreadsheet where you could plug in variables and test against T/F conditions to see how the sensor would react, but that spreadsheet currently does not report correctly. Not sure if something changed in the Bayes Sensor or via ongoing updates. That was my motivation to build a test page and dynamically modify true/false/prior values to see updated outcomes. FWIW I can get those attribute values via code and put them into an input_number or variable, but have not been able to UPDATE those values back into the Bayes sensor.
Interestingly, one can also modify the attributes of a Bayes sensor in the Developer Tools States tab. However, the updated values are not applied to the calculation. The only way I have found is to update the YAML code and refresh the Bayesian Sensor, but this is way less efficient.

This post is the feature request :slight_smile: can vote at the top, I do have an updated spreadsheet I use, I’ll upload here in the next day or two

Voted! Thank you, and yes, I would love to see your spreadsheet when you have a moment to post it.

Voted! Here is my use case (less complex) - any maybe it can be achieved another way:

I use my Apple Watch and iPhone AND room presence (3 inputs) to determine if I am near my laptop.

This works fine until I take my laptop to work and now the room presence is no longer valid.

I can switch between two separate Bayesian sensors with different inputs based on location, OR…

Set probabilities to extremely low numbers for the room sensor when I am not at the house.

Has there been any progress in adding templating to Bayesian sensors. This would make tweaking a Bayes sensor much easier for situations where the item of interest may change often, such as Is it raining/windy/cold or for testing purposes.
Yes, I know there is a spreadsheet available, however I’ve noticed that when configured EXACTLY the same, the spreadsheet and the HA sensor give different answers, so for me, the spreadsheet is not a viable solution for testing.
Lastly, I also noticed that the PRIOR value is not obtainable for use in a template or automation. All other values are, except for PRIOR.