Developers:
I am a total newbie and have been trying to mash up bits and pieces from available apps to create what I’m trying to do. I haven’t been able to get it to work and wanted to see if some of the more experienced developers could give me some guidance.
Here’s want I’m trying to do:
- Select a motion sensor
- Select a phone number to text to
- Select a start time of day to monitor the “active” state of the motion sensor
- Select an end time of day to stop monitoring the “active” state of the motion sensor
- Select a time of day to send a text with the times during the window defined by 3 and 4 above when motion occurred.
I want to log the times of day within the start-end time window when motion is detected. I have teenagers in the house and it seems like I wake up to find then up and wandering around when they ought to be sleeping. I’d like to record those times and then get a text message at say 9:00AM which lists the times when the motion sensor was active. If it is a quiet night and the set the start time at midnight and the end time at 6:00AM, I would expect I should see not logs of in the text message of the motion sensor being active.
In the code I tried to write (shown below) I assumed there would be no more than 6 instances of the motion sensor going active. There’s probably a much more elegant way to make an array where I wouldn’t be limited by a number but that is way beyond my coding skills. Also, I tried to but a 300 second delay after the motion sensor goes active so I wouldn’t log essentially the same roving teenager event.
Again any points or portions or the entirety are very welcome.
Thanks,
GilS
/**
* TimesMovingAtNight
*
* Author: gil.schmitt.public@gmail.com
* Date: 2013-09-02
*/
preferences {
section("Motion Sensor") {
input "motion1", "capability.motionSensor", title: "Pick your Motion Sensor"
}
section("Text me at..."){
input "phone1", "phone", title: "Phone number?"
}
section("Time In Bed"){
input "startTime", "time", title: "Start Time"
input "endTime", "time", title: "End Time"
}
section("Time To Send Report Text"){
input "textTime", "time", title: "Text Time"
}
def installed() {
subscribe(motion1, "motion.active", motionActiveHandler)
}
def updated() {
unsubscribe()
subscribe(motion1, "motion.active", motionActiveHandler)
}
// inTimeWindow sets the period of time from startTime to stopTime where the app is to record the time that motion1 is active.
// If the current time is outside of this window, the app does nothing
def inTimeWindow()
{
def result
def now = now()
if(now > startTime && now < endTime){
return true
}
else{
return false
}
result
}
// It is expected that during the inTimeWindow period there will be no more that 6 times that motion1 goes active.
// This module defines each of these events as movement1Time, movement2Time, etc..
// The module check first if the current time is in the inTimeWindow and then progressively the movement#Time that are null value.
// This was successive movement#Time variables are written to and the ones already written are not written over
def motionActiveHandler(evt)
{
def movement1Time
def movement2Time
def movement3Time
def movement4Time
def movement5Time
def movement6Time
if(inTimeWindow() == true && movement1Time == null)
{
movement1Time = now()
runIn(300, motionActiveHandler())
}
if(inTimeWindow() == true && movement1Time != null && movement2Time == null)
{
movement2Time = now()
runIn(300, motionActiveHandler())
}
if(inTimeWindow() == true && movement1Time != null && movement2Time != null && movement3Time == null)
{
movement3Time = now()
runIn(300, motionActiveHandler())
}
if(inTimeWindow() == true && movement1Time != null && movement2Time != null && movement3Time != null && movement4Time == null)
{
movement4Time = now()
runIn(300, motionActiveHandler())
}
if(inTimeWindow() == true && movement1Time != null && movement2Time != null && movement3Time != null && movement4Time != null && movement5Time == null)
{
movement5Time = now()
runIn(300, motionActiveHandler())
}
if(inTimeWindow() == true && movement1Time != null && movement2Time != null && movement3Time != null && movement4Time != null && movement5Time != null && movement6Time == null)
{
movement6Time = now()
}
}
// When the current time is equivalent to the text time set in the preferences section, an SMS text message is sent with the times of movement1Time,
// movement2Time, ...
if(now() == textTime)
{
sendSms(phone1, "${movement1Time} ${movement2Time} ${movement3Time} ${movement4Time} ${movement5Time} ${movement6Time}")
}
}