Different Timers used by pushbutton

Hello all,

I would like to create a timer for defined minutes. In the sitemap I would like to see the remaining time.
I created a stimemap as follows:

Switch item=Pushbutton mappings=[0="0", 30="30", 60="60", 90="90"]

and following item:

Number Pushbutton     "Timer in Minuten [%d]"  <time>   

Now, I would like to create a rule and I am struggling. I thought a switch / case method would fit to my approach. I created following frame:

var Timer timer = null
rule "Timer"
when
    Item Pushbutton received update  
then
    switch (Pushbutton.state as DecimalType) {
        case 0 :    { 
                Kamin_Light.sendCommand(OFF)
        }
        case 30 :    { 
               Light.sendCommand(ON)
               timer = createTimer(now.plusMinutes(30), [ |
               //Do the timer stuff
                Kamin_Light.sendCommand(OFF)
                timer = null // cancel the timer
            ])     
        }
       case 60 :    { 
               Light.sendCommand(ON)
               timer = createTimer(now.plusMinutes(60), [ |
               //Do the timer stuff
                Kamin_Light.sendCommand(OFF)
                timer = null // cancel the timer
            ])     
        }
        case 90 :    { 
               Light.sendCommand(ON)
               timer = createTimer(now.plusMinutes(90), [ |
               //Do the timer stuff
                Kamin_Light.sendCommand(OFF)
                timer = null // cancel the timer
            ])     
        }
    }
end

Any idea how to update item Pushbutton with remaining time?

Might find this adaptable

Thanks @rossko57
I thought to use something like this:

I removed already the startup sequence and socket_02

I am getting for all imports errors. Any clue why?

Yes. That very old example is for openHAB-1
If you must use it with openHAB-2, you’l need to follow this migration guide -

Thanks rossko57

Now its clear expect one warning:

Function2 is a raw type. References to generic type Function2<P1, P2, Result> should be parameterized

Even it is working, would be good to fix this as well…

Since I am really a beginner and this is not my written code, would be great to get your advice which parameters has to be add in the <>. I marked them with quesitonmarks…

        var org.eclipse.xtext.xbase.lib.Functions$Function2<?,?> makeTimer = [
        int myDelay,
        org.eclipse.xtext.xbase.lib.Functions$Function2<?,?> makeTimer2 |
            if(myDelay>0) {
                postUpdate(garden_Timer_Remaining, myDelay)
                            // to save processes I only update UI once a minute
                timerFon=createTimer(now.plusMinutes(1)) [|
                postUpdate(garden_Timer_Remaining, myDelay-1)
                makeTimer2.apply(myDelay-1, makeTimer2)
                ]
            }else {
                // timer reach zero
                postUpdate(garden_Timer_Selector,0)

                // what I want to happen when the timer stops
                //sendCommand(Socket_02, OFF)
                Kamin_Light.sendCommand(OFF)
                postUpdate(garden_Timer_DateTime_Stop, new DateTimeType())
            }
    ]

As a beginner, I would suggest not use Functions in Rules DSL. They’re error prone and unhelpful about why they might have gone wrong.

Also as a beginner, someone else will be along in a minute to advise you not to put too much effort into learning Rules DSL, and suggest using the alternative Jython rules system.

I agree. Never use staff which you dont understand. But I was happy to use ready code sample which works in general fine…

I dont want to learn much about Rules DSL, just fix the warnings:)

You can get rid of one of the the warnings (and make it a little simpler) by defining the lambdas as:

import  org.eclipse.xtext.xbase.lib.Functions

val makeTimer = [Integer myDelay, Functions$Function2 makerTimer2 |

It’s still going to complain about the makerTimer2’s Functions$Function type though but it’s going to be impossible to deal with that because the stuff in < > expands infinitely. So the warnings simply cannot be elimianted.

Frankly, this code stinks. It made sense to do it like this at one point but I cannot recommend this approach any more. I kind of dislike lambdas for something like this and I really dislike passing lambdas to lambdas. And there is absolutely no need to use two timers for this. rossko57’s Countdown Timer is far simpler and far less brittle than this.

If you use Scripted Automation, I’ve a reusable library version that implements rossko57’s DP. All you have to do is import it and give it the amount of time to run, the Item to update with the time remaining, and the function to call when it’s done. https://github.com/openhab-scripters/openhab-helper-libraries/pull/237

Thanks @rlkoshak for your feedback. I will switch the timer since - to be honest - I dont want to implement something which I dont understand…

Many thanks for the proposals