Setting item Label from rules

Hi,

is there a possibility to set the item label dynamically in a rule?
I have not found anything.

Hi,

could you probably more elaborate what you want to achieve?

there are ways to make items visible based on the content - but this is more on the sitemap side - not on the item itself.

Check the wiki @ https://github.com/openhab/openhab/wiki/Explanation-of-Sitemaps#dynamic-sitemaps

Using an asistance item you might be able to achieve what you want.

Cheers
Karsten

Sure, thank you for your quick reply.
I started using the caldav binding and want to show a couple of appointments in my sitemap.
At the moment I have on item “Name” and one item “Date”.
This is not very pleasing, I’d rather set the name of the appointment as an item label and the date as the value of the item.
I’m trying to combine both items into a helper item now, but this is not a very elegant solution.

Not at this time. You only have control over the Item’s state and how that state is displayed to the right of the static label.

Your best bet is to continue with the helper item.

I have a question regarding setting an items label. I have been setting the label of a Date item before:

rule "Update CalDav_Date4 label based on type of waste (for sitemap display)"
when
    Item CalDav_Date4 changed
then
	CalDav_Date4.label = CalDav_Muelltonne4.state.toString

	if (CalDav_Muelltonne4.state.toString.contains("Gelbe")) {
		CalDav_Muellicon4.postUpdate(1)
	} else if (CalDav_Muelltonne4.state.toString.contains("Papier")) {
		CalDav_Muellicon4.postUpdate(2)
	} else if (CalDav_Muelltonne4.state.toString.contains("Bio")) {
		CalDav_Muellicon4.postUpdate(3)
	} else if (CalDav_Muelltonne4.state.toString.contains("Rest")) {
		CalDav_Muellicon4.postUpdate(4)
	} else if (CalDav_Muelltonne4.state.toString.contains("Glas")) {
		CalDav_Muellicon4.postUpdate(5)
	}
end

That worked just fine. Unfortunately, for each of my four items I created the same rule, so a lot of duplicate code. So now I trying to do the same in a new rule that does it for a number of items:

rule "test"
when
    Item NextTrash_1_Type changed or
	Item NextTrash_2_Type changed or
	Item NextTrash_3_Type changed or
	Item NextTrash_4_Type changed
then
	var nextTrash = gNextTrashType?.members

	nextTrash.forEach[ trash | 
		val tmp = gNextTrashDate.members.findFirst[name.equals(trash.name.replace('Type', 'Date'))]
		logInfo("blub", tmp.name)
		tmp.label = trash.state.toString

		postUpdate(gNextTrashIcon.members.findFirst[name.equals(trash.name.replace('Type', 'Icon'))], 
			(if (trash.state.toString.contains("Gelbe")) "1" 
			 else if (trash.state.toString.contains("Papier")) "2"
			 else if (trash.state.toString.contains("Bio")) "3"
			 else if (trash.state.toString.contains("Rest")) "4"
			 else if (trash.state.toString.contains("Glas")) "5"))
	]
end

Setting the label via tmp.label does not work, even though the item is found (I do see the logInfo line in my log with the items name). I am getting the following error:

2018-02-22 10:40:00.028 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'test': Couldn't invoke 'assignValueTo' for feature JvmVoid:  (eProxyURI: muell.rules#|::0.2.0.2.0.1.7.0.1.0.2::0::/2)

Any idea if there is a way to fix that?

I think I got it to work myself, doing the following:

var DateTimeItem tmp = gNextTrashDate.members.findFirst[name.equals(trash.name.replace('Type', 'Date'))]
tmp.label = trash.state.toString