Bed occupancy DIY sensor

So I changed the power supply yesterday from my Apple one, to a Sony one, and no change, except the voltage dropped (not sure whats going on with the readings since 17:00 where it seems to go up and have slightly less swing though - its still sitting in exactly the same place on my desk! Air pressure maybe?)

Tomorrow I will build up my spare D1 mini and try swapping out both that and the sensor…

I saw a similar swing in my testing one that I just left out… I’m guessing temperature/humidity or something. Still no luck on my end. Have you tried wiring power direct to the sensor?

Not tried direct power yet - after @tom_l said he tried both ways with no change, I didn’t rush to try. I’ll give it a try in the next couple of days after I try swapping out the other bits. I found another Apple supply to try as well!

I installed my spare bed sensor and gave it a test last night.

Not as big a voltage swing due to stronger bed slats but definitely workable.

The long term drift is due to temperature, humidity, and deformation. Wood is not a dimensionally static material. Also these sensors seem to have a fairly high temperature coefficient. It’s manageable though.

Noise. I still don’t know what is going on there. Next thing for you to try would be the capacitors. A 0.1uF monolithic ceramic and a 470uF electrolytic across the sensor power supply. Also shorten up the connecting leads as much as possible.

1 Like

damn you @tom_l and your accurate sensors… I didn’t even know there were different types of capacitors, so that next step may be a bit over my head. you are just going to have to ship me your whole setup and call it a day :wink:

You can definitely find better deals than this. Just showing an example of what is required.

The ceramic capacitor can be connected in any orientation. The electrolytic is polarised and has (+) and (-) connections, be careful to get the orientation correct. (+) lead to (+) power supply connection.

Thanks again Tom - I’ll raid the electronics suppliers at work later :smiley:

@tom_l
Respect, do you use your self made gaffer tape sensor now or this tiny piece of the original sensor to get this clean output?

The gaffer tape and velostat sensor was my ‘plan B’. I’ve never used it.

1 Like

That looks like very accurate data! Here’s mine from last night:

(Yes I didn’t get into bed until 3am)

This is using a strain gauge under one of the slats, connected to an esp32 running esphome.

This was my config from the esp32.

sensor:
  - platform: adc
    pin: A0
    name: "Bed Left"
    update_interval: 50ms
    attenuation: 11db
    filters:
      - sliding_window_moving_average:
          send_first_at: 10
          window_size: 10
          send_every: 10
      - or:
        - delta: 0.05
        - throttle: 30s

I used the delta and throttle filters to have it report to HA at a minimum interval of 30 seconds, but also report immediately if the value changes more than 0.05.

You can see it was a little noisy still, with the values fluctuating almost 0.4 even when nothing was moving.

At 8am I changed the esp32 to the following config:

sensor:
  - platform: adc
    pin: A0
    name: "Bed Left"
    update_interval: 100ms
    attenuation: 11db
    filters:
      - sliding_window_moving_average:
          send_first_at: 10
          window_size: 10
          send_every: 10
      - or:
        - delta: 0.1
        - throttle: 60s

I’m hoping that sends a little bit less data to home assistant but still responds quickly. So far I’ve configured a simple binary sensor in HA:

  - platform: template
    sensors:
      bed_occupied:
        friendly_name: "Bed Occupied"
        value_template: "{{ states('sensor.bed_left') | float < 2.1 }}"

I’m not convinced a straight up threshold is going to be reliable enough given the drift and spikes, but we’ll see how it goes after a few more nights.

Interesting. That delta filter is probably never letting the average report. Your sliding average is doing nothing.

What does the signal look like without it?

Also 50ms is pretty fast. Not sure what the settling time of the ADC is but slowing your sample time down might improve things too.

Like this:

    update_interval: 1s
    filters:
      - sliding_window_moving_average:
          window_size: 10
          send_every: 10
          send_first_at: 10

Swapped out the strain gauge today, and still the same. Also switched to a much shorter cable, and still the same. I’ll swap out the Wemos tomorrow, and try powering the sensor direct and see what happens. I found the 470uF caps at work, but need to order the 0,1uF ones, so been trying to eliminate all the other options in the mean time!

So i’m pretty sure the sliding_window_moving_average was doing something. Here’s a new snapshot. First is the previous config I pasted above. I slept from 1am-7am in that config too, then right before 10am I changed it to remove the sliding_window_moving_average and use just the delta and throttle and as you can see it got a lot noisier!

What I meant was it might be better just using the sliding window. Could you try that?

I’m interested to see what it looks like without:

      - or:
        - delta: 0.1
        - throttle: 60s

The problem with that is it means I have to either flood home assistant with data all the time, or send data at an interval so slow that it would feel unresponsive. I tried that and it makes the device push to home assistant at a strict interval (update_interval * send_every).

I want the lights to react within a second or two of getting on or off the bed, but sending data every 2-3 seconds to home assistant is too much.

That was my reasoning for using the combination throttle and delta. The idea is I want to sample the sensor at least once a second, but only push it to home assistant if the value changed more than X, as well as at least once every Y seconds. I’m going to keep fiddling with the intervals I’m using to see if I can improve it more.

You don’t have to leave it like that. I’m just interested in what your baseline noise is like compared to mine and two other users here (who have a noise problem).

Also with this filter:

    update_interval: 1s
    filters:
      - sliding_window_moving_average:
          window_size: 10
          send_every: 10
          send_first_at: 10

it is very responsive (less than 10s usually and doesn’t really flood HA. One update every 10sec.

1 Like

Update:

I’ve been running this config for a few days now, and it’s been working out really well!

  - platform: adc
    pin: A3
    name: "Bed Left"
    update_interval: 100ms
    attenuation: 11db
    filters:
      - sliding_window_moving_average:
          send_first_at: 30
          window_size: 30
          send_every: 30
      - or:
        - delta: 0.3
        - throttle: 120s

It responds within about 2-3 seconds of getting on or off the bed, and the graph is pretty clean in Home Assistant!

As you can see, I added another sensor for the other side of the bed. Something is wrong with it, probably how I glued the flex sensor to the slat, and I’m not getting good readings from it at all. Unfortunately it means I have to rip that one off and re-solder a new flex sensor onto the chip, so that’ll be an adventure.

It would be nice for the others attempting this sensor if you could address my request.

I’ll give that a shot in the next couple days once I get the replacement sensor for the other side. I’ll have two to compare that way too.

1 Like

I have no input at the moment, as I’m vacation, but I just wanted to say that I’m so happy people are posting about their attempts. This gives me hope for my setup in the future! Thanks!!