Peter Hajas



Detecting Whether I'm in Bed or Not

Some Background

I’ve been learning about and using Home Assistant for the past few months. It’s an open source home automation platform, but it can be turned into much more. It’s kind of like your own personal “if-this-happens, do-that” service.

At the core of the system are entities, which represent stuff the system knows about. Entities are prefixed with their type, for example person.peter_hajas, light.bedside_table, sensor.patio_temperature, or sun.sun. Home Assistant supports a suite of user-authored entities for sensors (things that feed the system data), commands (actions triggered by the system), and semantic devices (like “covers”, Home Assistant’s catchall for garage doors, sun shades, etc.).

Building the Sensor

One of the platforms for a binary sensor (determines if something is true or false) is the Bayesian binary sensor. This lets you use Bayes’ theorem to combine observations into a sensor value.

While spending some time at my home alone recently, I wanted to play with automations keyed off of whether or not I was in bed. This could be used to shut off lights and turn off appliances. I’ve read about some of the bed occupancy sensors you can build, but I wanted to see how far Bayesian sensing could get me.

Here’s my “am I at home in bed” binary_sensor:

# Am I at home in bed?
- platform: bayesian
  name: "peter_in_bed"
  prior: 0.33
  probability_threshold: 0.9
    - entity_id: "person.peter_hajas"
      prob_given_true: 0.5
      platform: "state"
      to_state: "home"
    - entity_id: "sensor.phajas_phone_battery_state"
      prob_given_true: 0.75
      prob_given_false: 0.05
      platform: "state"
      to_state: "Charging"
    - entity_id: "sun.sun"
      prob_given_true: 0.75
      platform: "state"
      to_state: "below_horizon"
    - entity_id: "binary_sensor.orion_active"
      prob_given_true: 0.4
      platform: "state"
      to_state: "off"

These sensors are written in YAML. You describe a prior (at any given time, how likely is it this is true?), an optional threshold (only be true if the chance is above the threshold), and observations. These observations are in terms of entities or Home Assistant templates. Here’s a breakdown of this sensor:

How Well Does it Work?

I’d say it works pretty well. Some observations: