[CAN'T DO] How to get an item's "text label" programmatically within rule?

How can I access an item’s text label from within a rule?
Example: the .items file has this:

Group gMyGroup “my group” (All)

Now in a rule I want to do this:

rule “test rule”
when
Item gMyGroup updated
then
say(gMyGroup.getTextLabel() + " was updated")
end

Problem is that there is no .getTextLabel() on a GroupItem (or any item AFAIK). How can the text label be accessed from within a rule?

Thanks,

Bernd

You can’t. :frowning:

The only thing you can get is the Item’s name.

O.k., thanks. Now I wonder if I should mark this question as [SOLVED] or [CAN’T DO]…

I’m not sure how to do that either. I don’t see anything in my menus.

I’ll edit the topic subject line, done.
Thanks again for clarifying this. I looked at the source code but couldn’t find it anywhere. I thought that some components (e.g. the GUIs) are able to access it somehow, but it’s definitely not clear how to do it from within a rule

This feature would be really helpful in many cases - e.g. when notifying someone/something. Why would the sitemaps/GUI have exclusive access to this information? It is even specified in the .items files (as well os the icon). I reckon the label should probably be readOnly due to context issues see this discussion.

The workaround is hard coding the item label string in the rule for every single item, meaning you now have two places to maintain the same string.

Also take the example of an item group of some kind of status (temperature, battery level etc.) at different rooms/locations. It would be handy to be able to make a single rule, iterating through the items and notifying someone (using item label) on some criteria.

This leads me to another nice-to-have: The possibility to make a rule which is executed upon a change to one of its members and additionally giving access to the specific member.

BR
Claus

An added complicatio is which label would be available in the rule, the one defined in the Items file or the one on the sitemap?

You can iterate through all the items of a group and do something now, you just dont have access to the label. For example:

gMyGroup.members.filter[i|i.state==ON].forEach[i|
   ... Do stuff with i
] 

So if you used your workaround you could lookup the hard coded label in a hashmap in the rule or build the label here.

With some caveats, you can trigger a rule and get the member that triggered.

Thread::sleep(100)
val triggered = gMyGroup.members.sortBy[lastUpdate].last

The caveats are:

  • you have to trigger the rule on Item myGroup received update or you need to trigger on the items individually
  • received update on a group will trigger the rule multiple times for each update to one of its members
  • you must have persistence on the items in the group for lastUpdate to work
  • you have to sleep to give persostence time to save the latest value. The amount of time depends on your system
  • it will not work if events take place too close together (e.g. Closer than the sleep) as the new event could override the older one.

Thanks for your suggestions.

I do not agree. To me it is clear that it should be the one specified on the item (if specified). The same goes for the icon. The sitemap is context specific and just a presentation that may or may not override the item label. Items can exist in many sitemaps as well.

Yes, it can be done by work-arounds, but it is not a pretty solution. My point being that internally the group update is initiated by an item, so the information is available already. Just need to be forwarded as a variable on the group item (eg. lastUpdatedItem)