[SOLVED] Determine which Item of a Group is switched ON

.label was what i was looking for …

Inside your findfirst filter i just need to find the first item that’s on so i wrote:
…findFirst[state==ON] as GenericItem
but it’s not working. I’ll try a bit :wink: thanks!

try something like this, with an additional filter for the state==ON. I’m not at home to test it.

        gMAPDB?.members.filter[name.equals(triggeringItem.name + "_Number")].forEach[light |
            logInfo("Logger","light: " + light.name)
        ]

This will get the “ZDF” of your item name if it is: TV_Sender_ZDF

triggeringItem.name.split("_").get(2)

Provided you use the same name pattern for your item you can then get the “name”

There is no way to get the item label as these are variable in content and format:
example: “Garage Door [%1$tm/%1$td %1$tH:%1$tM]”

Hi!

i made some progress!
The filter works and when i use the log of your example and combining it with the additional filter of vzorglub it just plots the name - great so far! :slight_smile:
but i am not able to store the filtered name of the val into a string.
sorry, i’m still a noob :frowning:
my code:

rule "Post Sender Name to String"
	when 
		//Item GrSender received update
		//or 
		//Item GrSender received command
				Time cron "0/1 * * * * ? *"

	then
		val triggeringTVchannel = GrSender?.members.filter[i|i.state==ON].forEach[j|
		logInfo("Logger","Kanal: " + j.name.split("_").get(2))]
		TV_String_Kanal.postUpdate(triggeringTVchannel.name.toString)
end

another question:
Where did you learn that stuff - i mean that split command for example.
For the items. I mostly used Item.state. is there really a command for Item.name and Item.label? Where can i read about that? Or is it just valid inside the filters?

Thank you so much!

Change your rule as follow:

rule "Post Sender Name to String"
	when 
		//Item GrSender received update
		//or 
		//Item GrSender received command
				Time cron "0/1 * * * * ? *"

	then
		val triggeringTVchannel = GrSender?.members.filter[i|i.state==ON].forEach[j|
		logInfo("Logger","Kanal: " + j.name.split("_").get(2))]
                val channelName = j.name.split("_").get(2)
		TV_String_Kanal.postUpdate(channelName)
end

I learned the same way as you as doing now, make rules and lots of mistakes
Looking in the community and docs and asking questions…
And reading the threads
Keep at it and you will be help someone one day

item.state and item.name as called properties (I think) not commands and return a value from the item
item.postUpdate or item.sendCommand are methods (like a function) and tell the item for do something
They are available from the openHAB docs
Unfortunately there is not label property for the reason given above

this means, you make a static variable (that’s val) and fill it with your logInfo Statement (that’s what you do with the forEach[...] syntax. That’s rubbish - I’m certain you’ll have some entries in your logs for this one!
If you’d like to have your channel logged and update your item:

		GrSender?.members.filter[i|i.state==ON].forEach[j|    // this is just a wrapper, it filters the member-items for stat==ON and make a forEach-statement for every item, which is ON
		 	var String triggeringTVchannel =  j.name.split("_").get(2)     // writing the split-stuff in a _variable_
			logInfo("Logger","Kanal: " + triggeringTVchannel)              // logging that one
			TV_String_Kanal.sendCommand(triggeringTVchannel)               // updating the item (I guess, it's a Proxy item, so sendCommand is best) - as it is already a String, you want Need `toString` and as this one is a simple variable, you don't Need some Argument (which would give you the Name of the variable back, not the value nevertheless... ?)
		]

PS: Two caveats with this:

  1. if for some reason your ON-item.name doesn’t contain two “_” - the split will terminate with an ERROR
  2. the variable “i” or “j” are created while the wrapper for “GrSender” is filled, so they only exist for filtering (i) and for the forEach-condition (j).

PS: how you learn that stuff? Getting a “programming for Dummies”-book or working intensively with existing Code and try to understand it! :wink:

A programming book for dummies is a good idea. Go for it.

I started 35 years ago copying manually printed BASIC (the language) programs into my father’s first PC at his work (256k memory and 1 floppy drive. When you stated the computer you had to put the OS floppy in first and then when loaded you could put you own 5 inch floppy for your own stuff… Great times)

There are also a lot of tutorials online.
Start with Python and/or JavaScript

2 Likes

64K Memory, 985 kHz and already a floppy drive! :blush:
but changed quickly on a 128K Memory and 2MHz powerhorse! :smile:

1 Like

Hi!
Is this working with a present version of openhab now?

It should be

1 Like

the “dirty” workaround posted above still works :wink:
I was wondering if i can now just use:

Item Group received update

then

do sth with the triggeringItem

(I tried and it did not work.)

EDIT:
Please pardon my ignorance … I am trying too hard right now :wink: i just found this in the docs and will give it a try. i’ll report back …

Member of <group> received command [<command>]

REEDIT:
Yes, it works :slight_smile:
just that you know how it looked before :wink: :slight_smile:

You’ll have realized by now that triggeringItem would be the group, not whichever Item that caused the group to update. That’s working as intended.

For Member of group, the triggeringItem is the member Item, not the group.

2 Likes

Just further to this, I have a rule which switches off a group of lights. Rather than doing that, and potentially switching devices already off, to off, which seems like a waste of resources on the zwave network how would I be able to switch off, only members of the group which are switched on?

I saw Vincent’s post about

members.filter[i|i.state==ON].forEach[j|

However I’m wanting to use an IF statement, if if member of group is on, send it off.

Cheers

That’s what the filter does, gives you a list of members with state ON. Be aware it may be empty.

I know that, thats why I posted it. But that doesnt really help because the filter is being used with a variable. Im trying to use it for an if statement.

You would use a filter to select only members of the group with state ON, and then send each of those an off command, If you must include the keyword IF, you could always test their state again to be really really sure that the filter coulddo it on it’s own.

Thinks; is it the i’s and j’s here causing trouble?

myGroup.members.filter[i | i.state==ON]

Okay, here we filter any members of the group. We refer to each member as “i”, it’s arbitrary could be “banana”, then test the state of i to see if it is ON. The results go into a list (which might be empty).
We don’t do anything with that list yet.

myGroup.members.filter[i | i.state==ON].forEach[ j |
   j.sendCommand(OFF)
]

So we make the same list, then for-each through the list referring to each list entry as “j”

@binderth adds a nice refinement with a ? , which just suppresses any silliness if the group has no members etc.

myGroup?.members.filter[i | i.state==ON].forEach[ j |
   j.sendCommand(OFF)
]
1 Like

Thanks rossko57, a very informative post. I assume I can then do:

gAllLights?.members.filter[Lights | Lights.state==ON].forEach[ LightsON |
if(LightsON.state==ON){
LightsON.sendCommand(OFF)
]
}

You could, but there’s no need since LightsOn.state == ON for each LightsOn… that’s what the filter did for you.

2 Likes

Thanks Scott, that works well. Much cleaner

1 Like