Create alternative title with metadata?

Hello,

i got another short question: Is it possible to add an alternative title to an item at the metadata (custom namespace) to display it in a custom widget?

If this is possible, how can i access the metadata in the widget?

Thanks in advance,
Alex

I think Item metadata is only accessible from the oh-repeater widget. It’s not available in any other widget type so the answer is probably not.

This would be fine for me. I use the repeater component in an custom widget - but i would need another displayed title for the group items…

uid: widget_lights
tags: []
props:
  parameters: []
  parameterGroups: []
timestamp: Apr 19, 2021, 7:56:34 PM
component: f7-card
config:
  style:
    border: 0px
    shadow: 0px
slots:
  default:
    - component: f7-row
      config:
        style:
          padding: 10px
          padding-left: 16px
          padding-right: 16px
          row-gap: 10px
      slots:
        default:
          - component: oh-repeater
            config:
              for: item
              sourceType: itemsInGroup
              groupItem: Home_Licht
              fragment: true
            slots:
              default:
                - component: oh-button
                  config:
                    icon: f7:lightbulb
                    iconSize: 15
                    iconColor: black
                    text: =loop.item.name
                    action: group
                    actionGroupPopupItem: =loop.item.name
                    raised: true
                    class:
                      - col-50

There is an example posted by Yannick showing accessing Item metadata from an oh-repeater widget but I can’t seem to find it right now. It used the metadata to choose a different icon for different types of Items if I remember correctly.

I saw in this thread, that the metadata seems to be used somehow, but i dont know how to adapt it to metadata of a custom namespace:

- component: oh-label-item
          config:
            icon: =props.icon
            iconColor: '=items[loop.i.name].state === "ON" || items[loop.i.name].state > 0 ? "green" : "gray"'
            footer: =loop.i.metadata.uiSemantics.config.preposition + loop.i.metadata.uiSemantics.config.location
            title: =loop.i.metadata.uiSemantics.config.equipment
            item: =loop.i.name

When the oh-repeater return items (from the itemsInGroup or itemsWithTags etc.) the object that you can access via the loop variable is the complete object returned by the api ​/items​/{itemname} call. So, if you need to see how to access something via the loop object, just use the API Explorer to see what the return of that item looks like.

I have the ToggleControl custom namespace defined for several items. If I use the api explorer to show me one of those items with that namespace specified, the return payload looks like this:

{
  "link": "http://localhost:9080/rest/items/Remote_LivingRoom_Button",
  "state": "1.0",
  "stateDescription": {
    "pattern": "%.0f",
    "readOnly": false,
    "options": []
  },
  "metadata": {
    "ToggleControl": {
      "config": {
        "2.0": "ServerDummySwitch",
        "1.0": "Switch_OfficeFanLight_OnOff"
      }
    }
  },
  "editable": true,
  "type": "Number",
  "name": "Remote_LivingRoom_Button",
  "label": "Button",
  "category": "wallmote2",
  "tags": [
    "Control"
  ],
  "groupNames": [
    "Remote_LivingRoom",
    "gRemotes"
  ]
}

In that JSON you can see the metadata namespace with my custom ToggleControl metadata in it. So, if I had this item as part of an oh-repeater (with, say, remoteItem as the repeater variable) then loop.remoteItem.metadata.ToggleControl.config["1.0"] would get the string Switch_OfficeFanLight_OnOff.

Thanks for this! This seems to be exactly what i need. Now i just stuck with adding the metadata correctly to the item.
I tried to add the namespace altTitle:

value: " "
config:
  "1.0": Wohnzimmer

This RestAPI shows:

  "link": "https://192.168.1.103:8443/rest/items/WZ_Licht",
  "state": "ON",
  "metadata": {
    "altTitle": {
      "value": " ",
      "config": {
        "1.0": "Wohnzimmer"

But the in the widget its undefined:

uid: widget_lights
tags: []
props:
  parameters: []
  parameterGroups: []
timestamp: Apr 19, 2021, 7:56:34 PM
component: f7-block
config:
  style:
slots:
  default:
    - component: f7-row
      config:
        style:
          gap: 10px
          margin-left: -10px
          margin-right: -10px
      slots:
        default:
          - component: oh-repeater
            config:
              for: item
              sourceType: itemsInGroup
              groupItem: Home_Licht
              fragment: true
            slots:
              default:
                - component: oh-button
                  config:
                    text: =loop.item.metadata.altTitle.config["1.0"]
                    #text: =loop.item.name
                    action: group
                    actionGroupPopupItem: =loop.item.name
                    raised: true
                    style:
                      background: var(--f7-card-bg-color)
                      color: var(--f7-card-color)
                    class:
                      - col-50

There is one extra piece that you have to add to the oh-repeater. You’ll note that to make the API call you had to specify the metadata namespace you wanted returned. The API cannot just return all metadata to with an item. So, for the repeater, you also have to specify this namespace using fetchMetadata so it can pas that along to the api call. For your example then you just have to add the one line:

- component: oh-repeater
  config:
    for: item
    sourceType: itemsInGroup
    groupItem: Home_Licht
    fragment: true
    fetchMetadata: altTitle

Also, you don’t have to use “1.0” as a key for the metadata, that’s just what my example item uses as part of one of my automation systems because that corresponds to one of it’s potential states. You can use whatever you wish that make more sense (and also avoids the awkward ["1.0"] as part of the object call. For instance, if you just used:

value: " "
config:
  title: Wohnzimmer

Then your widget call would be:

loop.item.metadata.altTitle.config.title

which is a little more readable.

2 Likes

Great! Thank you, this works perfectly.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.