Sleep is not something I can directly observe in my smart home (at least not until I build that DIY bed sensor). It would be nice to know though – then the alarm could be set, doors locked, thermostat set, music volume slowly dimmed, and internet bandwidth reprioritized automatically when it’s time for bed. We spend a lot of time sleeping in our homes, so it makes sense to be able to detect it.
Using Home Assistant’s Bayesian binary sensor it’s possible to guess pretty accurately when everyone’s tucked away. See below for example YAML and explanation.
This assumes you have your binary sensor’s split into a separate binary_sensor.yaml file.
- platform: bayesian prior: 0.5 name: 'Bayesian Sleeping' probability_threshold: 0.85 observations: - entity_id: 'group.tracked_users' prob_given_true: 0.99 prob_given_false: 0.5 platform: 'state' to_state: 'home' - entity_id: 'sensor.sun' prob_given_true: 0.9 prob_given_false: 0.2 platform: 'state' to_state: 'below_horizon' - entity_id: 'group.all_lights' prob_given_true: 0.9 prob_given_false: 0.4 platform: 'state' to_state: 'off' - entity_id: 'sensor.coffeemaker' prob_given_true: 0.8 prob_given_false: 0.4 platform: 'state' to_state: 'Ready' - entity_id: 'variable.last_motion' prob_given_true: 0.9 prob_given_false: 0.3 platform: 'state' to_state: 'Bathroom Motion' - entity_id: 'binary_sensor.brad_phone_pluggedin' prob_given_true: 0.95 prob_given_false: 0.5 platform: 'state' to_state: 'on'
What’s Going On Here?
The Bayesian sensor attempts to guess whether something is true or not based on a series of observations. Those observations are weighted by how likely it is they are occurring currently, if our guess is either true or false. Let’s break down this example, trying to guess if I am asleep or not.
- prior – The overall probability that I am asleep. Let’s say roughly a half of the time I’m home.
- probability_threshold – How sure should we be before guessing true? 85% – pretty sure.
Now, for the observations. prob_given_true is the likelihood the observed state is occurring if I am asleep. The prob_given_false is the likelihood the observed state is occurring, but I’m not asleep.
- Home – Obviously, I need to be home to be asleep, so 99% if true. But just because I’m home doesn’t mean I’m passed out. Let’s say I’m home but not asleep half the time.
- Sun Down – If it’s dark out that certainly increases the chances I’m asleep dramatically. Let’s set it to 90% if true, 20% if false
- All lights Off? If all the lights are off and I’m home, I’m either asleep or watching a movie. So another strong indicator – but not always.
- Coffee Maker Ready? I’m a big coffee drinker and usually set up the coffee machine before bed, so another observation that’s useful here.
- Last Motion in Bathroom – I always brush my teeth before bed, so if that was the last place Home Assistant saw motion, another likely indicator that I went to bed after.
- Phone Plugged In – Batteries are the bane of my existence, and I always, always plug the phone in at night. Another very strong indicator I’m asleep.
None of these things individually are very helpful for knowing whether I am asleep or not. Taken together, it is very accurate. Think creatively about the observed states you already are tracking with Home Assistant, how those things could possibly indicate something like sleep.
By making many observations based on small sensor values, we are making our home truly ‘smart’ and able to do things with no interaction from the users.