I struggle with finding the proper or preferred way to display the “uptime” of my garage door drive!
openHAB’s KNX-binding receives a 4-byte value of DPT 13.100 (long_DeltaTimeSec) representing the uptime of the drive in seconds.
I would like to display a string, that shows the uptime based on days, hours, minutes and seconds.
I could receive the value into a number item and to the necessary calculations with a JS-transformation. But I can’t return a string-based result into a number item, right?
I could also trigger on an item update, do the necessary maths in a rule and update an unlinked string item to display the uptime value.
Probably there is also a way to store the value in a datetime item - but I’m unsure whether it’s possible to format the object into time difference (opposed to an absolute date/time value)… .
Any hints or suggestions?
Thanks for your comments and have a nice weekend!
-bernie
I noticed that this doesn’t hold > 24hrs, but it is probably fine for the OP’s needs.
I stand corrected. It does hold time > 24hrs. I thought it didn’t because my state description / format was to display HH:MM:SS so it literally just displayed the hour/min/sec part. Converting the state to seconds and getting the integer value shows that it does hold whatever is stored in it
There is a limit though. The formatting for Number:Time is really kind of a hack. It treats the value like a DateTime which is why it uses the DateTime String formatting. As such, because Java’s String formatter only supports up to day of the month and not Julian date, the maximum number of days that it supports is 31. After that it cycles back to 1. Also, because there are no 0 days of the month, it really falls down on days over all. If you need more than 24 hours you need to use a transform.
You could use a String Item and do the transform in the Thing or a Profile before it gets to the String Item. But then you couldn’t do stuff like charting.
You can set the State Description Pattern to use the transform which means the Item’s state remains unchanged but where it’s shown on the UI it will be in DD:HH:MM:SS format.
thanks a lot for your contributions and pointing me in a direction! I have not yet got the idea behind number:time. But I guess this just requires some more reading… .
I will try and find a solution based on the suggestions made and then post my findings here…
I feel bad about bumping this topic, but as daily business is a time killer, sometimes, I didn’t manage to cope with this topic anytime sooner!
8 months ago, I promised to post my solution. So here it is, what I implemented yesterday:
In order to display the uptime of the door drive in a coherent way, I store the time value in a string-item and apply following JS-transform to it.
timeformat.js:
(function(timestr) {
data = parseInt(timestr);
days = Math.floor(data/3600/24);
hrs = Math.floor((data-days*3600*24)/3600);
mins = Math.floor((data-days*3600*24-hrs*3600)/60);
secs = Math.floor(data % 60);
return (days+"d "+('0'+hrs).slice(-2)+" hrs");
})(input)
Thanks for your comments, again!
Have a nice week!
-bernie