Edit — 2026-11-28: Added a link to the companion article, Beyond Occupancy: Design Retrospective — How the Architecture Evolved.
Edit — 2026-11-26: All three parts are finally available. My first post had to go through manual moderation, so the article itself ended up spending some time in an incomplete state.
I’ve also moved the topic to the Share your Projects! category, added links to the table of contents and cross-references throughout the article, and made some minor formatting adjustments for better readability.
Introduction
This is my first article sharing my smart home experience, and hopefully the first of many. Please go easy on me!
This article was prepared with the assistance of ChatGPT in organizing, writing, and translating my ideas.
What happens when a presence sensor is wrong?
Most presence-based automations assume that the sensor provides a sufficiently reliable representation of whether a room is occupied. When that assumption fails, we usually adjust sensitivity, extend occupancy timeouts, add more sensors, or introduce additional conditions to individual automations. I started with those approaches too.
Over time, however, I ended up building a presence system that separates sensor observations, spatial inference, internal state memory, and the effective presence used by device controllers. The system uses the physical layout of my home to evaluate whether occupancy transitions are plausible. It can actively refresh questionable sensor observations, temporarily change its output policy without destroying internal state, and recover from system interruptions or unusual operating conditions.
This article describes how and why I developed these mechanisms, the problems they address, and the limitations that remain. It is not a step-by-step installation guide. Instead of sharing configuration files specific to my home, I want to explain the underlying architecture so that individual mechanisms can be adapted to different homes, sensors, and automation requirements.
For a shorter account of how this architecture emerged, including the earlier approaches, design decisions, and mechanisms that were eventually removed, see Beyond Occupancy: Design Retrospective — How the Architecture Evolved.
Due to the forum’s character limit, this article is divided into three consecutive posts within the same topic. The table of contents below covers all three parts.
Table of Contents
Part I — The Problem and Core Architecture
1. Why Presence Detection Is Still a Problem
2. System Overview: Observation, State, and Output
3. Filtered Presence: Handling Unreliable Observations
4. Spatial Presence: Giving Occupancy a Memory
Part II — Error Handling and Recovery
5. Active Refresh: Asking the Sensor to Observe Again
6. Presence Fallback: Retaining Memory Without Getting Stuck
* 6.5 Quiet Mode (Optional)
7. Bypass: When the Normal Spatial Model Does Not Apply
8. Rebuilding: Recovering Without Resetting Everything
Part III — Application and Design Review
9. Extending the Model Across Spatial Scales (Optional)
10. Putting the System Together
11. Limitations, Adaptability, and Observability
12. Conclusion
Suggested Reading Paths
This is a fairly long article, and not every section will be relevant to everyone.
| If you are interested in… | Suggested sections |
|---|---|
| Understanding the overall architecture | 1, 2, 10 |
| Preventing false detections and unexpected lights-off | 3, 4, 6 |
| Using spatial relationships to infer occupancy | 4 |
| Recovering stuck mmWave sensors | 3.3, 5 |
| Handling robot vacuums | 7, 8 |
| Recovering from HA or Z2M interruptions | 3.2, 8 |
| Combining rooms or determining whole-home occupancy | 9 |
| Practical timing parameters | 10.3 |
| Adapting the system to PIR sensors | 11.2 |
| Troubleshooting and manual recovery | 11.3, 11.4 |
| Understanding the overall design philosophy | 2, 10, 12 |
1. Why Presence Detection Is Still a Problem
Presence detection is probably one of the most fundamental functions of a smart home. It is also one of the hardest to get right. With mmWave presence sensors becoming increasingly accessible, detecting someone sitting on a sofa, working at a desk, or lying in bed is no longer as difficult as it was with traditional PIR motion sensors. At least in theory.
In practice, I still encountered several problems:
- Lights turning off while I was sitting quietly in a room.
- Sensors reporting presence long after I had left.
- Curtains, fans, and robot vacuums triggering presence detection.
- Sensor states becoming unavailable during system interruptions.
- Different rooms producing inconsistent presence states as I moved between them.
Some of these problems can be mitigated by adjusting sensor sensitivity, detection range, or occupancy timeout. However, tuning these parameters only gets you so far. At some point, I started questioning whether the problem was actually the sensor itself, or how I was using its output.
A sensor reporting presence is not the same as a room being occupied.
One is an observation. The other is a state that we infer from available information. That distinction eventually became the foundation of my presence system.
Consider a typical presence-based lighting automation. When the sensor detects someone, the lights turn on. When it stops detecting someone, the lights turn off after a delay. If the lights occasionally turn off while someone is still in the room, the usual solution is to increase the delay.
But how long should that delay be? A longer delay helps prevent false offs, but it also makes the system slower to respond when someone actually leaves. These are two different situations with two different requirements.
| Situation | Desired behavior |
|---|---|
| Someone is still in the room, but the sensor misses them | Retain presence for as long as necessary |
| Someone has actually left the room | Clear presence as soon as reasonably possible |
A single time parameter cannot reliably satisfy both requirements. Increasing the delay does not improve the system’s understanding of whether someone has left. It simply postpones the consequences of that decision.
The Off-Delay Trade-off
Instead of continuing to adjust the delay, I wanted to find another way to distinguish these situations. The physical layout of my home turned out to be useful information.
2. System Overview: Observation, State, and Output
My smart home runs on Home Assistant, with Zigbee2MQTT (Z2M) managing most of my Zigbee sensors. The SONOFF SNZB-06P is my primary presence sensor.
Over time, my presence system evolved into three main layers:
-
Filtered Presence (F): the sensor observation after filtering and validity checks.
-
Internal Presence (IP): the remembered occupancy state, maintained using spatial evidence.
-
Effective Presence (EP): the presence state actually used by downstream device controllers.
The main information flow is:
F can also provide EP directly when Fallback or Bypass is active. In addition, the relationship between F and IP provides feedback to the sensor refresh mechanism. Each layer answers a different question.
| Layer | Question |
|---|---|
| Raw Presence | What is the sensor reporting? |
| Filtered Presence | What observation can currently be used? |
| Internal Presence | What occupancy state should the system remember? |
| Effective Presence | What presence state should downstream controllers use right now? |
I did not start with this architecture. Each layer was introduced to solve a problem that the previous design could not adequately handle. The important part is that these states do not always have to agree. Separating them allows the system to handle uncertainty, exceptions, and recovery without making every device automation responsible for those problems.
Sections 3 and 4 describe the observation and inference layers. Sections 5–8 explain how the system handles errors, exceptions, and recovery.
3. Filtered Presence: Handling Unreliable Observations
3.1 Known Interference
One advantage of building presence detection within a smart home system is that the system already knows about many of the activities that might interfere with its sensors. For example, when an automated curtain starts moving, Home Assistant knows that the curtain is moving. If testing confirms that this movement affects a nearby presence sensor, I can account for that interference before the sensor output reaches the rest of the system.
This is one of the purposes of Filtered Presence. Rather than adding curtain-related exceptions to every lighting or HVAC automation, I handle confirmed interference at the observation layer. I use a noise window to represent periods during which sensor observations may be affected by known activities.
During these periods, F can retain its previous state instead of accepting potentially unreliable changes from Raw. I refer to this behavior as Freeze.
F is not intended to eliminate every possible false detection. It deals with conditions that the system already knows can make an observation unreliable. Unknown false positives and missed detections are handled separately.
3.2 Unavailable Does Not Mean Unoccupied
Another problem comes from the architecture of Home Assistant and Zigbee2MQTT.
Home Assistant and Z2M can restart independently. Communication between them can also be interrupted. When Z2M goes offline, sensor entities in Home Assistant may become unavailable. Consider a simple Home Assistant template:
{{ is_state('binary_sensor.presence', 'on') }}
If the sensor becomes unavailable, this expression returns False. But that does not mean the sensor has detected an empty room. It means the system cannot currently obtain the observation. If that result is directly used for device control, a Z2M interruption could cause lights to turn off throughout the home. To avoid this, F also uses Freeze when reliable observations are unavailable.
| Strategy | Implicit assumption |
|---|---|
| Force Off | No observation means nobody is present |
| Force On | No observation means someone is present |
| Freeze | No reliable new observation is available |
Freezing does not guarantee that the retained state is still correct. Someone could move between rooms while the system is offline. However, it prevents a communication failure from being interpreted as a physical change in occupancy. It also creates another requirement: when communication is restored, the system needs a way to recover from potentially outdated states. This recovery process is addressed in Section 8.1.
3.3 Refresh Can Also Introduce Unreliable Observations
As described in Section 5, my system can actively refresh a presence sensor by temporarily changing its sensitivity. In my experience with the SNZB-06P, this operation generally causes Raw Presence to return to Off before the sensor begins detecting again. This is useful when a sensor is stuck in the On state. However, it can also temporarily produce an Off observation while someone is actually in the room.
Without additional handling, the refresh mechanism could introduce the same kind of unwanted state transition that it is intended to help prevent. I therefore include the refresh script’s execution period in the noise window. In my current implementation, this period lasts approximately 12 seconds. During that period, F is frozen and retains its previous state. Once the refresh operation is complete, F resumes accepting observations.
This means the system can actively intervene in the sensor’s detection process without immediately propagating the temporary effects of that intervention. The same filtering mechanism handles both environmental interference and interference introduced by the system’s own recovery operations.
3.4 The Responsibility of Filtered Presence
The general filtering logic can be summarized as follows:
Short filtering delays can also be applied to suppress brief changes.
| Condition | F behavior |
|---|---|
| Normal sensor operation | Accept Raw with configured filtering |
| Confirmed environmental interference | Freeze during the noise window |
| Active sensor refresh | Freeze during the refresh window |
| Z2M unavailable or system not ready | Freeze until observations become usable |
The purpose of F is not to determine whether a room is occupied. Its purpose is to provide a usable observation to the rest of the system.
4. Spatial Presence: Giving Occupancy a Memory
Even after filtering known interference and handling unavailable states, F can still be wrong. A sensor can detect something that is not a person, or fail to detect someone who is still in the room. Instead of trying to eliminate every sensor error, I wanted to control when an observation was allowed to change the remembered occupancy state.
4.1 Rooms Are Connected Spaces
A home is not simply a collection of independent sensor coverage areas. Rooms are connected through doors, corridors, and open spaces. People generally need to move through these connections to enter or leave a room.
Suppose someone is inside a bedroom and the door remains closed. If the presence sensor suddenly reports Off, there is no additional evidence that the person has actually left the room. The opposite situation is also worth considering. If an empty room suddenly reports presence while its door has remained closed, that observation may be a false positive.
The layout of the home provides information about whether an occupancy transition is physically plausible.
For example:
The spatial relationships do not prove that someone has entered or left, but they give the system something more than the presence sensor alone.
4.2 Door Sensors as Transition Evidence
In my system, door sensors (DRS) are not used as presence sensors. They provide information about the possibility of movement across a spatial boundary.
| Door state | What it tells the system |
|---|---|
| Open | Movement across the boundary is possible |
| Closed | Movement across the boundary is normally restricted |
| Recently opened | Movement may have occurred recently |
A door opening does not prove that someone has passed through it. It does not indicate direction or the number of people involved. For this reason, I also use activity in adjacent spaces as additional evidence. If someone has entered or left a room, there is a reasonable chance that the movement also produces a presence observation in an adjacent space.
4.3 Adjacent Source
Not every occupied adjacent room should continuously provide transition evidence. For example, someone working in a bedroom with the door closed should not continuously provide evidence that people may be entering or leaving the living room. To address this, I introduced an entity called Adjacent Source.
For a typical room with a door:
Adjacent Source = EP AND Door Open
For an open space without a door:
Adjacent Source = EP
| Room presence | Door | Adjacent Source |
|---|---|---|
| Off | Closed | Off |
| Off | Open | Off |
| On | Closed | Off |
| On | Open | On |
This allows each space to expose its own usable transition evidence without requiring every neighboring space to understand its internal configuration. It also avoids treating occupancy behind a closed spatial boundary as continuously available evidence of movement.
4.4 Recent Windows
Physical movement, door events, and presence sensor updates do not happen at exactly the same time. Someone might leave a bedroom, close the door, and walk through the corridor before the bedroom sensor finally reports Off. By that time, both the door and the adjacent presence sensor might no longer provide active evidence.
To accommodate these timing differences, I introduced two types of recent windows:
- Door Recent Window
- Adjacent Recent Window
These windows retain evidence from recent events for a configurable period.
For example:
A recent window is different from an off delay. It does not keep a device running for a fixed period. It preserves the relevance of an event so that subsequent observations can still be evaluated against it.
The appropriate duration depends on the physical layout, sensor occupancy timeout, and actual reporting behavior. My representative timing parameters are provided in Section 10.3.
4.5 Allow to Change
Different rooms have different spatial configurations. Some have doors, some are open spaces, and some connect to multiple adjacent areas. Rather than making a generic state machine understand every possible room configuration, I encapsulate the spatial conditions in a Boolean entity called Allow to Change.
For a typical room with a door:
Spatial Evidence and Recent Windows
This is not a universal rule for every room. An entrance, corridor, or open living area may require different conditions. The state machine does not need to know how Allow is calculated. It only needs to know whether a transition is currently permitted. This keeps the spatial configuration separate from the state transition logic.
4.6 Internal Presence as a State Machine
The actual state machine is quite simple. It evaluates IP when either of the following occurs:
- F changes to a valid On or Off state.
- Allow to Change becomes On.
The second trigger is important because transition evidence and sensor observations do not necessarily arrive in the expected order.
At each evaluation:
| Current IP | F | Allow | Resulting IP |
|---|---|---|---|
| Off | On | On | On |
| Off | On | Off | Off |
| On | Off | On | Off |
| On | Off | Off | On |
| On | On | Either | On |
| Off | Off | Either | Off |
This handles two different sensor problems.
IP Off, F On
The sensor reports activity, but there is no supporting transition evidence. IP remains Off, preventing a potential false positive or non-human activity from reaching downstream controllers. If Allow subsequently becomes On, IP can accept the existing F On observation. F does not need to change again.
IP On, F Off
The sensor stops detecting activity, but there is no supporting departure evidence. IP remains On, preventing a temporary missed detection from turning off devices. If valid departure evidence subsequently becomes available while F remains Off, IP can change to Off.
Unlike a conventional off delay, IP is retained because the system lacks sufficient transition evidence, not simply because a timer has not expired.
| Conventional Off Delay | Spatial State Machine | |
|---|---|---|
| State retention | Fixed duration | Until an eligible transition |
| Transition evidence | Sensor and time | Sensor and spatial context |
| Normal departure | Waits for delay | Can respond when evidence is available |
| Main limitation | Delay duration trade-off | Incomplete transition evidence |
The state machine does not make the sensor more accurate. It makes the system less dependent on every individual sensor update being correct. However, it also introduces another problem: F and IP can remain inconsistent. The two directions of inconsistency are addressed differently, as described in Section 5 and Section 6.



























