cap:contact - contact sensing

This capability is used to mark sensors that report a wether contact is detected, such as for door and window sensors that detect if the door or window is open.

if(thing.matches('cap:contact')) {
  console.log('Has contact:', await thing.contact());
}

API

contact()

Boolean representing if the sensor is currently detecting contact.

Returns:Promise that resolves to if the sensor is detecting contact.

Example:

if(await thing.contact()) {
  console.log('Thing has detected contact');
}
isOpen()

Boolean representing if the sensor is currently open (not detecting contact).

Returns:Promise that resolves to if the sensor is in an open state.

Example:

console.log('Is open:', await thing.isOpen());
isClosed()

Boolean representing if the sensor is currently closed (detecting contact).

Returns:Promise that resolves to if the sensir is in a closed state.

Example:

console.log('Is closed:', await thing.isClosed());

Events

contactChanged

The contact value has changed. Payload is the new contact state as a boolean.

Example:

thing.on('contactChanged', v => console.log('Contact is now:', c));
opened

The sensor has detected it is does not have contact and is now opened.

Example:

thing.on('opened', v => console.log('Sensor is now open'));

Protected methods

updateContact(value)

Update if the sensor is currently detecting contact.

Arguments:
  • value – The new contact status as a boolean.

Example:

// Set the sensor to open
this.updateContact(false);

Implementing capability

Implementors of this capability should call updateContact whenever the contact state changes.

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

class Example extends Sensor.with(Contact) {

  constructor() {
    super();

    this.updateContact(true);
  }

}