triggeringItem in rule


I’m trying to write a code that checks if my windows are open,
I did make a Group with all my windows in, and it shows OPEN if one window is open, and i can see in the log which window it triggered

But how do I show the item in, it just shows me Panel_Lock that triggered the rule and not the Windows group trigger.

rule "Kontrol af vinduer og døre"
    Item alarm_status changed to Tilkoblet or  Item Panel_Lock changed
   	var String AudioSink
    AudioSink= "sonos:PLAY1:RINCON_949F3E72CEE801400"
    var String SayText1
	var String SayText2
	var String SayText3    
    SayText1="Jeg kontrolere lige om døre og vinduer er lukket"
   // say(SayText1,null,AudioSink)
 	logInfo("Kontrol_af_Døre_og_vinduer", "SayText: {}",SayText1)
 	//Is windows group open?
    if (Windows.state == OPEN) 
    	{ + " Står åbent"
        logInfo("Kontrol_af_vinduer", "SayText: {}",SayText2)
      //  say (triggeringItem.label.toString + SayText2,null,AudioSink)
    		SayText3="Ingen vinduer er åbne"
    		logInfo("Kontrol_af_vinduer", "SayText: {}",SayText3)

Hi, @zamzon. You may want to consider using a Member of <group> changed trigger.

Yes I have considered that, but I cant see how to run my rule with this trigger
The rule should start when I arm my alarm, and not when i group is OPEN

The trigger you have now triggers if your alarm_status item changes state or if the Panel_lock item changes state. To me, that isn’t checking the state of Panel_lock, rather it is reporting changes in its state.

If alarm_status triggered the rule, will be the string “alarm_status”. When Panel_lock changes state, will be the string “Panel_lock”. To get the individual window contact item that caused Panel_lock (I assume that is the group to which all of your window contacts belong) to change state, you will have to use some form of the Member of <group> changed trigger type, or create a list of Item changed triggers with the or operator between each, where each of your window contact items is listed, like this:

when    Item Window_1 changed
     or Item Window_2 changed
     or Item Window_3 changed

I’m afraid I am a bit unclear about what, exactly, your rule is supposed to do. A more detailed description of what you want it to do would help me understand.

My Item Panel_lock is just for testing the rule, the alarm_status is my alarm, so when the alarm_status changes to Tilkoblet (Armed), the rule should check the group Windows if it is in OPEN state.
If so, it should say which window is open.
It no windows are open it should say no windows are open.

The logInfo is only for testing, i will uncomment the say code when the rule works.

Sorry if I have been unclear before.

That explanation helped a lot! Now I have a much better notion of what you are trying to accomplish.

In words, what your rule should do to determine which windows are open, is filter the members of your Windows group item to build a set of all open windows. This technique and others are discussed at length in @rlkoshak’s Design Pattern: Working with Groups in Rules post:

1 Like

I am not quite sure what you mean.
Can you give an example?

Your original problem is that the implicit variable triggeringItem only relates to the item that triggered your rule, not arbitrary items.

The following snippet embedded into your rule is pasted (nearly verbatim) from the post I referenced:

if (Windows.state == OPEN)
    val StringBuilder sb = new StringBuilder
    Windows.members.filter[ w | w.state == OPEN].forEach[ow | sb.append(", " + ]
    sb.append(" Står åbent")
    val String SayText2 = sb.toString()
    logInfo("Kontrol_af_vinduer", "SayText: {}", SayText2)
    //  say (SayText2,null,AudioSink)

This obviously doesn’t correct the grammar for one versus more than one open window.

Thanks a lot, I will test it later today,

There is many new thinks in this rule a never seen before, so I will try to read the code to understand it.


It works as it should
thanks again.