[OBSOLETE] SmartBulb Power Outage handler

When I first wrote this, I was trying to find a better way to know that the power went out and/or came back on. There is flag on the hub is “on battery power”, but besides not finding any way to get that property in directly in the Groovy IDE, I realized it won’t help my situation, as the internet is out when the power is out. By the time the internet is connected and the script runs, the flag is back to false and the app doesn’t know it changed.

I think this app performs more consistently when internet goes down with the power. By the time the internet connects and app runs, the bulbs are connected. It the internet is up during a power failure, it’s a crap shoot.

You can probably add a delay in the checkRestore routine before it does the restoreState and that may solve the issue.

If I get a chance to play with this I will see what I can do, but I haven’t had a chance to mess with any of this in a few years… :grin:

I took a quick look and changing the checkRestore to this below should do the trick. Add the the runIn to the restoreState. I’m guessing 15 seconds would be more than enough delay, but you might have to mess with that. Change the number on the runIn call from 15 to the number of seconds you want. Hope this helps.

def checkRestore(evt) {
    log.debug "Checking Restore"  
    pollCanary() 
    log.debug "Canary is ${canary.currentSwitch}"
    if ("on" == canary.currentSwitch) { 
    	log.debug "Turning stuff off"
        runIn (15,restoreState)
        canary.off()
        }
    pollSlaves()
    saveStates(evt)
}
1 Like

I’ll give that a try. But my guess is that if bulbs other than the canary bulb re-connects to the hub then the saved state ends up being not what it was when the power goes off. And thus when the restore happens it comes out wrong.

For example:
Bulb A is off.
Power goes off.
Power comes on.
Bulb A reconnects saying it is on.
Canary connects.
Restore runs, but Bulb A is believed to have been on before power off.

I have tried some various timing or delays, but so far no luck.

I’m wondering if maybe not subscribing to the bulbs but rather just do a poll every minute or so. One could still have a situation where the timing was just right (or wrong) and get a false restore. But that would probably be a rare occurence. I’m not sure on the bulbs if all of them support polling or not. I read where this may not be the case.

In any event, the best scenario is to somehow detect the power off when it happens rather than when it comes on. I have a couple ideas on hardware to do this, just haven’t worked it all out yet. And whether its worth all that much trouble. I have a trigger in Echo to turn off all the lights, so when it happens I can always use Echo to do it. Just not as wife friendly.

Thanks for the help.

Another thought. I wonder if doing a refresh on the bulbs every so often to get the current state would work rather than doing a subscribe? Then hopefully the canary bulb status would update before a refresh was done again.

I just checked and I see where you do a refresh. I might try disabling the subscribe and see what happens.

I hadn’t thought about the subscribe tripping things up. Not sure what a refresh gets as a status if there is no response from the bulb. I didn’t test that.

The app was written with the expectation that internet is down when the power is down. Having the internet up kinda shoots itself in the foot!

A concept as simple as “set my lights the way they were” shouldn’t be this complex. Keep us posted if skipping the subscribe helps.

1 Like

I tried to follow the directions for this, but it seems the site has changed since they were made. Does anyone have updated instructions? I have no problems sleeping through the lights coming back on after an outage, my wife on the other hand. . .

@nsweet Update. Just using the refresh seems to work ok. We have had a couple power outages here lately and everything has reset just fine.

Hello,

So from what I read, you installed a lamp with a smart bulb in it, that is plug in an outlet, but always off. Then when a power outage arrive, that bulb will turn on when the power is back, so that will trigger an automation to turn off all the light in the house, I understand correctly?

But I suppose that this process could take some time ? Since the light turn on before the ST hub is back on, what is the possible delay ?

Thanks!

I will test this solution of yours, hopefully everything will work out here at home too. Here I have Gledopto led strips and Gledopto GU10 lamps.
I was wondering if there is a possibility to make a SmartApp for some ZigBee sockets that I have. Every time I have power outages when the power comes back on, the sockets are turned off, so I have to manually turn them on one by one. I was wondering if you could make a solution based on that same app, where when the light turned on, you also turned on the sockets.

Has anyone got this working in 2020?

It still works for me, but it’s been installed for over 2 years.

Still working here too, though it has taken a backup role to the native Hue restore functionality.

Can you post the current working version of the Canary? Tnx

How can Hue restore function be achieved in SmartThings hub v2? I have some Hue bulbs.

It’s only available when the Hue bulbs are connected via a Hue bridge. Then you set it up through the Hue app and it works fine. :sunglasses:

But it’s not available if the bulbs are connected directly to an ST hub without their own bridge.

1 Like

Can’t find if @YankeeJohnboy has a Hue bridge connected to SmartThings hub.

That user has not posted for 8 months and hasn’t been in the forum for 5 months, so I’m not sure if they’re active any longer.

Do you know where the handler to download?

I haven’t changed it since 2018, but here’s the latest version:


/**
 *  Smarter Bulbs
 *
 *  Copyright 2016 Nick Sweet.
 *
 *  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.
 *
 */

/************
 * Metadata *
 ************/
definition(
	name: "Smarter Bulbs",
	namespace: "nsweet68",
	author: "nick@sweet-stuff.cc",
	description: "Save the state of a bunch of bulbs and reset when 'Canary' bulb turns on",
	category: "My Apps",
	iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/App-LightUpMyWorld.png",
	iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/App-LightUpMyWorld@2x.png"
)
preferences {
    section("Canary Bulb") {
        input "canary", "capability.switch", title: "Who sings?"
    }
    section("Zigbee bulbs to monitor") {    
        input "slaves","capability.switch", multiple: true
    }
}

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

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

def initialize() {
    subscribe(slaves, "switch", saveStates)
    subscribe(canary,"switch.on", checkRestore)
    pollCanary()
    saveStates() 
	runEvery5Minutes(checkRestore)
    
}

def saveStates(evt) {
	log.debug "Checking States"
    if ("off" == canary.currentSwitch ) {
    	def lightsOff = [:]
    	slaves?.each {
			if (it.currentSwitch == "off"){
        	log.debug "${it.displayName} value ${it.currentSwitch}" 
        	lightsOff[it.id]="off"
        	}
		}
   	state.lOff = lightsOff
	}
}

def checkRestore(evt) {
    log.debug "Checking Restore"  
    pollCanary() 
    log.debug "Canary is ${canary.currentSwitch}"
    if ("on" == canary.currentSwitch) { 
    	log.debug "Turning stuff off"
        runIn (15,restoreState)
        canary.off()
        }
    pollSlaves()
    saveStates(evt)
}

private restoreState() {
  slaves?.each {
				if (state.lOff[it.id] == "off") {
                log.debug "turning $it.label off"
				it.off()
                }
			}
		}

private pollCanary() {
    def hasPoll = canary.hasCommand("poll")
    if (hasPoll) {
    	canary.poll()
        log.debug "Poll canary"
    }
    else
    {
    	canary.refresh()
        log.debug "Refresh canary"
    }
}

private pollSlaves() {

slaves.each {slave ->
def hasPoll = slave.hasCommand("poll")
    if (hasPoll) {
    	slave.poll()
        log.debug "Poll ${slave.displayName}"
    }
    else
    {
    	slave.refresh()
        log.debug "Refresh ${slave.displayName}"
    }
}
    
}
3 Likes

Tnx, I will give it a try to learn how it works.