Populating and Displaying a variable size json array

I’m running openhab 2.5.10

There’s an api which returns an (json) array of short strings (each string with length of 2-15 chars) which can change in size. Typically the size varies between 0 and 10 entries (but not guaranteed).

The goal is to see all the values on the sitemap and also the size, the format does not matter at all (should not be ugly :sweat_smile:)

I tried quite some to solve the issue but each solution I find is not ideal. The problem is that just concatenating the array values, the resulting string is too long to just store it in a single item and display it on the sitemap, the sitemap would truncate or not show the string at all.

So I tried to split it up, each value of the array to a single item. However since the size of the array varies I could not find a way to dynamically create items or so. So right now I hardcoded 10 items, each one of them use the http binding to fetch which works but is not very nice.

String      MyItem1 "[%s]" (My_Group)  { http="<[https://url:30000:JSONPATH($.state[0])]"}
String      MyItem2 "[%s]" (My_Group)  { http="<[https://url:30000:JSONPATH($.state[1])]"}

The second problem is how to display it on the sitemap. So I thought to display the size of the array on root level and when clicking on it go to a subscreen to list the individual items. However since I hardcoded 10 items I cannot just use the Group element on the sitemap since the size would always be 10 then. So right now I use a different item to store the size of the 10 items who actually have a value (not null). But then since that’s a different item I cannot use Group also, so I need to manually enter all 10 items to the sitemap.

Text item=MyItemCount label="Count [%d]" {
            Default item=MyItem1
            Default item=MyItem2
            .....
}

Any ideas how to solve such a problem?

For the first part, you could work with a rule to assign different values to different items.
Iterate through the JSON.
Then you can work with the visibility property to show the item(s) or not. Which should cover your second question as well.

I don’t get how that would solve any of the issues. I still would need to hardcode the items. The population of the values is not a problem.

It’s okay, Items are fairly cheap. You know your likely maximum number? As @BeanzBE says, you can hide th unused ones e.g.with no content.

Alright then it seems like there is no better solution. I mean the current solution works but that’s just incredible cumbersome, what if the maximum count would go to 30, I would need to copy and paste 30 items, always increasing a number by one, that just feels wrong. Then next month it could increase to 35, I need to adjust my setup again? or I just think ahead now and create 100 items by copy paste…

Well, it’s your odd requirement for display purposes. This is not something commonplace in a home automation system.

Why not have a look at how people do umm “tankerkoenig” gas price comparison applications and so on.

Or write it into a webpage-let and use a webview.

why not? imagine an array of users (or smartphones or network devices or whatever) currently at your home, the api returns an array of strings. I’d like to see how many there are and then also the details which devices actually

It isn’t too hard to dynamically create and delete Items when using Jython, JavaScript or Groovy Rules. So you can create your Items dynamically if you need to. But BasicUI is never going to do more than just show all the Items in a given Group through the Group element. And I’m not 100% certain that it would pick up the changes dynamically. At a minimum the UI would have to be reloaded.

BasicUI is, as it’s name implies, basic. It’s not that dynamic and it’s not that capable. But there is HABPanel, HABot, and the new OH 3 MainUI which support more dynamic UIs.

It isn’t too hard to dynamically create and delete Items when using Jython, JavaScript or Groovy Rules. So you can create your Items dynamically if you need to. …

@rlkoshak How would you do that? I’m looking for a way to do the same but didn’t find a good example. I’ve also browsed OH core libraries for an API without success so far. Any hint you can give?

Using the Helper Libraries

ETOD_ITEM = "TimeOfDay"
if ETOD_ITEM not in items:
    from core.items import add_item
    add_item(ETOD_ITEM, item_type="String")

See openhab-helper-libraries/items.py at master · openhab-scripters/openhab-helper-libraries · GitHub for how to do it without the helper library.