Beyond Occupancy: Designing a Resilient Spatial Presence System in Home Assistant

Part 2


5. Active Refresh: Asking the Sensor to Observe Again

5.1 Using Inferred State as Feedback

When IP is Off and F is On without valid transition evidence, the state machine can prevent the observation from affecting downstream devices. This does not mean Refresh is required for the presence system to continue operating. If someone subsequently enters the room and Allow becomes On, IP can accept the existing F On observation.

However, a sensor that remains incorrectly On is still a problem. It provides less useful information about subsequent activity and may continue reporting an occupied room long after it has become empty. I therefore use the inferred occupancy state as feedback to identify observations that may deserve another look.

The purpose of Refresh is not to force IP and F to agree. It is to improve the underlying observation when the relationship between F, IP, and the spatial context suggests that the sensor may be wrong. The state machine and Refresh remain separate processes.

Process Responsibility
State Machine Determine whether IP should accept F
Refresh Attempt to improve a questionable sensor observation

While experimenting with the SNZB-06P, I found that temporarily changing its sensitivity and then restoring the original setting could sometimes resolve persistent false detections. I call this operation a Sensitivity Bump.

5.2 Sensitivity Bump

The operation consists of three steps:


In my setup, the sensitivity change is maintained for approximately five seconds. The complete refresh script takes around 12 seconds, including the additional waiting periods.

This serves a different purpose from requesting the current occupancy state through MQTT /get. A state request may help if the problem is related to reporting or communication. However, if the sensor itself still believes the room is occupied, reading its current state again may simply return the same incorrect result.

A Sensitivity Bump attempts to influence the sensor’s detection process. In my experience, Raw generally returns to Off during the operation and then begins detecting again.

I do not know exactly what happens inside the sensor firmware when its sensitivity changes. I only know that the operation has helped resolve certain persistent detection problems in my setup. It is an empirical workaround, not a guaranteed sensor reset. As explained in Section 3.3, F is frozen during the refresh script so that temporary changes in Raw do not immediately affect the rest of the system.

5.3 Separating Refresh Execution from Refresh Strategy

I use a reusable script blueprint to implement the Sensitivity Bump. Each room has a refresh entry point configured for its own sensor. Higher-level automations decide when to call that entry point.

This means I can independently modify how a sensor is refreshed and when it should be refreshed. The execution mechanism can remain the same even when new refresh strategies are introduced.

5.4 Different Refresh Strategies

Not every refresh is triggered for the same reason.

Strategy Purpose
Conflict Refresh Re-observe a sensor when IP Off / F On persists
Stale Refresh Re-evaluate observations that may no longer be reliable
Door Refresh Re-observe after relevant boundary events
Adjacent Refresh Re-observe in response to relevant neighboring activity
Environmental Refresh Re-observe after known interference ends
Global Refresh Coordinate re-observation across multiple spaces
Manual Refresh Allow the user to investigate an unexpected sensor state

Conflict Refresh and Stale Refresh are particularly important to distinguish. Conflict Refresh responds to an inconsistency between F and IP. Stale Refresh does not necessarily require such an inconsistency. Both F and IP could be On, while the surrounding spatial conditions suggest that the sensor observation deserves another look.

For example, activity in another room combined with the absence of valid transition conditions in the current room can help identify a potentially stale detection. The refresh strategies can have different waiting periods, eligibility conditions, retry intervals, and execution limits. Some are immediate event-driven operations. Others monitor a persistent condition and retry when necessary.

I have also considered unconditional periodic refresh, but it is not my preferred approach. Refreshing a sensor can itself introduce temporary state changes. Even with a noise window, there is little reason to continuously intervene when the system is operating normally.

5.5 When Refresh Does Not Work

Sensitivity Bump does not resolve every persistent detection problem. For a continuing conflict, the system can perform a limited number of refresh attempts. If the inconsistency remains, it eventually notifies the user. There is no guaranteed automatic sensor repair beyond that point.

The system cannot conclusively determine whether the sensor is wrong or the remembered occupancy state is wrong simply by observing their disagreement. Forcing them to agree could introduce a different error. Repeatedly refreshing the sensor without a limit would also be undesirable. This is an intentional limitation of the system. It attempts recovery where possible, but stops when it no longer has a reliable way to improve the observation.

Manual recovery options are discussed in Section 11.4.


6. Presence Fallback: Retaining Memory Without Getting Stuck

The opposite inconsistency, IP On and F Off, requires a different strategy. In this case, retaining IP is exactly what I want the state machine to do when a sensor temporarily misses someone. However, the same mechanism can also retain IP when someone has actually left but the system failed to obtain valid departure evidence.

6.1 Spatial Evidence Can Be Missed

The state machine depends on the availability and timing of spatial evidence.

There are several ways this can fail:

  • Someone moves faster than the sensor’s occupancy timeout.
  • Adjacent presence disappears too early.
  • A recent window expires before F changes.
  • A sensor misses activity in a transition area.
  • Neighboring spaces create dependencies that prevent a valid transition.

In these situations, IP can remain On even after the room becomes empty. Without an additional mechanism, that state could continue indefinitely.

6.2 Why Not Simply Clear IP After a Timeout?

A straightforward solution would be to force IP Off after F has remained Off for a certain period. But consider what happens if someone is actually still in the room.

Once IP has been forcibly cleared, the next F On observation may not have valid transition evidence. The door has not opened, and there may be no adjacent activity. The state machine could therefore reject the new observation. By clearing IP, the system has destroyed the memory that was originally intended to protect against missed detection.

I wanted to limit the effect of stale IP without necessarily changing IP itself. This is why Effective Presence exists.

6.3 Effective Presence and Fallback

Under normal conditions, EP follows IP. When Fallback is enabled, EP follows F instead.

Non-destructive Fallback

When IP remains On while F remains Off beyond a configurable threshold, Fallback can be activated. IP continues to remember the previous occupied state, but EP is now allowed to follow the current observation. Downstream devices can turn off without the system clearing its internal memory.

Stage IP F Fallback EP
Normal occupancy On On Off On
Missed detection or missed departure On Off Off On
Persistent inconsistency On Off On Off
Presence detected again On On On On
Alignment confirmed On On Off On

If F later returns to On, EP immediately follows it, even if there is no new Allow to Change. F and IP are now aligned, so Fallback can be disabled and EP can return to IP.

Recovery can also occur if the state machine obtains valid departure evidence and changes IP to Off while F is already Off. In either case, F and IP naturally converge. The system does not need to force a state transition just to exit Fallback.

Forced IP Timeout Presence Fallback
Persistent F Off Clears IP Changes EP source
Original memory Lost Retained
Subsequent F On May require Allow EP can recover directly
Recovery Rebuild IP Wait for alignment
Modifies internal state Yes No

This is one of the most useful consequences of separating internal state from effective output.

6.4 Redistributing the Responsibility of Time Delays

The system still uses time delays. However, they no longer serve exactly the same purpose as a conventional off delay.

A conventional approach treats all F Off events in the same way:

When valid departure evidence exists, the state machine can accept the observation without waiting for the Fallback threshold. Only when the transition cannot be established does the longer time threshold become relevant.

A five-minute Fallback threshold is therefore not equivalent to a five-minute lighting off delay. One limits the impact of a persistent inconsistency. The other delays every departure. This allows me to retain occupancy during temporary missed detection without unnecessarily slowing down normal departure detection.

6.5 Quiet Mode (Optional)

Even a persistent F Off is not always a good reason to activate Fallback. During sleep or other low-activity periods, a sensor may repeatedly or continuously miss someone who is still in the room. In these situations, the remembered occupancy state can be more useful than the current observation. I therefore use Quiet Mode as an optional additional condition for automatic Fallback activation.

Automatic Fallback Entry:

    IP is On
    AND F is Off
    AND Quiet Mode is Off
    AND the condition persists for the configured duration

Quiet Mode does not force presence On. It does not create a separate occupancy state machine either. It simply prevents a particular recovery policy from overriding the remembered state under circumstances where missed detection is expected.

Context Fallback behavior
Normal operation Persistent IP On / F Off can activate Fallback
Quiet Mode Normal automatic Fallback entry is inhibited
Existing Fallback Can still exit through state alignment
Explicit system rebuilding Fallback can be activated by the recovery process

The state machine itself remains unchanged. Quiet Mode is not required to implement Spatial Presence. It is an extension for situations where long periods of limited human movement are expected. The same Fallback mechanism is also used during system rebuilding, as described in Section 8.


7. Bypass: When the Normal Spatial Model Does Not Apply

The spatial state machine is based on assumptions about human movement. However, not all activity in a smart home comes from humans, and ordinary presence sensors cannot reliably distinguish between human and non-human movement. Robot vacuums are a particularly useful example.

7.1 Robot Vacuums and the Problem of Activity Origin

A robot vacuum can start cleaning while nobody is home. Unlike a person entering the home, the robot does not arrive through the entrance or necessarily move into its first cleaning area from an adjacent space. It starts moving from its dock. From the perspective of the spatial state machine, activity has effectively appeared inside a previously empty space. The sensor may correctly detect that movement, but the spatial model has no corresponding human entry evidence.

Under the normal Conflict Refresh policy, the system could interpret this as a questionable sensor observation. It may refresh the sensor repeatedly, but the robot is still moving. The sensor will continue detecting activity. Eventually, the system could generate unnecessary notifications.

Stage Normal inference behavior
Robot leaves dock F detects activity
No valid transition evidence IP retains Off
Persistent inconsistency Conflict Refresh becomes eligible
Robot continues moving Refresh does not eliminate detection
Retry limit reached Unnecessary fault notification

The problem is not necessarily that the sensor is wrong. The problem is that a valid observation is being evaluated using an inappropriate movement model.

7.2 Context-Specific Bypass

When the robot vacuum is away from its dock, I allow the relevant effective presence states to follow F instead of IP. I also bypass the normal conflict handling that would otherwise treat robot-generated movement as a sensor problem.

This serves two purposes:

  • Avoid inappropriate refresh attempts and fault notifications caused by expected robot activity.
  • Allow downstream devices to respond to detected activity when useful.

For example, presence-based lighting can provide illumination to a robot vacuum cleaning a dark room. The lighting assistance is a useful consequence of the Bypass policy, rather than the primary reason for introducing it.

Both Fallback and Bypass can make EP follow F, but they exist for different reasons.

Mechanism Reason Purpose
Fallback Persistent inconsistency Limit the impact of stale internal state
Bypass Known operating context Temporarily use a more appropriate output policy

The general output selection can be expressed as:

IF Bypass OR Fallback:
    EP = F
ELSE:
    EP = IP

The exact bypass conditions can remain specific to each space or control requirement. There is no need to incorporate all these exceptions into the generic state machine. This also illustrates why EP should not necessarily be interpreted as a strict representation of human occupancy. EP is the effective presence signal used for a particular control purpose.

7.3 Returning from Bypass

Starting the robot is only half of the problem. The other half is ensuring that the system converges correctly when cleaning ends.

Consider a robot cleaning an empty home. During cleaning, sensor observations and remembered occupancy states may change. Someone could also leave the home while cleaning is already in progress. When the robot returns to its dock, immediately switching EP back to IP could expose internal states that no longer represent the current situation.

I therefore use the completion of robot activity as an opportunity to re-evaluate presence. Once the relevant robots have remained docked for a configured period, the system initiates presence re-evaluation and provides the appropriate transition eligibility for House Presence.

The objective is to allow the system to return to a consistent state even when cleaning begins and ends while nobody is home. The recovery process is shared with other rebuilding scenarios and is described in Sections 8.2–8.4. The application of transition evidence at the whole-home scale is discussed in Section 9.2.


8. Rebuilding: Recovering Without Resetting Everything

8.1 System Initialization

As described in Section 3.2, when Z2M is offline or the system is not ready, F retains its previous observation. Once Z2M reconnects, the system needs to allow time for sensor information to become usable again.

There are two separate recovery problems:

  1. When should F resume accepting observations?
  2. How should existing IP states be reconciled with those observations?

My initialization process handles these separately.

By enabling Fallback before normal observation updates resume, the system can use newly available observations without requiring every internal state to be corrected first. IP remains under the control of the normal state machine.

The initialization delay in my setup is 30 seconds. This is not a guarantee that every sensor has produced a new observation. It is a stabilization period before normal observation updates resume.

8.2 Whole-Home Presence Evaluation

Another situation that benefits from explicit re-evaluation is leaving the home. Sometimes, individual rooms retain occupancy states after everyone has left.

My initial approach was to clear all internal presence states. However, this could interfere with transitions that were still in progress. For example, manually clearing the entrance IP while someone is still standing at the entrance does not necessarily produce a new F or Allow event that would restore it. I eventually replaced the destructive reset approach with a whole-home evaluation process.

The system enables Fallback across the relevant spaces and establishes an evaluation window. Sensor refresh can also be coordinated as part of the wider re-evaluation process where appropriate. This allows F to provide effective output while IP continues to be maintained by its state machine. My current evaluation window is 30 seconds.

If a room’s F and IP align before the evaluation window ends, its Fallback can exit early. There is no requirement for every room to remain in Fallback for the entire duration.

8.3 Presence Rebuilding

System initialization, whole-home evaluation, and recovery after robot vacuum activity have different triggers. However, they share a common requirement: existing internal states may no longer be reliable enough to immediately resume normal output. I represent this shared recovery phase using a Presence Rebuilding state.

Conceptually:

Presence Rebuilding =
    NOT System Ready
    OR Presence Evaluating

This provides a common recovery lifecycle without requiring the individual room state machines to understand why rebuilding was initiated.

The mechanisms that initiate rebuilding can remain independent. The reconciliation mechanism does not need to distinguish whether the system has recovered from a communication interruption, completed robot cleaning, or initiated a whole-home evaluation.

8.4 Reconciliation

Normally, Fallback exits when F and IP become aligned. However, when Fallback is explicitly enabled during initialization or evaluation, some rooms may already have matching F and IP states. In an event-driven system, there may be no new alignment event to trigger Fallback exit. An explicit reconciliation check addresses this.

After rebuilding ends and the system has had time to stabilize:

IF F and IP are valid and equal:
    Disable Fallback
ELSE:
    Retain the existing Fallback state

This check does not modify IP. It also does not require the system to remember which process originally enabled Fallback.

F IP Reconciliation result
Off Off Disable Fallback
On On Disable Fallback
Off On Retain existing Fallback
On Off Retain existing Fallback
Invalid Any Do not reconcile

In my setup, the reconciliation check occurs after Presence Rebuilding has remained Off for 30 seconds. The evaluation timer is not intended to enforce Fallback for a fixed duration. Its purpose is to provide an explicit reconciliation opportunity after the evaluation process.

Rooms that recover earlier can exit earlier. Rooms that remain inconsistent can continue using Fallback until they recover naturally.