I was tired of coming home or waking up to find all my smart bulbs turned on due to a power outage.
This app was inspired by the concept of a “Canary” bulb from @JDRoberts ( see Power Outage and Zigbee Bulbs)
Basically you set up a smart bulb that you never use. If this bulb is on, then the app turns it off and any bulbs that were off before the power outage occurred.
There is a lot of discussion on other posts about whether you should be changing the states of bulbs because you don’t know if anyone intended to change the states of the light while the power was out or after the power came back on and before the ST hub reconnects to the internet. This option works for my usage, hopefully it works for yours.
The code:
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}"
}
}
}