String to Item and than change the label of the Item

look at my rules:

val bmitem = "KNX_BM_"+splititem+"_ist"
val mybmitem = gBMTermostate.members.findFirst[ i | == bmitem ]
sendLogNotification("my BM Item:\n" + mybmitem,"radiator","Debug")

So far everything is ok, but after then I try this:

mybmitem.label = "Hallo Welt!"

Why it isn’t possible to change the label like this?

mybitem.label, which is the same as my item.getLabel, gets the label. To set it, use…

mybmitem.setLabel("Hallo Welt!")

You should also look into using ScriptServiceUtil instead of findFirst…

I got this log Notification:

Rule 'Raumtemperatur ändert sich': 'setLabel' is not a member of 'org.eclipse.smarthome.core.items.Item'; line 13, column 2, length 65

How does your line 13 and the lines before and after look like ?

I made a little test with this Rule

rule "change item- name"
    Item Dummy4 changed to ON
  val vRadiator ="_Temp","")
  logInfo("change item- name","Name: {}", vRadiator)
  val vTriggerItem ="4","_new").replace("Dummy","Flummy")
  logInfo("change item- name","Name: {}", vTriggerItem)

and used the syntax Scott advised (see Line next to last). And for me it works. But you have to know that your sitemap will nt change directly. You have to refresh it.


I tried this.
And again this notification:

Rule 'Raumtemperatur ändert sich': 'setLabel' is not a member of 'java.lang.String'; line 21, column 2, length 30

In line 21:

bmitem.setLabel("Hallo Welt!")

Hole code in rules:

rule "Raumtemperatur ändert sich"
	Member of gTTermostate changed
val bmitem ="_T_","_BM_")
sendLogNotification("my BM Item:\n" + bmitem,"radiator","Debug") //all right

bmitem.setLabel("Hallo Welt!") //line 21


You are getting the rror because bmitem is not an Item but a String. setLabel is a method of GenericItem. You first need to get the Item (see may second post :wink:).

This is another example of how scripted automation with Jython is easier to use than the DSL…

from core.rules import rule
from core.triggers import when

@rule("Raumtemperatur ändert sich")
@when("Member of gTTermostate changed")
def example_rule(event):
    bmitem = event.itemName.replace("_T_","_BM_")
    sendLogNotification("my BM Item:\n" + bmitem,"radiator","Debug")
    itemRegistry.getItem(bmitem).setLabel("Hallo Welt!")
1 Like

As Scott said you are dealing with a simple variable(primitive) not with an item and so there’s no method “setLabel” for simple variables.

First to say, it is easier to use Jython an you have a lot more possibilities. I have migrated all my DSL-Rules to Jython and using scripted automation (NGRE/Jython).

So looking on your problem, the DSL-Rule could look:

import org.eclipse.smarthome.model.script.ScriptServiceUtil

rule "change item- name1"
    Item Dummy4 changed to ON
  val vTriggerItem ="4","5")  // find Triggering item name (Dummy4)and change it as you want (Dummy5)
  logInfo("change item- name1","Name: {}", vTriggerItem)
  var GenericItem bmitem = ScriptServiceUtil.getItemRegistry.getItem(vTriggerItem) as GenericItem   // now get the new Item as Generic
  bmitem.setLabel("Hello World")  // and change the label
  logInfo("change item- name1","Name: {}", bmitem)


and the log:

2020-08-21 22:29:34.100 [INFO ] [home.model.script.change item- name1] - Name: Dummy5
2020-08-21 22:29:34.104 [INFO ] [home.model.script.change item- name1] - Name: Dummy5 (Type=SwitchItem, State=OFF, Label=Hello World, Category=null, Groups=[gPower])

BTW: I learned this from Scott :wink: