cap:temperature - read temperature

This capability is used to mark sensors that report a temperature.

if(thing.matches('cap:temperature')) {
        const temperature = await thing.temperature();
        console.log('Temperature:', temperature.celsius);
}

API

temperature()

Get the current temperature.

Returns:Promise that resolves to the current temperature.

Example:

console.log('Temperature is:', thing.temperature);

Events

temperatureChanged

The temperature has changed. Payload is the new temperature.

Example:

thing.on('temperatureChanged', temp => console.log('Temp changed to:', temp));

Protected methods

updateTemperature(value)

Update the current temperature. Should be called whenever a change in temperature was detected.

Arguments:
  • value – The new temperature. Will be converted to a temperature, the default conversion uses degrees Celsius.

Example:

// Defaults to Celsius
this.updateTemperature(20);

// temperature value can be used to use Fahrenheit (or Kelvin)
const { temperature } = require('abstract-things/values');
this.updateTemperature(temperature(45, 'F'));

Implementing capability

Implementors of this capability should call updateTemperature whenever the temperature changes.

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

class Example extends Sensor.with(Temperature) {

        constructor() {
                super();

                this.updateTemperature(22);
        }

}