Bed occupancy DIY sensor

I hope that’s a resistive mat and not a switch. Otherwise the weight of your mattress is going to trigger it permanently.

1 Like

Let u know the outcome

1 Like

If it doesn’t work out under the mattress, I’m currently using one of those pressure mats under a floor mat to trigger a light when the mat is stepped on. Works quite well!

@tom_l Look forward to hearing how well the strain gauge works. I’ve been wanting to do a bed occupancy sensor for a while.

I’m using the Xiaomi vibration sensor. When it detects a vibration and there is no movement in the house for 10 minutes HA is considering me in bed. It works really good.

2 Likes

I have been using 2 off the door mat sensors for a couple of months. As long as you have it positioned perfectly under the mattress it works perfectly. Too close to the sides and it’s triggered all the time.

First night’s data with ‘in bed’ indicated by the added green line:

Unfortunately it’s a bit noisy using the raw data from the ESP every 30 seconds. Though it does work surprisingly well as a sleep monitor and clearly shows periods of light and deep sleep:

However this is not what I want. So I need to average a number of readings in the ESP (it is very fast to update) and send a 30 or 60 second average to HA. I dont want to filter in HA because that would necessitate updates from the sensor far too often, loading up the MQTT server and Pi’s CPU. Mainly because I want a fast response time from this sensor (30-60 seconds max).

Unfortunately I’m having trouble getting the ESPhome moving window average to work. That’s today’s challenge.

2 Likes

Thanks for the heads up

When I do this kind of project I have found taking the last 10 readings removing the 2 outliers (highest and lowest) and then averaging is the best result.

That would be great but I can’t even get an average working:

Interresting, finally found something usefull for this vibration sensor. Do you mind sharing how your automation works? Do you continuously get vibration data from the sensor and use the no motion as a condition?

You can find the automations and the script here: https://gist.github.com/dnuc/9816782b796c07e5139e95d39306006a

2 Likes

When I did something similar with Arduino and temperature sensor my “averaging” function was to define an integer variable, read 50 or so values adding each to the variable, then just divide by 50 and report that reading. It’s not a true sliding windows, but good enough for this application.

Add a couple of seconds pause between each report to not blow up your mqtt. And be careful to not overrun your integer variable (declare it as a long if necessary).

I finally got windowed averaging working on the ESP. I had to drop monitoring the status and wifi of my sensor though. Hopefully this will will be rectified with an ESPhome update.

So now I have this, a perfect bed occupancy sensor for less than $15 AUD that is easy to install (no messing about with load cells under bed legs).

When I first tested the filtered sensor I was surprised to see a much bigger difference in signal for in and out of bed. However when I went to bed there was a ‘clunk’ as the slat dropped into its guide correctly and the sensor value difference dropped (see chart above). I worked out that lifting the slat that has the strain gauge fitted above the other slats by 5mm using a shim gave a much better signal as it is taking more weight. This is not noticeable through the innerspring mattress.

Only problem now is that I have no use for my neat DIY Velostat pressure sensor:

I’m not too fussed about this, especially as using it with the strain gauge amplifier would have necessitated a bit of rocket surgery. The sensor operates from about 2K Ohm nominal to 1.2K Ohm when pressed. Where as the strain gauge was 360 Ohm nominal. This means I would have had to replace three 0805 360R resistors with 2K Ohm versions in the module’s Wheatstone bridge. I am set up with a stereo microscope to do this but am glad I don’t have to. I would also have had to make two of these sensors (and use an ESP32 with more than one analogue input) and fit them to either side of the bed - vs only one strain gauge sensor on the slat.

So in summary. Success!

Now I just have to fit another one to the guest bed and work out the convoluted logic for turning everything off at night depending on if I have guests and/or if I am home or not.

5 Likes

Hi, cool stuff. I am very interested in occupancy and pressure sensors.

In 2017 I have designed a simple and not expensive pressure sensor based on a capacitive principle. It was made for Fochica, a device designed to alert parents that unintentionally leave their children in a car.

The article is at: https://blog.yavilevich.com/2017/10/40-cent-diy-pressure-sensor-based-on-a-capacitive-principle/ . LMK if you have feedback or improvements.

I have two HX711 with four Load Cell each, mounted between bed-frame. It works very well so far. Also you can see if you gained weight :slight_smile: (thus some downsides regarding WAF (Woman acceptance factor) - so I converted int a Binary Occupancy Sensor)

I can add some details if someone interested

Yes, please!

UPDATE:

The cheap ebay strain gauge sensor and D1 mini solution has been working perfectly all week.

2 Likes

Using the data from your sensor you could start to calculate time in deep sleep by counting the periods in bed with less fluctuations.

1 Like

If you look back at the image in this post where I was using the unfiltered data from the strain gauge it’s obvious where the deep sleep periods are.

Unfortunately now that I am filtering the data in the ESP module to get a reliable ‘in bed’ state it’s less obvious where these periods are. And I don’t really have any use for this data.

This is great, I might have a go at this. Seems like a good first project for someone whose only electronics experience so far is flashing sonoffs.

A couple of questions:

  • How would this fare in a double bed with two people? And
  • Regarding guest beds, is there a question over privacy here as this might indicate when the occupants are doing something other than sleeping?
1 Like