Bed occupancy DIY sensor

Nope. I’ve used the statistics sensor in HA and looked at the change rate attribute quite successfully. Response time was very close to the 10sec I use for filtering in ESPhome.

Okay, technically you can use HA to do “quick” transitions but it isn’t a good idea if you can handle it from within ESPHome.io and it seems like Aaron can. If you do it with HA you’re spending a lot more resources and CPU cycles to do something you can do with much less processing/overhead. In my mind, it’s an efficiency waste. I prefer to do as much on an ESP8266 as possible before sending the results back to HA.

I agree but can ESPhome do change rate?

I didn’t actually find an advantage of using change rate as my sensor does not drift too much with temperature/humidity dead tree effects. So I dropped using it simply because it was an extra sensor I did not need.

I do have a slider I can use to set the in/out of bed threshold. I’ve been thinking of adding a bit of hysteresis (or two sliders, one for in bed level, one for out) to prevent the very occasional out of bed false trigger when moving during the night.

Now that ESPhome can import sensors from home assistant it would make sense to do this level comparison in ESPhome and only report change of state (in / out of bed) rather than reporting the level very frequently.

So I have esphome set up to do a first pass at filtering this data:

  - platform: adc
    pin: A0
    name: "Bed Right"
    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

That effectively sends data to HA every 3 seconds or immediately if there is a change larger than 0.3. I then have HA configured to look for a simple above/below a value to determine in/out of bed.

If I were to use only the delta filter, I can’t rely on the fact that just because the value has gone up X means I’m actually out of bed. The values from the sensors drift over time, often where the drift is more than X. So I need some sort of threshold, or looking for a large change in a short period of time.

The problem with doing this 100% inside of esphome is that it essentially gives me no visibility in debugging it. If it fails to trigger, I won’t have any way to look at logs to find out why. The way it currently works I can at least go look at the last 24 hours of readings in HA and look at the values.

This might be the best option going forward. Right now I have to edit the HA config and restart it to change it.

Do you mean you’d still configure the threshold with your sliders in HA, but then the ESPhome device would read that value and only report data outside that range? That sounds potentially interesting. Got a link to the ESPhome docs on that feature? I can’t seem to find it.

Sort of. Instead of reporting values outside the range it would set/reset a template binary sensor in esphome. The state of this sensor (in/out of bed) is all that is reported to home assistant. I’m not sure if this:


will support input_numbers of if I will have to create a template sensor in home assistant to follow the input number.

Also I will still need to send the level of the strain gauge for display in home assistant so that I can see what to set the input_number to. However this can be at a much lower rate than the response time required.

It works. ESPHome:

binary_sensor:
  - platform: template
    name: "Master Bed Occupied"
    lambda: |-
      if (id(master_bed_sensor).state < id(trigger_level).state) {
        return true;
      } else {
        return false;
      }

sensor:
  - platform: adc
    pin: A0
    name: "Master Bed Sensor"
    id: "master_bed_sensor"
    icon: mdi:hotel
    update_interval: 1s
    filters:
      - multiply: 3.25
      - sliding_window_moving_average:
          window_size: 10
          send_every: 1
      - or: 
          - throttle: 180s
          - delta: 0.05

  - platform: homeassistant
    name: "Trigger Level"
    id: "trigger_level"
    entity_id: input_number.master_bed_trigger_level

Home assistant, input_number:

master_bed_trigger_level:
  name: Master Bed Trigger Level
  min: 0
  max: 2.5
  step: 0.05
  unit_of_measurement: V
  icon: mdi:arrow-collapse-down
6 Likes

For you poor souls with a lot of noise in your sensor. Bad news:
Most probably its isn’t a issue with your power supply or connections.
I installed 2 sensors with the help of an ads1115 on to my standard ikea bed.

One sensor is working very well.

the other one not so much


when I change the cables from one side to another the noise is still on the left (in this case A1-GND) sensor.

Only issue I have now that I always take over her side of the bed as soon as she is getting up. :stuck_out_tongue:
that gets me such readings on the working sensor - somehow hard to interpreter.
image

@tom_l, I’m very interested in creating the same kind of thing you have with a velostat. I had previously purchased a set of strain gauge sensors but it turns out my bed doesn’t use slats. The entire subframe is steel so I have to take this another direction. I don’t suppose you could furnish details on what you used to build and how you built the velostat sensor?

Thanks,
-Greg

Duct tape, copper foil tape and velostat: Bed occupancy DIY sensor

There’s a better option here though: Bed occupancy sensor, using parts you have

1 Like

LOL, awesome idea! I thought that was cloth or something. Fantastic!!

I am totally trying this right now. OMG.

1 Like

What do you think the cause of the noise is then?

Crappy strain gauge amplifier?

I definitely expect it to be the case. New ones are on the way - First i gonna replace the amplifier and see whats going on. When this isn’t helping then it must be the gluing technique or the bend sensor itself. They should arrive soon, so i keep you posted.

2 Likes

Just FYI — Be sure to keep the sensor detection logic in the firmware at the sensor. The sensor should only ever show a binary state.

A lot of information can be determined by the raw data which is highly personal.

I did a project several years back on monitoring devices for an assisted living company. Along with a lot of other monitoring, they needed to know bed dwell time, and frequency/duration of night wanderings.

We had to tune the software at the sensor to force the sensor to be binary in|out (pots adj sensitivity for user weight.)

Let’s just say we learned a lot of things we didn’t really need to know.

What if that extra data is exactly what we want?

I want my bed to know if I’m in it and what I am doing as well. To trigger automations and what not.

It can also track my weight change.

Honestly, if someone has the nerv, to hack into my safe home network, the data from the bed sensors, is the least I worry about! :smiley: :smiley: :smiley:

I can’t even think of something more personal, than my home server, including HA. If you actually are in there… Fortunately, I try to take precautions, including not exposing it to the internet, but as we all know, that is just the idea, that my neighbous network is easier to hijack. :smiley:

Anyone having issues here with noise did find a solution?
I’ve finally installed the sensor yesterday and I’m getting a lot of noise without any hints of the weight changing the value.
Interestingly I had a huge shift after the sensor started for the first time:

I’ve noticed pretty drastic changes in the values that tend to shift seasonally. It doesn’t correspond exactly to temperature or humidity, but I have to recalibrate the threshold that I use to report “in bed” a few times a year. I ended up giving myself little sliders in home assistant so that I don’t have to rebuild or restart anything in order to change the thresholds and it’s been working pretty well now.

Screen Shot 2020-11-08 at 3.04.27 PM

I just found this thread and would be looking at making something similar. I have a question though.

My bed design means that I would need two of these sensors to detect occupancy (left side and right side) can I hook both of these up to a single ESP8266?

No. You would need an ESP32. The ESP8266 only has one ADC.

Would an analogue multiplexer work instead? Seemingly let’s you plug multiple analogue inputs into a single output. How it seems to work I am unsure.

I will look at an esp32 as well. I have a few 8266 D1 mini boards so worst case I could make two sensors and power both D1 minis off a single 5V.