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"
when
Member of gBeregnung received command
then
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
]
}
end
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.