KB102: Indigo. Check for low power on battery devices

KB102: Indigo. Check for low power on battery devices

This code sample will loop round all active battery powered devices and send an email if any of┬áthem has dropped below a pre-defined threshold. To stop a device being included in the check, un-check ‘Comm Enabled’ in the Device view.

## Check devices battery levels

# Set the target battery level and the email address to notify

# Shortcut the log function
def log(msg):
 indigo.server.log(msg, type="Low Battery Notification Script")

log ("Starting check")
emailBody="The following devices have low batteries: \r\n\r\n"

# Loop round all the devices
for dev in indigo.devices.itervalues():
 if (dev.batteryLevel != None) and (dev.enabled):
 log (emailBody)
 if (dev.batteryLevel < TargetLevel):
 log ("Device below threshold, sending email")
 emailBody=emailBody+"\t"+(u'%s (battery level: %s' % (dev.name, dev.batteryLevel)) + "%)\r\n"
if LowBatteryCount >0:
 indigo.server.sendEmailTo(EmailAddress, subject="Indigo battery alert", body=emailBody)
log ("Finished check")