# Script get all battery of modules

**URL:** <https://community.homey.app/t/script-get-all-battery-of-modules/93614>\
**Category:** Developers\
**Created:** [November 3, 2023, 3:42pm UTC](https://community.homey.app/t/script-get-all-battery-of-modules/93614 "2023-11-03T15:42:05Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)\
**Post date:** [November 3, 2023, 5:39pm UTC](https://community.homey.app/t/script-get-all-battery-of-modules/93614/3 "2023-11-03T17:39:30Z")

</div>

This one should work better

```auto
// 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):

> [@The Flow Exchange(r) - Exchange Your Flows with Others!](https://community.homey.app/t/the-flow-exchange-r-exchange-your-flows-with-others/68981/13):
>
> Sensor (or any device) Connection Check. Sometimes, you’ll discover a sensor or other device doesn’t report it’s status or value anymore, but it does NOT notify you of being offline… Well, this flow checks the, most recent update time of your sensors. You can run the flow every day, twice a day, just add or remove a time triggercard. The ‘inactive time’ can be edited as well (24hrs as default) and you can choose to not send notifications when all sensors did report inside the ‘inactive time…

---

_[View the full topic](https://community.homey.app/t/script-get-all-battery-of-modules/93614)._
