looping through existing items in a rule

719 views
Skip to first unread message

Jason Brunk

unread,
Jan 20, 2015, 10:42:52 AM1/20/15
to ope...@googlegroups.com
I am looking to try to create a rule based on some other conversations I am having now that will compare some new data coming from an mqtt node to existing items.

I was wondering if there is a way to loop through the items.  

something like

items() = getItems();
for (x=0;x++;x<items.length) 
    if (items(x).name == newinfo)
       do something
    


   

Tobias Bräutigam

unread,
Jan 20, 2015, 11:05:30 AM1/20/15
to ope...@googlegroups.com
If you have a group named 'All' which contains all items (like in the demo.items file) you could do something like:

All?.allMembers.filter(s | s.name.equals(newinfo)).forEach[item|
   
do something
]

Jason Brunk

unread,
Jan 20, 2015, 11:08:54 AM1/20/15
to ope...@googlegroups.com
so i would just need to make sure that all my items going forward i include in that group. or atleast any i need to compare against



--
You received this message because you are subscribed to a topic in the Google Groups "openhab" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/openhab/eC82wArQsFM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to openhab+u...@googlegroups.com.
To post to this group, send email to ope...@googlegroups.com.
Visit this group at http://groups.google.com/group/openhab.
To view this discussion on the web visit https://groups.google.com/d/msgid/openhab/3d94a61a-072c-435c-ab1a-69d20666b10e%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Tobias Bräutigam

unread,
Jan 20, 2015, 11:59:38 AM1/20/15
to ope...@googlegroups.com
You don´t have to include them in the All group directly, it would be enough if they are part of another group, which itself is a member of the All-group.

For example, if your items are grouped in the following structure:
All
|- Subgroup 1
|--Subgroup11
|-Subgroup2
|--SubSubgroup21
|---SubSubSubgroup211

All?.allmembers reaches every item in any of the mentioned groups.

Doug Meredith

unread,
Jan 21, 2015, 7:31:47 AM1/21/15
to ope...@googlegroups.com


On Tuesday, January 20, 2015 at 12:59:38 PM UTC-4, Tobias Bräutigam wrote:

All?.allmembers reaches every item in any of the mentioned groups.

How does "allmembers" differ from "members"? The demo rules user "members", which seems to be broken under Java 8. It appears to iterate over null references:

2015-01-21 08:24:30.151 [ERROR] [m.r.internal.engine.RuleEngine] - Error during the execution of startup rule 'Set random room temperatures': Could not invoke method: org.openhab.model.script.actions.BusEvent.postUpdate(org.openhab.core.items.Item,org.openhab.core.types.State) on instance: null
2

I just tried changing the rules to use "allmembers" and it no longer throws any exceptions.

Doug

Tobias Bräutigam

unread,
Jan 21, 2015, 7:45:53 AM1/21/15
to ope...@googlegroups.com
members just iterates over the items, which are directly assigned to the group. If you want to get items which are members of the subgroups you must use allmembers.
Just take a look at the source:
https://github.com/openhab/openhab/blob/master/bundles/core/org.openhab.core/src/main/java/org/openhab/core/items/GroupItem.java#L65-L91

I don´t know why the you are getting the error you described, both methods return an item list (in the demo both lists should contain the same items, as there are no subgroups in the temperature group)
--
You received this message because you are subscribed to a topic in the Google Groups "openhab" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/openhab/eC82wArQsFM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to openhab+u...@googlegroups.com.
To post to this group, send email to ope...@googlegroups.com.
Visit this group at http://groups.google.com/group/openhab.
Reply all
Reply to author
Forward
0 new messages