Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Number:Time] Error in displaying days in format string #1766

Closed
TBail opened this issue Oct 26, 2020 · 15 comments
Closed

[Number:Time] Error in displaying days in format string #1766

TBail opened this issue Oct 26, 2020 · 15 comments
Labels
UoM Units of Measurement

Comments

@TBail
Copy link

TBail commented Oct 26, 2020

I am on oh3 build #1986. Ihave a Number:time item that gets ist value fromm a mqt channel that represents an uptime. In the state Description i use the following format to display a human readable time value

image

Thats works well with one exception. As shown in the following picture the item is up for one day an 43 (sorry missed to 42) seconds. Fact is that the uptime at this moment was only 43 seconds. The one day is an error.

image

@Rossko57
Copy link

$tj is for date formatting, day-of-year (first day is 1).
$td (day-of-month) is I think gong to give you the same result.

I think this is misapplication of java datetime formatter, you have to do your own transformations e.g. from 90 seconds to 1m30s

@9037568
Copy link
Contributor

9037568 commented Oct 27, 2020

I believe Number:Time is based on the OH DateTimeType, which is based on Java's ZonedDateTime.
So you can't use it like a duration without, as Rossko57 has said, hacking it yourself.

@TBail
Copy link
Author

TBail commented Oct 27, 2020

A thats an argument that is true, but what is then the use case for Number:Time?

@cweitkamp
Copy link
Contributor

Formatting Number:Time is done by the java.util.Formatter, a printf-style format for strings. You can use the syntax for Date/Time Conversions. Internally the value is converted to ZonedDateTime before the formatter will be applied.

'j' is the conversion character for

Day of year, formatted as three digits with leading zeros as necessary, e.g. 001 - 366 for the Gregorian calendar.

thus the result in the OP is correct.

What is missing is a way to print the number of days. In general using Date/Time Formatting here is not the best solution here, I tried to find a way how to format a Duration (see #1470 (comment)) but I was not successful. Any hints are appreciated.

@TBail
Copy link
Author

TBail commented Oct 27, 2020

Just an idea. Is ist possible to apply a dynamic format based on the value.

43 Seconds are automaticalls displayed as "43s"
2 Minutes and 10 Seconds as "2min 10s"
and 3 Day and some thing as "3 Tage 0 Std. 3 min 4 s"

Units could be based on selected language.

@cweitkamp
Copy link
Contributor

Nice idea but I would rather introduce a custom conversion character to represent the "days" value instead of a language dependent formatting. The substitution and the calculation of the value can be done internally.

@Rossko57
Copy link

Number;Time is exactly intended as duration.
It's a Quantity i.e. 2.6 hours or 140.5 minutes, just like 3.5km The framework can scale between units like 3.5km to 3500m, but not to "3km and 500m" which is essentially what you are asking for.

It's certainly possible to embed some highly specific duration formatter - I have doubts it would please everybody. "I want to display 5 days 3 hours with no minutes or seconds when it's that long. Or just 45 seconds without 0 days 0 hours 0 minutes when it's that short"

Meantime custom display is what transformations are good at.
Old example -
https://community.openhab.org/t/javascript-transform-example-with-system-uptime/40063
An updated version for Number:Time - where the js can read the input units from the state - would be generally useful.

@TBail
Copy link
Author

TBail commented Oct 28, 2020

Compared to other Quantities time is based on differnent bases unlike other quatities where the base is always a power of 10. With time we have fractione of a secons that is base 10 based, Minutes and Hours that are based on 60 day based on 24 ...

So the type should reflekt that. and it should be userfriendly. 2.6 hours is almost useless.

@Rossko57
Copy link

Rossko57 commented Oct 28, 2020

It's an Item state. It can hold a Quantity Type - this is a single numeric value and a unit. It's of little practical difference to you if it holds 1.5 hours or 90 minutes internally, they're the same duration. What you're on about is presentation.

Conversions - no, time duration is not about especially exotic maths. openHAB is quite happy to convert metres to inches (which are not x10), °F to K (which involves an offset as well as factor).

There's nothing stopping you requesting an enhancement for Quantity Type in general to allow multi-sectioned quantities, such as "5 feet 3 inches". I can't see much value in that myself.

@TBail
Copy link
Author

TBail commented Oct 28, 2020

Lets say it in a different way. If i have a Number:teperature item, then i expect to format it in °C or °F or what ever in my area is used. If i have a Number:Time than that is a Duration and i expect to format it in an comon way Durations are displayed.

I don't care about the stuff behind it in java code.

@Rossko57
Copy link

Rossko57 commented Oct 28, 2020

Are you confirming you have a presentation issue and you do not care about the internal workings of Number:Time ?

Transformations are good for custom presentations.
I remind you that other people might want a Number;Length of 2.0m displayed as 6.56 ft or 6 feet 6 inches or 6'6" or ...
Duration is not special in this regard.

EDIT - I was curious enough to create a demo transformation, described here
https://community.openhab.org/t/number-time-formatting-for-display/107301

@TBail
Copy link
Author

TBail commented Oct 29, 2020

I think we should close this topic. For me is no need to use Number:Time because in my world it brings no benefits above a simple Number.

@Rossko57 offers a huge example how to format a Number:Time which could be use for Number as well. Thnaks for that!

@TBail TBail closed this as completed Oct 29, 2020
@TBail
Copy link
Author

TBail commented Oct 29, 2020

As written in my last comment. Thanks for your efforts.

@Rossko57
Copy link

Rossko57 commented Oct 29, 2020

Follow up, the biggest benefit I see of the Quantity types is when you get to non-trivial uses, e.g comparing a length from one source "523mm" to another source "0.761 ft", or more like in this case adding "0.76 hours" to "3213.7 s". You don't have to do conversions, it just works.

The price would be the added complexity of the trivial stuff ... formatting. Having said that, there isn't much difference between formatting a plain Number and a Number;Time in the style that you wish here.

@cweitkamp cweitkamp added the UoM Units of Measurement label Dec 2, 2021
@openhab-bot
Copy link
Collaborator

This issue has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/number-time-item-in-s-convert-to-hh-mm/130463/2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
UoM Units of Measurement
Projects
None yet
Development

No branches or pull requests

5 participants