Bed Sensor With Home Assistant

Overview

I had got a set of Ikea FYRTUR which I managed to pair with the SmartThings v3 hub.

The goal was to automate the blinds, lights and heated blankets.

Instructions and details are provided here as is.

Existing Method


At the time, I was using Withings Sleep Mat, IFTTT and Home Assistant to create the workflow for closing.

The Sleep Mat would provide the presence detection, go to Withing’s web service and trigger an IFTTT applet. The Applet would in turn call a web hook in Home assistant with data to say if someone got on (in) to bed or out. IFTTT doesn’t have a great reputation for speed however in this case it doesn’t matter.

The flaws with this setup was the mat was designed for a single person. If I rolled onto the other side of the bed or if someone else got into bed on the other side then the mat may not pick that up.

A second Sleep Mat may solve the issue however they retail for £100 and not for the intended purpose of sleep tracking.

DIY materials

Here comes the DIY method. It requires soldering so it may not be for everyone but it works out a lot cheaper.

Bill of materials:

All prices above were at the time of purchased between 2020-2021. The total was £53.82. I used a spare USB cable and I had solder and soldering iron already.

Optional extras in addition to the above:

The Wemos D1 mini is the brains for measuring and sending the measurements to Home Assistant. The Wemos D1 mini could be replaced with any ESP32/ESP8266 board. The Pro is a bit overkill but for the extra £1 or so I get a pig tail WiFi antenna as the main benefit.

The FSRs are the sensors that change values when pressure is applied to them. This includes bending action. The FSR can vary in shape and size. I planned to lay them on the bed slats so SF15-600 was sufficient. Pay attention to the max weight as well.

The ADS1115 is used to extended the number of sensors the Wemos can support. It only has 1 analogue pin so in theory it can only read one sensor. The ADS1115 multiplexer listed supports 4 analogue pins and sends those readings digitally to the controller (Wemos).

Wemos D1 Mini Firmware

I prefer to get the software done and out the way first. This is so that you can connect and test the hardware when it’s ready.

I use Tasmota because it provides a web UI to configure everything. Home Assistant works best with ESPHome but lacks Web UI so it might be a future project.

To get Tasmota firmware on the Wemos, download the Tasmotizer. A good tutorial to do this is Measure Propane, CO2, Salt, and so much more with this DIY MQTT Weight Sensor using Tasmota at 4:15.

Plug in the Wemos D1 Mini into a computer using a USB cable. Make sure the port is correct because some devices like keyboards which you do not want to flash.

For the image, select release and use the tasmota-sensor.bin file. This is a pre-built image to handle various sensors it can be connected to the ESP chip. Select Self-resetting for a Wemos and erase before flashing. You may also want to back up the existing firmware before erasing. Check the check box to do this and it will ask to save this somewhere. When ready, press the Tasmotize! button.

In Windows, the USB disconnected and connected sounds would play. Ensure you don’t disconnect the power when it is flashing the firmware.

Create a username and password for your preferred MQTT broker. I use the Mosquitto which is available as an add-on in Home Assistant

Create a username and password for Wemos.

When it’s complete, re-select the port again and proceed to setup the WiFi and MQTT details. WiFi is fairly self explanatory. Use the MQTT details to add the username and password. The host should be your MQTT broker or Home Assistant address. The topic is like the queue the messages from the device will go to. Set this to what you like with no spaces. The name/friendly name can be set to what you like.

Once the configuration is sent to the Wemos D1 mini, the device will reset again. When it’s back up, use the Get IP button to see what IP address it is connected to.

Software

Use the IP address from the above and open the site in your browser connected on the same network.

Go to Configuration > Configure Module and select Generic (18) in the module type. Press the save button and the device will restart.

Go back to Configuration > Configure Module.

Set A0 GPIO17 to ADC Input if you are not using the ADS1115.

For ADS1115 set the following:

Save the settings and it will restart again. Depending on if you are using and ADS1115 or not, the next time it starts up, there will be values shown.

Hardware

Regardless if you’re using a ADS1115 or not these are common bits to have before going below to whichever setup you’re going for.

The resistors used are 3.3k ohm. The FSR does not have any polarity so it does not matter which end is connected to positive or negative.

Unplug the Wemos d1 Mini from the USB power first before starting.

Hardware – Without ADS1115

Wire the force sensistive resistors to the A0 pin on the Wemos d1 mini.

If you have more than one FSR then you could wire it like this:

The downside to not using a muxer like ADS1115 is if either one has weight on the sensor it will be read by the Wemos however there is no way of telling which one.

Hardware – With ADS1115

Connect the Wemos d1 mini to the ADS1115 like this:
Wemos D1 mini to ADS1115

Wire the FSR to the ADS1115 like the below:

To test it, power on the Wemos with the USB cable. Navigate to the website and try bending (not so far to crease the strip) or put some pressure on the strip and the values should change. The site may take a second or two to update.

Hardware Checks And Troubleshooting

Plug the USB power into the Wemos. Tasmota should be able to see the ADS1115 chip and display all the analogue pin values.

At this point, check all the connections. If the strips are tort straight, the values should be very low (approximately 100 or less). When you bend (do not bend it too far to cause a crease) or pinch the strip with the pads of your finger then Tasmota should show an increase in the values being read (100+). The critical thing is the amount of change from straight no pressure to pressure and / or a bend in the FSR.

If you encounter any issues at this point, check all the connections. You can unplug the wire from the force resistive sensor and Tasmota should read ideally zero however anything less than 100 is good. I have seen it go to a negative number which doesn’t make sense to me.

You could swap the pins going from A0-A3 on ADS1115 to see if it’s a faulty board.

Bed Install

The hard part is done. Unplug everything and I stuffed all the boards including the Wemos into a cardboard box. More elegant solutions are available.

The bed will need a USB power and WiFi signal. In my example, we’re fortunate to have an otterman style bed so it made getting under the bed really easy.

The bed has horizontal bed slats so I lined up to where the top of the back / shoulder would normally be for the top strips. The lower strips I aimed for the butt area. The idea being those areas would mostly contact the bed and put weight to change the resistance on the strip.
FSR on bed slats

I used Sellotape to hold the strips in place and electrical tape to provide relief and fix the wires.
Tape holding down wiring

Once the required strips were installed, connect the power to the Wemos, check it’s working and it should have very low readings with no weight on the strips. When everything is working, lay the mattress on top and check the readings again. There should be very obvious change between mattress on and when someone lays on the bed. For mine it was a minimum difference of 1000+.

Automations

I won’t go into detail on these (unless people want me to) and to give people ideas of what I’m using it for and automations I have planned.

Quirks

I noticed using the ADS1115 over the analogue input on the Wemos D1 was slower to update in Home Assistant. Not sure if this why so if anyone has any suggestion please let me know. It’s about 30-120 seconds difference between someone getting into bed and the blinds closing.

There’s also some sort of noise which I cannot get rid of. The problem with the noise is it can trigger false positives. Unplugging the FSR shows 0-50 values so I suspect it’s a problem with the wiring or FSR rather than the ADS1115. As it’s on the right side of the bed, I wonder if there’s some sort of uneven pressure between the left and right side.

The two history graphs are separate graphs so the x-axis with time may not 100% line up but it’s close enough.

You can see from 9am there are two sensors that stay relatively high which makes it difficult to set a threshold of when it senses someone in or out of bed. Any help with this would be appreciated.

Summary


It has been a really rewarding project to do and helps (when it works) with moving towards the lazy automated life at home. I’m sure there’s a better way to package all of this up so any help with that would be great. I built a second one for another bed it reads better so I know the schematic is correct.

Next stop maybe chairs / sofas.

ESp8266 and force sensitive resistor example

Part 1 – How to Expand Tasmota/Sonoff Devices with the I2C Protocol – I²C

About Danny

I.T software professional always studying and applying the knowledge gained and one way of doing this is to blog. Danny also has participates in a part time project called Energy@Home [http://code.google.com/p/energyathome/] for monitoring energy usage on a premise. Dedicated to I.T since studying pure Information Technology since the age of 16, Danny Tsang working in the field that he has aimed for since leaving school. View all posts by Danny → This entry was posted in Hardware, Houseware and tagged , , , , , , , , , , , . Bookmark the permalink.

5 Responses to Bed Sensor With Home Assistant

  1. Johan Uribe says:

    Can you tell me please the differeces if I choose to use an ESP32 instead of the wemos d1 pro? And if you know, which would be the hardware diagram for it? Excelent work. Thanks

  2. Amerigo Rossi says:

    Hi Danny, do you think I can use a battery shield for this project? If yes what capacity of battery it’s better to recharge it 1 time for month?

    • Danny says:

      Hi Amerigo,

      I haven’t tried using a battery with this setup. I have experimented with running a ESP32 with a BME680 and a resistive take on a 20amp USB battery running at 1 minute sampling interval. Wifi enabled and Bluetooth disabled it ran for ~3-5 days.

      • Amerigo Rossi says:

        Thanks Danny, I’ll try using a 1amp battery with a 20 minute interval, I’m waiting for the sensors to arrive (should arrive last week of January) meanwhile have a happy new year

Leave a Reply

Your email address will not be published. Required fields are marked *.

All comments must go through an approval and anti-spam process before appearing on the website. Please be patience and do not re-submit your comment if it does not appear.

This site uses Akismet to reduce spam. Learn how your comment data is processed.