Bed occupancy sensor, using parts you have

hey i know this is late reply but i did this project and got around the slate bed with kids hard covered books and double sided taped.

3 Likes

Clever!

This is a really cool idea so I had to test it out myself!
I have added cut up sponges inbetween the paper for a higher range and I am quite happy with the result so far

However, since I had many Wemos D1 Mini lying around, I wanted to incorporate an ESP8826 rather than an ESP32, so for those of you that also rather use the ESP8826, here are a few pointers:

This library makes it possible - in short, you have an output and an input connected with a resistor and your selfmade capacity and calculate the time it takes for the input to trigger. The value you get is arbitrary, but is correlated to the capacity due to the time-constant RC

You have to be aware however that not every pin works due to internal pull up and pull down resistors, and you also have to take them into account when choosing a resistor to connect to as is discussed here

Important hereby is:

Input pin impedance

The esp pins seem to have a much lower impedance than the atmega pins. With a 1MOhm resistor, my Wemos D1 mini board was not able to pull up the receiver pin. With a 470kOhm resistor, it worked.

and

There are a limited number of pins on the ESP8266 which will work, I have identified them as follows, they are the ESP8266 GPIO identification, and this assignment must be used in your code.

GPIO 4 (used as output)
GPIO 5 (tested as Input)
GPIO 12 (tested as input)
GPIO 13 (tested as input)

and a bit offtopic:
I have also hooked up a pir motion sensor to track my sleeping pattern, apparently when you move around it is easier to wake up rather than when you are completely still. I am planning to combine this with my alarm clock - has anyone tried this concept yet?

2 Likes

Can I use a raspberry-pi 4 and connected the fields to it or with help of an MCP3008?

I would have to find out how to measure a capacitor with a raspberry pi 4?
I thought the MCP3008 could help me and bought one but unfortunately I can’t get anywhere with it.
I didn’t want to use an Arduino or an ESP32 for a project. I would like to read out my “capacitors” myself as I can measure them with my multimeter.

I was thinking of a way to have Alexa constantly announcing to my kid to go to bed. This project maybe the solution.

1 Like

I am new to ESPHome and MQTT. In the code, what do I input for these settings?

# Uncomment this if you use Home Assistant
#api:
#  password: “change_this_please!”


# Over-The-Air updates
ota:
  password: “change_this_please!”

I am planning to use MQTT Broker. How do I Configure the sensor with MQTT?

You really need to read the docs at esphome.io

Hi @duceduc, you will need to change these settings

# Fill your MQTT information
# Delete this if you're using Home Assistant
mqtt:
   broker: 10.0.0.2
   username: livingroom
   password: MyMQTTPassword

Thanks for the clarification. I didn’t realize I pasted the same 2 settings above. The other one is the mqtt settings as you have pasted. I was confused by the comment.

# Delete this if you're using Home Assistant

I am using Home Assistant.

Yes, what I meant to do was to make it easier for Home Assistant users not wanting to use MQTT, since the native ESPHome API integration is much easier to use. Sorry for the confusion :sweat_smile:

I used @N-l1’s paper pressure sensor as a guide and scaled up to cover the large middle portion of my bed (roughly where my hips and torso lie). Instead of paper I used a single 36"x60" closed-cell foam sheet and covered each side with copper-coated aluminum foil. I used closed-cell foam to allow the capacitance to rebound/reset after I leave the bed. I wired one side to a capacitance pin on my ESP32 and the reverse side to ground. Works like a champ with the following config in ESPHome:

esp32_touch:
  setup_mode: True
  iir_filter: 10ms
  sleep_duration: 1ms

binary_sensor:
  - platform: esp32_touch
    name: "ESP32 Whole Bed GPIO4"
    pin: GPIO4
    threshold: 4
    filters:
      - delayed_off: 2500ms

In Home Assistant I did wrap this sensor with a second sensor to prevent flapping while I tested out threshold values:

binary_sensor:
  - platform: template
    sensors:
      esp32_bed_occupancy:
        entity_id: binary_sensor.esp32_whole_bed_pressure
        delay_on:
          seconds: 2
        delay_off:
          seconds: 2
        value_template: >-
          {{
            is_state('binary_sensor.esp32_whole_bed_pressure', 'on')
          }}

Going forward I’ll either integrate this logic into ESPHome or remove it altogether.

1 Like

@N-l1
I’ve got it setup and only used 3 pads for a kid’s size bed.

Threshold :

Top: 3
Middle: 3
Bottom: 4

Got a question:
Is it possible to disable the red LED on the ESP32?

Hi @duceduc, if you are using the ESP32 NodeMCU and referring to the red LED, it seems like it is an indicator of whether there is power and is unable to be turned off. However, the blue LED on the ESP32 is connected to the GPIO2 pin and you can turn it off (when there is no error) by setting it as a status LED in ESPHome:

status_led:
  pin: GPIO2

Hmm. I do have the ESP32 NodeMCU board. However, I have seen only the red power light on. Never the blue. Does that mean I have an error somewhere? The sensors seems to be working proper.

No :sweat_smile:, what I meant was simply you cannot turn the red LED off but you will be able to control the blue LED (it is not on by default). You can set the blue LED as a status LED in ESPHome and it will blink if there is an error.

ah, gotcha.

Now I understand why some of you have padded your sensors. I have came up with my version since I don’t have any foam pads laying around.

I used the battery holder springs. Hot glue them to a plastic sheet and lay another sheet over it for some added padding since it is kinda pointy. Sandwich between the 2 foil sheets and include them in plastic folder.

I will report back to see if it is any better than my first attempt.

Update.1: Initial Test
Sensors seems to be working. Threshold numbers have changed of course. Also, the hot glue used to bond the springs is making this clicking sound when the spring wire rub against the glue. I place a piece of cotton between the two. Annoying clicks FIXED.

Threshold:

Top: 26
Middle: 23
Bottom: 30

I need to sleep on it over night to really test the springs if it will rebound back to it’s original position in an instant.

1 Like

Hi Trevor,

Please can you share your Esphome and HA configurations for “Bed sensor”?

Many thanks!

Hi all,
Is it possible to know how many people are on the bed? (Me and my wife)
I had with weight sensor (load cell) it was pretty simple but it was not stable enough for me.
Esphome config:

esphome:
  name: bed_pressure_sensor
  platform: ESP32
  board: nodemcu-32s

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  manual_ip:
    static_ip: xxxxxxxx
    gateway: xxxxxxx
    subnet: xxxxxxxx
  
# Enable logging
logger:

# Enable Home Assistant API
api:
  password: !secret api_password

ota:
  password: !secret ota_password

# Setting up the pressure mats
esp32_touch:
# Delete this after you are done with the setup process
   setup_mode: true
# All the pressure mats
# Change the pins to the pins you have used

binary_sensor:
###Right side
   - platform: esp32_touch
     name: "Bottom Right"
     pin: GPIO14
# See step 11
     threshold: 4
     id: top_right
     filters:
       - delayed_off: 2500ms
       
   - platform: esp32_touch
     name: "Top Right"
     pin: GPIO27
# See step 11
     threshold: 5
     id: bottom_right
     filters:
       - delayed_off: 2500ms
###Left side
   - platform: esp32_touch
     name: "Bottom Left"
     pin: GPIO15
# See step 11
     threshold: 7
     id: top_left
     filters:
       - delayed_off: 2500ms   
   - platform: esp32_touch
     name: "Top Left"
     pin: GPIO33
# See step 11
     threshold: 7
     id: bottom_left
     filters:
       - delayed_off: 2500ms

   - platform: template
     name: "1"
     lambda: |-
       if (id(top_left).state ||
           id(bottom_left).state) {
         return true;
       } else {
         return false;
       }

   - platform: template
     name: "Right Side Bed Sensor"
     lambda: |-
       if (id(top_right).state ||
           id(bottom_right).state) {
         return true;
       } else {
         return false;
       }

# # The main sensor that is created using the all the zones    
#   - platform: template
#     name: "Bed Sensor"
#     lambda: |-
#       if (id(top_right).state ||
#           id(top_left).state ||
#           id(bottom_right).state ||
#           id(bottom_left).state) {
#         return true;
#       } else {
#         return false;
#       }

text_sensor:
  - platform: version
    name: bed_pressure_sensor_esphome_version

  - platform: template
    name: Bed pressure sensor uptime
    lambda: |-
      uint32_t dur = id(uptime_s).state;
      int dys = 0;
      int hrs = 0;
      int mnts = 0;
      if (dur > 86399) {
        dys = trunc(dur / 86400);
        dur = dur - (dys * 86400);
      }
      if (dur > 3599) {
        hrs = trunc(dur / 3600);
        dur = dur - (hrs * 3600);
      }
      if (dur > 59) {
        mnts = trunc(dur / 60);
        dur = dur - (mnts * 60);
      }
      char buffer[17];
      sprintf(buffer, "%ud %02uh %02um %02us", dys, hrs, mnts, dur);
      return {buffer};
    icon: mdi:clock-start
    update_interval: 30s

switch:
  - platform: gpio
    pin: GPIO26
    name: "UnderBed light"
    inverted: yes

sensor:
  - platform: uptime
    id: uptime_s
    update_interval: 30s
  - platform: wifi_signal
    name: "Bed pressure sensor WiFi Signal"
    update_interval: 60s
1 Like

This idea works great if there is just one sensor. However, if I add a second sensor, it becomes completely unstable. The sensors are mostly unavailable, the logs show connectivity errors. I wonder if it is the chip. Can anyone confirm using multiple sensors with a single module? I am using mhetesp32devkit.