Script in widgets


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: [ 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")) || ( != null &&"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…


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
  - card
    - 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
  title: =props.title
    - component: oh-list
          - component: oh-repeater
              fetchMetadata: ExtendedInfo
              for: item
              fragment: true
              groupItem: =props.groupItem
              sourceType: itemsInGroup
                - component: oh-list-item
                    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[].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: " "
  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.