User Tools

Site Tools


links:sw-dev:homey:homey-scripts

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
links:sw-dev:homey:homey-scripts [2023-02-07 09:55] – [Check last update for devices] anders_adminlinks:sw-dev:homey:homey-scripts [2023-02-07 09:57] (current) – [Check last update for devices] anders_admin
Line 22: Line 22:
 await tag('InvalidatedDevices', invalidatedDevices.join(', ')); await tag('InvalidatedDevices', invalidatedDevices.join(', '));
 return invalidatedDevices.length != 0; return invalidatedDevices.length != 0;
 +</code>
 +
 +<code>
 +// Sensor.Check, to check on sensor last update time. Argument: inactive time (in seconds):
 +
 +const INVALIDATE_AFTER = (args[0]); // Example 86400sec./3600 = 24hrs
 +
 +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 (device.capabilitiesObj.measure_temperature || device.capabilitiesObj.alarm_contact || device.capabilitiesObj.alarm_motion ) { 
 +
 +      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 + "\n   (zone: " + device.zoneName + ")" );
 +  }
 +}
 +
 +// 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: \n" + invalidatedDevices.join('\n') + "\n\nflow Sensors Check"
 +          },
 +        });
 +
 +  console.log("Devices without updates for " + INVALIDATE_AFTER / 3600 + "hrs: \n" + invalidatedDevices.join('\n'));
 +  return("Devices without updates for " + INVALIDATE_AFTER / 3600 + "hrs: \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\n\nflow Sensors Check"
 +          },
 +        });
 +
 +  console.log("Devices without updates for " + INVALIDATE_AFTER / 3600 + "hrs: None" );
 +  return invalidatedDevices.length != 0;
 +}
 +
 +// Original script by Robert Klep  
 </code> </code>
links/sw-dev/homey/homey-scripts.txt · Last modified: 2023-02-07 09:57 by anders_admin