Low Battery Alert Smart App


(Aaron) #1

Hey guys. I’ve lurked here a lot, but I finally registered to share my latest Smart App. This is the third or forth app I’ve written for SmartThings. Normally I don’t share because they’re unique to my own setup, but I’m seeing a lot of posts for low battery alert. Well, I whipped this up and so far it seems to be working.

This Smart App will poll all devices that use a battery and then send you an alert. The check happens on 1st and 15th each month at 10 AM. It requires nothing special. Install, configure, go. If you use my app, I ask that you please to not modify the code except for the parameters (battery level and frequency of the check) and to not remove my name.

Let me know if you have any questions. Thanks and enjoy!

/**
 *  Battery Level Alert
 *
 *  Author: Aaron Crocco
 *  Date: 2014-01-29
 *	This app will poll chosen devices that use a battery and send an alert when it's low.
 *	As written, this will:
 *		Poll on the 1st & 15th every month at 10AM
 *		Alert when the batteries are lower than 13%.
 */
preferences {
		section("Moniter battery level on...") {
		input "batteryDevices", "capability.battery", title: "Which?", multiple: true
	}
}

def installed() {
	log.debug "Installed with settings: ${settings}"
 	subscribe (batteryDevices, "battDevs", batteryThings) 
	initialize()
}

def updated() {
	log.debug "Updated with settings: ${settings}"
	unsubscribe()
    subscribe (batteryDevices, "battDevs", batteryThings) 
	initialize()
}

def initialize() {

	//Schedule a check of the battery at 10am on the 1st and 15th days of the month
    schedule("0 0 10am 1,15 * ?", checkBattery)
}

def checkBattery() {

	//Gets the current battry level

	log.debug "checking batteries"
    def battery = batteryDevices.currentValue("battery")      
    def whichDevice
	def x=0
    
    while (x < 50) {
		if (battery[x] < 13) {
        	whichDevice = batteryDevices[x]
            if (whichDevice != null) {
            	log.debug "The $whichDevice battery is low"
                sendPush("The $whichDevice battery is low.")
			}
        }   
        x=x+1
    } 
}

def batteryDevices (evt) { }

Android 2.0.7 - Release Notes
(Ardenite) #2

Great app, Aaron! When I run it, however, it doesn’t pick up my smartsense multis. Am I missing something? The only thing i can think of is that they are set up as smartsense (configurable).


(Aaron) #3

Thanks for trying it out. I’m not sure why it’s not picking up your multis. When I install it, I get an option for every ‘thing’ in my house that uses a battery. As far as how you’re configuring them (as smartsense) I don’t know what that means. That may be it though.


(Boris M) #4

It only seems to work with “smartthings” devices and not with all devices that use battery.


(Cory S) #5

the (configurable) is an alternate device type SmartThings has for it. It allows you to turn on or off the different sensors from within the app, or IDE.

Great app though btw, I feel like this should had been built in from the start.


(Aaron) #6

Thanks Cory. I actually thought this WAS already built into SmartThings. It’s only because the battery is dead on one of my presence tags did I realize that’s not the case.

As for non-SmartThings devices, if anyone knows how to poll them for the battery level then adding it in should work. I simply poll the devices for their battery status and get an integer result in an array.


(Todd Wackford) #7

I think I have something. I’ll dig through my notes/code and get back to you.

Twack


(Todd Wackford) #8

Something that will list out like this too?

battery list pic


(Todd Wackford) #9

I’ll have some time this weekend to get something out.


New "Home on Code Unlock Too"
(Todd Wackford) #10

@macstainless,

I thought I had something the grabbed all the devices so you don’t have to pick, but I didn’t. Here is battery checker that I hacked just to see if it would grab non-ST devices and it does. I then looked again at your code and it’s essentially the same. So why your not getting the non-ST device battery level while I am is a mystery to me.

Anyway, here is the code that I did to test. Run it and see what the diff’s are between it and your stuff. Let me know if there are diff’s and I’ll dig deeper.

Twack

/**
 *  Battery Tester
 *
 */
preferences {
	section("Battery Alarm Level") {
		input "alarmAt", "number", title: "Alert when below...", required: true
        input "batteryDevices", "capability.battery", title: "Which devices?", multiple: true
	}
}

def installed() {
	log.debug "Installed with settings: ${settings}"

	initialize()
}

def updated() {
	log.debug "Updated with settings: ${settings}"
    
	unschedule()

	initialize()
}

def initialize() {
	
    //schedule the job
    schedule("0 0 10am 1,15 * ?", doBatteryCheck)
    
    //run at install too
    doBatteryCheck()
    
}

def doBatteryCheck() {

	def belowLevelCntr = 0
    
    def pushMsg = ""
    
	for (batteryDevice in batteryDevices) {
    
    	def batteryLevel = batteryDevice.currentValue("battery")

        if ( batteryLevel <= settings.alarmAt.toInteger() ) {
			
            pushMsg += "${batteryDevice.name} named ${batteryDevice.label} is at: ${batteryLevel}% \n"
            
            belowLevelCntr++
        }
    }
    
    if ( belowLevelCntr ){
    
    	pushMsg = "You have ${belowLevelCntr} devices below the set alarm level. \n" + pushMsg
    
    } else {
    	
        pushMsg = "Battery Check App executed with no devices below alarm level"
    }
    
    log.debug(pushMsg)
    
    sendPush(pushMsg)
}

Device Type preferences: input validation
(John Magrane) #11

Hi guys,
Thanks for posting this code. I just tried it out in my system and it works great except it doesn’t seem to find all my non-ST devices. It discovers my Aeon Multisensor, but not my Aeon Recessed Door/Window Sensors. When I check my list of devices, battery status is not listed as one of the states for those. The Aeon documentation says that battery level is reported through the ZWave Battery Command Class. I’m new to this platform, so would appreciate any help on implementing that.

John


(Jeff DeWolfe) #12

Detects all of my devices, although I only have 2 that are not SmartThings branded. Thanks for the code!


(Todd Wackford) #13

The door-window sensor device file does not appear to have the battery status reporting coded into it. In looking at the Aeon docs, the device does have the capability to do it. You should submit a support ticket to add the battery reporting status for the door/window contact sensor. Once ST implements that, the app will report it.

HTH,
Twack


(Richard) #14

@macstainless, I’m trying to use your Dark Weather app but don’t have a moisture sensor. Anyway you could make this work with SmartWeather tile as well? Thanks


(Aaron) #15

HI @RichardH. Sorry I never saw this reply to me until just now! Were you able to get Dark Weather working? I haven’t updated the code at all but one simple workaround is to make the weather sensor input optional. The WU call will easily do the rest. I happen to have a virtual weather center tile (prior build to the SmartWeather tile) that I called things from. Let me know.


(Andrea Kealy) #16

Hi I know it’s a while since you published this but what does def batteryDevices (evt) { } do?


(Aaron) #17

@dreais Honestly, I probably put that there for my own reference in case I wanted to add to the app later on. Sloppy coding on my part. My guess is you can take it out and the app should run fine. Sorry!


(Andrea Kealy) #18

Thanks a million @macstainless. No problem, I’m new to all this so just wanted to double-check!


(Edgar Santana) #19

Tried installing today. it recognized all of my devices which are mix of schlage, ecolink, aeon, smartthings, linear and others. I get the app up on my iphone but when I hit save, it says failed to save: default page. Any thoughts?


(Andrea Johnson) #20

Same thing here…when trying to set it up I get ‘Failed to Save: Default Page’ in the red banner at the top. Any suggestions?


Battery Minder App