Bayesian
The bayesian
binary sensor platform observes the state from multiple sensors. It uses Bayes’ ruleprobability_threshold
, the sensor is on
; otherwise, it is off
.
This allows for the detection of complex events that may not be readily observable, e.g., cooking, showering, in bed, the start of a morning routine, etc. It can also be used to gain greater confidence about events that are directly observable, but for which the sensors can be unreliable, e.g., presence.
Theory
A key concept in Bayes’ Rule is the difference between the probability of the ‘event given the observation’ and the probability of the ‘observation given the event’. In some cases, these probabilities will be similar. The probability that someone is in the room given that motion is detected is similar to the probability motion is detected given that someone is in the room when motion sensors are accurate. In other cases, the distinction is much more important. The probability one has just arrived home (the event) each time the front door contact sensor reports open
(the observation) (p=0.2) is not the same as the probability the front door contact sensor reports open
(the observation) when one comes home (the event) (p=0.999). This difference is because one opens the door several times a day for other purposes.
In the configuration use the probability of the observation (the sensor state in question) given the event (the assumed state of the Bayesian binary_sensor).
Estimating probabilities
- Avoid
0
and1
, these will mess with the odds and are rarely true - sensors fail. - When using
0.99
and0.001
. The number of9
s and0
s matters. - Most probabilities will be time-based - the fraction of time something is true is also the probability it will be true.
- Use your Home Assistant history to help estimate the probabilities.
-
prob_given_true:
- Select the sensor in question over a time range when you think thebayesian
sensor should have beentrue
.prob_given_true:
is the fraction of the time the sensor was into_state:
. -
prob_given_false:
- Select the sensor in question over a time range when you think thebayesian
sensor should have beenfalse
.prob_given_false:
is the fraction of the time the sensor was into_state:
.
-
- Don’t work backwards by tweaking
prob_given_true:
andprob_given_false:
to give the results and behaviors you want, use #4 to try and get probabilities as close to the ‘truth’ as you can, if your behavior is not as expected consider adding more sensors or see #6. - If your Bayesian sensor ends up triggering
on
too easily, re-check that the probabilities set and estimated make sense, then consider increasingprobability_threshold:
and vice-versa.
Configuration
To enable the Bayesian sensor, add the following lines to your configuration.yaml
The configuration.yaml file is the main configuration file for Home Assistant. It lists the integrations to be loaded and their specific configurations. In some cases, the configuration needs to be edited manually directly in the configuration.yaml file. Most integrations can be configured in the UI. [Learn more] file.
After changing the configuration.yaml
The configuration.yaml file is the main configuration file for Home Assistant. It lists the integrations to be loaded and their specific configurations. In some cases, the configuration needs to be edited manually directly in the configuration.yaml file. Most integrations can be configured in the UI. [Learn more] file, restart Home Assistant to apply the changes.
Configuration Variables
The prior probability of the event (0 to 1). At any point in time (ignoring all external influences) how likely is this event to be occurring?
The posterior probability at which the sensor should trigger to on
. use higher values to reduce false positives (and increase false negatives) Note: If the threshold is higher than the prior then the default state will be off
An ID that uniquely identifies this bayesian entity. If two entities have the same unique ID, Home Assistant will raise an exception.
Sets the class of the device, changing the device state and icon that is displayed on the frontend.
The observations which should influence the probability that the given event is occurring.
The supported platforms are state
, numeric_state
, and template
. They are modeled after their corresponding triggers for automations, requiring to_state
(for state
), below
and/or above
(for numeric_state
) and value_template
(for template
).
Defines the template to be used, should evaluate to true
or false
. Required for template
.
Assuming the bayesian binary_sensor is true
, the probability the entity state is occurring.
Full examples
These are a number of worked examples which you may find helpful for each of the state types.
State
The following is an example for the state
observation platform.
Numeric State
Next up an example which targets the numeric_state
observation platform,
as seen in the configuration it requires below
and/or above
instead of to_state
.
Template
Here’s an example for template
observation platform, as seen in the configuration it requires value_template
. This template will evaluate to true if the device tracker device_tracker.paulus
shows not_home
and it last changed its status more than 5 minutes ago.
Multiple state and numeric entries per entity
Lastly, an example illustrates how to configure Bayesian when there are more than two states of interest and several possible numeric ranges. When an entity can hold more than 2 values of interest (numeric ranges or states), then you may wish to specify probabilities for each possible value. Once you have specified more than one, Bayesian cannot infer anything about states or numeric values that are unspecified, like it usually does, so it is recommended that all possible values are included. As above, the prob_given_true
s of all the possible states should sum to 1, as should the prob_given_false
s. If a value that has not been specified is observed, then the observation will be ignored as it would be if the entity were UNKNOWN
or UNAVAILABLE
.
When more than one range is specified, if a value falls on below
, it will be included with the range that lists it in below
. below
then means “below or equal to”. This is not true when only a single range is specified, where both above
and below
do not include “equal to”.
This is an example sensor that can detect if the bins have been left on the side of the road and need to be brought closer to the house. It combines a theoretical presence sensor that gives a numeric signal strength and an API sensor from local government that can have 3 possible states: due
when collection is due in the next 24 hours, collected
when collection has happened in the last 24 hours, and not_due
at other times.
To achieve a similar result for multiple template
observations targeting a single entity, write your templates to return True
or None
rather than True
or False
.