Need help coding


(Cass) #1

I want to use this code but need to add a timeframe for it to be active. for example between 5:00AM and 6:00AM Monday-Friday. Thanks

/**
 *  Light Follows Me If There Isn't Enought Light
 *
 *  Author: SmartThings
 */

preferences {
	section("Turn on when there's movement..."){
		input "motion1", "capability.motionSensor", title: "Where?", multiple: true
	}
	section("And off when there's been no movement for..."){
		input "minutes1", "number", title: "Minutes?"
	}
	section("If the light intensity on..."){
		input "lightSensor1", "capability.illuminanceMeasurement"
	}
	section("is lest than..."){
		input "lux1", "number", title: "Lux?"
	}
	section("Turn on/off light(s)..."){
		input "switches", "capability.switch", multiple: true
	}
}

def installed() {
	subscribe(motion1, "motion", motionHandler)
	subscribe(lightSensor1, "illuminance", illuminanceHandler)
}

def updated() {
	unsubscribe()
	subscribe(motion1, "motion", motionHandler)
	subscribe(lightSensor1, "illuminance", illuminanceHandler)
}

def illuminanceHandler(evt) {
	log.debug "$evt.name: $evt.value and lux: $lux1"
    int illuminanceState = Integer.parseInt(evt.value);
	if (illuminanceState >= lux1) {
        def switchesState = "off";
		def switchesValue = switches.currentState("switch")
		for (String item : switchesValue.value) {
    	  if (item == "on") {
      		switchesState = "on"
    	  }
		}	
		if (switchesState == "on"){
            log.debug "there is enought light: turning lights off"
        	switches.off()
        }    
    } else {
        def motionState = "inactive";
        def motionValue = motion1.currentState("motion")
        for (String item : motionValue.value) {
          if (item == "active") {
            motionState = "active"
          }
        }
        log.debug "motionState: $motionState"
        if (motionState == "active") {
            log.debug "Motion is active and the there isn't enought light: turning lights on"
            switches.on()
        }
    }
}

def motionHandler(evt) {
	log.debug "$evt.name: $evt.value"
	if (evt.value == "active") {
        def illuminanceState = lightSensor1.latestValue("illuminance")
        log.debug "$illuminanceState < $lux1"
		if (illuminanceState < lux1) {
            log.debug "turning on lights"
            switches.on()
        }
	} else if (evt.value == "inactive") {
		runIn(minutes1 * 60, scheduleCheck)
	}
}

def scheduleCheck() {
    def motionState = "inactive";
    def motionValue = motion1.currentState("motion")
	log.debug "Schedule check... Sensor $motionValue.value"
	for (String item : motionValue.value) {
      if (item == "active") {
      	motionState = "active"
      }
	}
    log.debug "motionState: $motionState"
	if (motionState == "inactive") {
        log.debug "Motion has stayed inactive long enough since last check: turning lights off"
        switches.off()
    } else {
    	log.debug "Motion is active, do nothing and wait for inactive"
    }
}

(Boris M) #2

You could create a mode “between 5:00AM and 6:00AM Monday-Friday” and have it only execute in that specific mode…


(Morgan) #3

Take a look at this, I have since gone to using a mode, but this is the same thing. It is alerting me during certain times of the day. I believe the time frames aren’t perfect yet, and it could be conversion between UTC, but I haven’t looked at it since I’ve started using modes.

Hopefully, that helps.

/**
 *  Unexpected Motion
 *
 *  Author: Morgan
 *
 *  Designed to Notify me of when a motion sensor goes off, but during certain time
 *   periods when I'm not expecting activity in the location.
 *
 */
preferences {
	section("Notify when there is motion with which motion sensor..."){
		input "motionSensor", "capability.motionSensor", title: "Where?"
	}
	section ("Start of Quiet Time") {
		input "begTime", "time", title: "When do you expect no activity?", description: "Time of day"
	}
	section ("End of Quiet Time") {
		input "endTime", "time", title: "When do you want to end notifications?", description: "Time of day"
	}
    section("Via a push notification and/or an SMS message"){
		input "phone", "phone", title: "Phone Number (for SMS, optional)", required: false
		input "pushAndPhone", "enum", title: "Both Push and SMS?", required: false, metadata: [values: ["Yes","No"]]
	}
	section("Minimum time between messages") {
		input "frequency", "decimal", title: "Minutes"
	}
}

def installed() {
	
	subscribe(motionSensor, "motion", motionHandler)
}

def updated() {
	unsubscribe()
	subscribe(motionSensor, "motion", motionHandler)
}

def motionHandler(evt) {
  	log.debug "$evt.name: $evt.value" 
    /* This means the Motion is Active - Check Schedule */
	if (evt.value == "active") {
    	if (checktimes(evt)) {
			log.debug "sending notification due to motion"
			sendMessage(evt)
		}
		state.motionStopTime = null
	}
	else {
		state.motionStopTime = now()
	}
}

/*
 * Send a Message, check if it is push or to SMS 
 *
 */
private sendMessage(evt) {
	log.debug "sendMessage"
	def msg = "${motionSensor.label ?: motionSensor.name} detected motion"
	log.debug "$evt.name:$evt.value, pushAndPhone:$pushAndPhone, '$msg'"

	if (!phone || pushAndPhone != "No") {
		log.debug "sending push"
		sendPush(msg)
	}
	if (phone) {
		log.debug "sending SMS"
		sendSms(phone, msg)
	}
	if (frequency) {
		state[evt.deviceId] = now()
	}
}

private checktimes(evt) {
	log.debug "checktime $evt.name"
	def result
      
    if (frequency) {
		def lastTime = state[evt.deviceId]
		if (lastTime == null || now() - lastTime >= frequency * 60000) {
            result = true
		}
        else {
        	log.debug "no message because too often."
        }
	}
	else {
		result = true
	}
    
	if (result == true) {
        def t = now()
        def startTime = timeToday(begTime, location.timeZone)
        def doneTime = timeToday(endTime, location.timeZone)
        result = t < startTime.getTime() || t >  doneTime.getTime()
	}
    if(result == false)
    {
    	log.debug "motion detected, but outside time frame."
	}
    result
}