Rule with email notification when one (or more) item in a groupe changes

Hi there I need some help with setting up some rules. My goal: When the item Presence is “Off” I will receive an email, when one of my windows opens.
I have set up an item “Presence”. When I sitch it to off I already receive an email - this rule works fine.
But the following rule does not work and I cannot find a clue in the logs (see below):

    rule "Fenster Abwesend"
    when
        Member of Fenster changed to OPEN and
        item Presence is OFF
    then
        sendMail("mymail@mydomain",triggeringItem.label+" wurde geöffnet !","Hallo Andreas, "triggeringItem.label+" wurde geöffnet !")
    end

2019-04-25 15:09:38.496 [ome.event.ItemCommandEvent] - Item ‘Presence’ received command OFF
2019-04-25 15:09:38.497 [vent.ItemStateChangedEvent] - Presence changed from ON to OFF
2019-04-25 15:11:04.627 [vent.ItemStateChangedEvent] - HMSecSCoNEQ1481824_1_State changed from CLOSED to OPEN
2019-04-25 15:19:04.456 [vent.ItemStateChangedEvent] - HMSecSCoNEQ1481824_1_State changed from OPEN to CLOSED

Rules are triggered by events. You cannot have and conditions in rule triggers, because that would require two things to happen at the exact same instant. That’s why you don’t see any examples or documentation with that and construction.
Because rule triggers are events, not states, there is likewise no is syntax.

You should find an eror message about the rule in openhab.log, not events.log.

What you’ll want to do is trigger from the change and then check any state conditions.using if() in the rule body.

You can try this. On this way you can avoid the and command.

when 
Member of Fenster changed to Open 

then 
if(item Presence.state == OFF) {

sendMail("mymail@mydomain",triggeringItem.label+" wurde geöffnet !","Hallo Andreas, "triggeringItem.label+" wurde geöffnet !")
}

end

Thanks for your help! But unfortunately I got this error in my logs:

2019-04-26 08:11:36.190 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘Benachrichtigung_Fenster_Abwesend.rules’ has errors, therefore ignoring it: [6,9]: missing ‘)’ at ‘Presence’
[6,30]: mismatched input ‘)’ expecting ‘end’

This is my Rule:

rule "Fenster Abwesend"

when Member of Fenster changed to Open 

then 
if(item Presence.state == OFF) {
    sendMail("mymail@mydomain",triggeringItem.label+" wurde geöffnet !","Hallo Andreas, "triggeringItem.label+" wurde geöffnet !")
}

end

I do not understand the log. In fact there is a “)” at Ln6/Col30

Remove item in the if condition

And add a + between "Hallo Andreas, " and triggeringItem.label, so in total like this:

rule "Fenster Abwesend"
when 
    Member of Fenster changed to Open
then 
    if(Presence.state == OFF) {
        sendMail("mymail@mydomain",triggeringItem.label+" wurde geöffnet !","Hallo Andreas, " + triggeringItem.label + " wurde geöffnet !")
    }
end

Thanks all for your help - unfortunately it is still not working. I have the rule in place, did change presence to off, openend two windows: noting happend andn no clue in the logs:
2019-05-02 11:58:07.545 [vent.ItemStateChangedEvent] - Presence changed from ON to OFF
2019-05-02 11:58:28.826 [vent.ItemStateChangedEvent] - HMSecSCoOEQ0391526_1_State changed from CLOSED to OPEN
2019-05-02 11:58:57.556 [vent.ItemStateChangedEvent] - HMSecSCoOEQ0200341_1_State changed from CLOSED to OPEN
2019-05-02 11:59:10.733 [vent.ItemStateChangedEvent] - HMSecSCoOEQ0391526_1_State changed from OPEN to CLOSED
2019-05-02 11:59:20.289 [vent.ItemStateChangedEvent] - HMSecSCoOEQ0200341_1_State changed from OPEN to CLOSED
2019-05-02 11:59:34.186 [ome.event.ItemCommandEvent] - Item ‘Presence’ received command ON
2019-05-02 11:59:34.188 [vent.ItemStateChangedEvent] - Presence changed from OFF to ON

any suggestions?

In order to check wether the rule is triggered correctly put a logInfo line into the if clause.
Additionally check all Quotation marks if they are the straight ones and not the tilted ones.

Hi Jürgen,
how do I put a logInfo line into the if clause?
I do not think it is something related with the quatation marks. I use this for sending an info and it is working fine:

rule "Abwesend Benachrichtigung"
when
  Item Presence changed to OFF
then
  sendMail("mymail@mail.com", "Neuer Status: Abwesend", "Hallo Andreas, der Status ist auf Abwesend gesetzt.")
end

Try it like this. You should get two log-entries. The first just states the rule has triggered and all conditions are met, the second shows if the myText variable is composed correctly.

rule "Fenster Abwesend"
when 
    Member of Fenster changed to Open
then 
    if(Presence.state == OFF) {
       logInfo("Fenster Abwesend", "Rule triggered!")
       var myText=triggeringItem.label+" wurde geöffnet !","Hallo Andreas, " + triggeringItem.label + " wurde geöffnet !"
       logInfo("Fenster Abwesend", "myText: {}", myText)

        sendMail("mymail@mydomain",myText)
    }
end
1 Like

Now I have an Error log:

2019-05-02 13:57:48.866 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘Benachrichtigung_Fenster_Abwesend.rules’ has errors, therefore ignoring it: [8,56]: mismatched input ‘,’ expecting ‘}’
[12,5]: extraneous input ‘}’ expecting ‘end’

which means:

rule “Fenster Abwesend”

when Member of Fenster changed to Open

then
if(Presence.state == OFF) {
logInfo(“Fenster Abwesend”, “Rule triggered!”)
var myText=triggeringItem.label+" wurde geöffnet !",“Hallo Andreas, " + triggeringItem.label + " wurde geöffnet !”
logInfo(“Fenster Abwesend”, “myText: {}”, myText)

sendMail("andreas@luft-net.de",myText)
}

end

Erroer here: “,”
and:

Try it with this line:
var myText=triggeringItem.label+" wurde geöffnet ! Hallo Andreas, " + triggeringItem.label + " wurde geöffnet !”

Note also that sendMail expects (at least) three arguments, I think you’ll need to make separate strings for subject line and content.