Wakeup routine rule

  • 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.

I get this error atm

2020-03-08 17:20:12.185 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Alarm Clock': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.BusEvent.sendCommand(org.eclipse.smarthome.core.items.Item,java.lang.Number) on instance: null

There are some elementary issues in your rule.

  1. imports with placeholder are deprecated.
  2. the import for joda.time is not necessary at all
  3. NEVER ever use Thread::sleep() with values over, lets say 500, this is a very bad idea. Thread::sleep is: freeze the thread for an amount of time, so the thread is blocked for other rules! Even worse, there is no timer cancelling, so the rule can create more and more timers.

I have no idea what’s the purpose of guitime vs. epoch but I suspect you have an issue with gui time vs. system time.

The error message is about a sendCommand() with a number value, which is null.
As the only sendCommand with a var as parameter is AlarmTime.sendCommand(guitime), this must be the line causing the error.

What’s the Item type of AlarmTime?

That sounds a bit like a simple state machine sequence. (another of Udo’s top tips)

1 Like