[OBSOLETE] SmartBulb Power Outage handler

I’m glad I could help!

1 Like

So I have something weird going on. I have a z-wave siren that, when turned on, is triggering the app as if the siren is a canary device. Debug logging shows “canary is on” when the siren is activated. I don’t have the siren device selected in the Smarter Bulbs app, so I’m not sure how it’s changing the state of the app’s canary device…

Here’s the debug entry when saving the app settings. The siren device isn’t here:

Updated with settings: [canary:Canary Bulb, slaves:[Bar Lamp, Desk Lamp, Left Couch Light, MBR Left Bed Light, MBR Left Closet Light, MBR Right Bed Light, MBR Right Closet Light, Right Couch Light, Side Yard Floodlight, Trash Area Floodlight]]

Nevermind all that. Apparently when I was creating the SmartApp, I installed an instance of it through the web IDE and set the siren device as the canary. I didn’t even know it was possible to activate a SmartApp thru the IDE and have it invisible from the mobile app…

Back to your usual scheduled programming.

1 Like

So, the power went off for a few minutes the other day. The power came back on, but smartthings didn’t pick up the canary bulb as on, so all the lights stayed on.
I had a prior power outage and things worked as expected, so I guess this is hit or miss.

At one point I tried using poll() and refresh() to try to get the status of CREE bulbs without any success. Anyone know if either works with the GE bulbs?

Any other suggestions?

Nevermind. Not sure what I was doing the last time I tested polling. I added polling to the canary bulb and it should now handle a missed “canary on” event. (also only turn the canary off, if I think it is on).
As an added bonus, you can use a Cree bulb as the canary and it should work without any issues.

This has been committed to GitHub.

1 Like

Updated the code to also poll the monitored light bulbs. This should now pick up bulbs that were turned on by the physical switch and smartthings didn’t pick up the change (mainly for CREE bulbs). This should avoid turning those lights off after a power outage.

/**
 *  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"
        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}"
    }
}
    
}

1 Like

Power went out during our little mini heat wave today. Smart bulbs back to their intended states as soon as generator kicked in! App worked perfectly. There were a few utility recloser activations right as my evening light routines started turning some bulbs on. Even with the power going on and off, all the bulbs ended up in the correct state (minus a few GE Links, but that’s another story). Great app.

2 Likes

Great to hear! I haven’t had a power outage since I made the last updates so I really appreciate the feedback.
Thanks!

1 Like

My power went out last night (2nd time since I set up your app) worked great both times. Thank you!!!

1 Like

Thanks for the feedback and I’m glad to hear it’s working as expected!
I have pulled plenty of apps and code from the community. I’m glad I can contribute!

I finally had a short power outage today and the app worked turning off the bulbs that were off before the outage.
Next step is put the bulbs that were on back to their proper dimming level…

@nsweet
I am trying to get this to work with my OSRAM Lightify bulbs. I have 4 of these that are dimmable and can change white color. These bulbs do the same thing in that upon power restoration they go full brightness and on. I went and bought another that is not dimmable in any way basic on off.That one is my canary.

I installed new bulb into smart things fine but it came up as a dimmable zigbee switch. I changed it to a regular Zigbee switch. I can turn it on and off fine with either type selected.

I installed the smart app, then tried to configure it. I get this error in the live logging.

27eee8e7-beab-4d65-9fc0-7fb7b94cb4c0 ‎6‎:‎18‎:‎37‎ ‎PM: error java.lang.IllegalArgumentException: Command ‘poll’ is not supported by device 4233d37d-8286-451e-99b7-da255a9f46df of type ‘ZigBee Switch’. Supported commands: [on, off, configure, refresh]

Way I see it the bulb (canary) says it cannot support the poll command. So will this not work with these bulbs? I hope I am just missing something simple.

Any breadcrumb or pointers in the right direction would be much appreciated.

You could try to change the device to a cree or ge link bulb and see if that works.
Otherwise you could change the SmartApp and replace .poll() with .refresh().
(i.e. canary.poll() would become canary.refresh()).
As long as your other bulbs support refresh you should be okay. Otherwise I’ve seen another app (?pollster?) where it had two selections when setting up the app. One of things to poll and another of things to refresh.

Hope this helps. Let me know how you make out.

1 Like

actually, since only the canary needs the refresh, you could just change the canary lines and you should be good to go.

@nsweet

I got the App to go without errors by changing everything to .refresh on both the canary and slaves.
Now when I turn the canary on, (in live logging) it looks like it runs and restores one of the bulbs. The others don’t change.
I just realized another possible hiccup. 3 of my bulbs are controlled by a grouping smart app. Also all 4 of the other bulbs I’m trying to turn off are dimmable. I see in the live log a off followed by level 0 on the next line.

I’ll have to wait until daytime to play any more as the lights are kinda needed right now and the family won’t like me turning them off and on!

Thanks for your help so far

I don’t think the dimming will make a difference, most bulbs will come on at 100% when the power is restored. The grouping app could be affecting things.
Also, I’m not sure of the differences (if any) between polling and refresh. That may affect things, but as far as I know, I think they should both work. (any experts out there, feel free to chime in!)
Basically the app is checking all monitored bulbs every time a bulb is turned off or on via smartthings and saving a list of the bulbs that are off (you should see this in the live logging).
The polling (refresh) is done every five minutes to pick up any state changes that may have been missing. This was mainly done for the cree bulbs that don’t report a change if they are turned on by a physical switch.
Additionally, if the canary bulb is on, it doesn’t check the state of the other bulbs, as the assumption is they are all on due to the power being restored and we don’t want to capture this state.

When the app sees the canary bulb is on, it simply turns off the canary and then turns off any lights in the list of lights that were off.
Testing is a little difficult unless you are willing to flip the main breaker of your house or have all the lights you are testing on the same circuit. I tested with a subset of lights that I was able to turn off and on together.

Hopefully this will help with your troubleshooting. Keep me posted

When you turn off the light switch and turn it back on a few minutes later, does the SmartApp still show that it’s off?

Some bulbs can detect that they’ve been turned on, like GE Link Bulbs, but others like Cree Bulbs don’t.

Polling the device is the workaround for the bulbs that don’t, but if you’ve removed the polling feature and the light doesn’t report that it’s been turned on, I doubt you’ll be able to use it as the canary.

All bulbs I’ve seen turn on at 100% when the power is restored so using a dimming bulb as the canary shouldn’t cause any problems.

From looking at the device types for the Cree bulb and GE Link, poll() does a ZigBee on/off refresh and level refresh and refresh() does the same plus a ZigBee on/off config and level config. I’m not sure what the ZigBee config does but it would appear that refresh should still take care of the issues with Cree and the like.
However the checking of the bulbs happens every five minutes, so if you turn the bulb off and on with the physical switch, it may take up to 5 minutes for the app to be in sync with the bulb(s).

If the power goes out before the poll/refresh cycle then the those bulbs may still be considered “off” by the app.
I’m not sure what state is reported by a poll/refresh if the physical switch is off during the poll.

I’ve updated the code (both in GitHub and in the posts above) to handle devices that don’t do polling, but do refresh.
It polls if poll is available, otherwise it does a refresh.
To test, I changed a Cree bulb to a ZigBee Switch device type, which refreshed and picked up the correct state.
If your device doesn’t poll or refresh, then throw it away :smile:

2 Likes

After updating to the latest code you posted above everything seems to be working. I took my canary, one lamp not in the dimmer group, 1 lamp in the dimmer group put them all on 1 extension cord so I could control power application.

At first for whatever reason I couldn’t get it to work. So my usual first troubleshooting step is to reinstall everything. So redid the smart app code. Opened it and selected only the canary and bulb not in group. Worked like a champ turns them off before they can even get full brightness.

Next I added the lamp from the group, it didn’t work?? So I removed it from the group app. Then tested and it worked. So you would say to yourself it’s the group app.

So I put the lamp back into the group control app. Low and behold it works?? I can still control the lamp using the group app, or individually using the smartthing android app.

So I have no idea why the change. Gremlins??? Doesn’t really matter it appears to be working.

Thanks again for everyone’s help.