A flow to identify inactive sensors (values) / unavailable devices

Good afternoon and thanks for the help in advance! I (think) I’ve figured out most of the steps but keep getting this error. Any help would be appreciated!

Please post the code from the Homeyscript card, Albert.
And welcome to Homey forum.

1 Like

@Peter_Kawa Thanks for the assist. Here’s the code:

// Check last_update status. Argument: inactive time (in seconds):

const INVALIDATE_AFTER = args[0]; // xmpl: 86400 = 1 day (24h)

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)) {

    // Exclude by app names virtual devices and other app devices
    // (find correct app names via https://tools.developer.homey.app/tools/devices and look for "Driver")
    if (device.driverUri.match('vdevice|DeviceCapabilities|chronograph|betterlogic|devicegroups|callmebot|netscan' )) continue;
    // Exclude by (parts of) device name
    if (device.name.match('^se|^pr|TST|z_|z |z. |IR|rris|eboo|rada|KNM|Timeline|#1|#0|ESP|esp|TV C|TV S|iffu|Unav|Wekk|OUD|OLD|Peter|Custom|Afval|LEDs')) continue;

    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);
  }
}

// When there are matching sensors
if ( invalidatedDevices.length != 0 ) {
  // Create / update HomeyScript variable
  await tag('InvalidatedDevices', invalidatedDevices.join(', \n'));

  // Send Timeline notification
  Homey.flow.runFlowCardAction({
          uri: 'homey:manager:notifications',
          id: 'create_notification',
          args: {
            text: "\nDevices without updates for " + INVALIDATE_AFTER / 3600 + " hrs / " + INVALIDATE_AFTER / 3600 / 24 + " days:\n" + invalidatedDevices.join(', \n')
          },
        });

  console.log("Devices without updates for " + INVALIDATE_AFTER / 3600 + " hrs / " + INVALIDATE_AFTER / 3600 / 24 + " days: \n" + invalidatedDevices.join(', \n'));
}
  return("Devices without updates for " + INVALIDATE_AFTER / 3600 + " hrs / " + INVALIDATE_AFTER / 3600 / 24 + " days: \n" + invalidatedDevices.join(', \n'));

// When there's no matching sensor
if ( invalidatedDevices.length == 0 ) {
  // just report "None"
  // Create / update HomeyScript variable
  await tag('InvalidatedDevices', 'None');

  // Send Timeline notification
  Homey.flow.runFlowCardAction({
          uri: 'homey:manager:notifications',
          id: 'create_notification',
          args: {
            text: "\nDevices without updates for " + INVALIDATE_AFTER / 3600 + "hrs: None"
          },
        });

  console.log("Devices without updates for " + INVALIDATE_AFTER / 3600 + "hrs: None" );
}

return ("Devices without updates for " + INVALIDATE_AFTER / 3600 + "hrs / " + INVALIDATE_AFTER / 3600 / 24 + " days: None" );

// Original script by Robert Klep
// https://community.homey.app/t/a-script-to-check-sensor-last-update/63142/9

Some things have changed in Homeyscript;
Please get udated scripts here:

1 Like