Converting numbers

Hi.
I’m so glad I converted to OpenHAB2, as its functionalities and binding of all kinds of products is eminent…
But for now I’m facing a little problem. A problem which 78% of you good guys probably can solve in seconds.

I’m getting a number (count down timer) from my IHC, but this number is unreadable directly in OpenHAB panel.
So this number, which I have in my items file, needs some converting.
Sadly I don’t know how…

HELP! :slight_smile:

You need to provide a lot more details.

Post your relevant items.

What binding do you use to get the count down number from the IHC?

What have you tried thus far?

What are the errors, if any, in your logs?

What openHAB panel? Do you mean BasicUI, PaperUI, Habpanel?

What do you mean by unreadable? It doesn’t show up? You get errors (see above)? The Item doesn’t show up?

First, thanks for helping me out here.

My items file looks like this:
Number livingroom_timer1 “timer1” (Livingroom, Lights) { ihc=“xxxxxxxxx” }

When the timer starts counting down in IHC, I get the number 36000 in my OpenHAB Panel…
I’m showing it in my panel like this:
{
“name”: “Timer 1”,
“sizeX”: 1,
“sizeY”: 1,
“item”: “livingroom_timer1”,
“type”: “dummy”,
“row”: 1,
“col”: 1,
“font_size”: “14”,
“useserverformat”: false,
“unit”: “”,
“format”: “”
},

I don’t get any error in the log.

Hope this is info enough…

Try adding the value of the Item’s state to the Item’s label.

I’m assuming you are using Habpanel (it is not named openHAB Panel).

I’ve little experience with it.

Anyway:

Number livingroom_timer1 "Timer 1 [%d]" (Livingroom, Lights) { ihc="xxxxxxxx" }

In the phone apps and sitemap based UIs (BasicUI, ClassicUI) this will put the value of the Item’s state into the Item’s label as an Integer.

Look in events.log to make sure that livingroom_timer1 is being updated also.

Thanks for that @rlkoshak
I figured oyt that the number that is displayed, is 3600000 and this number is counting down OK. So the update of the Timer1 seems to work.
If I can find a way to divide this number by 60000, I will get 60 minutes. The question is, will it count down OK?

You either need to use a Rule and a Proxy Item to store the result of the divide or use a JS transform and do the math in JavaScript in the Item’s label.

I don’t understand the question.

I mean, its counting down from 3600000 (miliseconds is my best guess) as it is right now.
But I want it to count down in minutes (so its 3600000 divided by 60000 equals 60 minutes).
But I don’t know where to put this calculation…?

Like I said, create a rule or create a JS transform.

http://docs.openhab.org/addons/transformations/javascript/readme.html

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

OK. I’m not the born programmer, but I’ll give it a try.
Thanks alot for your input :slight_smile:

As said, I’m not a programmer, but by reading this community, I’ve come up with this code, and put it into a rules file, but I get some errors.
Can you tell me what could be the problem?

val int totalMinutes = seconds/60
val int remainderSecs = seconds%60
val int totalHours = totalMinutes/60
val int remainderMins = totalMinutes%60

while
(livingroom_timer1 >= 0) {
val int seconds = (livingroom_timer1.state).intValue
// val int seconds = (Countdown.state as DecimalType).intValue
val seconds = 3600
val int totalMinutes = seconds/60
val int remainderSecs = seconds%60
val int totalHours = totalMinutes/60
val int remainderMins = totalMinutes%60
// val formattedTime = String::format("%02d", totalHours) + “:” +
val String formattedTime = totalHours + “:” + remainderMins + “:” + remainderSecs
}
end

ERRORS:

2017-08-04 14:32:59.199 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'home.items’
2017-08-04 14:33:00.380 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘home.rules’ has errors, therefore ignoring it: [9,1]: missing EOF at ‘while’

@Tonny_Kohler you may want to repose using code fences:
use three tick marks ``` ( ` is the symbol for tick mark) before and after your code, that will make it much more readable, right now it is very hard to read. You can also use code fences picking the symbol on the top line in the window where you type in your comments.

In addition, you may want to use the Eclipse Smart Home Designer; it will catch a lot of mistakes such as forgotten parenthesis, typo, etc; I would believe that you either have some extra characters or another formatting issue (for example, I believe while needs to be followed by the parenthesis in the same line). In any case, if you are on windows do NOT use notepad, notepad++ and others work.

In addition the use of val works best for constants as you cannot reassign a new value to it, you may want to use var in this case:

var int totalMinutes = seconds/60
var int remainderSecs = seconds%60
var int totalHours = totalMinutes/60
var int remainderMins = totalMinutes%60

Further, I don’t think that the use of val in your while loop is correct, try just to delete them to read:

seconds = (livingroom_timer1.state).intValue
seconds = 3600
totalMinutes = seconds/60
remainderSecs = seconds%60
totalHours = totalMinutes/60
remainderMins = totalMinutes%60
formattedTime = totalHours + “:” + remainderMins + “:” + remainderSecs

Your code may have worked too, but you redefined and initialized all constants by using val
Please note too, that you use seconds twice in a row, which leave it in every loop iteration as 3600 irrespective of what your timer says.

Thanks for all your answers, but I’m simply not a god enough programmer to cope with this.
Just one more question: Is it possible that one of you guys could give me a code and where to put it, for the following…
If Switch is pressed (could be a Button in programmed in happanel), then set present time to a variable.
This variable should then be presented in HAPPanel.

It would be greatly appreciated, as this is the last thing I have to do, in order to get my OpenHAB2 up and running just the way I want.

Thanks… Best regards Tonny