Calling device.refresh() once per hour?


(Linda Thomas-Fowler) #1

I’ve got code that calls the refresh command on a set of devices once per hour. I’m doing it because some of the devices only send events very infrequently so I’m checking to see if they are still alive and responding. The code works fine but I was wondering if anyone had a sense for how I might be impacting battery life or zigbee/zwave traffic. I’m thinking the impact would be minimal but wanted to get some other insights.


(Bruce) #2

Battery impact would vary by device, obviously. There is another way to do this without touching the battery life at all. You can look at the log info for the device. I wrote a little app that does that, and sends a notice if a device hasn’t reported for some period of time. In the case of a device whose battery has died, and hence does not report, this will catch that.

Edited to add: This is written for contact sensors, but could easily be changed for any type of device…

/**
 *  Notify when no events
 *
 *  Copyright 2015 Bruce Ravenel
 *
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 *  in compliance with the License. You may obtain a copy of the License at:
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
 *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
 *  for the specific language governing permissions and limitations under the License.
 *
 */
definition(
    name: "Notify when no events",
    namespace: "bravenel",
    author: "Bruce Ravenel",
    description: "Notify when a device doesn't wake up",
    category: "My Apps",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
    iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png")


preferences {
	section("When one of these sensors fails to wake up...") {
    	input "devices", "capability.contactSensor", title:"Contact Sensors", multiple: true
	}
    section("For this many hours") {
    	input "hours", "number", title:"Maximum hours of no events"
    }
    section("Send sms (leave blank for push)") {
    	input "phoneNumber", "phone", title: "Phone number", required: false 
    }
}

def installed() {
	initialize()
}

def updated() {
	initialize()
}

def initialize() {
    runEvery1Hour(checkSensorStatus)
    runIn(61,updateSensorStatus)
}

def send(msg) {
	log.debug "$msg"
    if (phoneNumber) sendSms(phoneNumber, msg) 
    else sendPush(msg)
}

def updateSensorStatus() {
	checkSensorStatus()
}

def checkSensorStatus() {
    devices.each {
    	def lastTime = it.events(max: 1).date
        def rightNow = new Date()
        if(lastTime) {
        	def minutes = ((rightNow.time - lastTime.time) / 60000)
            if(minutes < 0) minutes = minutes + 1440
        	if(minutes > hours*60) send("No events for $it.displayName in over $hours hours")
        } else send("No recent events for $it.displayName")
    }
}

(Linda Thomas-Fowler) #3

You must be reading my mind (or me reading yours since you did it first!). I wrote something very similar but I have a flood sensor down by the sump pump and it seems to send events very infrequently. The temperature down there is very stable so it doesn’t change much and thankfully there’s no water getting it wet so it’s got no moisture to report. The last event in the log was three days old until I forced it to refresh. I thought the device might be reporting infrequently but in this case it seems be to quite a stretch without any events. So I threw in a refresh and then check for the events a couple of minutes later.


(Bruce) #4

That app was for a guy with embedded contact sensors, and it was driving him crazy because he couldn’t tell if the batteries had died or not. In his case, the frequency of reporting was settable, and set for several hours. That app doesn’t deal with days old events correctly.

If your device can go days without reporting anything at all, that’s a different problem. I’d say the frequency of throwing a refresh() at it should coincide with your own frequency of concern. Once an hour is probably excessive.


#5

I’m pretty sure polling is considered “bad practice” for mesh networking protocols like Zwave and Zigbee. This is because you can never anticipate how/where the signal will bounce before reaching its destination, thus it may have a negative impact on the whole system as well as negatively impacting battery life of the sensor at the final destination.

It looks like @bravenel 's script circumvents any of these problems, and is what you would want to use. Good job amigo.


(Linda Thomas-Fowler) #6

I may play with the interval and also only do a refresh on sensors that don’t have an existing event in the time window to minimize the polling. Trying to balance knowing a battery has died vs.putting extra drain on the battery and traffic on the network. Thanks for your insights!