Code help luminance and motion


(Joe) #1

Sorry about this guys. I am sure this is very basic. I took the “Light up the night” code and I wanted to change the value of luminance so I set the level. The code I have doesn’t do anything, but I am pretty sure it’s because I am newb.

My ultimate goal is to have motion only turn on a light when luminance is below a certain level and then turn off the light once motion stops.

/**
 *  Motion in the dark
 *
 *  Copyright 2014 Joe Rosiak
 *
 *  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.
 *
 */
definition(
    name: "Motion in the dark",
    namespace: "Keo",
    author: "Joe Rosiak",
    description: "Turns the light on when luminance is low.  This code should allow you to set the level of light and if it's too low motion will turn on the lamp.",
    category: "Convenience",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
    iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
    oauth: true)


preferences {
	section("Monitor the luminosity...") {
		input "lightSensor", "capability.illuminanceMeasurement"
        input("lightLevel", "number", title: "Set Your Lunination percentage:")
	}
	section("Turn on a light...") {
		input "lights", "capability.switch", multiple: true
	}
    
}

def installed() {
	log.debug "Installed with settings: ${settings}"
    subscribe(lightSensor, "illuminance", illuminanceHandler)

	initialize()
}

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

	unsubscribe()
    subscribe(lightSensor, "illuminance", illuminanceHandler)
	initialize()
}

// New aeon implementation
def illuminanceHandler(evt) {
	def lastStatus = state.lastStatus
	//if (lastStatus != "on" && evt.integerValue < lightLevel) {
      if (evt.integerValue < lightLevel) {
		lights.on()
		state.lastStatus = "on"
	}
	//else if (lastStatus != "off" && evt.integerValue > lightLevel) {
   	else if (evt.integerValue > lightLevel) {
		lights.off()
		state.lastStatus = "off"
	}
}

def initialize() {
	// TODO: subscribe to attributes, devices, locations, etc.
}

I turn off all of the lights and expected the light module to turn on, but nothing. I don’t even see anything in logging.

Any advice?


(Brian) #2

to begin with you shouldn’t have parenthesis around your lightLevel input.

You should also maintain the last status part of your if statement

def illuminanceHandler(evt) {
	def lastStatus = state.lastStatus
	if (lastStatus != "on" && evt.integerValue < lightLevel) {
		lights.on()
		state.lastStatus = "on"
	}
	else if (lastStatus != "off" && evt.integerValue > lightLevel) {
		lights.off()
		state.lastStatus = "off"
	}
}

bear in mind that asking for one light level will cause your switch to blink on & off if the luminance in the room hovers around the lightLevel set point. You should set a swing

else if (lastStatus != "off" && evt.integerValue > lightLevel + 20) {

Also you have to handle the condition that when this code runs and the light switch is turned on, is your code going to immediately see the illumance level go up above the off threshold and turn it right back off? again a blinking loop. I would set a variable for run once per day and look for it in your handler.

PS. I would recommend changing your headline to something more descriptive to receive better feedback


(Joe) #3

Thanks for pointing these out. This is really my first attempt at writing my own app, so I am taking baby steps. Ultimately I am just going to use the luminance level to determine whether motion will turn on a light. For instance, if it’s a gloomy cloudy day, I want the light to turn on when I go into a room, but not if the sun is out. I figured I’d just post as I needed help.

I’ll change the subject to directly reflect what my app is going to do.


(Brian) #4

Looks like you’re on the right track. I’ve only been developing apps for a few months. You’ll catch on quick


(Joe) #5

So I found an app that SmartThings had that already did what I wanted except it had the Lux Values hard coded in the app. I just had to add an input for the lux min and max values. Thanks for your help. At least I am starting to understand how this works. It should would be nice if ST released some official documentation that listed the different events, attributes, and such for each device they support.


(Brian) #6

Word is that someone has just been hired specifically for documentation. Fingers crossed.

The docs site is also a git repo so you can fork it, make changes and do pull requests to have your documentation included in the official.


(Joel) #7

i would really love to access an app like this that let’s me turn on lights based on motion and only when certain lux levels are set using weather tile (whilst only having it run in certain modes).

Does this exist or do I need to modify the app that Keo mentions above?


(Joe) #8

Look for an app called “Brighten When Dark And/Or Bright After Sunset” It should do what you need. I am currently using that.


(Joel) #9

Thanks I’ll check it out