[SOLVED] Set item label with forEach loop for all group members


I have groups of items defined and I would like to adjust the item label from a rule with a forEach loop. Here is the basic concept:

import org.eclipse.smarthome.model.script.ScriptServiceUtil
rule "Set LastRun"
  Member of gBeregnung received command 
var String itemName = triggeringItem.name

if ( triggeringItem.state == ON ) {
  ScriptServiceUtil.getItemRegistry.getItem(itemName + "_LastRun").postUpdate(new DateTimeType())

if ( triggeringItem.state == OFF ) {
  val oldDate = new DateTime((ScriptServiceUtil.getItemRegistry.getItem(itemName + "_LastRun").state as DateTimeType).calendar.timeInMillis)
  val newDate = new DateTime(now())
  val runTime = ( newDate.millis - oldDate.millis ) / 1000
  ScriptServiceUtil.getItemRegistry.getItem(itemName + "_LastRun").postUpdate(new DateTimeType())
  gIrrigationLastRun.allMembers.filter[i | i.name.contains( itemName )].forEach[ lastRun |
    logInfo(filename,"Beregnung: lastRun ->" + lastRun.name.toString + "<- with runTime: ->" + runTime + "<-")
    lastRun.label = runTime + "s - " + itemName.label

When I run this, I get the error message in openhab.log:

2019-07-02 07:20:33.498 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Set LastRun': 'label' is not a member of 'java.lang.String'; line 367, column 44, length 14

When I set the label by referencing the item directly, then it works perfectly well. But this would mean I have to write a huge case block and I would not be flexible in regards to the group members.

What is the best way to get an item by name with the chance to change the label as well?



If you explicitly set the item type there for lastRun does it behave?
Try displaying (logwarn) lastRun.toString and you’ll see what the object type is.


1 Like

You already knew that, you asked for

var String itemName

You want to fetch the label of the item, not the label of its name.
Look at your “foreach”

gIrrigationLastRun.allMembers.filter[i | i.name.contains( itemName )].forEach[

That says get me each item and refer to it as “i” and test the name …

So to operate on item referred to as “i” …

lastRun.label = runTime + "s - " + i.label

Thank you for your support!

@CDriver: item type of lastRun is DateTimeItem. But the thing is, that I want to set the label (what is a string) of that item.

@rossko57: You was absolutely right. My Bad. Sorry for that. I changed line to

lastRun.label = runTime + "s - " + triggeringItem.label

Nevertheless I still get an error message:

2019-07-04 06:44:18.925 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Set LastRun': Couldn't invoke 'assignValueTo' for feature JvmVoid:  (eProxyURI: irrigation.rules#|::

Any other ideas?

My last post was mostly wrong because I got confused about what was to be written where :crazy_face:

This suddenly rang a bell with me, label in forEach
Here is the same problem (without solution)

I now think @CDriver has the answer, within the forEach you should explicitly set type to get access to the undocumented .label method. Try -

gIrrigationLastRun.allMembers.filter[GenericItem i | i.name.contains( itemName )].forEach[GenericItem  lastRun |

Off topic - you may find ‘refresh’ of changed labels a bit poor in any of the UI’s - they don’t expect labels to change.

The GenericItem made the trick!
Thanks @rossko57 and @CDriver!!!

rgd, refresh: for now I can deal with it. I’m too lucky that it works now that I will think too much about it :wink:

Brilliant; nice to have a resolution. Funny how I’d looked at that years ago and never sorted it out.