Good, then you have the latest changes to UoM.
OK, so assuming it’s a Number:Time Item, everything I said above still applies. In addition you can set the label on the Item or sitemap element the same as I showed above for the state description pattern.
Yes, assuming a Number:Time Item it does work out of the box.
No, that’s the whole purpose of UoM. You didn’t have to convert between units in a time yourself. You just have to indicate what units you want and the UoM subsystem does the math for you.
If you have just a Number Item without a type or units though, you are responsible for doing everything yourself through rules or transformations. But the preferred/recommended approach is to use UoM.
Number:Time MyTimeItem { unit="weeks" }
rule "Get time in different units"
when
then
val curr = MyTimeItem.state as QuantityType<Time>
logInfo("units", "Default: " + curr) // weeks
logInfo("units", "Days: " + curr.toUnit("d")) // converted to days
logInfo("units", "Seconds: " + curr.toUnit("s")) // converted to seconds
// compare 28 to days, the Item's state is in weeks, the UoM handles conversion to do the comparison for you
if( curr > 28|d ) {
logInfo("units", "MyTimeItem is over 28 days: " + curr)
}
// Add 7 hours to the Item, remember the Item's state is in weeks and the UoM subsystem converts between units as needed
MyTimeItem.postUpdate(curr + 7|h)
end
Show the time as weeks on Sitemaps
Number:Time MyTimeItem { unit="weeks" } // Item's state will always be weeks, therefore the default unit shown will be weeks, any update to the item not in weeks will be converted to weeks automatically
Number:Time MyTimeItem "Duration [%d week]" // item state is in seconds, system default, but shows on Sitemaps in weeks
In your sitemap.
Text item=MyTimeItem label="Duration [%d week]" // no matter the unit metadata or the Item label, display the Item's state as weeks for this element
In this case, if there is no unit metadata, the Item carries seconds which is converted to weeks by the sitemap automatically.
Notice there isn’t a divide by 168 anywhere. That’s handled by the UoM subsystem. You just have to tell OH what unit you want. Give it a try.