[Solved]Calculate duration from number (seconds)

Tags: #<Tag:0x00007f5caa0c1710>

I have an item which receives a number from an http request. that works as expected
I want to calculate the hours and minutes (hh:mm) and send the new value to another item. Then I want to display that duration.

e.g. 6060 = 01h 41min

Can anyone help`?

I tried several rules but non woked

val totalSecs = SecondsNumber.state as Number

val sec = totalSecs % 60
val min = (totalSecs / 60) % 60
val hrs = (totalSecs / (60*60)) % 24
val day = totalSecs / (60*60*24)

Could I ask to give a hint how to include your code to my Player-Item to show the min:sec instead of dezimal.
Here my item (Squeezeboxplayer)
I think I have to replace the %d, but how?

Number SB_ARZ_Time "Time [%d]" { channel="squeezebox:squeezeboxplayer:LMS:SB_ARZ:currentPlayingTime" }

You don’t. The code above is the body of a Rule. You must create Rules in text .rules files. See

http://docs.openhab.org/tutorials/beginner/index.html

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

OK,
I created a rule:

rule "Calculate decimal time counter"
when
	Item SB_ARZ_Time changed
then
	val totalSecs = SecondsNumber.state as Number
	val sec = totalSecs % 60
	val min = (totalSecs / 60) % 60
	val hrs = (totalSecs / (60*60)) % 24
	val day = totalSecs / (60*60*24)
end

Because of my limited knowledge with object-orientated programming I don’t know how to handle the error which I see in the log.

Rule 'Calculate decimal time counter': The name 'SecondsNumber' cannot be resolved to an item or type; line 5, column 18, length 13

It just says that you don‘t have an Item called „SecondsNumber“

Also, this is just a code fragment. You still need to do the work to take day, hrs, min, and sec into a String and postUpdate that to your appropriate Item.

SB_ARZ_Time contains the number, so I changed to:

rule "Calculate decimal time counter"
when
	Item SB_ARZ_Time changed
then
	val totalSecs = SB_ARZ_Time.state as Number
	val sec = totalSecs % 60
	val min = (totalSecs / 60) % 60
	val hrs = (totalSecs / (60*60)) % 24
	val day = totalSecs / (60*60*24)
end

Now I get this error:

Rule 'Calculate decimal time counter': Unknown variable or command '%'; line 6, column 12, length 14

I really appreciate your help with some code.

Well shoot. It looks like the Rules DSL can’t work with Numbers for the modulo operator.

val int totalSecs = (SB_ARZ_Time.state as Number).intValue

Hi Rich,

thank you for your quick reply! I will try it the next days. The follow up by Doxer is also very useful to me …
Once I have a working solution I will post it here … but it may take some days :slight_smile:

kind reagrds
Markus

This is the working rule:

rule "Calculate decimal time counter"
when
	Item SB_ARZ_Time changed
then
	val totalSecs = (SB_ARZ_Time.state as Number).intValue
	val sec = totalSecs % 60
	val min = (totalSecs / 60) % 60
	val hrs = (totalSecs / (60*60)) % 24
	val day = totalSecs / (60*60*24)
	logInfo("default.rules", min.toString + ":" + sec.toString)
end

Now I have to investigate for the formating (ss:mm) and how to transfer this to the item. :frowning:

2 Likes

Something like this should work:

TimeString.postUpdate(String::format("%02d:%02d:%02d:%02d", day, hrs, min, sec))

Where TimeString is your String Item.

1 Like

Got my problem solved and it is running as expected.

Short summary what I did:

Thos items are active:

Number  VU_Dauer    "Dauer [%s]"             		( VUPLUS )	{ http="<[http://192.168.1.6:80/web/getcurrent:3000:REGEX(.*?<e2eventduration>(.*?)</e2eventduration>.*)]" }
String  VU_Duration_converted    "Dauer1 [%s]" 		( VUPLUS 

This rule is set:

rule "Dauer"
when
		Item   VU_Dauer changed
then
		val totalSecs = (VU_Dauer.state as Number).intValue
		val min = (totalSecs / 60) % 60
		val hrs = (totalSecs / (60*60)) % 24
		val txt = (VU_Dauer)
		VU_Duration_converted.postUpdate(String::format("%02d:%02d",  hrs, min))
		logWarn("hilfe", "" +totalSecs)		
end

Log tells me the correct received Number in seconds

2017-12-21 11:37:49.498 [WARN ] [eclipse.smarthome.model.script.hilfe] - 21600

Running on OH 2 and OH 2.2 (release)
Thank your for the help provided, appreciate it very much!

Hi Markus,
your post helped me how to transfer the value from the rule to the item. Learning by doing :slight_smile:

1 Like