Change DateTime format and units of mesurement for things in OH3

Hey, I’m trying to find any way to change DateTime format.
In OH2 it was easy way. only changing it in item label.
But now I’m getting DateTime as

2020-11-18T20:16:12.306+0100

And now it is stored like this.
How to change it so it will be stored like before in OH2?

[%1$td.%1$tm.%1$tY, %1$tH:%1$tM] - this works only in sitemap label.

Same thing is with units of any kind of item.
For example:
Energy produced by PV panels from inverter. It is sending data as “Wh”. But normal is “kWh”.
Now it is persisitng data with this new unit and because of this charts in grafana are not accurate.

Other thing is that some of temp sensors sent data without unit.
In OH2 whe You add unit do item label it saved it to persistance.
Now it is without unit…

I think that it is possible to change this everything by using transformations.
But how?:slight_smile:

I have tried to use JS transformations but they are not working at all…

Ok, I found solution:)

In new UI there is something like “State description” for Item.
And there is option called “Pattern”.
There You can put format like %1$td.%1$tm.%1$tY, %1$tH:%1$tM or like %.2f kWh

2 Likes

Hi Kamil

can you explain in more detail how you managed to get the time formatted on the page / widget?

okay, it took me a while, but I finally figured it out…

to format a datetime inside a OH3 widget I went with this:

            - component: Label
              config:
                text: =dayjs(items[props.shellyID + "_TIME"].state).format('DD.MM.YYYY HH:mm:ss')
                style:
                  white-space: nowrap

I struggle with a similar issue.
I have a number item for which I want to turn the sign, when showing in a widget.
But I also want to add the UoM and set the pattern to %,.0f.
I manage to do only either or, i.e. either turn the sign directly in the widget with using the .state, or show the right format with .displayState but then with the wrong sign.

How can I manipulate the format directly in the widget?

Can you show us examples of what you mean? It sounds like you want to do a string replace of “-” with “”

Sure.
Item.state: -1966,73
I applied this pattern in the metadata of the item: %,.0f W
Which results in:
Item.displayState: -1 966.73 W
But what I want to see is: 1 966.73 W

So usually I would multiply by -1 and then apply the format.

Okeydoke, so you cannot multiply the string “-1 966.73 W” by anything, but you could replace() the “-” character.

Yes, but what if the item turns positive?
In that case I need the sign.

Is there no way to define the pattern in the widget?

That’s why I asked for examples. So you want “-1 966.73 W” replaced with “1 966.73 W”, and “2 336.7” replaced with “+2 336.7” ?

Yes.

-1966.73 → 1 966.73 W
+2336.7 → -2 336.7 W

So you can already get the signed numbers that you want, but want to append the string " W"?

Append the string, have a thousand separator and round.
Sorry, I was not precise in my prior post:
-1966.73 → 1 967 W
+2336.7 → -2 337 W

Really making work for yourself.

I think one approach would be -
get the state
multiply by -1
use a javascript (not java) formatter
then get the displayState
strip out the unit
append the unit to the format from earlier

Formatting numbers in javascript is a pain

OK.
I went with the replace option for now :grinning:

Thanks for your help!