Timer Value


(Joerg) #1

Hi all,
I created:

timer = createTimer(now.plusSeconds(180))

is there a way to Display the value of a timer in my Sitemap in iOS App?
I tried to Format the string somehow but nothing works :frowning:

Any help is much appreciated.

Regards
Joerg


Display Timer variable as count down in habpanel
(Rich Koshak) #2

No, a timer is a rule thing, not something that is exposed to the sitemap. The purpose of a timer is for it to execute some code that you pass to it at a certain time in the future. So what you have is essentially a no-op (i.e. doesn’t do anything). A proper timer in a rule would be:

timer = createTimer(now.plusSeconds(180), [|
    // code that should execute 180 seconds from now
])

To have a countdown timer like you are looking for you would need to create a rule with a loop and a sleep that updates an item every second.

rule "Countdown"
when
    Item StartCountdown received command ON
then
    var count = 180
    while(count >= 0) {
        Countdown.postUpdate(count)
        count = count - 1
        Thread::sleep(1000)
    }
end

Creating a Countdown-Clock in HabPanel
(Thomas) #3

Hello Rich,
thank you for the your explanation.
I tried to implement you “countdown” code but I received the following message from OpenHab Designer. "Couldn’t resolve reference to JvmIdentifiableElement ‘Countdown’.
No idea what i did wrong.

As you can see my programming knowledge is not brilliant.

So any help is much appreciated


(Rich Koshak) #4

Have you defined Countdown in a .items file anywhere? Did you save that .items file?

You need to define all Items that you use in a .items file.

Number Countdown "My Countdown [%d]"

(Thomas) #5

Thank you for the fast replay!
Yes I defined the Countdown in the .items file. (like all the outer Items).
I restarted OpenHAB Designer and then the error messages dissipated. It is now working for me :smile: Tknaks for your help!


(Jonas) #6

Hi,

i got it working for me too but i want a number (set from habpanel) as the timer value so i just replaced the 180 with number.state and it does not work.
It updates my current time once but then is stuck.
i cant see why because the while-loop should be still active.


(Udo Hartmann) #7

Please use

(number.state as DecimalType)

instead


(Jonas) #8

Hi!

Thanks for your reply!

still no luck - same issue … here my rules-file:

rule "Countdown"
when
    Item WoZi_Schalter_Countdown received command ON
then
    var count = (WoZi_Nummer_Countdown_Wunsch.state as DecimalType)
    while(count >= 0) {
        WoZi_Nummer_Countdown_Ist.postUpdate(count)
        count = count - 1
        Thread::sleep(1000)
    }
    WoZi_Licht_Couchtisch_Alarm.sendCommand("LSELECT")
end

and the items:

/* Timer */
Number WoZi_Nummer_Countdown_Wunsch "Wohnzimmer-Countdown [%d]"
Number WoZi_Nummer_Countdown_Ist "Istzustand Countdown [%d]"
Switch WoZi_Schalter_Countdown "Starte Countdown Wohnzimmer"

(Rich Koshak) #9

Try

var count = (WoZi_Nummer_Countdown_Wunsch.state as Number).intValue

(Jonas) #10

working like charm! thank you! can you link me to an article where i can see some commands? or is this java script? then i know where to read :wink:

thank you


(Rich Koshak) #11

http://docs.openhab.org/configuration/rules-dsl.html


(Jonas) #12

Tanks a lot!
Now i have another issue. I kind of understand why but i don’t know how to solve it:

Everytime i push the Switch the timer function (rule) is called again and this messes up the current timer (it posts updates from each instance of functions that is called).
to solve this i wanted to reset the counter to 0. When the rule is called (i assume in a new instance) the var counter is of course not the same as before so i cant just write:

var counter = 0

after the when.
So i decided to create a global variable in the items as a number. (just added:

Number counter “Zaehler”

to items. I don’t quite understand why this does not solve my problem and how to write to number variables.
i used sendCommand in the past but this won’t help if i use math-functions as in this case.

Why can i not just assign a value to the Number-Variable like:
counter = 0 (counter defined in items files as described above).

Thank you!


(Rich Koshak) #13

There is nothing “Number-Variable like” about a Number Item. Items are complex Objects with lots of moving parts and the Number Item just happens to carry a State that is a Number.

To change the state of an Item you must use either sendCommand or postUpdate. This is the only way to change the state of an Item.

I don’t understand this statement. I and others do math with and update the results from calculations using postUpdate and sendCommand all the time. Please review:

and if that doesn’t solve your problem post your code.