Bed occupancy sensor, using parts you have

What do you mean by “one sensor”? That you can only add one of the tin foil mats? Than something’s wrong.

I’m running four “sensors” and an LED strip with that specific ESP32.

This is my esphome config for this one:

esphome:
  name: bedroom_bed_occupancy
  platform: ESP32
  board: nodemcu-32s

# [...]

# The actual thingy
esp32_touch:
  # Comment after setup is done
  #setup_mode: true  

binary_sensor:
  - platform: esp32_touch
    id: steffi_top
    name: "Bedroom Bed Steffi top"
    pin: GPIO15
    threshold: 14
  - platform: esp32_touch
    id: steffi_bottom
    name: "Bedroom Bed Steffi bottom"
    pin: GPIO14
    threshold: 11
  - platform: esp32_touch
    id: pat_top
    name: "Bedroom Bed Pat top"
    pin: GPIO13
    threshold: 14
  - platform: esp32_touch
    id: pat_bottom
    name: "Bedroom Bed Pat bottom"
    pin: GPIO12
    threshold: 12
    
  - platform: template
    name: "Bedroom Bed Steffi Occupancy"
    lambda: |-
      if (id(steffi_top).state && id(steffi_bottom).state) {
        return true;
      } else {
        return false;
      }
  - platform: template
    name: "Bedroom Bed Pat Occupancy"
    lambda: |-
      if (id(pat_top).state && id(pat_bottom).state) {
        return true;
      } else {
        return false;
      }

light:
  - platform: rgb
    name: "Bedroom_bed_bottom_led"
    red: output_red
    green: output_green
    blue: output_blue
    restore_mode: RESTORE_DEFAULT_OFF

output:
  - platform: ledc
    id: output_red
    pin: GPIO2
  - platform: ledc
    id: output_green
    pin: GPIO4
  - platform: ledc
    id: output_blue
    pin: GPIO5

I’m not happy with the amount of sensor data collected and send, but that is a different problem for a different time. :smiley:

As someone who is going to try this soon (i’m sick of drifting strain gauges) what are you unhappy about?

Too many false triggers?

Or does it drop out too often when occupied?

Unhappy would sound to harsh, I love this project! :wink: It works great for me, the data that is collected is accurat (in my case) and reliable. At least as long as you check the threshold every few weeks/months. :slight_smile:

The sensor itself works like a charme, it sends data since I installed it - no errors or things like that. :smiley:

But the amount of data, the frequency for each sensor if you will, is huge. I get two to three values per sensor per second. That amounts to twelve values every second for one bed. I’m quite sure there would be ways to control that via esphome, but I didn’t had the time or muse to take a look at it. :wink:

Right now it doesn’t disturb anything, so I’m not to bothered, but in the future I need to resolve this, it’s simply not nice. :slight_smile: My idea would be to hide the real sensors for HA and only send the lamda value every 30 seconds to HA. That should bring some relief. :slight_smile:

Yes, exactly. I have three mats. If I run any one of them (by commenting out the others), it is stable and works perfectly. However, if I run any two of them, every sensor on the chip becomes unavailable most of the time. I can vary how bad the problem is by changing settings for

esp32_touch:
# Delete this after you are done with the setup process
#  setup_mode: true 
  iir_filter: 10ms
  sleep_duration: 400ms

or

  - platform: esp32_touch
    name: "Master Bed Top Right"
    pin: GPIO27
    threshold: 40
    filters:
      - delayed_off: 1000ms
    id: top_right

But it is always unstable. If I run all three there is nothing but errors. I notice in your yaml script that you are using a different module. IN particular, you are using the ESP32-S2 chip, whereas I am using ESP32. I wonder if/why that would make a difference.

I’m using this NodeMCU with an ESP32: https://az-delivery.myshopify.com/products/esp32-developmentboard?_pos=2&_sid=543d96ebe&_ss=r

I’d suggest you start minimal. :slight_smile: No delays or filters, just the basics and see how far that goes.
What I’d check as well are the used GPIOs. Are they really able to work with the touch input (as this is what we use here)? With my board eg GPIO27 is touch, GPIO26 is not. :wink:
And is your ground correct? I wired all four sensors together to have one good ground.

On no. That’s the exact reason I was going to change from strain gauges. If it offers no improvement then there’s not much point me changing to this.

Yeah, I began simple, and used the right pins, supposedly (27,32,33). In fact, this is confirmed by the fact that each one works individually. I tried without delays and filters, etc, but it always fails. Changing things systematically is what has led me to suspect that it is the chip. I wanted to use a chip that also has bluetooth to detect presence, but that might not be possible.

That is going to depend on what spacer you use. The open-celled foams will have a very long relaxation time that makes them problematic. A good closed-cell foam shouldn’t have this problem, but my other issue is preventing me from checking that right now.

As the entry in this thread shows, I had installed this sensors at the end of June 2020. Since then I had to adjust the values once. :slight_smile: I’d speculate it depends on the temperature. The change in values was just around the time where we fell under 10°C to 15°C outside temperature.

And I changed the bed frame in the meantime (without needing to adjust the sensors), so I’d say it is not very often. What I do, but I guess that’s my inner “Monk”, I check the sensors every few weeks as a “preventative maintenance”. :slight_smile:

Hey! I had that issue when trying to use multiple touch sensors for my leak detectors:

Indeed! That looks just like my problem, but you are also using the ESP32-S2 chip. So how is @paddy0174 getting his to work ?? You also are using the iir filter. I thought that had checked that, but let me double check…it is the filter. I turned off ‘iir_filter’ and it is more stable. @paddy0174 was right! I can leave the ‘sleep_duration’ setting, which might help with the spamming problem.

1 Like

Excellent. Thanks for the update. I’ll mention it in my water leak topic.

I know that this post is focused on parts you have. However, I think that it might be useful to some people to know other options as well. I used the following sensor:

You just need to connect it to a pin and ground and set it up as a binary sensor on esphome. It works great.

4 Likes

Unfortunately, that one says “No longer available”. I saw something similar for a bed on Amazon for like $25, but aside from being much more expensive than this one, it seemed binary, so wasn’t sure if it would always work. Does it just act like a contact switch? How do you know that the weight of your mattress won’t set it off. Seems like this one is similar:

Yes, that’s exactly what I bought. Yes, basically it’s a contact switch. It is sensitive and activates with little weight/pressure. It worked very well for me, I installed two on a the support of a queen bed, one for each side of the bed. The weight of the mattress does not trigger it even though it’s a 10-inch memory-foam mattress . I guess that might depend on how the mattress is sitting on the supporting platform. As soon as me or my wife get on the bed, only the correct side/sensor gets triggered. I didn’t play with it very long before installing it, but it seems that several of the pressure points need to make contact. I ordered more to install it on my office chair to use it as presence and automate my standing desk and other devices. I will preform more tests when I do that.

Thanks for the link to the “not so binary” automation. I adapted it to my sensor. Eventually, I would like to program all of this on the ESP module. Here is what I have (for @Dorko) in automations.yaml:

# MasterBed status
- alias: Mark masterbed as Just Occupied
  trigger:
    - platform: state
      entity_id: binary_sensor.master_bed_sensor
      from: "off"
      to: "on"
  action:
    - service: input_select.select_option
      data:
        entity_id: input_select.masterbed_status_dropdown
        option: Just Occupied

- alias: Mark masterbed as Occupied
  trigger:
    - platform: state
      entity_id: input_select.masterbed_status_dropdown
      to: "Just Occupied"
      for:
        minutes: 2
    - platform: state
      entity_id: input_select.masterbed_status_dropdown
      from: "Just Left"
      to: "Just Occupied"
  #  condition:
  action:
    - service: input_select.select_option
      data:
        entity_id: input_select.masterbed_status_dropdown
        option: Occupied

- alias: Mark masterbed as Just Left
  trigger:
    - platform: state
      entity_id: binary_sensor.master_bed_sensor
      from: "on"
      to: "off"
  action:
    - service: input_select.select_option
      data:
        entity_id: input_select.masterbed_status_dropdown
        option: Just Left

- alias: Mark masterbed as Empty
  trigger:
    - platform: state
      entity_id: input_select.masterbed_status_dropdown
      to: "Just Left"
      for:
        minutes: 2
  action:
    - service: input_select.select_option
      data:
        entity_id: input_select.masterbed_status_dropdown
        option: Empty

- alias: Mark masterbed as Extended Empty
  trigger:
    - platform: state
      entity_id: input_select.masterbed_status_dropdown
      to: "Empty"
      for:
        hours: 24
  action:
    - service: input_select.select_option
      data_template:
        entity_id: input_select.masterbed_status_dropdown
        option: Extended Empty

And what is in configuration.yaml:

input_select:
  masterbed_status_dropdown:
    name: Masterbed
    options:
      - Occupied
      - Just Occupied
      - Just Left
      - Empty
      - Extended Empty
    initial: Empty

And, finally, the button on put on Lovelace to indicate the current state. It is color-coded with the state, and the icon changes when occupied. If the bed is just left, empty or extended empty, the background is white. If it is Occupied or Just Occupied, the background is blue. The text color changes with status, too:

type: 'custom:button-card'
styles:
  grid:
    - grid-template-areas: '"i" "l" "slider" "n"'
    - grid-template-columns: 1fr
    - grid-template-rows: min-content fr min-content min-content
size: 40%
state:
  - value: 'off'
    styles:
      icon:
        - color: black
  - value: 'on'
    styles:
      name:
        - color: 'rgb(3, 169, 244)'
      icon:
        - value: 'mdi:lightbulb-on'
        - color: 'rgb(3, 169, 244)'
      card:
        - background-color: yellow
        - boxshadow: none;
        - overflow: unset
  - value: unavailable
    styles:
      icon:
        - color: red
custom_fields:
  slider:
    card:
      full_row: true
      hide_state: true
      type: 'custom:slider-entity-row'
      entity: light.gosund_dimmer_1_gosund_dimmer_1
    overflow: unset
    filter: opacity(80%)
    color: 'rgb(3, 169, 244)'
entity: light.gosund_dimmer_1_gosund_dimmer_1
name: Livingroom
icon: 'mdi:lightbulb'

You need the custom-button-card from HACS for it to work. I adapted it from a blog I can’t find right now, mostly for lights.

2 Likes

Very cool. Looks very similar to what I am currently running as well.

Would like to share? :wink: :slight_smile:

Input Select

  sleep_state:
    name: Sleep State
    options:
      - Just Laid Down
      - Asleep
      - Just Left Bed
      - Back to Bed
      - Awake

Automations:

- id: '1593379646748'
  alias: Bed (Just Laid Down)
  description: ''
  trigger:
  - entity_id: binary_sensor.top_right
    for: 00:00:20
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: input_select.sleep_state
    state: Awake
  action:
  - data: {}
    entity_id: light.master_bedroom_overhead_lights
    service: light.turn_off
  - data: {}
    entity_id: switch.closet_light_west
    service: switch.turn_off
  - data: {}
    entity_id: light.master_bathroom
    service: light.turn_off
  - data:
      option: Just Laid Down
    entity_id: input_select.sleep_state
    service: input_select.select_option
  mode: single
- id: '1593384350402'
  alias: Bed (Asleep)
  description: ''
  trigger:
  - entity_id: binary_sensor.top_right
    for: 00:10:00
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: input_select.floor_occupancy
    state: Upstairs
  action:
  - data:
      option: Asleep
    entity_id: input_select.sleep_state
    service: input_select.select_option
  - data: {}
    entity_id: input_boolean.trevor_asleep
    service: input_boolean.turn_on
  mode: single
- id: '1593384510588'
  alias: Bed (Just Left Bed)
  description: ''
  trigger:
  - entity_id: binary_sensor.top_right
    from: 'on'
    platform: state
    to: 'off'
  condition:
  - condition: or
    conditions:
    - condition: state
      entity_id: input_select.sleep_state
      state: Asleep
    - condition: state
      entity_id: input_select.sleep_state
      state: Just Laid Down
  action:
  - data:
      brightness: 45
      kelvin: 2700
    entity_id: light.mstr_bath_ne
    service: light.turn_on
  - data:
      option: Just Left Bed
    entity_id: input_select.sleep_state
    service: input_select.select_option
- id: '1593384691621'
  alias: Bed (Awake)
  description: ''
  trigger:
  - entity_id: binary_sensor.top_right
    for: 00:10:00
    platform: state
    to: 'off'
  condition: []
  action:
  - data:
      option: Awake
    entity_id: input_select.sleep_state
    service: input_select.select_option
  - data: {}
    entity_id: light.master_bedroom
    service: light.turn_off
  - data: {}
    entity_id: light.master_bathroom
    service: light.turn_off
  mode: single
- id: '1593385265500'
  alias: Bed (Back to Bed)
  description: ''
  trigger:
  - entity_id: binary_sensor.top_right
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: input_select.sleep_state
    state: Just Left Bed
  action:
  - data: {}
    entity_id: light.mstr_bedroom_lamp_left
    service: light.turn_off
  - data:
      option: Asleep
    entity_id: input_select.sleep_state
    service: input_select.select_option
  - data: {}
    entity_id: light.mstr_bath_ne
    service: light.turn_off

Probably needs some cleanup but here it is.

2 Likes

Has anyone tried using this type of copper tape stuck to either side of some foam?