You could create a schedule to make the request to /devices/<deviceId>/health
and when the device is offline, send the event to DeviceWatch-DeviceStatus
.
For you to take control over the DeviceWatch events, you need to enroll as follows:
//initialize
sendEvent(name: "DeviceWatch-DeviceStatus", value: "online")
sendEvent(name: "DeviceWatch-Enroll", value: [protocol: "cloud", scheme:"untracked"].encodeAsJson(), displayed: false)
//event to change the deviceStatus
sendEvent(name: "DeviceWatch-DeviceStatus", value: "offline")
Then, to create the subscription for a specific device health you’ll have to use that attribute.
context.api.subscriptions.subscribeToDevices(deviceConfig, 'healthCheck', 'DeviceWatch-DeviceStatus', 'healthEventHandler')
//payload example
{
eventId: 'xxxx-xxxx-xxxx',
locationId: 'xxxx-xxxx-xxxx',
ownerId: 'xxxx-xxxx-xxxx',
ownerType: 'LOCATION',
deviceId: 'xxxx-xxxx-xxxx',
componentId: 'main',
capability: 'healthCheck',
attribute: 'DeviceWatch-DeviceStatus',
value: 'offline',
valueType: 'string',
stateChange: true,
data: {},
subscriptionName: 'healthEventHandler_0'
}
Or, you can use the health subscription part of the SDK, this is at a location level, so, you will receive the events from all the devices in it.
context.api.subscriptions.subscribeToDeviceHealth('healthEventHandler')
//eventHandler
.subscribedDeviceHealthEventHandler('healthEventHandler', (context, event) => { })
//payload example
{
eventId: 'xxxx-xxxx-xxxx',
locationId: 'xxxx-xxxx-xxxx',
deviceId: 'xxxx-xxxx-xxxx',
hubId: 'xxxx-xxxx-xxxx',
status: 'ONLINE',
reason: 'NONE'
}