Tuesday 10 February 2015

Using Phi Accural Failure Detectors to Improve Occupancy Sensors

Aeon Labs MultiSensor (left) and
Fibaro Motion Sensor (right)
We’ve got a bunch of Z Wave occupancy sensors (Aeon Labs MultiSensors and Fibaro Motion Sensors - shootout coming soon) to automate the lighting in the office and tv room.  Unfortunately these are both rooms you tend to sit very still in, and having to periodically wave your hands to turn the lights on is seriously damaging the WAF of the system.  I was looking for an approach that wasn’t particularly sensitive to sensor positioning and that didn’t involve fine tuning timeouts.

The approach I’ve gone for is to use a python implementation of the Phi Accural Failure Detector to better interpret the signals from the motion sensors.  These detectors are more commonly used to decide the health of hosts in a distributed system, such as in Apache Cassandra.  These detectors record the intervals observed between heartbeats (pings in the distributed system case), and use this distribution to decide if the target is alive at a given time.

I’m using the detector to determine if a room is occupied (as opposed to a host being alive), but unfortunately the Z Wave motion sensors don’t send heartbeats periodically for me to feed to the detector.  Instead, the sensors send edge-triggered events when they believe the room’s occupancy state has changed, based on their own internal algorithm, which I’m reasonably sure is just a simple timeout.

The initial approach to this was to synthesize heartbeats to the detector periodically, when the sensor says that the room is occupied.   The detector is then periodically used to decide whether to turn the lights on.  The implementation of this can be seen in this changeset.  As the logic for the home automation system runs on App Engine, there is relatively little control over the exact timing of this periodic cron job.  It was very possible that the sensor would detect motion (and the system would heartbeat the detector), then a few seconds later the cron job would run, see the sensor says that the room is occupied, and heartbeat the detector again.  Similarly, the cron job could run, heartbeat the detector, then moments later the sensor could timeout (and the previously synthesized heartbeat could be considered wrong).

As the detector is based on recording heartbeat inter-arrival times, this approach doesn’t work particularly well - the recorded inter-arrival times would be too artificial (based on the cron job timing) and occasionally completely misleading; either too short (at the beginning of the period) or completely wrong (at the end of the period).

The improved approach is based on knowing the internal timeouts the sensors use, which luckily the Z Wave sensors expose as a configuration value.  Now, the system only synthesizes events and feed them to the detector when the sensors “stops” detecting motion - and can therefore avoid the synthesized events being outright wrong (by making the last event happen timeout seconds in the past) and avoid the event inter-arrival times being too artificial (by spacing them timeout seconds apart).   While this approach makes the detector useless while the sensor is “on”, they are not needed at this point - we know the room is occupied!


UI tile showing lighting controls
for a room
You can find the implementation of this approach on Github.  Since implementing this we’ve not had to wave our hands to turn the lights on once.  One downside of this approach is that the lights tend to stay on longer than we would like once we leave the room - about 30 minutes.   This is not a big problem - I’d rather the lights were on for too long rather than occasionally turning off when I’m in the room, and the lights are all low powered LED types (Philips Hue, for example).   I think lowering the timeouts on the sensors will also help here.

4 comments:

  1. "I'm sorry, Tom. I'm afraid I can't let you do that". - HAL

    ReplyDelete
  2. Hi there,
    Thank you sharing such a useful information.
    We are leading motion sensors, smart lights, home automation products manufacturers in Vadodara

    Visit our website for more information - PIR Motion Sensor

    ReplyDelete
  3. Very good & informative content on topic
    Thanks for sharing such a useful tips.

    Visit our website for Software development company in Vadodara

    ReplyDelete
  4. Thank you for sharing such a useful information.

    iMagic Baroda
    Apple Showroom in Vadodara

    ReplyDelete