- Platform information:
- Hardware: Raspi 4
- OS: Openhabian
- openHAB version: newest stable
I have a rule running that is turning the light on and a speaker based on the time of the android alarm.
Now i wanted to enhance it by adding some features. I want it to first turn on the heating, wait, turn on the light, wait, turn on the speaker and turn off the heating.
Here is the code.
import org.joda.time.*
var Timer timerAlarm = null
rule "Alarm Clock"
when
Item TimeToAlarm changed
then
if (TimeToAlarm.state as Number == 0) {
if (timerAlarm !== null) {
timerAlarm.cancel
timerAlarm = null
SonosPlayer.sendCommand(PAUSE)
}
logInfo("alarm", "All alarms are cancelled")
AlarmTime.sendCommand(0)
} else {
// Für das UI
var epoch = new DateTime((((TimeToAlarm.state as Number))-4260000).longValue) //1 h 11 vor Android//alt 600000
var guitime = new DateTime((((TimeToAlarm.state as Number))-600000).longValue)
AlarmTime.sendCommand(guitime)
logInfo("alarm", "Scheduling alarm for " + epoch.toString)
//
if (timerAlarm !== null) {
logInfo("alarm", "Reschedule alarm")
timerAlarm.reschedule(epoch)
} else {
logInfo("alarm", "New Alarm")
timerAlarm = createTimer(epoch, // epoch 4680000
[ k |
// Turn on stuff, e.g. radio or light
Temperatur.sendCommand(21)
Thread::sleep(3600000) //<-1 Stunde //60 Sekunden =60000
Licht.sendCommand(ON)
Thread::sleep(60000) //<-1 Min
SonosLautstarke.sendCommand(12)
SonosPlayer.sendCommand(PLAY)
Temperatur.sendCommand(17)
//10 Min später sollte der Android Wecker tun.
logInfo("alarm", "alarm is expired")
]
)
}
}
end
I think using timers AND Thread::sleep causes problems…
Maybe someone could give me a hint how can I create a rule that does what I want and is reliable and not overcomplicated.