Low Battery Notification Script

Posted on
Mon Oct 12, 2015 5:29 am
DaveL17 offline
User avatar
Posts: 6742
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Low Battery Notification Script

This very simple script sends an email message when any battery operated device drops below a defined threshold. I run this script once each day at midnight.

Requirements:
- Indigo email settings must be properly configured.
- Must have two Indigo Variables established (battery threshold and target email address.)

Code: Select all
target_level = int(indigo.variables[VARIABLE ID NUMBER].value)  # lowBatteryLevel
email_address = indigo.variables[VARIABLE ID NUMBER].value  # "notificationEmailAddress"
email_body = ""

for dev in indigo.devices.itervalues():
    if dev.batteryLevel:
        if dev.batteryLevel <= target_level:
            email_body += f"{dev.name} battery level: {dev.batteryLevel}\n"

if email_body != "":
    email_body = "The following Indigo devices have low battery levels:\n" + email_body
    indigo.server.sendEmailTo(email_address, subject="Indigo Low Battery Alert", body=email_body)
 

(Variations of this script are located elsewhere in the forum, just adding it here for reference and future enhancements.)

[EDIT 1] moved to the Dave's Scripts forum.
[EDIT 2] fixes bug that results in all devices being reported as low.
[EDIT 3] refinements and updates for Python 3.
[EDIT 4] fixes bug that results in report being sent even if there's no low batteries.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sun Sep 05, 2021 12:01 pm
pxzel offline
Posts: 39
Joined: Jan 19, 2019
Location: Lombard, IL.

Re: Low Battery Notification Script

Hi, having trouble with this script notifying me only when batteryLevel is below or equal variable I set up. I have Indigo 2021.1. I'm also using your charting script and it works great. Would like to run that monthly and run this particular script daily.

I've setup a schedule to execute this as an embedded python script in the action section. It runs, but it always sends me the battery levels for each battery powered device. I've setup my variable to 4 so any battery powered device below or equal to 4 will trigger an email and sent. None of my devices are below 4. So it seems that the part that evaluates battery levels is not working... it just looks at all battery powered devices and send me a listing of all battery level percentages.

The embedded python code I'm using:
Code: Select all
# Send an email containing a list of devices that have a low battery.

target_level = indigo.variables[1420350235].value  # lowBatteryLevel
email_address = indigo.variables[970406852].value  # "notificationicationEmailAddress"
email_body = ""

for dev in indigo.devices.itervalues():
    if dev.batteryLevel:
        if dev.batteryLevel <= target_level:
            email_body = email_body + (u'%s battery level: %s\n' % (dev.name, dev.batteryLevel))

if email_body != "":
    indigo.server.sendEmailTo(email_address, subject="Indigo Low Battery Alert", body=email_body)

Posted on
Sun Sep 05, 2021 1:31 pm
DaveL17 offline
User avatar
Posts: 6742
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Low Battery Notification Script

Indigo variables are strings, so you need to change this line:
Code: Select all
target_level = indigo.variables[1420350235].value  # lowBatteryLevel
To this:
Code: Select all
target_level = int(indigo.variables[1420350235].value)  # lowBatteryLevel
Otherwise what you're doing is comparing an int to a string (which is logically allowed in Python):
Code: Select all
>>> print(10 <= "4")
True
>>>

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sun Sep 05, 2021 1:55 pm
pxzel offline
Posts: 39
Joined: Jan 19, 2019
Location: Lombard, IL.

Re: Low Battery Notification Script

Thanks for the quick response. I think I understand....trying to comprehend why you didn't have that originally in the script. I know enough to be dangerous... beginner Python skills.

I was trying other ways to achieve what your script does so elegantly... within the Indigo interface and conditionals area... creating a schedule/action for each battery powered device.

Thanks for all your help, this stuff is addictive when you imagine all the possibilities.

Posted on
Sun Sep 05, 2021 2:10 pm
pxzel offline
Posts: 39
Joined: Jan 19, 2019
Location: Lombard, IL.

Re: Low Battery Notification Script

Oh, int means integer?

Posted on
Sun Sep 05, 2021 5:42 pm
DaveL17 offline
User avatar
Posts: 6742
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Low Battery Notification Script

Yes, sorry -- was on my way out the door when I responded. int() converts values into integers; float() would also work, but AFAIK battery values are always reported as integers.

The original script above didn't have that in it because of what we in the business call "a mistake". :) In all seriousness, though--that one's been sitting out there for 6 years and you're the first one to report the problem (which I have fixed). Thanks for that!

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest