# A script to check sensor last update

**URL:** <https://community.homey.app/t/a-script-to-check-sensor-last-update/63142>\
**Category:** Questions & Help\
**Tags:** homeyscript\
**Created:** [April 29, 2022, 7:44am UTC](https://community.homey.app/t/a-script-to-check-sensor-last-update/63142 "2022-04-29T07:44:44Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [May 1, 2022, 3:50pm UTC](https://community.homey.app/t/a-script-to-check-sensor-last-update/63142/9 "2022-05-01T15:50:54Z")

</div>

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:

```auto
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.

---

_[View the full topic](https://community.homey.app/t/a-script-to-check-sensor-last-update/63142)._
