Bed occupancy DIY sensor

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

It would register even more clearly. More weight = bigger voltage swing.

Sampling every 4 seconds and averaging 15 samples (as I do) probably would not show that sort of activity.

Perfect (on both counts :slight_smile:)
If I go ahead with trying this I may have more questions :wink:

Let me pre-empt a few questions:

  1. Make sure you use an ESP module that can handle 3.3V on the ADC input. The Wemos d1 mini for example or NodeMCU-32S with attenuation. Or use a resistive divider.

  2. Make sure you use a top quality 5V power supply. Switch mode power supply noise or ripple will affect the readings making it difficult to differentiate in/out of bed.

  3. Keep the connecting wires as short as possible.

  4. Before gluing to the bed adjust the potentiometer on the sensor to give about 3V out (factory setting is usually ok). The voltage will drop with weight.

  5. Make sure you glue the strain gauge in the centre of the slat that takes the most weight (under your bum). Glue it length-wise along the slat (see first photo). Use a small amount of super glue. More will stiffen the sensor, reducing sensitivity. Raise the slat up slightly higher (5mm) than the other slats for added sensitivity.

  6. Example ESPhomeyaml below:

esphomeyaml:
  name: master_bed
  platform: ESP8266
  board: d1_mini

wifi:
  ssid: 'WAPLO'
  password: !secret wifi_pwd
  manual_ip:
    static_ip: 10.1.1.71
    gateway: 10.1.1.1
    subnet: 255.255.255.0

mqtt:
  broker: 10.1.1.100
  username: !secret mqtt_usr
  password: !secret mqtt_pwd

logger:
#  level: WARN # enable this once working to reduce unnecessary mqtt messaging

ota:
  password: !secret esp_pwd

binary_sensor:
  - platform: status
    name: "Master Bed Sensor Status"

sensor:
  - platform: wifi_signal
    name: "Master Bed Sensor WiFi Signal"
    update_interval: 3s
    filters:
      - sliding_window_moving_average:
          window_size: 15
          send_every: 15
          send_first_at: 15

  - platform: adc
    pin: A0
    name: "Master Bed Sensor"
    update_interval: 1s
    filters:
      - multiply: 3.3 
      - sliding_window_moving_average:
          window_size: 10
          send_every: 10 # 10 second updates @ 1 second scan interval.
          send_first_at: 10
9 Likes

Thank you!

Sorry for the newbie question, how do you convert voltage readings from the sensor to a binary occupied/unoccupied sensor? Is there some missing logic on homeassistant’s side on this or is esphomeyaml able to do this?

I’ve been messing around with load cells (and even velostat), but using a strain gauge seems like a much better option so thanks for this idea!

I do it on the home assistant side. Define a binary template sensor like so:

#################################################################
## Master Bed Occupied Sensor
#################################################################

- platform: template
  sensors:
    master_bed_occupied:
      friendly_name: "Master Bed Occupied"
      entity_id:
        - sensor.master_bed_sensor
      value_template: >-
        {{ states('sensor.master_bed_sensor') | float < 0.895 }}

Adjust the 0.895 level to the average of your in and out of bed readings.

A similar binary sensor could be defined on the ESP but it’s more difficult to adjust.

1 Like

Understand that you didn’t want to work with load cells, in my case i have a boxspring bed so doing it this way is not really an option since it does not have a bending element that much. But it was a lot of work for having 4 load cells, and thats just 1 bed, i have another to do :slight_smile:

Does work very nice for me too.

Bit of an update. All still working very well but tonight I replaced the cheap Chinese 5V power adaptor I was using to power the D1 mini and strain gauge with a properly regulated power supply. This has improved the noise so much that I think I will be reducing my filtering (and thus making the update interval quicker):

Ok, so my first foray into homemade sensors is a (partial) success.
Except that I don’t seem to be able to adjust the voltage by turning the screw on the potentiometer. Could it just be a faulty component or am I doing something wrong?

Which ESP module are you using?

Is your wiring correct?

D1 Mini.

I think so :slight_smile:
Out on the strain guage goes to A0

image

1 Like

Yeah that all looks good.

Do you have a multimeter to measure the voltage at A0?

If so you can see if it changes when you carefully bend the sensor or turn the pot.

I do…

[stupid question alert]
What do I measure the voltage across? I am guessing A0 and any GND?

Yep. That’s correct.

No change, either turning the pot or bending the sensor…
It stays at around 4.8v

That’s a dead sensor board then. Did you buy a 3.3V or 5V module?

5v
I guess I need a new one…

Got a soldering iron and some non-ROHS (40-60 tin-lead - SnPb) rosin cored solder?

If so give the wires that connect the strain gauge to the board a bit of fresh solder. The solder joints in the image in the link you posted look suspiciously dry. Possibly due to the metal used for the strain gauge leads. A bit of flux from some fresh SNPb rosin cored solder might help.