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.
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" }
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
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.
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
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
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.
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