I'm working on troubleshooting some code. I would really like to learn a bit more about Groovy so I'm diving in to solve a problem with an app. Two questions.
-
Is there a way to log to an external file? I have some code that needs to run at least 24 hours to capture and midnight event. Specifically I am trying to see what is causing a light switch that is told to only be on daily between sunset and 10pm PST. For some reason the routine trips at midnight and I can't figure out from the logs why because they roll over by the next morning.
-
I found the following piece of code on a Smartthings forum.
def contactHandler(evt) {
// Door is opened. Now check if today is one of the preset days-of-week
def df = new java.text.SimpleDateFormat("EEEE")
// Ensure the new date object is set to local time zone
df.setTimeZone(location.timeZone)
def day = df.format(new Date())
//Does the preference input Days, i.e., days-of-week, contain today?
def dayCheck = days.contains(day)
if (dayCheck) {
def between = timeOfDayIsBetween(fromTime, toTime, new Date(), location.timeZone)
if (between) {
roomLight.on()
} else {
roomLight.off()
}
}
}
Can someone tell me if this code would execute properly if executed at midnight? I think it would but I'm not sure how to test it in a test environment.
Any help or guidance is appreciated. Thank you.
Try this... sorry didn't have time to test but give it a try. You x-out notification if you don't have one setup.
/*
* Copyright 2017, 2018 Hubitat, Inc. All Rights Reserved.
*
* This software if free for Private Use. You may use and modify the software without distributing it.
* You may not grant a sublicense to modify and distribute this software to third parties.
* Software is provided without warranty and your use of it is at your own risk.
*
*/
definition(
name: "Lights_On_Off",
namespace: "you...",
author: "you...",
description: "what ever....",
category: "My Apps",
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"
)
preferences {
page(name: "main")
}
def main(){
dynamicPage(name: "main", title: "Send Hub Events", uninstall: true, install: true){
section("Select Switch") {
input "roomLight", "capability.switch", title: "Select Light", required: true, multiple: false
}
section("Input Notification Device") {
input "notificationdevice", "capability.notification", title: "Select Notification Device", required: true, multiple: false
}
}
}
def installed() {
initialize()
}
def updated() {
unsubscribe()
initialize()
}
def initialize() {
scheduleTurnOnOffTimes()
}
def scheduleTurnOnOffTimes() {
def sunsetTime = location.sunset
def timeforSunset = new Date(sunsetTime.time)
def time_off = "22:00" //10pm
time_off = new Date(timeToday(time_off).time)
//schedule ON and OFF events to run daily
schedule(timeforSunset, turnOn) //on time at sunset
schedule(time_off, turnOff) //off time
}
def turnOn(evt) {
roomLight.on() //turn on
mypause()
def curr_time = get_currenttime() //get current time
def roomLightcurrentValue = roomLight.currentValue("switch") //get switch current state
if (roomLightcurrentValue == "on") {
notificationdevice.deviceNotification("lights now on at $curr_time") //send notification to my phone
log.debug "light state: $roomLightcurrentValue"
}
}
def turnOff(evt) {
roomLight.off() //turn off
mypause()
def curr_time = get_currenttime() //get current time
def roomLightcurrentValue = roomLight.currentValue("switch") //get switch current state
if (roomLightcurrentValue == "off") {
notificationdevice.deviceNotification("lights now off at $curr_time") //send notification to my phone
log.debug "light state: $roomLightcurrentValue"
}
}
def mypause(){
def executetime = now() + 4000
while (now() < executetime){}
}
def get_currenttime(){
def dateTime = new Date() //get current time
def mycurr_time = dateTime.format("h:mma E", location.timeZone) //get current time
return mycurr_time
}
1 Like
Thank you! THAT is quite helpful, @wjohnson54. Hopefully I will get a chance to try this and experiment over the next few evenings. I am looking forward to getting more involved with Groovy programming but it seems rather overwhelming.
Thank you again.
I think you can also leave the log page open overnight on an always on computer as well.
1 Like
Please, please, please do not use this! This is a terrible waste of processing power to "pause" execution, you are making the hub do a ridiculous amount of work to achieve a pause.
1 Like
Thanks Chuck
That's a carryover from ST where they warned not to use "Pause" since they said it was being removed. Not sure if that ever occurred though.
fischer1049: see Chuck's direction on pause and pls make substitution.
1 Like