Simulated light sensor?


#1

I need a simulated light sensor that I can use to store an average lux value from several motion sensors…but I don’t see one.

I tried a simulated motion sensor, but it doesn’t seem to have an illuminance property, or at least I can’t read/write to it.

What am I missing?


#2

Here is how to save the average lux reading from 3 independent lux sensors into a variable.
First define a global variable (if you wish to use it in more than one piston) and then either copy and paste this in or write it yourself. This will update everytime a lux event (change) comes in from any of these sensors.


#3

Thanks, but saving it to a variable isn’t a problem. I need a simulated light sensor that I can display on tiles and use like other physical devices.

BTW - Why are you manually averaging the values instead of using the avg function?


#4

Ah. OK.
EDIT: This is from the early days of webCoRE when avg was not available.


#5

Gotcha.

Here is my function to save to a variable. It includes the event trigger as well as a timer in case something gets lost in the innerwebs. (It would be better if I could OR the event trigger with the timer, but I haven’t figured that one out yet :slight_smile: )

It works great… just need to figure out how to get that into a light sensor device.


#6

Curious why the need for the every minute?


#7

Mostly just for testing or constant monitoring. I had that set up as a fall back while I was trying different event scenarios.

In reality, I’d probably still have a timer for every hour or two, just in case events get lost in the mail…but it probably isn’t necessary assuming lux changes are getting reported properly.


#8

Ah, got it :slight_smile:

Edit: I’m not much of a coder but I wonder if you can finnagle your way around into modifying the Settable Temperature Sensor DTH to convert it into a Settable Lux Sensor :slight_smile:


#9

Here’s my crack at it, not pretty but I think it gets the job done :smiley:

image

/**
 *  Copyright 2015 SmartThings
 *  Modified by eibyer@webcore for Lux, original author below
 *
 */
metadata {
	definition (name: "Settable Lux Sensor *", namespace: "ericvitale", author: "[email protected]") {
		capability "Illuminance Measurement"
		capability "Sensor"
        
        command "setLux", ["string"]
	}
    
    preferences {
    	section("Setting") {
			input "logging", "enum", title: "Log Level", required: true, defaultValue: "DEBUG", options: ["TRACE", "DEBUG", "INFO", "WARN", "ERROR"]
        }
    }

	// simulator metadata
	simulator {
		for (int i = 0; i <= 4000; i += 10) {
			status "${i}lx": "illuminance: $i lx"
		}
	}

	// UI tile definitions
	tiles {
		valueTile("illuminance", "device.illuminance", width: 3, height:2) {
			state("illuminance", label:'${currentValue}',
				backgroundColors:[
                    // Lux Color Range
                    [value: 0, color: "#000000"],
                    [value: 10, color: "#333300"],
                    [value: 50, color: "#666600"],
                    [value: 90, color: "#999900"],
                    [value: 200, color: "#cccc00"],
                    [value: 400, color: "#ffff00"],
                    [value: 600, color: "#ffff33"],
                    [value: 1000, color: "#ffff66"],
                    [value: 2000, color: "#ffff99"],
                    [value: 3000, color: "#ffffcc"],
                    [value: 4000, color: "#ffffff"]					
				]
			)
		}
        
        standardTile("refresh", "device.refresh", inactiveLabel: false, decoration: "flat", width: 2, height: 2) {
			state "default", action:"refresh.refresh", icon:"st.secondary.refresh"
		}

		main(["illuminance"])
		details(["illuminance", "refresh"])
	}
}

def determineLogLevel(data) {
	if(data.toUpperCase() == "TRACE") {
    	return 0
    } else if(data.toUpperCase() == "DEBUG") {
    	return 1
    } else if(data.toUpperCase() == "INFO") {
    	return 2
    } else if(data.toUpperCase() == "WARN") {
    	return 3
    } else {
    	return 4
    }
}

def log(data, type) {
    
    data = "Settable Lux -- " + data
    
    try {
        if(determineLogLevel(type) >= determineLogLevel(logging)) {
            if(type.toUpperCase() == "TRACE") {
                log.trace "${data}"
            } else if(type.toUpperCase() == "DEBUG") {
                log.debug "${data}"
            } else if(type.toUpperCase() == "INFO") {
                log.info "${data}"
            } else if(type.toUpperCase() == "WARN") {
                log.warn "${data}"
            } else if(type.toUpperCase() == "ERROR") {
                log.error "${data}"
            } else {
                log.error "Settable Temp -- Invalid Log Setting"
            }
        }
    } catch(e) {
    	log.error ${e}
    }
}


// Parse incoming device messages to generate events
def parse(String description) {
	def name = parseName(description)
	def value = parseValue(description)
	def unit = name == "illuminance" ? lx : null
	def result = createEvent(name: name, value: value, unit: unit)
	log.debug "Parse returned ${result?.descriptionText}"
	return result
}

private String parseName(String description) {
	
    if (description?.startsWith("illuminance: ")) {
		return "illuminance"
	}
    
	null
}

private String parseValue(String description) {
	if (description?.startsWith("illuminance: ")) {
		return zigbee.parseHALuxValue(description, "illuminance: ", lx)
	} 
    
	null
}

def setLux(val) {
    log.debug "Setting illuminance for ${device.displayName} from external input, illuminance = ${val}."
	sendEvent(name: "illuminance", value: val, unit: lx)
}

def refresh() {
	log("Refresh", "DEBUG")
}

def updated() {
	log("Updated -- ${device.displayName} is ${device.currentState("illuminance")}.", "DEBUG")
   
}

Daily countdown of days to event that will display on ActionTiles
#10

cool. I’ll have to play around with that. Thanks!


#11

Nice one. I like it.

Hmmm. I can see more uses for this type of application springing into my tiny little mind.
Even though I don’t need this at the moment I going to create the DH and the piston so I have a copy to play with/amend in the future
The platform that just keeps giving…
Thanks again.