String to Item and than change the label of the Item

Hello,
look at my rules:

val bmitem = "KNX_BM_"+splititem+"_ist"
val mybmitem = gBMTermostate.members.findFirst[ i | i.name == 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"
  when
    Item Dummy4 changed to ON
  then
  val vRadiator = radiator_valve_07_Temp.name.replace("_Temp","")
  logInfo("change item- name","Name: {}", vRadiator)
  val vTriggerItem = triggeringItem.name.replace("4","_new").replace("Dummy","Flummy")
  logInfo("change item- name","Name: {}", vTriggerItem)
  Dummy4.setLabel("Test4")
end

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.

2 Likes

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"
when
	Member of gTTermostate changed
then
val bmitem = triggeringItem.name.replace("_T_","_BM_")
sendLogNotification("my BM Item:\n" + bmitem,"radiator","Debug") //all right

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

end

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"
  when
    Item Dummy4 changed to ON
  then
  val vTriggerItem = triggeringItem.name.replace("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)
end

result:

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:

2 Likes