cap:motion - motion sensing

This capability is used to mark sensors that monitor movement.

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

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

API

motion()

Get the motion status.

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

Example:

console.log('Motion is:', thing.motion);

Events

motionChanged

The current motion status has changed.

thing.on('motionChanged', 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

updateMotion(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.updateMotion(false);

// Set motion to true and automatically switch back after 20 seconds
this.updateMotion(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, Motion } = require('abstract-things/sensors');

class Example extends Sensor.with(Motion) {

  constructor() {
    super();

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

}