FSR - the best bed occupancy sensor

Hi Mark, approach sounds right, I’ve done the same thing & it’s working nicely. Change all the names & the pin number. From the error, there might be one more place needs changed. Yes, send YAML.

Here’s my code … Good luck :slight_smile:

substitutions:
  devicename: bed-sensor
  upper_devicename: Bed Sensor
  ssid: SSID

esphome:
  name: $devicename
  on_boot:
    priority: 50
    then:
      - light.turn_off: bedlight

esp32:
  board: nodemcu-32s

wifi:
  networks: 
    - ssid: $ssid
      password: !secret wifi_password
  
  ap:
    ssid: $upper_devicename
    password: !secret wifi_password
  
web_server:
  port: 80

logger:

api:
  encryption:
    key: !secret api_password

ota:
  password: !secret ota_password

captive_portal:

light:
  - platform: neopixelbus
    type: GRB
    variant: WS2812
    pin: GPIO25
    num_leds: 30
    name: "Bed Light"
    id: bedlight
    on_turn_on:
    - delay: 5min
    - light.turn_off:
        id: bedlight

binary_sensor:
  - platform: template
    name: Right ${upper_devicename}
    id: right
    device_class: occupancy
    lambda: |-
      if (id(right_bed_sensor).state > id(right_trigger_level).state) {
        return true;
        id(right).set_icon("mdi:bed");
      } else {
        return false;
        id(right).set_icon("mdi:bed-empty");
      }

  - platform: template
    name: Left ${upper_devicename}
    id: left
    device_class: occupancy
    lambda: |-
      if (id(left_bed_sensor).state > id(left_trigger_level).state) {
        return true;
        id(left).set_icon("mdi:bed");
      } else {
        return false;
        id(left).set_icon("mdi:bed-empty");
      }

  - platform: template
    name: Single ${upper_devicename}
    id: single
    device_class: occupancy
    lambda: |-
      if (id(right).state ||
          id(left).state) {
        return true;
        id(single).set_icon("mdi:bed");
      } else {
        return false;
        id(single).set_icon("mdi:bed-empty");
      }

  - platform: template
    name: Both ${upper_devicename}
    id: both
    device_class: occupancy
    lambda: |-
      if (id(right).state &&
          id(left).state) {
        return true;
        id(both).set_icon("mdi:bed");
      } else {
        return false;
        id(both).set_icon("mdi:bed-empty");
      }

sensor:
  - platform: adc
    pin: GPIO34
    attenuation: 11db
    name: Right ${upper_devicename} Voltage
    id: right_bed_sensor
    icon: mdi:bed
    update_interval: 0.5s
    filters:
      - sliding_window_moving_average:
          window_size: 5
          send_every: 1
      - or:
          - throttle: 180s
          - delta: 0.02

  - platform: adc
    pin: GPIO33
    attenuation: 11db
    name: Left ${upper_devicename} Voltage
    id: left_bed_sensor
    icon: mdi:bed
    update_interval: 0.5s
    filters:
      - sliding_window_moving_average:
          window_size: 5
          send_every: 1
      - or:
          - throttle: 180s
          - delta: 0.02

number:
  - platform: template
    name: Right Bed Threshold
    id: right_trigger_level
    optimistic: true
    restore_value: True
    min_value: 0
    max_value: 3.5
    step: 0.05
    icon: mdi:arrow-collapse-down
    unit_of_measurement: V 

  - platform: template
    name: Left Bed Threshold
    id: left_trigger_level
    optimistic: true
    restore_value: True
    min_value: 0
    max_value: 3.5
    step: 0.05
    icon: mdi:arrow-collapse-down
    unit_of_measurement: V 
1 Like

Nothing there jumps out at me.
Mine’s a little simpler:

esphome:
  name: esp1
  friendly_name: esp1

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "xxxxxxxxxxxxxxxxxxxxxxx"

ota:
  password: "xxxxxxxxxxxxxxxxxxxxxxxx"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esp1 Fallback Hotspot"
    password: "xxxxxxxxx"

captive_portal:

binary_sensor:
  - platform: template
    name: "Master Bed Left Occupancy"
    device_class: occupancy
    id: mb_l
    lambda: |-
      if (id(master_bed_left_sensor).state < id(trigger_level).state) {
        return true;
      } else {
        return false;
      }
  - platform: template
    name: "Master Bed Right Occupancy"
    device_class: occupancy
    id: mb_r
    lambda: |-
      if (id(master_bed_right_sensor).state < id(trigger_level).state) {
        return true;
      } else {
        return false;
      }

sensor:
  - platform: adc
    pin: GPIO34
    attenuation: 11db
    name: "Master Bed Left Sensor"
    id: "master_bed_left_sensor"
    icon: mdi:bed
    update_interval: 0.5s
    filters:
      - sliding_window_moving_average:
          window_size: 10
          send_every: 1
      - or:
          - throttle: 180s
          - delta: 0.02
  - platform: adc
    pin: GPIO35
    attenuation: 11db
    name: "Master Bed Right Sensor"
    id: "master_bed_right_sensor"
    icon: mdi:bed
    update_interval: 0.5s
    filters:
      - sliding_window_moving_average:
          window_size: 10
          send_every: 1
      - or:
          - throttle: 180s
          - delta: 0.02

number:
  - platform: template
    name: Master Bed Trigger Level
    id: trigger_level
    optimistic: true
    restore_value: true
    min_value: 0
    max_value: 3.5
    step: 0.05
    icon: mdi:arrow-collapse-down
    unit_of_measurement: V 


1 Like

I just use the Single and Both sensors in conjunction with the alarm so that it will set if one of us is in bed and the other is out. Also I use the Both in bed sensor to run the Daily Reset script.

Thanks guys – I was able to get it to work by paralleling yours as samples. Thank you.

I still got some voltage fluctuations, and after several tests, I noticed that using a multi-USB charger, either from Aliexpress or Amazon, do actually generate these fluctuations:

Has anyone ever encountered this? Is there an electronic or software solution for using a multi-USB charger with an ESP32? Or should I change my multi-USB charger for a better one?

Thanks!

Yes I’ve had issues with noisy USB power supplies before (another ESPHome project). Throw the Ailiexpress one away and get a better one.

Got it. Even this one or this one give me the exact same result. Any relevant multiUSB wall charger brand/model?
Or is there any electronic workaround (resistor/capacitor…) to mitigate this issue?
Thanks!

for some reason, mine isnt working, sensor keeps saying 3.13V
image

seems to be sending data, just not the correct data, doesnt matter if on or off the bed, just seems to always show 3.13V

i assume something isnt quite right though, just unsure what that would be. its an ESP32

Check your wiring.

I was wondering that, maybe I cooked the resistor.

I have a usb port on my esp board. I can use that to power it yeh? I assumed yes as there’s still 5v and 3v3 at the pins.

Wiring seems fine, same with resistor. I power it with a usb, then soldered an earth out wire to the board. Put one end of the resistor to G34 and the other end to 3v3 along with a wire coming off that same length as my earth and they are what goes to my fsr.

Excuse the ugliest of it. But it’s is what I have.


I think I know what I did wrong, I did the power wire out to my fsr on the 3v3 and not the G34, I’ll revise and retest

1 Like

Ha! Yep that will do it.

Success, 0.7-0.8V in bed, goes to 2.8v ish out.
Thank you!

What kind of terminals/pre made wires do I need so I can just plug and play wires instead of trying to solder them to the dev board pins

1 Like

Search for “DuPont jumpers”.

Soldering is always more reliable though.

who modified the code to have two films using only one Exp? I read that someone did it but too many comments don’t allow me to find it

I assume you meant Esp, not Exp? If so, my complete code for 2 strips is a little further up.

1 Like

Thank you. can i ask u the schema on esp8266 for two strips?
Thank you

It would seem that it is not possible on the ESP8266 as it only has one ADC.

Screenshot 2024-03-26 215336