Get Group members which state is ON

Dear Community!

I want to have a rule which will send me a group items based on some criteria which state is on.
Now I have this:

val ZonesOn = Zones.members.filter [ i | i.state == "ON" ] as StringItem

However I get a cast error:

2018-07-22 20:37:10.668 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Triggered Zone': Could not cast [Zone7 (Type=StringItem, State=ON, Label=Bejárati ajtó, Category=switch, Groups=[Zones])] to org.eclipse.smarthome.core.library.items.StringItem; line 9, column 18, length 63

What I’m doing wrong? Also, it can happen sometimes that more than 1 member is ON. So I want to concatenate to the end of a string. How this can be done?

Thanks!

val ZonesOn = Zones.members.filter [ i | i.state == ON ].map[ name ].reduce[ s, name | s + ", " + name ]
logInfo("___",ZonesOn)

Thanks, I’ll try that!

The error is gone :slight_smile: However this only returns ‘null’ even when I can clearly see that there is members which state is ON. Can you help me with this a little?

Post the Intem-definitions and the log-entry please.

Items:

Group:Switch:OR(ON, OFF) AlarmZones "Alarm zones [%s]" <motion>

String Zone1 "Előszoba [%s]" <switch> (AlarmZones) {mqtt="<[mosquitto:Paradox/Zone/Bejarati infra:state:REGEX(((ON)|(OFF)).*)]"}
String Zone2 "Réka szobája [%s]" <switch> (AlarmZones) {mqtt="<[mosquitto:Paradox/Zone/Haloszoba infra:state:REGEX(((ON)|(OFF)).*)]"}
String Zone3 "Hálószoba [%s]" <switch> (AlarmZones) {mqtt="<[mosquitto:Paradox/Zone/Udvari bal infr:state:REGEX(((ON)|(OFF)).*)]"}
String Zone4 "Nappali [%s]" <switch> (AlarmZones) {mqtt="<[mosquitto:Paradox/Zone/Nappali infra:state:REGEX(((ON)|(OFF)).*)]"}
String Zone5 "Kristóf szobája [%s]" <switch> (AlarmZones) {mqtt="<[mosquitto:Paradox/Zone/Udvari jobb inf:state:REGEX(((ON)|(OFF)).*)]"}
String Zone6 "Teraszajtó [%s]" <switch> (AlarmZones) {mqtt="<[mosquitto:Paradox/Zone/Nappali nyitas:state:REGEX(((ON)|(OFF)).*)]"}
String Zone7 "Bejárati ajtó [%s]" <switch> (AlarmZones) {mqtt="<[mosquitto:Paradox/Zone/Bejarati ajto:state:REGEX(((ON)|(OFF)).*)]"}

String ArmState "Arm state" <alarm> {mqtt=">[mosquitto:Paradox/C/P1/Arm:command:arm:''],>[mosquitto:Paradox/C/P1/Disarm:command:disarm:''],<[mosquitto:Paradox/Events:state:REGEX(.*;SubEvent\\: Bell squawk (.*)):Event\\:Bell status.*]" }
String ArmCurrState { mqtt=">[mosquitto:Paradox/Events:state:REGEX(.*;SubEvent\\: Bell squawk (.*)):Event\\:Bell status.*]" }

Rule

rule "Triggered alarm"
when
	Item AlarmZones changed
then
	if(ArmState.state.toString == "arm"){
		if(AlarmZones.state == ON) {
			val ZonesOn = AlarmZones.members.filter[ i | i.state == ON ].map[ name ].reduce[ s, name | s + ", " + name]
			sendTelegram("bot1","Alarm in zone(s): " + ZonesOn)
		}
	}
end

There is nothing related to this in the log…

The group-members must be type Switch.
You can change to

rule "Triggered alarm"
when
	Item AlarmZones changed to ON
then
	if(ArmState.state == "arm"){
		val ZonesOn = AlarmZones.members.filter[ i | i.state == ON ].map[ name ].reduce[ s, name | s + ", " + name]
		sendTelegram("bot1","Alarm in zone(s): " + ZonesOn)
	}
end

So if I use this, I don’t have to change all the items to Switch type?

No, you have to change, if you would use the group as switch.

PS:You get the trigger ON only once, if the first member change to ON, not if a second change to ON.

Thanks, I’ll try that! I think this is not really a problem that it will only trigger once…

You can try this

Group:Switch AlarmZones "Alarm zones [%s]" <motion>
Switch Zone1 "Előszoba [%s]" <switch> (AlarmZones) {mqtt="<[mosquitto:Paradox/Zone/Bejarati infra:state:REGEX(((ON)|(OFF)).*)]"}
Switch Zone2 "Réka szobája [%s]" <switch> (AlarmZones) {mqtt="<[mosquitto:Paradox/Zone/Haloszoba infra:state:REGEX(((ON)|(OFF)).*)]"}
Switch Zone3 "Hálószoba [%s]" <switch> (AlarmZones) {mqtt="<[mosquitto:Paradox/Zone/Udvari bal infr:state:REGEX(((ON)|(OFF)).*)]"}
Switch Zone4 "Nappali [%s]" <switch> (AlarmZones) {mqtt="<[mosquitto:Paradox/Zone/Nappali infra:state:REGEX(((ON)|(OFF)).*)]"}
Switch Zone5 "Kristóf szobája [%s]" <switch> (AlarmZones) {mqtt="<[mosquitto:Paradox/Zone/Udvari jobb inf:state:REGEX(((ON)|(OFF)).*)]"}
Switch Zone6 "Teraszajtó [%s]" <switch> (AlarmZones) {mqtt="<[mosquitto:Paradox/Zone/Nappali nyitas:state:REGEX(((ON)|(OFF)).*)]"}
Switch Zone7 "Bejárati ajtó [%s]" <switch> (AlarmZones) {mqtt="<[mosquitto:Paradox/Zone/Bejarati ajto:state:REGEX(((ON)|(OFF)).*)]"}
rule "Triggered alarm"
when
	Item AlarmZones received update
then
logInfo("___1",ArmState.state +" "+ AlarmZones.state.toString)
	if(ArmState.state == "arm"){
		val ZonesOn = AlarmZones.members.filter[ i | i.state == ON ].map[ name ].reduce[ s, name | s + ", " + name]
logInfo("___2",ZonesOn)
		if (ZonesOn === null) {logInfo("___3","=== null")}
//		sendTelegram("bot1","Alarm in zone(s): " + ZonesOn)
	}
end

Thanks this works really well! However the item names returned (Zone1, etc…). Is it possible to map this value to something else? Can I return the label of the item?

val ZonesOn = AlarmZones.members.filter[ i | i.state == ON ].map[ label ].reduce[ s, label | s + ", " + label]
1 Like