Basic rule advice

I am just getting started and having a problem with a basic rule problem.
What I wish to do is send a mqtt alert when a pir sensor is activated and nest is away.
my mqtt pi sensor displays correctly in site map as does nest away.

I am getting a bit frustrated.
my items are

Number  GRLight "Garage Light [MAP(binary.map):%S]"     (All)           { mqtt="<[broker:jpfy/house/sensor/garage/PIR:state:default]" }
Switch  GarageAlarm     "Garage Alarm"  { mqtt=">[broker:jpfy/house/warning/GarageAlarm:command:ON:1],>[broker:jpfy/house/warning/GarageAlarm:command:OFF:0]" }
String  home_away       "Home/Away [%s]"        <present>       (Nest)          { nest="=[structures(Home).away]" }

binary map is:

0=OFF
1=ON
OFF=0
ON=1

And my rule file is

//jpfy rules file v0.01
import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*

// If away from home and motion sensor in garage on send message
var Timer timer = null


rule "garageintruder"
        when
                Item GRLight==1 and
                Item home_away==away
        then
                if(timer==null){
                        sendCommand(GarageAlarm, ON)
                        timer = createTimer(now.plusSeconds(60)
                                }
                         else {
            if(timer!=null) {
                timer.cancel
                timer = null
                sendCommand(GarageAlarm, OFF)
            }
        }
    end

Thank you for any advice as its driving me mad.
Phil

The “when” section of a rule does not support the use of “and” because it’s only meant to be a trigger for when an event occurs. You could instead say something like:

var Timer timer = null

rule "garageintruder"
when
  Item GRLight changed to 1
then
  if (home_away.state == "away") {
    if (timer == null) {
      sendCommand(GarageAlarm, ON)
      timer = createTimer(now.plusSeconds(60) [| sendCommand(GarageAlarm, OFF) ] 
    } else {
      timer.cancel
      timer = null
      sendCommand(GarageAlarm, OFF)
    }
  }
end

Works a treat turning the alarm on but won’t turn it off after timer getting error

[ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'garageintruder': Index: 1, Size: 1

Now sorted it needed an extra ) after 60.

Thank you so much

1 Like