A script to check sensor last update

There isn’t a per-device “last updated” field so it has to check every capability of each device, that’s why you get multiple logs per device.

Right now, it will mark a device as “invalid” if one of the capabilities is older than 1 day, but I guess it makes more sense if it only invalidates a device is all of the capabilities are too old:

const INVALIDATE_AFTER = 86400;

const invalidatedDevices = [];
for (const device of Object.values(await Homey.devices.getDevices())) {
  if (! device.capabilitiesObj) continue;
  let count = 0;
  for (const capabilityObj of Object.values(device.capabilitiesObj)) {
    if (! capabilityObj.lastUpdated || (Date.now() - new Date(capabilityObj.lastUpdated) > INVALIDATE_AFTER * 1000)) {
      count++;
    }
  }
  if (count && count === Object.keys(device.capabilitiesObj).length) {
    invalidatedDevices.push(device.name);
  }
}
await tag('InvalidatedDevices', invalidatedDevices.join(', '));
return invalidatedDevices.length != 0;

EDIT: I modified the script to handle capabilities that don’t have a lastUpdated. These are now treated the same as an “expired” capability.

4 Likes