Formatting date/time of a label-item (within a list-card-widget in a page) with YAML

Within a label-item of a page which shows a date/time, I’d like to change its format to e.g. 04.03. - 22:34
In other posts it’s mentioned to do this in metadata but a) I don’t find any such option within the page/item definition and b) I’d like to define the format within YAML for reasons of flexibility. I’ve looked at other similar posts, too, but somehow they seem to address something different.
I’d have expected there’d be a label parameter like “format” but that doesn’t work and here (oh-label-card - Label Card | openHAB) no such option is mentioned.
So how could I do this?
Here’s what I mean:

a) You have to set the State Description metadata on the Item. Configure the Pattern field with the format you desire which follows the same syntax as is used by sitemap’s labels. Once defined, your Item will have a stateDescription parameter in addition to state which can be used in your custom widgets. See Items | openHAB.

b) Once you click to add the State Description metadata there is a code tab and you can enter it as YAML.

Another option you have is in expressions you can use the day.js library to format and manipulate DateTime Items. See String + Format · Day.js

That’s because the Item’s state formatting is used in so many places it makes the most sense to define it in one place (State Description metadata) instead of requiring one to redefine the format over and over again in each place it’s used.

I was searching for exactly the same thing.
So, thanks for writing.
I read the State Description Documentation, but, I do not find, where to customise this. Can you help?

Navigate to the Item you want to set the metadata for. Click on “Add Metadata”. Select “State Description” from the list of well known metadata. Populate the Pattern field with your state formatting options.

This is how all metadata is set on an Item.

Wow! Very fast response!!!
Good news, I found it, bad news, I didn’t get the syntax working.
What I want: item channel (Name of the channel: lastseen ) should be formatted.
I did several tries, but nothing happens. One of them is:

value: " "
  pattern: "[YYYY-MM-DD]"

I also put the channel name behind value. But, again, nothing happens.

the pattern should be something like this

pattern: "%1$tY-%1$tm-%1$td"

info can be found here

Are you sure it’s stateDescription? This doesn’t give me anything (undefined). I use displayState, although this is not working when I loop through my items (with oh-repeater)
Any idea where we can find all possibile parameters for items which can be use in widgets?

That’s right. It’s displayState.

If you just use items.MyItem in a text field you should see both.

Both of them does not change anything :frowning:
I am lost, I thought, formatting should be done with dayjs(), but anyway:
Putting the code in stateDescription or displayState ( this is a new Namespace ) does not change anything.
And, in the GUI, there is no other option possible.

component: oh-label-card
  item: FritzBox_Online
  title: FritzBox
  trendItem: FritzBox_Online
  action: analyzer
    - FritzBox_Online
  footer: =items.FritzBox_LastSeen.state
  background: =(items.FritzBox_Online.state =="ON") ? "lightgreen" :"red"
  icon: f7:shield
slots: null

The footer definition gives no other option than “.state”. Putting displayState or stateDescription here ends up with no display


Navigate to the Item.

Click on “Add metadata”

Select “State Description” from the list.

Put the formatting pattern in the Pattern field.


In your custom widget use the displayState.

footer: items.FritzBox_LastSeen.displayState

You made my day!!
It works!!!

That’s a good one!

I’m a little bit confused now regarding what is useable when I use a single item or through a loop (itemsInGroup)

single item
isRootInsert (don’t know what this is…)

loop item

You seem to live in a different time zone (I’live in Germany and am awake again :wink: - nice to see such a lively thread.
However, I didn’t get your solution to work, Rich.

I went to settings / item (Equipment Gaming PC) / metadata /pattern and added %te.%tm.-%tH:%tM . I also tried %1$tY-%1$tm-%1$td. And just to be on the safe side, I tried HH:mm. None of these changed the format in the item.
Here’s the code I get in “edit item metadata”
value: " "
pattern: “%te.%tm.-%tH:%tM”

Although I wouldn’t expect it to work (because no change is shown in the item itself), I then tried to enter the code in the page like this:

I see in your first screenshot that you are setting the State Description on the group and not on the item itselves.

If you want to see data from your items in the group, you shoukd also have a look to the Group Settings in the group.

1 Like

That makes sense, now it basically works.
“Just” one more problem: When I enter “%tM” I get the correct minute, same with hour. But when I try to combine more than one (e.g. %tH:%tM or what I’d really like %te.%tm.-%tH:%tM) I get ERR.

Try "%1$te-%1$tm-%1$tH:%1$tM"

1 Like

There is way more available in a loop in an oh-repreater element. Everywhere else all you have is the state and displayState. I don’t know what isRootInsert is either.

I just happened to select an Item at random. The fact that it’s a Group is just a coincidence.

But, assuming that the Group Item has a state it works exactly the same.

I have seen cases where the page needs to be refreshed before changes to that sort of metadata gets picked up. There is no other event generated to tell the web page that the metadata has changed.

Here is a state description (in YAML) from a Number:Time Item which takes the same formatting as a DateTime.

value: " "
  pattern: "%1$tH:%1$tM:%1$tS"

You cannot set this on a component. It’s completely separate from the widgets. It’s never going to work that way.

1 Like

well, thank you all, now it works like it should!
Looking at Formatter (Java SE 11 & JDK 11 ) I’d never have guessed the format. Didn’t see anything about “%1$” or what it means but I’ll remember now.

I’m trying to achive the similiar thing for an large numer of datetime Items that has been generated in the text file.
They have a display formating embeded and that works for displaying single item in the

DateTime SAIP_004_temperature_LastUpdate	"Temperature LastUpdate [%1$tY.%1$tm.%1$td %1$tH:%1$tM:%1$tS]" (modbus_LastUpdate) {  channel="mqtt:topic:eui-oh3:moxa:temperature_1" [profile="system:timestamp-update"] }

But when I try to do that in the oh-repeater loop I only display loop.item.state that will have default time formating.

The above solution is not working me as metadata are locked for bulk generated items.
is there a way to access proper formated datetime from within the loop for my case?

You mean for Items defined in .items files? Well yes, if you have the defined in text files you cannot change them in the UI. But you can add the metadata in your .items files. It will be something like

{ stateDescription=" "["pattern"="%1$tY.%1$tm.%1$td %1$tH:%1$tM:%1$tS"] }

I don’t do text based configs any more so there could be a typo in the above.

1 Like

Ok, thank for pointing me in the right direction so I did setup of the state description in the items file
The correct syntax accoring to some other post I found is (just pattern without “”)
DateTime SAIP_004_Temperature_LastUpdate "Temperature LastUpdate [%1$tY.%1$tm.%1$td %1$tH:%1$tM:%1$tS]" (modbus_LastUpdate) { channel="mqtt:topic:eui-oh3:moxa:temperature_1" [profile="system:timestamp-update"] , stateDescription=" " [pattern="%1$tY.%1$tm.%1$td %1$tH:%1$tM:%1$tS"] }

and it now shows up in the UI


but it looks like at the display in the widget it is ignored as I still get generic timestamp when I use =loop.item.state