# 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:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![dudu\_95](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/dudu_95/32/93061_2.png) [@dudu\_95](https://community.homey.app/u/dudu_95)
#### Post date: [November 3, 2023, 3:42pm UTC](https://community.homey.app/t/script-get-all-battery-of-modules/93614/1 "2023-11-03T15:42:05Z")

</div>

Hello, I’ve adopted a script from a user, but when I run it on Homey 2023, I encounter this error message. Can someone provide the fix?

```auto
// battery-report
let array = [];

const devices = await Homey.devices.getDevices({ filter: {capabilities: 'measure_battery'} });
_.forEach(devices, device => {
array.push( device.name + ': \n' + device.capabilitiesObj?.measure_battery.value + '%' );
});

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

return("Battery levels:\n" + array_sorted.join('\n') + "\n");

```

 ![capt-error](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/d/3/d3c8911db6b648bf7fc7fe858c2855d5bf3ec19e.png)

---

<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…

---

<div class="post-metadata">

### Author: ![dudu\_95](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/dudu_95/32/93061_2.png) [@dudu\_95](https://community.homey.app/u/dudu_95)
#### Post date: [November 3, 2023, 6:38pm UTC](https://community.homey.app/t/script-get-all-battery-of-modules/93614/4 "2023-11-03T18:38:34Z")

</div>

Thank you, I will try.

---

<div class="post-metadata">

### Author: ![dudu\_95](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/dudu_95/32/93061_2.png) [@dudu\_95](https://community.homey.app/u/dudu_95)
#### Post date: [November 3, 2023, 7:02pm UTC](https://community.homey.app/t/script-get-all-battery-of-modules/93614/5 "2023-11-03T19:02:05Z")

</div>

Sorry, but with your corrected code, I still have an error

 ![capt-error2](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/7/1/714b377e27761709b1a48feab2318aae9e66e3cf.png)

---

<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, 10:01pm UTC](https://community.homey.app/t/script-get-all-battery-of-modules/93614/6 "2023-11-03T22:01:38Z")

</div>

When I change the last line

```auto

return true;

```

to this

```auto

return "Sensors battery alert:\n" + array_sorted.join('\n');

```

the HS flow card finishes like it should  
I cannot reproduce the error you show.

Do you happen to own a Pro 2023? I run the script on a Pro 2019…

Did you check the other script I referred to earlier:

> [@Peter\_Kawa](#):
>
> 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! - #13 by Peter\_Kawa](https://community.homey.app/t/the-flow-exchange-r-exchange-your-flows-with-others/68981/13)

---

<div class="post-metadata">

### Author: ![dudu\_95](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/dudu_95/32/93061_2.png) [@dudu\_95](https://community.homey.app/u/dudu_95)
#### Post date: [November 4, 2023, 9:27am UTC](https://community.homey.app/t/script-get-all-battery-of-modules/93614/7 "2023-11-04T09:27:15Z")

</div>

Great, you’ve found the issue, but the script doesn’t return any results. It seems like something is missing in the code to display the list. Do you know why? Yes, I’m using Homey 2023, and I’ve also encountered this issue with Pushover, which throws an error but still works. For your information, the developer is asking if someone could review his code to make it compatible with Homey 2023, as he no longer has the time to address it. So, if you’re interested, send him a message.

---

<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 5, 2023, 1:30am UTC](https://community.homey.app/t/script-get-all-battery-of-modules/93614/8 "2023-11-05T01:30:27Z")

</div>

OK. You seem to focus on this script. Curious why don’t you try the, way better, alternative script I pointed out twice?

---

<div class="post-metadata">

### Author: ![dudu\_95](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/dudu_95/32/93061_2.png) [@dudu\_95](https://community.homey.app/u/dudu_95)
#### Post date: [November 5, 2023, 10:10am UTC](https://community.homey.app/t/script-get-all-battery-of-modules/93614/9 "2023-11-05T10:10:22Z")

</div>

> [@Peter\_Kawa](#):
>
> The Flow Exchange(r) - Échangez vos flux avec d’autres ! - #13 par Peter\_Kawa

Yes, I even installed it, but your script only brings back the modules where no action has been taken for XX minutes. What I want, in relation to the script I’m showing you, is the ability to display all the batteries with their percentages next to them, just like what’s available on the iOS application

---

<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 6, 2023, 2:42am UTC](https://community.homey.app/t/script-get-all-battery-of-modules/93614/10 "2023-11-06T02:42:46Z")

</div>

> [@dudu\_95](#):
>
> What I want, in relation to the script I’m showing you, is the ability to display all the batteries with their percentages next to them

Owww, my bad, coffee shortage most probably 😛 Somehow I thought it was the ‘device last-updated’ script…  
I now I get your plans.  
No clue what changed, I now only got 2 devices (both robovacs, it’s just scary) from 30-ish battery using devices. At the time of publishing the flow + script, it showed all battery levels 🤔

 ![Screenshot from 2023-11-06 03-40-45](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/3/f/3feb8041615524b8b7591fea531e214eccb2edb1.png)

.  
The array\_sort part wasn’t needed by the way,

```auto
// battery-report
let array = [];

const devices = await Homey.devices.getDevices({ filter: {capabilities: 'measure_battery'} });
_.forEach(devices, device => {
array.push( device.name + ': \n' + device.capabilitiesObj?.measure_battery?.value + '%' );
});

return "Battery levels:\n" + array.slice('\n') + "\n" ;

```

---

<div class="post-metadata">

### Author: ![dudu\_95](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/dudu_95/32/93061_2.png) [@dudu\_95](https://community.homey.app/u/dudu_95)
#### Post date: [November 7, 2023, 4:41pm UTC](https://community.homey.app/t/script-get-all-battery-of-modules/93614/11 "2023-11-07T16:41:13Z")

</div>

t’s okay, I’ve rewritten the code to make it work. Now it displays the battery levels for each module from the lowest to the highest. 😃

```auto
const devicesObject = await Homey.devices.getDevices({ filter: { class: 'sensor' } });
const devices = Object.values(devicesObject);

const batteryLevels = [];

for (const device of devices) {
  const batteryCapability = device.capabilitiesObj.measure_battery;
  if (batteryCapability) {
    const batteryLevel = batteryCapability.value;
    const deviceName = device.name;
    batteryLevels.push({ name: deviceName, battery: batteryLevel });
  }
}

batteryLevels.sort((a, b) => a.battery - b.battery);

const batteryInfoText = batteryLevels.map((device) => `${device.name}: ${device.battery}%`).join('\n');

console.log('Niveaux de batterie:\n' + batteryInfoText);

return 'Niveaux de batterie:\n' + batteryInfoText;

```

---

<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 7, 2023, 6:34pm UTC](https://community.homey.app/t/script-get-all-battery-of-modules/93614/12 "2023-11-07T18:34:53Z")

</div>

Nice!  
It only doesn’t return any device @ my Pro 2019 🤗  
No errors.

---

<div class="post-metadata">

### Author: ![dudu\_95](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/dudu_95/32/93061_2.png) [@dudu\_95](https://community.homey.app/u/dudu_95)
#### Post date: [November 7, 2023, 7:25pm UTC](https://community.homey.app/t/script-get-all-battery-of-modules/93614/13 "2023-11-07T19:25:45Z")

</div>

That’s a real shame! 😔 It works great on the 2023 version. Well, we either need to migrate to the new box or modify the code to make it work on the 2019 version.

---

<div class="post-metadata">

### Author: ![dudu\_95](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/dudu_95/32/93061_2.png) [@dudu\_95](https://community.homey.app/u/dudu_95)
#### Post date: [November 7, 2023, 7:31pm UTC](https://community.homey.app/t/script-get-all-battery-of-modules/93614/14 "2023-11-07T19:31:23Z")

</div>

Try this version and let me know if it works on your 2019 box. Unfortunately, I can’t test it as I have the 2023 version.

```auto
const devicesObject = await Homey.devices.getDevices({ filter: { class: 'sensor' } });
const deviceKeys = Object.keys(devicesObject);
const batteryLevels = [];

for (const deviceKey of deviceKeys) {
  const device = devicesObject[deviceKey];
  const batteryCapability = device.capabilitiesObj.measure_battery;
  if (batteryCapability) {
    const batteryLevel = batteryCapability.value;
    const deviceName = device.name;
    batteryLevels.push({ name: deviceName, battery: batteryLevel });
  }
}

batteryLevels.sort((a, b) => a.battery - b.battery);

const batteryInfoText = batteryLevels.map((device) => `${device.name}: ${device.battery}%`).join('\n');

console.log('Niveaux de batterie:\n' + batteryInfoText);

return 'Niveaux de batterie:\n' + batteryInfoText;

```

---

<div class="post-metadata">

### Author: ![Sebby5](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/sebby5/32/122738_2.png) [@Sebby5](https://community.homey.app/u/Sebby5)
#### Post date: [November 8, 2023, 10:56am UTC](https://community.homey.app/t/script-get-all-battery-of-modules/93614/15 "2023-11-08T10:56:07Z")

</div>

I tested this on my Homey Early 2016 and can confirm it works.  
Thank you for the efforts! 😊

---

<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 9, 2023, 2:08am UTC](https://community.homey.app/t/script-get-all-battery-of-modules/93614/16 "2023-11-09T02:08:42Z")

</div>

Thanks for your efforts!  
It still returns zero:

```auto
Niveaux de batterie:

———————————————————
✅ Script Success
↩️ Returned: "Niveaux de batterie:\n"

```

.

Now when I add this after line 3:

```auto
log ("deviceKeys: ", deviceKeys);

```

it shows the ID’s of 70 devices, but i have around 30 battery powered sensors.

> [@Sebby5](#):
>
> I tested this on my Homey Early 2016 and can confirm it works.

For me it works as well, but it’s a bit odd it doesn’t return any battery value 🤔  
Can you elaborate on “it works”? Thanks in advance.

---

<div class="post-metadata">

### Author: ![Sebby5](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/sebby5/32/122738_2.png) [@Sebby5](https://community.homey.app/u/Sebby5)
#### Post date: [November 9, 2023, 7:38am UTC](https://community.homey.app/t/script-get-all-battery-of-modules/93614/17 "2023-11-09T07:38:40Z")

</div>

Hi Peter,

Here’s my output with a little script change for language purposes:

 ![image](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/b/9/b9389dc4b83436a3e8be9fd3f4d1bf70834f1524.png)

---

<div class="post-metadata">

### Author: ![JPe4619](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/jpe4619/32/9017_2.png) [@JPe4619](https://community.homey.app/u/JPe4619)
#### Post date: [November 9, 2023, 2:06pm UTC](https://community.homey.app/t/script-get-all-battery-of-modules/93614/18 "2023-11-09T14:06:14Z")

</div>

You need a HP2016 for this 🤣 , that works fine for me too, same result as @Sebby5

---

<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 9, 2023, 3:09pm UTC](https://community.homey.app/t/script-get-all-battery-of-modules/93614/19 "2023-11-09T15:09:27Z")

</div>

I don’t see how a Pro 2019 is different from a 2016 model, Jan! 😂🤣😂  
A script doesn’t care about more RAM and an additional CPU core, right?
