Device
The device object represents a device installed in the platform. Certain scripts, such as LoRaWAN or MQTT data conversion scripts, receive a device object as a parameter representing the device to which the data is destined. In scripts executed from actions, it is possible to access the list of devices through the devices property of the global variable env, which represents the execution environment.
Properties
### address (string) The address property represents the address of the device, as text.
Examples
This example shows the address of a device in the log console.
env.log('Device address: ', myDevice.address);### endpoints (endpoint collection) The endpoints property represents the list of endpoints contained within the device. This list is an object of type endpoint collection.
Examples
This example shows the number of endpoints of a device in the log console.
env.log('Endpoint count: ', myDevice.endpoints.count);### description (string) The description property represents the description of the device.
Examples
This example shows the description of a device in the log console.
env.log('Device description: ', myDevice.description);Methods
### updateDeviceBattery(battery) The updateDeviceBattery() method allows updating the battery status of the device, including for devices that contain more than one battery (for example, main and backup battery).
Parameters
- battery (battery status object, or array of battery status objects): this parameter indicates the battery status. If the device contains a single battery, a battery status object should be passed. If the device contains more than one battery, an array of battery status objects should be passed, containing the status of all batteries. For each object passed as a parameter, at least the percentage property (if the charge percentage is available), or the voltage property (if the voltage is available), or both, should be specified. If the type property is omitted, the batteryType.default type will be assumed. When reporting the status of multiple batteries, it is mandatory to report the type property for each one.
Example 1
This example shows how to report a battery level of 45% on a device that has a single battery.
myDevice.updateDeviceBattery({ percentage: 45 });Example 2
This example shows how to report a battery level of 72% for the primary battery, and 68% for the secondary battery, on a device that has both primary and secondary batteries.
myDevice.updateDeviceBattery
(
[
{ type: batteryType.primary, percentage: 72 },
{ type: batteryType.secondary, percentage: 68}
]
);Example 3
This example shows how to report a battery level of 2.92 volts, on a device with a single battery that reports voltage instead of remaining charge percentage.
myDevice.updateDeviceBattery({ voltage: 2.92 });### updateDeviceFirmwareVersion(version) The updateDeviceFirmwareVersion() method allows indicating the firmware version currently installed on the device.
Parameters
- version (string): this parameter indicates the current firmware version of the device, using one of the following formats:
- "X", where X is a number between 0 and 65535.
- "X.Y", where X and Y are numbers between 0 and 65535.
- "X.Y.Z", where X, Y, and Z are numbers between 0 and 65535.
- "X.Y.Z.W", where X, Y, Z, and W are numbers between 0 and 65535.
For more information about version numbers, visit this page.
Example 1
This example shows how to indicate that a device has firmware version "1.2.3".
myDevice.updateDeviceFirmwareVersion("1.2.3");### updateDeviceRssi(rssi) The updateDeviceRssi() method allows updating the signal level (RSSI) of the device, including for devices that contain multiple wireless communication interfaces.
Parameters
- rssi (rssi status object, or array of rssi status objects): this parameter indicates the signal level. If the device contains a single wireless interface, an rssi status object should be passed. If the device contains more than one wireless interface (for example, cellular and Wi-Fi), an array of rssi status objects should be passed, containing the signal level of each interface. For each object passed as a parameter, at least the quality property (if the signal percentage is available), or the strength property (if the attenuation level is available), or both, should be specified. If the type property is omitted, the rssiType.default type will be assumed. When reporting the status of multiple interfaces, it is mandatory to report the type property for each one.
Example 1
This example shows how to report a signal level of 68% on a device that has a single communication interface.
myDevice.updateDeviceRssi({ quality: 68 });Example 2
This example shows how to report a signal level of 72% for the cellular interface, and 68% for the Wi-Fi interface, on a device that has both interface types.
myDevice.updateDeviceRssi
(
[
{ type: rssiType.cellular, quality: 72 },
{ type: rssiType.wiFi, quality: 68 }
]
);Example 3
This example shows how to report a signal level with an attenuation of -68 dBm, on a device with a single communication interface.
myDevice.updateDeviceRssi({ strength: -68 });### updateDeviceGeolocation(latitude, longitude) The updateDeviceGeolocation() method allows indicating the device's location, specifying latitude and longitude.
Parameters
- latitude (double): indicates the latitude of the device's current location.
- longitude (double): indicates the longitude of the device's current location.
Example 1
This example shows how to indicate that a device is located at coordinates (40.4052, -3.87699).
myDevice.updateDeviceGeolocation(40.4052, -3.87699);