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

Hi!

I try to set up a habpanel and show the current tv channel.
unfortunately my tv is not able to send out a string command but only on/off for single channels.
so i created a group “GrSender” that includes all of my channels.
Only one item of that group can be ON (the current channel you are watching).
Now i am looking for a way to plot the comment of the item that is ON into a string variable to display it in habpanel.
i hope it is clear what i am trying to achieve :slight_smile:
Items file:

Group:Switch:OR(ON,OFF)     GrSender        "TV Sender"

Switch TV_Sender_ZDF        "ZDF"                <television> (GrSender) ["Switchable"] {channel="sony:scalar:abfd6a30-96f2-4f01-a93e-942496750b50:avContent-content-tv-tv__3dvbs-0001-cn_status"}
.
.
.

take a look here, you’re searching for "MyGroup.members.filter[i | <condition> ]: Return a Set of all the members that meet the provided "

in your case something like GrSender.members.filter[i | i.state==ON ]

1 Like

Hi!

I finally had time to try it. :slight_smile:
Very helpful link! Helps me with a lot of other stuff as well.
I am not sure how to do it. I understand the filter and i think it should result in the item that is ON.
Now the next problem: I do now have the item that’s on but i want to display the text of the label of that item.

i thought about posting the label to a sting var but i have no idea how to do that … ;-(
And i don’t know if my rule fires:

rule "Post Sender Name to String"
when 
	Item GrSender received update
	or 
	Item GrSender received command
then
	TV_String_Kanal.postUpdate(GrSender.filter[i|i.state==ON])
end

because i am looking on update or command change. does the group receive an update when one item is turned on but at the same time (is this even possible) another one is turned off? I configured the group as
Group:Switch:OR(ON,OFF).

Thank you for your support so far! :slight_smile:

I’ve posted an example here:

maybe this will help you.

1 Like

Thank you! This will for sure help to trigger the rule :slight_smile:
But i still can’t find a solution for transferring the label of the item that is ON to the string-var.

not sure understood you right:

String ChannellOnTv “Testmessage [%s]”

val triggeringTVchannel = GrSender.members.findFirst[name.equals(triggeringItem.name)] as GenericItem
ChannellOnTv.label = triggeringTVchannel.label

1 Like

.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