cap:motion-detection - motion sensing

This capability is used to mark sensors that monitor movement.

if(thing.matches('cap:motion-detection')) {
        console.log('Detected motion:', await thing.motionDetected());

        thing.on('movement', () => console.log('Motion detected'));
        thing.on('inactivity', () => console.log('Inactivity detected'));
}

API

motionDetected()

Get the motion status.

Returns:Promise that resolves to a boolean indicating if movement is currently detected.

Example:

// Using async/await
console.log('Motion is:', awwait thing.motionDetected());

Events

motionDetectedChanged

The current motion status has changed.

thing.on('motionDetectedChanged', value => console.log('Motion changed to:', value));
movement

Emitted when movement has been detected and motion changes to true.

thing.on('movement', () => console.log('Movement detected'));
inactivity

Emitted when movement is no longer detected and motion changes to false.

thing.on('inactivity', () => console.log('Movement no longer detected'));

Protected methods

updateMotionDetected(value[, autoIdleTimeout])

Update the current motion status.

Arguments:
  • value (boolean) – The motion status, true if motion detected otherwise false.
  • autoIdleTimeout (duration) – Optional duration to switch back the motion status to false.

Example:

this.updateMotionDetected(false);

// Set motion to true and automatically switch back after 20 seconds
this.updateMotionDetected(true, '20s');

Implementing capability

Implementors of this capability should call updateMotion if motion is detected. Implementations may choose between using automatic timeouts for switching motion back to false or managing the switchin on their own.

const { Sensor, MotionDetection } = require('abstract-things/sensors');

class Example extends Sensor.with(MotionDetection) {

        constructor() {
                super();

                this.updateMotionDetected(true, '1m');
        }

}