Dynamically add/remove channel groups on a thing

Hello,

As I’m writing my binding for the open-meteo API, I’m trying to have it fit in the current ecosystem which means I have to create channel groups for each forecast hours, like for instance what the OWM binding is doing as can be seen here

My current stumbling point is that Open Meteo can provide hourly forecast for up to 16 days, which amounts to 384 channel groups!

I already know how to dynamically add/remove channels via the ThingBuilder instance returned by BaseThingHandler.editThing() but while there are the withChannel methods, I could not find any method that would do the same on the channel groups.
I believe it would be called withChannelGroup but it’s nowhere to be found.

I tried creating a channel with an unknown channel group Id in the secret hope that an error message would give me a clue, but it did not fail. The UI simply shows the channel with a group heading that is very small because there is not text for it:

Having channel group definitions present in the XML file that have no channels associated also works, and they are not displayed in the UI either, but it feels very wrong to clutter the XML definition with almost 400 channel groups…

What do you think of this? Is there a way to dynamically manage channel groups, or should I just settle on the XML file cluttering and be done with it?

The correct way is to implement a ChannelGroupTypeProvider and/or a ThingTypeProvider. In both cases you should(probably extend AbstractStorageBasedProvider.

Thanks, but I have a hard time understanding how I could change the type of a given thing at runtime, depending on the thing configuration.
Would there be an example of such behavior in an existing bundle that I could study?

Thinking about your application: You should not add a channel for every hour. Use a TimeSeries, that’s how it should be done in OH4.1.

Ok, changing the thing type at runtime is not possible for obvious reasons, but because unused channel groups are not showing up in the UI, it is indeed possible to use a ThingTypeProvider that creates the channel groups programmatically.

Yes, I understand that, as it had been suggested in the reply to my message asking for recommendations. But that reply also suggested that existing weather widgets would only work with one channel group per forecast milestone and so I decided to have those as well.
But in order not to clutter the system, I made those optional via a configuration option:

With the usage of a ThingTypeProvider this now works just the way I intend it to, thanks for the suggestion.

You can change the thing-type during runtime, there is a changeThingType(ThingTypeUID thingTypeUID, Configuration configuration) in the BaseThingHandler.