Battery checker

Here is a homey script that automatically goes in and finds the devices that are using battery and will give you a push notification on your mobile, with the name(s) of the device and how many % are left on the battery.

You can choose in the script how many % the battery must be below before you get a notification. Line 9 - (battery <= 20; // Here you can change %) It is set to 20%.

Remember to run the script before you create a flow, as then ”tags” will be created

Remember to install ”HOMEY SCRIPT”

Flow: My check once a day: AT 6:00 PM

If I don't answer any questions, it's because I'm sick.
But I hope you can figure it out.

It was me and AI who made this script. 😁

Here is the script:



// Get all devices and filter for those with a battery
let batteryDevices = Object.values(await Homey.devices.getDevices()).filter(device =>
  device.capabilities.includes('measure_battery')
);

// Find all devices with a battery level of 10% or less
let lowBatteryDevices = batteryDevices.filter(device => {
  let battery = device.capabilitiesObj.measure_battery.value;
  return battery !== null && battery <= 20; // Here you can change %
});

// Prepare the notification message
let message;
if (lowBatteryDevices.length === 0) {
  // If all devices are OK, the message is an empty string
  message = '';
} else {
  // Otherwise, create a clear list of devices with low battery
  message = 'Low Battery Alert:';
  lowBatteryDevices.forEach(device => {
    let battery = device.capabilitiesObj.measure_battery.value;
    message += `\n${device.name}: ${battery}%`;
  });
}

// Create a flow tag for the message
await tag('low_battery', message);

// This return value is for confirmation and is not used in the flow
return 'Script executed successfully and created the "low_battery" tag.';
1 Like

Besides using Hscript there’s an onboard solution as well:

@Peter_Kawa Very interesting! I currently have separate flows per battery device to monitor and inform me of a (custom) threshold. This flowcard would allow me to replace them with one flow. Do you know since when this flowcard has been made available? I haven’t seen it before.

1 Like

Owww, for me it’s been there since I bought my 1st Pro 2019 back in 2020.

BTW (maybe you knew alr) Homey automatically notifies you when a battery is below 20%, but it’s sent to the timeline only (or with push notifications as well if you have it enabled).

However, there’s this trigger card “when notification was sent”;
with a logic AND card “contains battery” you can send the notification to “anywhere”.

1 Like

Thanks. I must have completely missed that card then :blush: .

1 Like

Hey,

is it possible to exclude certain devices for this flow? I have a Battery for my solar power use and now i get every day a notification for that

Try this :slight_smile:

// Get all devices and filter for those with a battery
let batteryDevices = Object.values(await Homey.devices.getDevices()).filter(device =>
  device.capabilities.includes('measure_battery')
);

// Filter out specific devices by name
let filteredDevices = batteryDevices.filter(device => {
  // Replace 'Name of your device' with the exact name you want to exclude
  return device.name !== 'Name on your device';
});

// Find all devices with a battery level of 10% or less from the filtered list
let lowBatteryDevices = filteredDevices.filter(device => {
  let battery = device.capabilitiesObj.measure_battery.value;
  return battery !== null && battery <= 10;
});

// Prepare the notification message
let message;
if (lowBatteryDevices.length === 0) {
  // If all devices are OK, the message is an empty string
  message = '';
} else {
  // Otherwise, create a clear list of devices with low battery
  message = 'Low Battery Alert:';
  lowBatteryDevices.forEach(device => {
    let battery = device.capabilitiesObj.measure_battery.value;
    message += `\n${device.name}: ${battery}%`;
  });
}

// Create a flow tag for the message
await tag('low_battery', message);

// This return value is for confirmation and is not used in the flow
return 'Script executed successfully and created the "low_battery" tag.';



And if you need more devices to eksklude, then use the &&

return device.name !== 'Enhed 1' && device.name !== 'Enhed 2';

Meggi


Hey thanks for your idea but honestly spoken i do not have any clue how it works with home script. Is there also any solution with flow cards? Maybe to compare result tag “#Device” Name with Name of solar power battery.

Will it work like this?

Kan you translate your flow into english, just write whats in each “box”.

I check later, i’m going to the hospital today, so please be patient, as it can take a couple of days.

Meggi

Battery of any device changed → @Device consists not of: solar_p1uze → #Value is lower than 15 → Push Notification

It worked fine. So for everybody, who wants to exclude certain devices, this is a solution

Hi

I’m back.

I see you got it working, so that’s great. :grinning_face:

Meggi