Bed Sensor, wrong state being shown in HA

I’ve implemented a bed sensor via an Aqara Door/Window Sensor (connecting the two leads of the sensor mat to the ends of the reed switch). This bed sensor is being used with an elderly parent who needs certain machines (Oxygen, etc.) running when in bed.

In the Aqara app, it shows the sensor as being closed when the bed is occupied, and open when unoccupied, as expected.

This is being used in HA via the Aqara M2 hub integrated with the Matter integration. I’ve changed the entity to show as “Occupancy”. When the bed is occupied, it shows as “Clear”, and when unoccupied, it shows as “Detected”. This makes automations that are set to trigger when the “bed became occupied” trigger when leaving the bed, and vice versa.

For a workaround, I can have the “Bed Occupied” automation trigger when the “bed became unoccupied”, and the “Bed Unoccupied” automation trigger when the “bed became occupied”. However, this is confusing, and I’m sure will lead to other issues when building any other automation with this sensor.

My desired goal: have the bed sensor indicate occupied (detected) when the bed is occupied, and unoccupied (clear) when not occupied.

My system: HA installed on an x86 machine (Generic x86-64 - Home Assistant, method 1).
Core: 2024.7.0
Supervisor: 2024.06.2
Operating System: 12.4
Frontend: 20240703.0
I’m not sure how to get the Matter integration version (I have only seen it when it shows up needing an upgrade), but it was updated to the latest just yesterday.

I’m a newbie to HA, with just a few things being automated currently. I’m not sure if this is a bug (why would an occupancy sensor show as occupied without the triggering occupied signal from the sensor mat?), or how I would go about reversing the reported states of this sensor to be used in automations and reflected in dashboards, etc.

Any help in resolving this is greatly appreciated!

Without knowing how the reed switch is physically configured on the hardware side of the Aqara sensor, I’d assume that it’s Normally Open instead of Normally Closed.

This means that it needs to have the magnet close to it to close the reed switch and complete the circuit.
You have 3 options:

  • Check if your pressure mat has an unused wire which is marked as NO instead of NC. Wire NO up to the door sensor instead of the NC wire.
  • Replace the reed switch on the Aqara for a NC reed switch. They’re pretty cheap but you need basic soldering skills
  • Invert the status in software using a template sensor (which is the equivalent of ESPHome’s inverted: true) and use the newly-created sensor in your automations.

I can’t help much with the last option since I really suck at templating even the most basic stuff, but do put a vote for this request to make it easier for all of us in future.

The Occupancy device class always reports this state:

https://www.home-assistant.io/integrations/binary_sensor/#device-class
Screenshot 2024-07-05 at 09-21-01 Binary sensor

Unless your sensor mat has normally closed (NC) contacts that open when pressure is applied you can not change the state reported by the Aqara contact sensor. I can tell you now that all pressure mats only have normally open (NO) contacts that close when pressure is applied.

If you are into electronics you could possibly reverse engineer the Aquara sensor and change the way it senses the contact but this is not a trivial task and may involve soldering tiny surface mount components (1mm or less).

There is currently no way to invert a binary sensor from the UI. Vote for this feature request if you would like to see it included: Make the binary sensor result invertible

That leaves only one solution. A template binary sensor to invert the signal for you:

# configuration.yaml
template:
  - binary_sensor:
      - name: Patient X Bed
        device_class: occupancy
        state: "{{ not states('binary_sensor.your_aquara_contact_sensor_here')|bool }}"
        availability: "{{ has_value('binary_sensor.your_aquara_contact_sensor_here') }}"

FYI: In 6 months when your pressure mat fails by being permanently compressed (it probably will, I’ve tried a few and this happened to all of them) consider this sensor instead: FSR - the best bed occupancy sensor A PCB is being designed to make it easier to construct, see the posts later in the topic.

If you have spook installed

afbeelding

2 Likes

I did not know that. Neat. However nobody should use Spook :wink:

1 Like

Okay… why not?

Tom is not up-to-date with Spook. :laughing:

Spook was called “Not your Homie” in the past, and it was (jokingly) advised to not use it! :rofl:

BUT, that has changed, as Spook now IS your Homie, according to one of the last changes.

TL;DR
It was a joke! Use Spook, it is a great integration that has so many useful functions. :slight_smile:

Okay, I voted for it. Looks like it could use a few hundred more votes though…

Okay, I’ve tried this out.
When I create a template from the UI, it doesn’t have the “availability”, well, available to set. However, it seems to work properly.

I had seen this, but hadn’t read all of the posts. I see that Stephen has a nice unit that will be for sale soon, and I plan on going this route. I will probably get another for the master bed.

When I create the template from the UI, it always shows the status as “Clear”. However, the automations are working properly.