Convert a variable that contains a value (seconds) to HH:MM:SS

Hi

Does some now how to convert a variable that contains a unit (seconds) to a formatted string (hh:mm:ss)

Thanks
Jose

You could do this in a rule using the SimpleDateFormat class. If you know how to use Java classes/methods in rules already then this should be pretty easy, if not I’ll try and come back with an example rule later.

Hi

I dont have experiencw with that
Do you have any code to share?

Thanks
Jose

What is your seconds variable counting from? Does it represent a time such that once I convert it to HH:MM:SS is represents a time of day, or is it just measuring the amount of time since some arbitrary starting point (e.g. when something was switched on)?

If it represents a time of day then @danielwalters86’s suggestion of SimpleDateFormat is the way to go.

If it is the amount of time since some starting point you can do it with some simple math:

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

val formattedTime = totalHours + ":" + remainderMins + ":" remainderSecs

Hi

I tried the config and I have a problem. It seems not to like the %60
Do I need to import specific java library?
The counter is a number that represents a value (seconds minus thread sleep)

Thanks
Jose

% is the modulo operator and is standard java/xtend.
What error do you get?

Maybe put in a space. Perhaps it is trying to treat it as an escaped character as opposed to the modulo operator. According to the Xtend documentation ‘%’ is the right symbol for mod (i.e. remainder).

It also might not like that we are not explicit about type and BigDecimal doesn’t like mod math. If the space doesn’t work try:

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

val String formattedTime = totalHours + ":" + remainderMins + ":" remainderSecs

Struggling to tell the difference between this latest version and your previous. However there is a concat sign missing before remainderSecs in the last line.

Compare the second word of each line. I’ve added the type to all the vals.

You are correct, there is a missing “+” on the last line.

Hi

We are progressing…Here is the code and If I declare a value for
seconds, it works…:slight_smile:
The problems is how to declare the val seconds with the value of the
counter that is in countdown.

  while(Timer_Interrupteur1_ext_avant >= 0) {
       //val int seconds = Timer_Interrupteur1_ext_avant
       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) + ":" +

String::format("%02d", remainderMins) + “:” + String::format("%02d",
remainderSecs)

//Temps_Interrupteur1_ext_avant.postUpdate(Timer_Interrupteur1_ext_avant +
" sec")
Temps_Interrupteur1_ext_avant.postUpdate(formattedTime)

Temps1_Interrupteur1_ext_avant.postUpdate(Timer_Interrupteur1_ext_avant)
Timer_Interrupteur1_ext_avant = Timer_Interrupteur1_ext_avant - 1
Thread::sleep(1000)
}

Thanks
Jose

To get the value of an Item you use .state. Assuming your countdown Item is called Countdown and it is a Number Item it would be

val int seconds = (Countdown.state as DecimalType).intValue
1 Like

HI
I use a variable that is declared globally:
var Number Timer_Interrupteur1_ext_avant

Is it the same way to handle that?

Thanks

Hi

I tried :
val int seconds = (Timer_Interrupteur1_ext_avant).intValue

and it works

Thanks for your help
Jose