Hi guys, I can’t get my head around this.
I have this piece of HS code and, next to other data, I want to return the lastUpdated
timestamp. That works, but only for one capability (and I have three):
invalidatedDevices.push("- " + device.name + " - " + device.capabilitiesObj?.measure_temperature?.lastUpdated?.substring(0,16) + "\n (zone: " + device.zoneName + ")" );
Now I thought, I need the device capability measure_temperature
to be a wildcard, so it returns any capability.
(The capabilities are narrowed down alr on beforehand, to that one and alarm_contact
and alarm_motion
)
Is it even possible, and if yes, how?
Many thanks in advance!
~Peter
The complete script:
// Sensor.Check, to check on sensor last update time. Argument: inactive time (in seconds):
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 (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 + " - " + device.capabilitiesObj?.measure_temperature?.lastUpdated?.substring(0,16) + "\n (zone: " + device.zoneName + ")" );
}
}
// When there are matching sensors
if ( invalidatedDevices.length != 0 ) {
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'));
}
// Original script by Robert Klep
// https://community.homey.app/t/a-script-to-check-sensor-last-update/63142/9
@Arie_J_Godschalk any thoughts?