Timer Value

Very sorry for my late reply.

I’m afraid I did nor do have a lot more than this.

I use habpanel.

Let me know if that answers more or less your question?

Best regards

Raf

ok, I’ll give it one more try :slight_smile:

I realize this is an old post, but found @Raf_Daems starter code helpful and was able to setup the necessary working code to accomplish a simple timer for my concrete heater. You will notice, that in most ways the code is unchanged from that provided in Jul '18, but included the items definitions in case that helps other noobs like myself.

Item Definition in a file called mqtt.items:

Switch Concrete_Heater              "Concrete Heater"                           <snow>          (Actuation, gGar)      {channel="mqtt:topic:MQTTBroker:T71:switch"}
String conc_heater_countdown        "Concrete Heater Countdown Timer [%s]"      <time>          (Actuation, gGar)
Number conc_heater_count            "Concrete Heater Countdown Timer (seconds)"
Switch conc_heater_countdownRunning
Switch conc_heater_countdownReset
Switch conc_heater_countdownKeeprunning
Switch conc_heater_countdownStart

Rule Definition within a file called conc_heater.rules

var Integer conc_heater_startvalue = 7200 //starting timer value in seconds

rule "Initialize concrete heater countdown timer"  
when 
    System started
then
    if (conc_heater_countdownRunning.state == NULL) conc_heater_countdownRunning.postUpdate(OFF)
    if (conc_heater_countdownReset.state == NULL) conc_heater_countdownReset.postUpdate(OFF)
    if (conc_heater_countdownKeeprunning.state == NULL) conc_heater_countdownKeeprunning.postUpdate(OFF)
    if (conc_heater_countdownStart.state == NULL) conc_heater_countdownStart.postUpdate(OFF)
    if (conc_heater_count.state == NULL) conc_heater_count.postUpdate(conc_heater_startvalue)
end

rule "Start countdown when concrete heater is turned on"  
when 
    Item Concrete_Heater changed to ON
then
    if (conc_heater_countdownStart.state == ON) {
        logInfo("Concrete heater countdown timer","Restart timer")
        conc_heater_countdownReset.postUpdate(ON)
    }
    if (conc_heater_countdownStart.state == NULL) {
        logInfo("Concrete heater countdown timer","Start timer")
        conc_heater_countdownStart.postUpdate(ON)
    }
    if (conc_heater_countdownStart.state == OFF) {
        logInfo("Concrete heater countdown timer","Start timer")
        conc_heater_countdownStart.postUpdate(ON)
    }
end

rule "Stop countdown when concrete heater is turned off"  
when 
    Item Concrete_Heater changed to OFF
then
    if (conc_heater_countdownStart.state == ON) conc_heater_countdownStart.postUpdate(OFF)
    if (conc_heater_countdownStart.state == NULL) conc_heater_countdownStart.postUpdate(OFF)
end

rule "Concrete heater countdown timer running"
when
    Item conc_heater_countdownStart changed to ON
    or
    Item conc_heater_countdownKeeprunning changed to ON
    or
    Item conc_heater_countdownReset changed to ON
then

    //if(1==1) //formatting and outputting count to stringToSend 
    //{ 
        logInfo("Concrete heater countdown timer","formatting and outputting count to stringToSend --> concrete heater countdown timer")
        var sec = (conc_heater_count.state as Number).intValue  % 60
        var min = (((conc_heater_count.state as Number).intValue) / 60) % 60
        var hrs = (((conc_heater_count.state as Number).intValue) / (60*60)) % 24
        var day = ((conc_heater_count.state as Number).intValue) / (60*60*24)
        var String stringToSend 
            
        if ( conc_heater_count.state > 86400) 
            {
                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)
            }   
            
            
        conc_heater_countdown.postUpdate (stringToSend)        
    //}

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

    if (conc_heater_countdownReset.state==ON)
    {
        logInfo("Concrete heater countdown timer","if (countdownReset.state==ON)")
        conc_heater_countdown.postUpdate("resetting")
        conc_heater_countdownKeeprunning.postUpdate(OFF)
        conc_heater_countdownReset.postUpdate(OFF)
        conc_heater_countdownStart.postUpdate(OFF)
        createTimer(now.plusSeconds(1),  [ |
                conc_heater_count.postUpdate (conc_heater_startvalue)
                conc_heater_countdownStart.postUpdate(ON) 
        ])   
    }
    
    if ((conc_heater_count.state as Number).intValue == 0) 
    {
        logInfo("Concrete heater countdown timer"," if ((count.state as Number).intValue == 0)")
        conc_heater_countdownStart.postUpdate(OFF)
    }
end

rule "Cleaning up concrete heater countdown timer"
when
    Item conc_heater_countdownStart changed to OFF
then
    logInfo("Concrete heater countdown timer"," Item countdownStart changed to OFF")
        
    createTimer(now.plusSeconds(1),  [ |
               conc_heater_countdownKeeprunning.postUpdate(OFF)
               conc_heater_countdownReset.postUpdate(OFF)   
               conc_heater_countdown.postUpdate ("timer stopped")
               conc_heater_count.postUpdate (conc_heater_startvalue) 
    ])
    logInfo("Concrete heater countdown timer","Stop timer and turn off concrete heater")
    Concrete_Heater.sendCommand(OFF)
end      

Very simple, when the concrete heater is turned on, it will start a 7200sec timer (2hr) and display a string of the countdown just below the concrete heater switch in my UI. The heater is turned off when the timer reaches 0.

1 Like