Script in widgets

Hello,

I’m trying to do something, and I’m not sure in possible in openhab. I want to do something that is similar to the existing widget : battery status.

Basically, I want to display all my items representing a battery on a single page, and print their values to see at first glance if I have batteries that need changing. However, all my battery items are simply named ‘Battery’ and rather than changing all my labels, I wanted to display the parent group name.

To make it simple, I have:
Group bedroom “Bedroom Sensor” (batteries)
Item bedroom_battery “Battery” (bedroom)
Item bedroom_temperature “Temperature” (bedroom)
Group kitchen “Kitchen Sensor” (batteries)
Item kitchen_battery “Battery” (kitchen)

And my idea was to list all members of “batteries”, cast as GroupItem, find the battery item in its members, and then print group.label + " : " + item.state + " %"

I wrote the part that extracts the data I need:

batteries.members.map [ dev | java.util.Map.entry(dev, (dev as GroupItem).members.filter [ item | (item.label != null && item.label.toLowerCase().contains("battery")) || (item.category!= null && item.category.toLowerCase().contains("battery")) || (item.name != null && item.name.toLowerCase().contains("battery")) ])]

But then I realized I had no idea how to use it a widget. Or even if it was possible at all.
I tried to use oh-repeater with the ‘in’ parameter, but with no luck.

So yeah, is it possible at all to iterate over a custom list when creating a widget? Or calling a script and using its output…

Thanks

I had to do something similar and couldn’t figure out how to get the group name so I ended up using metadata instead to do a “friendly name” that’s different from the label. My items goes by timestamp but the title part you can probably use as a start:

uid: listOfLastReport
tags:
  - card
props:
  parameters:
    - description: Title of widget
      label: Title
      name: title
      required: false
      type: TEXT
    - context: item
      description: Group to list
      label: Group
      name: groupItem
      required: true
      type: TEXT
  parameterGroups: []
timestamp: Nov 8, 2022, 4:48:36 PM
component: f7-card
config:
  title: =props.title
slots:
  default:
    - component: oh-list
      slots:
        default:
          - component: oh-repeater
            config:
              fetchMetadata: ExtendedInfo
              for: item
              fragment: true
              groupItem: =props.groupItem
              sourceType: itemsInGroup
            slots:
              default:
                - component: oh-list-item
                  config:
                    badge: '=((dayjs(loop.item.state).diff(dayjs().startOf("day"), "days")) == 0 ? "OK" : "Not Responding")'
                    badgeColor: '=((dayjs(loop.item.state).diff(dayjs().startOf("day"), "days")) == 0 ? "green" : "red")'
                    footer: =items[loop.item.name].displayState
                    title: =loop.item.metadata.ExtendedInfo.config.friendlyName + " " + loop.item.label

and then on the items in the group, I have this custom metadata named ExtendedInfo - chosen just to keep it generic in case I needed to add more info later:

value: " "
config:
  friendlyName: Kitchen Motion Last Updated

As long as your chosen custom metadata name matches the “fetchMedatata” value in the widget, that’s what matters.

Edit: groupItem is a separate group that contains the items I want this widget to show for clarification

The Items you get in the browser that can be used in the widgets are not the same as the Items you work with in rules. In a regular widget, all you get is state and displayState. In the repeater widget you get a bit more like you can access Item metadata (like @tardismechanic demonstrates).

Furthermore, Building Pages - Components & Widgets | openHAB and Creating Personal Widgets | openHAB for details on the sorts of scripting that can be done in a widget to populate one of the widget’s fields.