Bed occupancy sensor, using parts you have

@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.

What do you mean by “one sensor”? That you can only add one of the tin foil mats? Than something’s wrong.

I’m running four “sensors” and an LED strip with that specific ESP32.

This is my esphome config for this one:

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

# [...]

# The actual thingy
esp32_touch:
  # Comment after setup is done
  #setup_mode: true  

binary_sensor:
  - platform: esp32_touch
    id: steffi_top
    name: "Bedroom Bed Steffi top"
    pin: GPIO15
    threshold: 14
  - platform: esp32_touch
    id: steffi_bottom
    name: "Bedroom Bed Steffi bottom"
    pin: GPIO14
    threshold: 11
  - platform: esp32_touch
    id: pat_top
    name: "Bedroom Bed Pat top"
    pin: GPIO13
    threshold: 14
  - platform: esp32_touch
    id: pat_bottom
    name: "Bedroom Bed Pat bottom"
    pin: GPIO12
    threshold: 12
    
  - platform: template
    name: "Bedroom Bed Steffi Occupancy"
    lambda: |-
      if (id(steffi_top).state && id(steffi_bottom).state) {
        return true;
      } else {
        return false;
      }
  - platform: template
    name: "Bedroom Bed Pat Occupancy"
    lambda: |-
      if (id(pat_top).state && id(pat_bottom).state) {
        return true;
      } else {
        return false;
      }

light:
  - platform: rgb
    name: "Bedroom_bed_bottom_led"
    red: output_red
    green: output_green
    blue: output_blue
    restore_mode: RESTORE_DEFAULT_OFF

output:
  - platform: ledc
    id: output_red
    pin: GPIO2
  - platform: ledc
    id: output_green
    pin: GPIO4
  - platform: ledc
    id: output_blue
    pin: GPIO5

I’m not happy with the amount of sensor data collected and send, but that is a different problem for a different time. :smiley:

As someone who is going to try this soon (i’m sick of drifting strain gauges) what are you unhappy about?

Too many false triggers?

Or does it drop out too often when occupied?

Unhappy would sound to harsh, I love this project! :wink: It works great for me, the data that is collected is accurat (in my case) and reliable. At least as long as you check the threshold every few weeks/months. :slight_smile:

The sensor itself works like a charme, it sends data since I installed it - no errors or things like that. :smiley:

But the amount of data, the frequency for each sensor if you will, is huge. I get two to three values per sensor per second. That amounts to twelve values every second for one bed. I’m quite sure there would be ways to control that via esphome, but I didn’t had the time or muse to take a look at it. :wink:

Right now it doesn’t disturb anything, so I’m not to bothered, but in the future I need to resolve this, it’s simply not nice. :slight_smile: My idea would be to hide the real sensors for HA and only send the lamda value every 30 seconds to HA. That should bring some relief. :slight_smile:

Yes, exactly. I have three mats. If I run any one of them (by commenting out the others), it is stable and works perfectly. However, if I run any two of them, every sensor on the chip becomes unavailable most of the time. I can vary how bad the problem is by changing settings for

esp32_touch:
# Delete this after you are done with the setup process
#  setup_mode: true 
  iir_filter: 10ms
  sleep_duration: 400ms

or

  - platform: esp32_touch
    name: "Master Bed Top Right"
    pin: GPIO27
    threshold: 40
    filters:
      - delayed_off: 1000ms
    id: top_right

But it is always unstable. If I run all three there is nothing but errors. I notice in your yaml script that you are using a different module. IN particular, you are using the ESP32-S2 chip, whereas I am using ESP32. I wonder if/why that would make a difference.

I’m using this NodeMCU with an ESP32: https://az-delivery.myshopify.com/products/esp32-developmentboard?_pos=2&_sid=543d96ebe&_ss=r

I’d suggest you start minimal. :slight_smile: No delays or filters, just the basics and see how far that goes.
What I’d check as well are the used GPIOs. Are they really able to work with the touch input (as this is what we use here)? With my board eg GPIO27 is touch, GPIO26 is not. :wink:
And is your ground correct? I wired all four sensors together to have one good ground.

On no. That’s the exact reason I was going to change from strain gauges. If it offers no improvement then there’s not much point me changing to this.

Yeah, I began simple, and used the right pins, supposedly (27,32,33). In fact, this is confirmed by the fact that each one works individually. I tried without delays and filters, etc, but it always fails. Changing things systematically is what has led me to suspect that it is the chip. I wanted to use a chip that also has bluetooth to detect presence, but that might not be possible.

That is going to depend on what spacer you use. The open-celled foams will have a very long relaxation time that makes them problematic. A good closed-cell foam shouldn’t have this problem, but my other issue is preventing me from checking that right now.

As the entry in this thread shows, I had installed this sensors at the end of June 2020. Since then I had to adjust the values once. :slight_smile: I’d speculate it depends on the temperature. The change in values was just around the time where we fell under 10°C to 15°C outside temperature.

And I changed the bed frame in the meantime (without needing to adjust the sensors), so I’d say it is not very often. What I do, but I guess that’s my inner “Monk”, I check the sensors every few weeks as a “preventative maintenance”. :slight_smile:

Hey! I had that issue when trying to use multiple touch sensors for my leak detectors:

Indeed! That looks just like my problem, but you are also using the ESP32-S2 chip. So how is @paddy0174 getting his to work ?? You also are using the iir filter. I thought that had checked that, but let me double check…it is the filter. I turned off ‘iir_filter’ and it is more stable. @paddy0174 was right! I can leave the ‘sleep_duration’ setting, which might help with the spamming problem.

1 Like