Timer Value

update with your hints:

What I’ve learned:

  • difference between sleep and createtimer
  • difference between sendcommand and postupdate
var Integer startvalue = 10

rule "Countdown initialise"  
when 
    System started
then
    if (countdownRunning.state == NULL) countdownRunning.postUpdate(OFF)
    if (countdownReset.state == NULL) countdownReset.postUpdate(OFF)
    if (countdownKeeprunning.state == NULL) countdownKeeprunning.postUpdate(OFF)
    if (countdownStart.state == NULL) countdownStart.postUpdate(OFF)
    if (count.state == NULL) count.postUpdate(startvalue)
   
end

rule "Countdown running"
when
    Item countdownStart changed to ON
    or
    Item countdownKeeprunning changed to ON
    or
    Item countdownReset changed to ON
then

    if(1==1) //formatting and outputting count to stringToSend 
    { 
        logInfo("Countdown","formatting and outputting count to stringToSend --> Countdown ")
        var sec = (count.state as Number).intValue  % 60
        var min = (((count.state as Number).intValue) / 60) % 60
        var hrs = (((count.state as Number).intValue) / (60*60)) % 24
        var day = ((count.state as Number).intValue) / (60*60*24)
        var String stringToSend 
            
        if ( count.state  < 25 && count.state > 20) 
            {
                day=5
                stringToSend = String::format("%1$02d days, %2$02d:%3$02d:%4$02d", day, hrs, min, sec)
            }
            else 
            {
                stringToSend = String::format("%1$02d:%2$02d:%3$02d", hrs, min, sec)
            }   
            
            
        countdown.postUpdate (stringToSend)        
    }

    
     
     
    if (countdownStart.state == ON)  //Start is ON
    {
        logInfo("Countdown","Start is on")
        if ((count.state as Number).intValue > 0 && countdownReset.state==OFF)
        {
            logInfo("Countdown","countdown value in seconds: " + count.state.toString)
            createTimer(now.plusSeconds(1),  [ |
               if (countdownStart.state == ON) //if start is STILL on this second later
               {
                    logInfo ("Countdown","((count.state as Number).intValue > 0 && countdownReset.state==OFF)")
                    count.postUpdate((count.state as Number).intValue -1)
                    countdownKeeprunning.postUpdate(OFF)
                    countdownKeeprunning.postUpdate(ON)
               }
            ])
        }
         
         
    }

    if (countdownReset.state==ON)
    {
        logInfo("Countdown","if (countdownReset.state==ON)")
        countdown.postUpdate("resetting")
        countdownKeeprunning.postUpdate(OFF)
        countdownReset.postUpdate(OFF)
        countdownStart.postUpdate(OFF)
        createTimer(now.plusMillis(1500),  [ |
                count.postUpdate (startvalue)
                countdownStart.postUpdate(ON) 
        ])   
    }
    
    if ((count.state as Number).intValue == 0) 
    {
        logInfo("Countdown"," if ((count.state as Number).intValue == 0)")
        countdownStart.postUpdate(OFF)
    }
end

rule "Countdown cleaning up"
when
    Item countdownStart changed to OFF
then
    logInfo("Countdown"," Item countdownStart changed to OFF")
        
    createTimer(now.plusMillis(1200),  [ |
               countdownKeeprunning.postUpdate(OFF)
               countdownReset.postUpdate(OFF)   
               countdown.postUpdate ("timer stopped")
               count.postUpdate (startvalue) 
    ])
    
end        
2 Likes