ESPHome

DIY bed sensors: knowing when the whole house is asleep

A thin-film pressure strip under the mattress, an ESP32 and a few lines of ESPHome. Three years later it's still the trigger that locks the house at night and starts the coffee in the morning.

DIY bed sensors: knowing when the whole house is asleep

Some of the most useful things in my smart home are also the simplest. The bed sensors are a good example. Each one is a pressure strip under the mattress, an ESP32 and about 40 lines of ESPHome YAML. They have run in our beds for three years without a single problem, and they are behind two of the automations I would miss most: locking the house down when everyone is in bed, and starting the morning when the first person gets up.

Why a bed sensor?

Home Assistant already knows who is home, from our phones. What it can't tell is whether the people who are home have gone to bed. Time-based "good night" automations break the first time you stay up late, and a button by the bed is one more thing to forget.

With a sensor in each bed, Home Assistant can answer one simple question: is everyone who is home also in bed? When the answer turns to yes, the house goes to sleep. When it turns to no in the morning, the house wakes up.

What you need

  • A thin-film pressure strip. I use a 600 mm flexible force-sensitive resistor (FSR) strip, sold as "SF15-600". You can find it on Amazon (not an affiliate link). It's long enough to cover most of the width of a single mattress, so it doesn't matter much where on the bed you lie.
  • An ESP32. I use an ESP32-C6 dev board, but any ESP32 that ESPHome supports will do. You only need one GPIO pin.
  • A USB power supply and a cable long enough to reach the bed.

That's it: one sensor and one ESP32 per bed.

Wiring

An FSR is just a resistor that drops in value when pressed. Connect one lead of the strip to GPIO2 and the other to GND. You don't need an extra resistor, because the ESP32's internal pull-up holds the pin high when nobody is in bed. When someone lies down, the strip's resistance drops and pulls the pin low.

In other words, the ESP32 treats the strip as a pressure-operated switch. You don't need an analog reading or any calibration.

The ESPHome configuration

Here is the full configuration for my sensor. My wife's is the same apart from the name. I've replaced the keys and passwords with !secret references.

esphome:
  name: bedsensor-bjarne
  friendly_name: Bedsensor Bjarne

esp32:
  board: esp32-c6-devkitc-1
  framework:
    type: esp-idf
    advanced:
      flash_chip: generic

logger:

api:
  encryption:
    key: !secret bedsensor_api_key

ota:
  - platform: esphome

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Fallback hotspot in case Wi-Fi fails
  ap:
    ssid: "Bedsensor-Bjarne"
    password: !secret fallback_ap_password

captive_portal:

binary_sensor:
  - platform: gpio
    name: "Bjarnes Bed Occupied"
    icon: mdi:bed
    pin:
      number: GPIO2
      mode: INPUT_PULLUP
      inverted: true
    device_class: occupancy
    filters:
      - delayed_on: 2s
      - delayed_off: 15s

The binary sensor is where the work happens:

  • INPUT_PULLUP turns on the internal pull-up resistor, so no external parts are needed.
  • inverted: true makes the sensor read on when the pin is pulled low, which happens when someone is in bed.
  • delayed_on: 2s ignores short presses, like sitting on the edge of the bed or a cat walking across it.
  • delayed_off: 15s is the important one. When you turn over in your sleep, the pressure on the strip can disappear for a moment. Without this filter, Home Assistant would think you got up every time you rolled over.

Placement: experiment!

This is the only part that needs some patience. Our sensors sit under the top mattress (the topper), across the bed at about the height of the upper back. That's where most of your weight rests whichever way you lie.

Your mattress, your sleeping position and your weight will all be different from ours, so treat that as a starting point. Open the sensor in Home Assistant or the ESPHome logs, lie down, roll over, lie on your side and sit up, and watch the state. Move the strip until it reads on reliably in every position you sleep in, and off when you get up.

From two beds to one question

A bed sensor on its own isn't that useful. It becomes useful when you combine it with presence. I made two template binary sensors (Settings → Devices & services → Helpers → Template):

  • Residents in Bed is on when everyone who is home is in bed. A person who is away counts as "in bed", so the house still goes to sleep when one of us is travelling. At least one person has to be home, or it would be on while the house is empty.
  • Residents NOT in Bed is the opposite: on when everyone who is home is out of bed.

Here is a simplified version of the first template for two people. Swap in your own person and bed sensor entities:

{% set people = [
  ('person.bjarne', 'binary_sensor.bedsensor_bjarne_bjarnes_bed_occupied'),
  ('person.partner', 'binary_sensor.bedsensor_partner_bed_occupied'),
] %}
{% set ns = namespace(home=0, ok=true) %}
{% for person, bed in people %}
  {% if is_state(person, 'home') %}
    {% set ns.home = ns.home + 1 %}
    {% if not is_state(bed, 'on') %}{% set ns.ok = false %}{% endif %}
  {% endif %}
{% endfor %}
{{ ns.home > 0 and ns.ok }}

For "Residents NOT in Bed", change is_state(bed, 'on') to is_state(bed, 'off'). Each extra person is one more line in the list.

What the automations do

Now that one sensor answers the question, the automations are simple. I won't go through every step, because your house will be different from mine, but here is what ours do. Use it as inspiration and build your own on the same triggers.

When everyone is in bed

Trigger: Residents in Bed turns on and stays on for 10 seconds, between 21:00 and 05:00.

  • It first checks the outside doors. If one is open, the Sonos speaker in the bedroom says which ones, and the automation stops there. There's no point locking up with the terrace door open.
  • Otherwise it locks the front door, the office annex and the shed.
  • It arms the alarm in night mode, unless we have guests staying or I've overridden the alarm.
  • It turns off all the remaining "bedtime" lights in the house.
  • It sets the outdoor cameras to record all the time.
  • After 20 seconds it checks that everything worked and announces in the bedroom either "The house is now locked down for the night" or exactly what failed: which lock didn't lock, or that the alarm couldn't be armed. It only announces this once per night, so getting up for a glass of water doesn't start the whole thing again.

When the first person gets up

Trigger: Residents in Bed turns off and stays off for 10 seconds, between 05:00 and 10:00 on workdays or between 06:00 and 10:00 on days off. (I use a workday sensor that also knows about holidays and our vacation calendar.)

  • It disarms the alarm and unlocks the doors.
  • It stops the cameras' constant recording.
  • If it's still dark, it turns on the kitchen lights at 60%.
  • It switches on the coffee machine. ☕
  • For five minutes, an LED strip in the kitchen glows green if the alarm was disarmed correctly, or red if it wasn't. It's a quiet "all good" signal before you open a door.

When everyone is up

Trigger: Residents NOT in Bed turns on between 05:00 and 10:00.

  • The bedroom clock switches from night to day mode, and the air conditioning in the bedroom turns off.

Three years later

This has worked flawlessly for my wife and me for three years. The strips haven't worn out, the ESP32s just keep running, and the 15-second off delay has meant we've never been "woken up" by the house because one of us turned over. It's a cheap afternoon project, and of everything in the house, it's one of the things I'd miss most if it stopped working.

The trigger is the valuable part. Once Home Assistant knows when everyone is in bed and when the first person gets up, you can hang whatever fits your home on it: heating, blinds, reminders, a morning briefing. Have fun building yours.