Help with colors and lighting - advanced states?

Hey all,

I just joined the ST club with the starter pack and some hue lights. Not being scared to code, I read through the documentation and started playing around with things. I’m not overly familiar with the language, and the last time I did any serious coding was with Java ~10years ago.

I’ve hit a brick wall with these lights, and I was hoping someone could help me out with the syntax or point me in the right direction. I don’t understand where to go to learn the exact abilities of each device and what I can do with each one. For example, light.on() is fairly obvious. Setting color was an hour long struggle, but I solved it. But, how can I poll the device to get the current state of color, hue, level, etc?

My first app is to monitor the garage and front door sensors (both samsung multipurpose sensors). If either of them open, then two lights are supposed to turn on for 5 minutes. One light is a white hue, and the other is a colored hue. The last step is that I want the app to revert the lights to whatever they were set prior to the action being triggered at the end of the 5 minutes(on/off/color/level/etc).

Any suggestions or advice?

Thanks in advance.

Current code below:

preferences {
	section("Sensor to be used for the Front Door:") {
		input "door", "capability.contactSensor", multiple: true, required: true
	}
	section("Sensor to be used for Garage Door:") {
		input "garage", "capability.contactSensor", multiple: true, required: true
	}
	section("White lights to be triggered on:") {
		input "whitelight", "capability.light", multiple: true, required: true
	}
	section("Green lights to be triggered on:") {
		input "colorlight", "capability.light", multiple: true, required: true
	}
	section("Return to previous state after:") {
                input "minutes", "number", required: true, title: "Minutes?"
    }
	    
}
def installed() {
    log.debug "Installed with settings: ${settings}"
    initialize()
}

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

def initialize() {
    subscribe(garage, "contact.open", contactOpenHandler)
    subscribe(door, "contact.open", contactOpenHandler)
}

def contactOpenHandler(evt) {
	whitelight.on()
    colorlight.on()
    
    colorlight.setColor(hue: 36, saturation: 87, level: 100, color: "#21FF44") 
    
    def del = minutes * 60
	runIn(del, turnitoff)
}
def turnitoff(){
	whitelight.off()
	colorlight.off()
}

You can use a function like runevery5minutes to implement a poll. In the poll function get/set the color attributes of the devices.

Thanks dudz40! I’ve been working on that. I figured out how to get the attributes from the light, as well as sending them back. Sadly, I am having an issue with data types I think? I get this error with the following code:

Cannot cast object ‘[100]’ with class ‘java.util.ArrayList’ to class ‘java.lang.Integer’ @ line 72

So I think the data type is stored in a list, which keeps getting updated. I tried to call the first arraylist index, and that works for the first time the app runs. But not sure how to proceed here.

def contactOpenHandler(evt) {

state.befs = colorlight.currentValue("switch")
state.befl = colorlight.currentValue("level")
	state.befh = colorlight.currentValue("hue")
state.befsat = colorlight.currentValue("saturation")

whitelight.on()
colorlight.on()
colorlight.setColor(hue:37, saturation: 87, level: 100) 
def del = minutes * 5
	runIn(del, turnitoff)
}
def turnitoff(){
state.nowh = colorlight.currentValue("switch")

                
                 
if (state.befs == state.nowh)  {
		log.debug "reverting to before status"
        colorlight.setColor(hue:state.befh, saturation: state.befsat, level:state.befl)
		            
}
else {
colorlight.off()
whitelight.off()
  

}
}

Which one is line 72?

Hey dudz! So I made some progress. But it still doesn’t work quite right. The setColor command wants integer values, but states are saved as Strings. I made the following modifications, and it kinda works, but not reliably. Usually works the first time, and then sets the wrong colors in subsequent tests. There has got to be a better way, this is just ugly code! Thanks again for all your help!

def contactOpenHandler(evt) {

	state.befs.clear()
    state.befl.clear()
	state.befh.clear()
    state.befsat.clear()
    state.befws.clear()
    state.befwl.clear()
    
    state.befs = colorlight.currentValue("switch")
    state.befl = colorlight.currentValue("level")
	state.befh = colorlight.currentValue("hue")
    state.befsat = colorlight.currentValue("saturation")
    state.befws = whitelight.currentValue("switch")
    state.befwl = whitelight.currentValue("level")
	log.debug "before settings CSwit ${state.befs} Clevel ${state.befl} Chue ${state.befh} Csat${state.befsat} Wswit ${state.befws} Wlevel ${state.befwl}"

    whitelight.on()
    colorlight.setColor(hue:37, saturation: 87, level: 100) 
    colorlight.on()
    def del = minutes * 5
	runIn(del, turnitoff)
}
def turnitoff(){
    state.noww = whitelight.currentValue("switch")
    state.nowc = colorlight.currentValue("switch")
    if (state.befs == state.nowc)  {
    		log.debug "reverting to before color status"
            colorlight.setColor(hue:state.befh[0], saturation: state.befsat[0], level:state.befl[0])
    		            
    }
    if (state.befws == state.noww) {
    		log.debug "reverting to before white status"
            whitlight.setLevel(state.befwsl[0])
            whitelight.on()
            }
    else {
    colorlight.off()
    whitelight.off()
  
    
}
}

you might want to take a look the build in states history of an attribute, you can ask for a number of the previous states, i think it is something like statesSince??