This one should work better
// Sensor.onBattery_Check.js
// to check sensors connection by comparing last update times
let array = [];
const devices = await Homey.devices.getDevices({ filter: {class: 'sensor', capabilities: 'measure_battery'} });
_.forEach(devices, device => {
array.push( device.capabilitiesObj?.measure_battery.lastUpdated?.substring(0,10) + " / [" + device.name + "]" );
});
//oldest date on top sort array = ["time_stamp device", "time_stamp device", "time_stamp device" ]
const array_sorted = array.slice().sort(function(time_stamp, device) {
const firsttime_stamp = time_stamp.split(" ")[0];
const secondtime_stamp = device.split(" ")[0];
if(firsttime_stamp < secondtime_stamp) return -1;
if(firsttime_stamp > secondtime_stamp) return 1;
return 0;
});
console.log("Sensors battery alert:\n" + array_sorted.join('\n'));
Homey.flow.runFlowCardAction({
uri: 'homey:manager:notifications',
id: 'create_notification',
args: {
text: "Sensors battery alert:\n" + array_sorted.join('\n')
},
});
return(true);
.
And in this example you can find an alternative version of the script which measures any preferred device, including a filtering option (open the post to view all of it):