“org.openhab” is a terrible name for the logger from this Rule. The name of the logger (first parameter) gets put at the end of the classname portion of the logging statement. For example logInfo("entry", "foo bar")
will generate a log statement that looks like:
2016-10-13 13:16:22.549 [INFO ] [eclipse.smarthome.model.script.entry] - foo bar
Furthermore, logInfo only takes two arguments, the name and the message. You are calling logInfo with three arguments.
Finally, that last argument is randomTimer_DateTime which doesn’t exist in this rule yet. You don’t declare and define it until another four lines of code further down in the rule. You cannot reference a variable before it has been at least declared.
This is the cause of your current error.
This sentence doesn’t make sense. You don’t set a Timer to an Item. You set a Timer to execute some code in the future and assign it to a variable (if desired).
As I look a the code more closely I see more odd things. What is the parse
for? now
is a DateTime object and plusSeconds returns a DateTime so what is the parse even doing? It certainly isn’t necessary.
Is tRandomLight_GF_Libing_Room_ON a global variable or an Item? It makes no sense if it is an Item. Timers are variables. You cannot set an Item to a Timer. Furthermore, you cannot assign anything to an Item and have it work. You must postUpdate or sendCommand.
I keep seeing lots of weird things in this code that I don’t understand and it makes me wonder if you understand how Rules work.
Here is how I would implement a rule that turns on the Lights at a random time after sunset and turns them off again at Sunrise
Items:
Group Living_Room_Lights ...
Switch Light1 (Living_Room_Lights) ...
...
Switch Sunset_Event { astro="planet=sun, type=set, property=start" }
DateTime dLivingrookLightsTimerON ... // presumably so you can see when the lights will turn on on your sitemap, it isn't needed for any other reason
Rule:
var Timer tRandomLight_GF_Living_Room_ON = null // global variable, not an Item
rule "Turn on Living Room lights at random time after sunset"
when
Item Sunset_Event received update ON
then
logInfo("Sunset Lights", "Sunsetting. Generating random time on for lights in living room") // only two arguments, reasonable logger name
// Calculate a random time in the next 30 minutes to turn on the lights
var int randomTime = (new java.util.Random).nextInt(1800)
var DateTime randomTimer_DateTime = now.plusSeconds(randomTime) // no need for parse
logInfo("Sunset Lights", "Living room lights will turn on at " + randomTimer_DateTime.toString)
dLivingroomLightsTimerON.postUpdate(randomTimer_DateTime) // if this generates an error use postUpdate(new DateTimeType(randomTimer_DateTime.millis))
// Create the timer
tRandomLight_GF_Living_Room_ON = createTimer(randomTimer_DateTime, [|
Living_Room_Lights.members.forEach[i | // I don't recommend using ? because typically you want an error if Living_Room_Lights has no members, avoid using reserved words like "item"
logInfo("Sunset Lights". "Switching light state to ON for " + i.name) // Unless this is a pretty new addition, I don't think Java and by extension the Rules DSL supports formatting of Strings using {} notation, logInfo only allows two arguments
i.sendCommand(ON) // I always recommend using i.sendCommand over the sendCommand action as it is better able to handle data type conversions
try{ Thread::sleep(5000) } catch(InterruptedException e) { }
]
])
end