Hi all
Just after a code review if possible.
I wrote an app when I got my smart things device and its job was simple…
- On door open, brighten up the dim value a light
- On door close, lower the dim value of the light
- When somebody returns, brighten up the dim value of a light
The dim values were hard set and has worked reliably for a long time. It would only change the values if the light was on.
I decided to expand upon the original code because I had a set of lights which only 1 would be on (out of 6) but in the event of a door opening, it would brighten the lot up and then return to the original settings, assuming one of the lights in the group was already on, and then off/dim to the original status on close, I wanted to be as flexible as possible so I could rotate which bulk head light was on to even the ‘wear’ on the bulbs. Also, I can use the below to group bulbs together that are at different low dim values in 1 smartapp rather than multiple ones.
So the below update is the outcome.
It seems to work, and I think I have captured most of the bugs over time, however when I swap out the old app for this one, it seems to increase the likelihood of my wemo bulbs to crash. I am not quite sure why, only thing I can think of is I am requesting more refreshes of the bulbs to update the app, and to ensure that the data I am storing between door opening and closing is up to date.
I am also not sure if the problem could be down to a firmware issue of the wemo bulbs. I have recently spent some time resetting, updating via a wemo hub and re-adding back to smart things, but have not re-tried the below script yet.
Any pointers?
Thanks
/**
* App Name: [Scene Machine] Brighten Outside Lights 2
*
* Use License: Non-Profit Open Software License version 3.0 (NPOSL-3.0)
* http://opensource.org/licenses/NPOSL-3.0
*/
// Automatically generated. Make future change here.
definition(
name: "Brighten When Open 2",
namespace: "",
author: "Stephen Harding",
description: "Controls multiple dim value of multiple bulbs when doors open/close/presence.",
category: "My Apps",
iconUrl: "http://cdn.device-icons.smartthings.com/Lighting/light11-icn.png",
iconX2Url: "http://cdn.device-icons.smartthings.com/Lighting/light11-icn@2x.png"
)
preferences {
section("Sensors"){
input(name: "contact1", type: "capability.contactSensor", multiple: true, title: "Which Door?")
input(name: "switches", type: "capability.switch", multiple: true, title: "Which Dimmers/Lights?")
input(name: "people", type: "capability.presenceSensor", multiple: true, title: "Which people?", required: false)
}
section("Brightness Settings"){
input(name: "numOpenDimVal", type: "number", title: "Open Dim Value%", required: true)
input(name: "numClosedDimVal", type: "number", title: "Closed dim value", required: true)
}
section("Others") {
input(name: "numMintoDim", type: "number", title: "Minutes after door closed to dim", required: true)
input(name: "boolReturnDimVal", type: "enum", title: "Return to previous dim value?", options: ["Yes","No"], required: true)
input(name: "boolSwitchOnOffLights", type: "enum", title: "Switch on lights which are off on open?", options: ["Yes","No"], required: true)
}
}
def installed() {
// log.debug "Installed with settings: ${settings}"
subscribe(contact1, "contact.open", contactOpenHandler)
subscribe(contact1, "contact.closed", contactClosedHandler)
subscribe(people, "presence", presenceHandler)
//subscribe(knockSensor, "acceleration.active", doorknockHandler)
}
def updated() {
// log.debug "Updated with settings: ${settings}"
unsubscribe()
subscribe(contact1, "contact.open", contactOpenHandler)
subscribe(contact1, "contact.closed", contactClosedHandler)
subscribe(people, "presence", presenceHandler)
//subscribe(knockSensor, "acceleration.active", doorknockHandler)
}
def presenceHandler(evt) {
if("present" == evt.value) {
//When somebody arrives, welcome them with brighter lights!
//I will assume they will go through a door so the closed routines should run later.
state.PresenceDetected = "True"
OpenRoutine()
}
}
def contactOpenHandler(evt) {
log.info("Open Door detected....")
OpenRoutine()
}
def contactClosedHandler(evt) {
log.info("Closed Door detected....")
def AddMinutes = numMintoDim * 60 //Convert to seconds
def nowdate = new Date()
log.info("Delay close routine for: $AddMinutes seconds")
//Because of the delay, there could be another open event within the time frame. To prevent
//sucking out the previous dim values before they have had had a chance to be re-set, set a flag
//and use it to prevent the old settings being wiped out with a new open event!
state.ClosedRunning = "True"
runIn(AddMinutes, ClosedRoutine)
//runIn(15, ClosedRoutine)
}
def ClosedRoutine(){
def switchState = ""
def dimmerValue = ""
def ClosedDimValue = numClosedDimVal
def ReturntoPrevDim = boolReturnDimVal
def cnt = 0
def DeviceName
def NewDimLevel
state.ClosedRunning = "True"
state.PresenceDetected = "False" //Reset Presence
//log.info("BWO: Closed dim Value (before if...) $ClosedDimValue")
if(ClosedDimValue > 100){
ClosedDimValue = 100
}
else if(ClosedDimValue < 0){
ClosedDimValue = 0
}
else{
//ClosedDimValue = 11
}
//log.info("Closed dim Value (after if) $ClosedDimValue")
log.info("Return to previous device dim value? $RerturntoPrevDim")
cnt = 0
for(mySwitch in switches) {
switchState = mySwitch.latestValue("switch")
dimmerValue = mySwitch.latestValue("level")
DeviceName = mySwitch.displayName
log.info "Bulb $cnt: $DeviceName: Current Switch State: $switchState / Dim Value: $dimmerValue"
log.info "Bulb $cnt: $DeviceName: Previous Switch State: ${state."BulbState_$cnt"} / Dim Value: ${state."BulbLevel_$cnt"}"
if(switchState == "off"){
log.info "Bulb $cnt: Switch is already off!"
}
else{
if("${state."BulbState_$cnt"}" == "off"){
log.info "Bulb $cnt: Previous State was off, so switch it off!"
mySwitch.off()
}
else{
if(ReturntoPrevDim == "Yes"){
log.info "Bulb $cnt: Previous State was on and app is set to go back to devices previous dim value!"
NewDimLevel = "${state."BulbLevel_$cnt"}".toInteger()
mySwitch.setLevel(NewDimLevel)
}
else{
log.info "Bulb $cnt: Previous State was on and app is set to go back to set dim value!"
mySwitch.setLevel(ClosedDimValue)
}
}
}
log.info ""
cnt++ //Next!
}
runIn(10,RefreshStatus) //Refresh bulb status after 5 seconds to allow the light to dim fully.
state.ClosedRunning = "False"
}
def OpenRoutine(){
def OpenDimValue = numOpenDimVal
def SwitchOnOffLights = boolSwitchOnOffLights
def switchState = ""
def dimmerValue = ""
def cnt = 0
def DeviceName
//Set default global on value....
state.GlobalOnStatus = "off"
//Fix new dim value if its outside sensible values
if(OpenDimValue > 100){
OpenDimValue = 100
}
else if(OpenDimValue < 0){
OpenDimValue = 0
}
else{
//OpenDimValue = 100
}
log.info("Open Dim Value: $OpenDimValue")
for(mySwitch in switches) {
mySwitch.refresh()
DeviceName = mySwitch.displayName
// Get the original light status....
switchState = mySwitch.latestValue("switch")
dimmerValue = mySwitch.latestValue("level")
//Store some data!
if("${state.ClosedRunning}" == "True"){
log.info "Bulb $cnt: $DeviceName: Closed Routine apppears to be running, do not store new values to preserve correct dim values"
}
else if("${state.PresenceDetected}" == "True"){
log.info "Bulb $cnt: $DeviceName: Presence routine recently ran, do not store new values to preserve correct dim values"
}
else{
state."BulbState_$cnt" = switchState
state."BulbLevel_$cnt" = dimmerValue.toInteger()
if(switchState == "on"){
state.GlobalOnStatus = "on"
}
}
cnt++ //Next!
}
cnt = 0
for(mySwitch in switches) {
DeviceName = mySwitch.displayName
log.info "Bulb $cnt: $DeviceName: Bulb Status: ${state."BulbState_$cnt"} / Dim Value: ${state."BulbLevel_$cnt"} / Global On: ${state.GlobalOnStatus} / Closed Routine Status: ${state.ClosedRunning} / Presense routine Status: ${state.PresenceDetected} / Switch on off: $SwitchOnOffLights"
// Done collecting data, lets move forward with changing the brightness....
if(switchState == "off") {
if("${state.GlobalOnStatus}" == "on" && SwitchOnOffLights == "Yes") {
log.info "Bulb $cnt: $DeviceName: Switch is off, but another in the group is on... so turn it on!"
mySwitch.on()
mySwitch.refresh()
mySwitch.setLevel(OpenDimValue)
}
else{
log.info "Bulb $cnt: $DeviceName: Switch is off, and leave off as no other lights are on within the group"
}
}
else{
log.info "Bulb $cnt: $DeviceName: Switch is on, set new dim value"
mySwitch.setLevel(OpenDimValue)
}
log.info ""
cnt++ //Next!
}
runIn(10,RefreshStatus) //Refresh bulb status to allow the light to dim fully.
}
def RefreshStatus() {
//Routine to refresh light status.
//log.info "BWO: Refreshing light status"
for(myCounter in switches) {
myCounter.refresh()
}
log.info "Done Refreshing light status"
}