J. Nick Koston b908b87c7d
Update template entity docs for entity_id removal and guidance on working without entities (#14343)
Co-authored-by: Anders Melchiorsen <amelchio@nogoto.net>
Co-authored-by: Swamp-Ig <github@ninjateaparty.com>
2020-09-03 18:51:07 +02:00

5.3 KiB

title description ha_category ha_iot_class ha_release ha_quality_scale ha_domain
Bayesian Instructions on how to integrate threshold Bayesian sensors into Home Assistant.
Utility
Binary Sensor
Local Polling 0.53 internal bayesian

The bayesian binary sensor platform observes the state from multiple sensors and uses Bayes' rule to estimate the probability that an event has occurred given the state of the observed sensors. If the estimated posterior probability is above the probability_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.

Configuration

To enable the Bayesian sensor, add the following lines to your configuration.yaml:

# Example configuration.yaml entry
binary_sensor:
  - platform: bayesian
    prior: 0.1
    observations:
      - entity_id: 'switch.kitchen_lights'
        prob_given_true: 0.6
        prob_given_false: 0.2
        platform: 'state'
        to_state: 'on'

{% configuration %} prior: description: > The prior probability of the event. At any point in time (ignoring all external influences) how likely is this event to occur? required: true type: float probability_threshold: description: The probability at which the sensor should trigger to on. required: false type: float default: 0.5 name: description: Name of the sensor to use in the frontend. required: false type: string default: Bayesian Binary Sensor observations: description: The observations which should influence the likelihood that the given event has occurred. required: true type: list keys: platform: description: > 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). required: true type: string entity_id: description: Name of the entity to monitor. Required for state and numeric_state. required: false type: string value_template: description: Defines the template to be used. Required for template. required: false type: template prob_given_true: description: The probability of the observation occurring, given the event is true. required: true type: float prob_given_false: description: The probability of the observation occurring, given the event is false can be set as well. required: false type: float default: "1 - prob_given_true if prob_given_false is not set" to_state: description: The target state. Required (for state). required: false type: string {% endconfiguration %}

Full examples

The following is an example for the state observation platform.

# Example configuration.yaml entry
binary_sensor:
  name: 'in_bed'
  platform: 'bayesian'
  prior: 0.25
  probability_threshold: 0.95
  observations:
    - platform: 'state'
      entity_id: 'sensor.living_room_motion'
      prob_given_true: 0.4
      prob_given_false: 0.2
      to_state: 'off'
    - platform: 'state'
      entity_id: 'sensor.basement_motion'
      prob_given_true: 0.5
      prob_given_false: 0.4
      to_state: 'off'
    - platform: 'state'
      entity_id: 'sensor.bedroom_motion'
      prob_given_true: 0.5
      to_state: 'on'
    - platform: 'state'
      entity_id: 'sun.sun'
      prob_given_true: 0.7
      to_state: 'below_horizon'

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.

# Example configuration.yaml entry
binary_sensor:
  name: 'Heat On'
  platform: 'bayesian'
  prior: 0.2
  probability_threshold: 0.9
  observations:
    - platform: 'numeric_state'
      entity_id: 'sensor.outside_air_temperature_fahrenheit'
      prob_given_true: 0.95
      below: 50

Finally, here's an example for template observation platform, as seen in the configuration it requires value_template.

{% raw %}

# Example configuration.yaml entry
binary_sensor:
  name: 'Paulus Home'
  platform: 'bayesian'
  prior: 0.5
  probability_threshold: 0.9
  observations:
    - platform: template
      value_template: >
        {{is_state('device_tracker.paulus','not_home') and ((as_timestamp(now()) - as_timestamp(states.device_tracker.paulus.last_changed)) > 300)}}
      prob_given_true: 0.95

{% endraw %}

The template is re-evaluated whenever an entity ID that it references changes state. If you use non-deterministic functions like now() in the template it will not be continuously re-evaluated, but only when an entity ID that is referenced is updated.

In this example, since the template is only evaluated on state change of device_tracker.paulus the template won't change state after 5 mins like intended. The ways to force template reevaluation are documented in the template binary_sensor.