Bayesian Integration - What do you use it for?

I am a new code owner for the Bayesian integration. I have some plans for the integration, but they are mostly based on my own opinions.

I would love to know whether you use the Bayesian integration, what you use it for, what your YAML looks like, what you don’t like about it and what you wish it could do.

Many thanks,

HarvsG

You can read more about my plans for it here: The Future of the Bayesian Integration - Thoughts wanted

HomeAssistant.io documentation - Bayesian Integration

If you’ve not used it before and are interested then this community post is a great starting point. (although some of the workarounds suggested are not necessary from 2022.10 onwards)

I use several Bayesian sensors to “guesstimate” presence around the house/yard, based on WiFi access point use, Bluetooth connectivity, and motion sensor triggers. They are very accurate, and I could not do without them.

2 Likes

Any chance you could share your yaml?
Or send it to me in a DM, I’m going to be writing some tests for the integration based on real user configs.

Be warned - likely some breaking changes coming in the next version, so you may need to tweak your config.

I use Bayesian sensors for controlling heat in my house and for presence. I have sensors for heating cable, heat pump, water heater, presence and for charging electric car. I have tried Home Assistant automation and Appdaemon Schedy and both have pro and cons. With Bayesian sensors everything works and it is easy to maintain. The hard part is to find the best probalility.
The credit for this idea to use Bayesian sensors for controlling heat goes to @stigvi

I have written about this here on a Norwegian forum and you find the YAML I use there: https://www.hjemmeautomasjon.no/forums/topic/9394-mitt-forsøk-på-prediktiv-reduksjon-av-strømbruk/?do=findComment&comment=102586

I used a spreadsheet which a YouTube - Binary Tech Labs made for calculate the probability. The video and the link to the spreadsheet is found here: A Deep Dive Into Bayesian Sensors For Home Assistant Giving Advanced Automations! - YouTube

I would have liked it to have been easier to be able to find out the best probability. A good spreadsheet is maybe needed and more examples to use Bayesian sensors in daily use.

2 Likes

Thanks for taking the time to reply.

I had a look at your YAML - you will need to add prob_given_false for each of those entities. It shouldn’t really be an optional variable (and from the next version it won’t be).
There is some updated documentation here: Bayesian - Home Assistant that will help you estimate the probabilities for prob_given_false and prob_given_true

1 Like

I am trying to use the Bayesian sensor for a novel user case. I have a large freshwater aquarium with a sump (basically a small tank below the aquarium) in which the recirculation pump, ph, temperature and ORP sensors are located. The aquarium has an automatic water changing system that on rare occasions has a failure in which the water drains out of the sump due to power outages or potentially other issues relating to the Reverse Osmosis intake.

This week, I had an event in which due to a small water leak in my RO unit, the sump water level started to decline. I realized that with a Bayesian sensor, I can monitor the PH and ORP sensors because the moment the water level drops below the probes, I can trigger an alarm or notification.

I need some help though because the YAML code isn’t working:

When I do a check code it gives me the following error:

Invalid config for [python_script]: expected dict for dictionary value @ data['python_script']. Got [OrderedDict([('platform', 'bayesian'), ('name', 'Sump Low'), ('prior', 0.6), ('probability_threshold', 0.8), ('observations', [OrderedDict([('platform', 'numeric_state'), ('entity_id', 'sensor.apex_ph'), ('prob_given_true', 0.7), ('prob_given_false', 0.1), ('below', 6.2)]), OrderedDict([('platform', 'numeric_state'), ('entity_id', 'sensor.apex_orp'), ('prob_given_true', 0.7), ('prob_given_false', 0.1), ('below', 400)])])])]. (See /config/configuration.yaml, line 50).

My config YAML is:

  - platform: "bayesian"
    name: "Sump Low"
    prior: 0.6
    probability_threshold: 0.8
    observations:
      - platform: numeric_state
        entity_id: sensor.apex_ph
        prob_given_true: 0.7
        prob_given_false: 0.1
        below: 6.2
      - platform: numeric_state
        entity_id: sensor.apex_orp
        prob_given_true: 0.7
        prob_given_false: 0.1
        below: 400

I haven’t done any refining of the probabilities yet.

Questions:

  1. Is bayesian part of the basic python installation or does one need to install anything else?
  2. What am I doing wrong in the YAML code?

The bayesian platform needs to be configured under your binary_sensor heading… from the error message it looks like you have it under python_script.

1 Like

I use the Bayesian sensor for presence detection, chance of rain specifically at my house using my PWS + other weather sources, and other assumptions of what we are currently doing around the house.

1 Like

Thanks Drew, adding this did the trick. I had it previously, but there was another error.

Thanks again!!!

That sounds cool. Would you mind sharing your YAML?

@HarvsG
Here are two different use cases for Bayes sensors. Both of these work exceptionally well given the data from multiple sensors. Added some comments for clarity.

#########################
#     Rain Sensors      #
#########################
- platform: 'bayesian'
  prior: 0.02
  name: 'Its Raining'
  probability_threshold: 0.95
  observations:
    - platform: template
      # Entity 1 (subjective)
      value_template: >
        {{ 'rain' in (states('weather.pirate_weather'))|lower() }}
      prob_given_true: 0.75
      prob_given_false: 0.10
    - platform: template
      # Entity 2 (subjective)
      value_template: >
        {{ 'rain' in (states('sensor.dark_sky_summary'))|lower() }}
      prob_given_true: 0.75
      prob_given_false: 0.25
    - platform: template
      # Entity 3 (subjective)
      value_template: >
        {{ 'rain' in (states('sensor.dark_sky_icon'))|lower() }}
      prob_given_true: 0.75
      prob_given_false: 0.25
    - platform: template
      # Entity 4 (subjective)
      value_template: >
        {{ (states('sensor.dark_sky_precip_probability'))|float(default=0) > 90  }}
      prob_given_true: 0.80
      prob_given_false: 0.20
    - platform: template
      # Entity 5 (objective)
      value_template: >
        {{ (states('sensor.dark_sky_nearest_storm_distance'))|float(default=0) < 5 }}
      prob_given_true: 0.75
      prob_given_false: 0.25
    - platform: template
      # Entity 6 (objective)
      value_template: >
        {{ (states('sensor.tidewater_hourly_rain_rate'))|float(default=0) > 0.00 }}
      prob_given_true: 0.95
      prob_given_false: 0.05
    - platform: template
      # Entity 7 (objective)
      value_template: >
        {{ (states('sensor.tidewater_hourly_rain_rate'))|float(default=0) > 0.1 }}
      prob_given_true: 0.98
      prob_given_false: 0.02
#

and

#########################
#   Presence Sensors    #
#########################
# ART ==============================
- platform: 'bayesian'
  name: 'Art Presence'
  device_class: 'presence'
  prior: 0.70
  probability_threshold: 0.9
  observations:
    - platform: 'state'
      to_state: 'home'
      entity_id: 'device_tracker.arts_iphone_11'
      prob_given_true: 0.95
      prob_given_false: 0.05
    - platform: 'numeric_state'
      entity_id: 'proximity.art_to_home'
      below: 1
      prob_given_true: 0.90
      prob_given_false: 0.10
      # numeric value in miles I am from Home (uses my iPhone)
    - platform: template
      value_template: >
        {{ is_state('device_tracker.arts_iphone_11','home') and ((as_timestamp(now(),0) - as_timestamp(states.device_tracker.arts_iphone_11.last_changed)) > 300) }}
      prob_given_true: 0.6
      prob_given_false: 0.4
      # am I stationary or moving in/out of home zone
    - platform: template
      value_template: >
        {{ states('sensor.presence_value_art')|int == 1 }}
      prob_given_true: 0.88
      prob_given_false: 0.12
      # this is based on a BT tracker (like a Tile device) attached to my keyring
    - platform: 'state'
      to_state: 'on'
      entity_id: 'input_boolean.art_work'
      prob_given_true: 0.34
      prob_given_false: 0.66
      # similar to next item, but uses an input_boolean toggle when arriving/departing work.
    - platform: template
      value_template: >
        {% if states('person.art') in ['Your zones','Your zones','Your zones','Your zones'] %} True {% else %} False {% endif %}
      prob_given_true: 0.10
      prob_given_false: 0.90
      # uses zones that are NOT HOME (work, shopping, church, etc)
#
2 Likes

Would it be possible to use this to detect if the dryer is on or off based off the current KW used by my home (from the main meter) ? My dryer causes the KW usage to fluctuate by about 6kw. Maybe with some difference helpers?
Thanks.

For detecting whether the dryer is on or off, It would be better to use a smart plug with power metering like https://www.shelly.com/en/products/shop/shelly-plug