After reading about someone else that just published his DIY bed occupancy sensor, I thought why not share it myself and the trouble I’ve had. I am not done yet as I am missing the MQTT integration.
The reason why I went for the more expensive option of Pi0WH + pHAT was the fact that I had already ordered it before knowing more about the esp8266 or similar options. The advantages to me as it is now:
- Python: I can use Python to code the sensor readings and publishing
- Pre-existing library: I could leverage the library that already existed for the readings
- Familiar environment: I am familiar with the Pi and have zero experience with the Arduino IDE and what it would take to flash the esp8266. This will hopefully change in the future, but this is my first DIY project and in the past I had problems finishing projects.
The problems as they currently stand and what I’ve encountered:
- Faulty wire connections for some of the dupont cables that I’ve used
- Attaching the FSR to a female header is very difficult given the fact that it’s rather thin and doesn’t really attach itself inside the header
- A faulty sensor by Adafruit from what it seems. I’ve ordered eight FSRs and one of them is not working properly. I am getting a non-zero value whereas the others consistently report 0. I’ve tried everything to fix it and have now isolated the problem to the sensor. Nonetheless, I’ve installed the sensor and figured I could work by changes in the value rather than the value itself in order to determine the current state.
I’ve installed four of the sensors on the slatted frame. Two on each side. The reason behind it is that I want to determine the position of someone and if my girlfriend is using too much of my side of the bed. I wanted to know over time how the values develop and maybe draw a few insights from it. I figured that if it wasn’t necessary, I could re-purpose the FSRs.
What I haven’t done:
- MQTT integration
- HA integration
- Sensor based automation
- Create a 3D-printed closure
Currently, I am only reading the values from the sensors every second and store them in a text file. I am doing this to determine the changes over time and get a better understanding of what is going on.
As a next step, I want to collect the values per second over a period of a minute and do the following calculations:
- Determine 20th and 80th percentile
- Get the average of all the values between those two percentiles
- Get the median
- Get the biggest change in for the 1-minute interval
- Standard deviation of the value changes
Based on that, I want to determine if someone is still in bed or not. Over time, I want to be able to determine several states:
- In bed
- Sleeping (probably unrealistic to differentiate between “in bed” and “asleep”)
- Nightly bathroom break
- Just out of bed
- Out of bed
The difference between “nightly bathroom break”, “just out of bed” and “out of bed” should be determined by time. A bathroom break shouldn’t take longer than 5 minutes, just out of bed is the first fifteen minutes and out of bed is anything after that. My problem is a bit that I would need to retrospectively change the state for the various states, i.e. the first five minutes of someone getting out of bed should not be nightly bathroom break, but should always be out of bed (does that make sense?).
The automations/states that I plan on getting out of this:
- In bed state: who is still in bed? If one person is in bed then some things should work, some don’t and that should depend on which person.
- Automatically turn stuff off
- Automatically turn stuff on
- Allow some Google Home devices to be eligible to speak to again (not even sure if this is possible to do to be honest), at least turn up volume again
I think the in bed/out of bed sensor is central to automation and whenever I’ve tried to do some automations with lighting in the past based on time, it fell short of the variety of states that an apartment can be in.
The HA integration otherwise should be fairly straight-forward using the MQTT broker. I hope I’ll be able to change the state retrospectively, maybe someone has an idea about that?
The last aspect is the enclosure, I live in Zurich, Switzerland, and the last time I had something printed, I paid $40 for a small item. So, this time I might go a different route and order from another place in the world, if feasible.
So, here is the interesting part now and I want to show a few pictures (asorted):
Given that I am currently on vacation, my sensor is pulling data, but nothing meaningful for the time being. Looking forward to analyzing the results when I return back home.
Any feedback is welcomed!