Situation now:
I set my alarm clock each evening for the next morning via Alexa.
I have my heating based on fixed cron times in my rules. They will heat the rooms so they are warm at the time I normally wake-up and stop the heating I normally leave home.
Goal:
Set a wake-up time for the next day (preferable via Alexa).
Have the heaters turn on a fixed time before the set wakeup time.
Play music, switch on light, whatever at the set wakeup time.
At first, I ignore the alexa part and try to get the timer. So what is the rule fragment to have a timer at 6 o’clock in the morning executing?
import org.joda.time.*;
var Timer wakeupTimer = null
rule "wakeup"
when
Item wakeup_time changed
then
wakeupTimer ?.cancel
wakeupTimer = createTimer(???) [
// Play wakeup music
]
end
… but in the old rule engine, you need to cast the state first. In the new rule engine, this is not necessary. I’m getting a little rusty with the old stuff !
For example…
Rules DSL
logWarn("Old Rules", "{}", (Virtual_Number_1.state as Number).intValue)
Scripted Automation (Jython)
from core.actions import LogAction
LogAction.logWarn("New Rules", "{}", items["Virtual_Number_1"].intValue())
==> /var/log/openhab2/openhab.log <==
2019-11-25 20:00:59.566 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'wakeup': An error occurred during the script execution: Couldn't invoke 'assignValueTo' for feature JvmVoid: (eProxyURI: timemachine.rules#|::0.2.1.2.0.1.1::0::/1)
Full code of the rule:
var Timer wakeupTimer = null
rule "initWakeup"
when
System started
then
postUpdate(wakeup_Felix_hour, 0)
postUpdate(wakeup_Felix_minute, 0)
logInfo("wakeup", "System start")
end
rule "wakeup"
when
Item wakeup_Felix_hour changed or
Item wakeup_Felix_minute changed
then
wakeupTimer?.cancel
if(wakeup_Felix_hour.state instanceof Number) nHour = wakeup_Felix_hour.state as Number
if(wakeup_Felix_minute.state instanceof Number) nMinute = wakeup_Felix_minute.state as Number
logInfo("wakeup", "Starting timer " + nHour + ":" + nMinute )
wakeupTimer = createTimer(now.plusDays(1).withTime(nHour, nMinute , 0, 0))[|
logInfo("wakeup", "Time is up")
wakeupTimer = null
]
end
the Vars aren’t defined before setting the value.
nHour and nMinute have to be of Type Integer. Both values have to have a default value and you have to ensure that both values are valid. Please take a look at the code in my posting Variable Wakeup-Time Sequence
rule "wakeup"
when
Item wakeup_Felix_hour received update
or
Item wakeup_Felix_minute received update
then
wakeupTimer?.cancel
if (wakeup_Felix_hour.state instanceof Number && wakeup_Felix_minute.state instanceof Number) {
var nHour = (wakeup_Felix_hour.state as Number).intValue
var nMinute = (wakeup_Felix_hour.state as Number).intValue
logInfo("wakeup", "Starting timer {}:{}", nHour, nMinute)
wakeupTimer = createTimer(now.plusDays(1).withTime(nHour, nMinute , 0, 0))[|
logInfo("wakeup", "Time is up")
wakeupTimer = null
]
} else {
logWarn("wakeup", "Could not start timer due to missing numbers: wakeup_Felix_hour={}, wakeup_Felix_minute={}", wakeup_Felix_hour.state, wakeup_Felix_minute.state)
}
end