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
TargetLevel=25
EmailAddress="example@example.com"
LowBatteryCount=0

# 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"
 LowBatteryCount+=1
emailBody=emailBody+"\r\n-End-"
if LowBatteryCount >0:
 indigo.server.sendEmailTo(EmailAddress, subject="Indigo battery alert", body=emailBody)
log ("Finished check")