[OH3] Dynamically change semantic model

Hi all,

I worked with OpenHab 2 for some time and now started to migrate to OH3. Since I struggled updating my existing stuff because of mixing file and UI configs I started porting to a fresh install.

However in my openhab2 setup I have a group for all the smart-sockets in my apartment which is also shown in the sitemap. Since I own some Shelly / SonOff / Tasmota plugs that might get offline because it is currently not used I setup a rule to dynamically add or remove items to the group if the online state changes:

import org.openhab.core.model.script.ScriptServiceUtil

val logName=“sockets.rules”

rule "Add / Remove sockets to group"
when
    System started or
     Member of Sockets_Online changed
then
	Sockets_Online.members.forEach[ s |

		val String plug_name 	= s.name.substring(0, s.name.lastIndexOf('_'))
		val plug_item 			= ScriptServiceUtil.getItemRegistry.getItem(plug_name)

		logInfo(logName, "Plug name: "+plug_name+", State: "+s.state.toString)

		if (s.state == ON) {
			logInfo(logName, "Adding "+plug_name+" to Sockets group")

			Sockets.addMember(plug_item)
		} else {
			logInfo(logName, "Removing "+plug_name+" from Sockets group")

			Sockets.removeMember(plug_item)
		}
	]
end

Now after migrating to OH3 I’d also like to use the same for the MainUI with semantic-model. So only the sockets that are online should be shown under Equipment->PowerOutlet.

Is there a possibility in OH3 to dynamically add/remove the equipment tag?

Thanks in advance

Regards,
Christian

A more proper way to handle this would be to keep it in the model at all times. If it’s something might move around it might make sense to add it as an equipment to the house instead of a specific room. The model isn’t really designed to be dynamic like that.

But you are actually not really worried about the model. You want to make them not appear in the automatically generated tabs of the overview page. For that, you can override the default list widget on the Item and set the Visibility Option with an expression that only shows it when the device is online (expressions can reference any number of other Items). That effectively removes that Item’s entry from the cards when it is not online.

What I don’t know though is whether the Equipment Card itself will be suppressed or not. If not, I’d recommend filing an issue as I at least think that if the Equipment doesn’t have any visible entries it shouldn’t be shown.

Neither tags nor Group membership works very well when dynamically modified from rules like that. There is no event generated in either case so you’d have to refresh the UI to see the changes. If you are using Member of triggers on these Groups you also have to reload your rules to pick up the changed membership.

Having said all of that, there is addTag and removeTag methods on the Item.

Thanks for the reply

The items might move around but that’s clear how to handle best. I have some Shelly plugs that I used for all the Christmas light. Now they are lying at my desk and waiting for new ideas until next December.
The devices are offline but still visible on the UI.

However I think with your ideas I can proceed.
Is there a how-to / example available how to change the default list widget?

Navigate to the Item. Click on “add metadata”. Select “default list widget”. Fill out the form to define the widget.

Great thanks

I was looking at the wrong location