100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Rules documentation work in progress
Michal Rutkac17b82a2860c
diff --git a/manager/server/src/main/resources/demo/rules/DemoApartmentPresencePrediction.drl b/manager/server/src/main/resources/demo/rules/DemoApartmentPresencePrediction.drlindex bfc5d25fd..140098dc5 100644--- a/manager/server/src/main/resources/demo/rules/DemoApartmentPresencePrediction.drl+++ b/manager/server/src/main/resources/demo/rules/DemoApartmentPresencePrediction.drl@@ -12,10 +12,11 @@ New:// ROOMpresenceDetected // true/false- lastPresenceDetected // Unix timestamp milliseconds+ presenceDetectedInteger // 1 on motion sense, 0 after 2 seconds. This behavior models typical PIR sensor+ lastPresenceDetectedTimeStamp // Unix timestamp milliseconds// RESIDENCE- presencePredication // true/false+ selfLearning // true/falsevacationDays // 0..123homeSceneStartMonday // "17:30:00"@@ -34,10 +35,10 @@ New:Old:- SENSOR_PRESENCE // "on"- PRESENCE_AS_INTEGER // 0, 1+ presenceDetected // "on"+ presenceDetectedInteger // 0, 1PRESENCE_LAST_DETECTED_TIME // "-", hh:mm- PRESENCE_DETECTED_LATCHED // "No", Latches for 15mins+ presenceDetectedUI // "Yes"/"No", Latches for 15mins; translates to somebody/nobody is in the officeVACATION_DAYS // "0"@@ -54,94 +55,91 @@ Old:ACTUAL_TIME_ARRIVAL // "-"ACTUAL_TIME_DEPARTURE // "-"- NEXT_EVENT // "-", Possible values are "-", "Departure", "Arrive"+ currentScene // "-", Possible values are "-", "Departure", "Arrive"+ // in blok61 it is "home", "away", "evening", "night"*/// ##########################################################################################################--rule "move sense"-when- SensorState($s:sensorName=="SENSOR_PRESENCE", value=="on")- SensorState(sensorName=="TIME_HH:MM:SS", $h:value)-then- LOG.info("Move sense on "+$s+" @"+$h);- commands.execute("PRESENCE_LAST_DETECTED_TIME", $h.toString());-end-+// This rule converts any manufacturer dependent movement sensor into standardized PIR behaviour.+// Here emulate PIR from motionCounter by Enervalisrule "person sense"when- SensorState($s:sensorName=="SENSOR_PRESENCE", value=="on")- SensorState(sensorName=="PRESENCE_AS_INTEGER", value!="1")+ AssetEvent(type == AssetType.ROOM, attributeName == "motionCounter", oldValue.asNumber < value.asNumber)+ AssetEvent($id: id, type == AssetType.ROOM, attributeName=="presenceDetectedInteger", value.asNumber!=1)then- commands.execute("PRESENCE_AS_INTEGER", "1");+ LOG.info("@"+(new java.util.Date(drools.getWorkingMemory().getSessionClock().getCurrentTime())).toString());+ assets.dispatch( new AttributeEvent($id, "lastPresenceDetectedTimeStamp", Json .create(1)) );end+// This rule models monostable PIR sensor behaviour, which goes to off state after 2s timeoutrule "no person sense"-timer(int: 2s)-when- SensorState(sensorName=="SENSOR_PRESENCE", value!="on")- SensorState(sensorName=="PRESENCE_AS_INTEGER", value!="0")-then- commands.execute("PRESENCE_AS_INTEGER", "0");-end--rule "Presence"+ timer(int: 2s)when- SensorState(sensorName == "PRESENCE_AS_INTEGER", value == "1")- SensorState(sensorName=="NEXT_EVENT", value=="Departure") // within expected attendance schedule+ AssetEvent(type == AssetType.ROOM, attributeName == "motionCounter", oldValue.asNumber < value.asNumber)+ AssetEvent($id: id, type == AssetType.ROOM, $s:attributeName=="presenceDetectedInteger", value.asNumber!=0)then- commands.execute("PRESENCE_DETECTED_LATCHED","Yes");+ LOG.info("@"+(new java.util.Date(drools.getWorkingMemory().getSessionClock().getCurrentTime())).toString());+ assets.dispatch( new AttributeEvent($id, "lastPresenceDetectedTimeStamp", Json .create(0)) );end-rule "Absence when 30 min no movement"-timer(int: 30m)+// Fetch last time when presence was detected. This time is used for period window adjustment.+rule "last move sense timestamp fetch"when- SensorState(sensorName == "PRESENCE_AS_INTEGER", value == "0")- SensorState(sensorName=="NEXT_EVENT", value=="Departure") // within expected attendance schedule+ AssetEvent($id: id, type == AssetType.ROOM, attributeName=="presenceDetectedInteger", value.asNumber==1, valueChanged)then- commands.execute("PRESENCE_DETECTED_LATCHED","No");+ // commands.execute("PRESENCE_LAST_DETECTED_TIME", $h.toString());+ long currentTime = drools.getWorkingMemory().getSessionClock().getCurrentTime();+ LOG.info("Move sense on "+$id+" @"+(new java.util.Date(currentTime)).toString() );+ assets.dispatch(+ // we want that this time stamp is persistent in memory because periods are quite long and it+ // is likely that a rule engine can restart in this time. Experience learns that this can+ // disturb the prediction time.+ new AttributeEvent($id, "lastPresenceDetectedTimeStamp", Json .create(currentTime))+ );end-rule "Absence after 15 min v02"-timer(int: 15m)+// Set the presence state which was previously used in the UI "Somebody is in the office"+rule "Presence"when- SensorState(sensorName == "PRESENCE_AS_INTEGER", value == "0")+ AssetEvent(type == AssetType.ROOM, $s:attributeName=="presenceDetectedInteger", value.asNumber==1)+ AssetState(type == AssetType.RESIDENCE, attributeName=="currentScene", value=="Departure") // within expected attendance schedule+ AssetState($id: id, type == AssetType.RESIDENCE, attributeName=="presenceDetectedUI", value.asString!="Yes")then- commands.execute("PRESENCE_DETECTED_LATCHED","No");+ assets.dispatch( new AttributeEvent($id, "presenceDetectedUI", Json .create("Yes")) );end-rule "Absence after scheduled presence time"+rule "Absence after 15 min"+ timer(int: 15m)when- SensorState(sensorName=="NEXT_EVENT", value!="Departure") // outsidse expected attendance schedule+ AssetEvent($id: id, type == AssetType.ROOM, $s:attributeName=="presenceDetectedInteger", value.asNumber==0)then- commands.execute("PRESENCE_DETECTED_LATCHED","No");+ assets.dispatch( new AttributeEvent($id, "presenceDetectedUI", Json .create("No")) );end-// ##########################################################################################################-+/*rule "Move ETA to ET at midnight"when- SensorState(sensorName=="TIME_DAY_NAME_IN_WEEK", $e:value)- SensorState(sensorName=="TIME_HH:MM:SS",$h:value,eval(util.parseTimestamp(value) >= util.parseTimestamp("00:02"))) // Wait 2 minutes to be sure that $e is OK- SensorState(sensorName matches ("UI_ESTIMATED_TIME_ARRIVAL."+$e), eval(util.parseTimestamp(value) > util.parseTimestamp($h)), $va:value)- SensorState(sensorName matches ("UI_ESTIMATED_TIME_DEPARTURE."+$e), $vd: value)- SensorState(sensorName=="NEXT_EVENT", value=="-")- SensorState(sensorName=="VACATION_DAYS", eval(Integer.parseInt(value.toString())==0))+ SensorState(attributeName=="TIME_DAY_NAME_IN_WEEK", $e:value)+ SensorState(attributeName=="TIME_HH:MM:SS",$h:value,eval(util.parseTimestamp(value) >= util.parseTimestamp("00:02"))) // Wait 2 minutes to be sure that $e is OK+ SensorState(attributeName matches ("UI_ESTIMATED_TIME_ARRIVAL."+$e), eval(util.parseTimestamp(value) > util.parseTimestamp($h)), $va:value)+ SensorState(attributeName matches ("UI_ESTIMATED_TIME_DEPARTURE."+$e), $vd: value)+ SensorState(attributeName=="currentScene", value=="-")+ SensorState(attributeName=="VACATION_DAYS", eval(Integer.parseInt(value.toString())==0))thencommands.execute("UI_ESTIMATED_TIME_ARRIVAL", $va.toString());commands.execute("UI_ESTIMATED_TIME_DEPARTURE", $vd.toString());- commands.execute("NEXT_EVENT", "Arrive");+ commands.execute("currentScene", "Arrive");endrule "Move ETA ETD for current day"when- SensorState(sensorName=="TIME_DAY_NAME_IN_WEEK", $e:value)- SensorState(sensorName matches ("UI_ESTIMATED_TIME_ARRIVAL."+$e), $va:value)- SensorState(sensorName matches ("UI_ESTIMATED_TIME_DEPARTURE."+$e), $vd: value)+ SensorState(attributeName=="TIME_DAY_NAME_IN_WEEK", $e:value)+ SensorState(attributeName matches ("UI_ESTIMATED_TIME_ARRIVAL."+$e), $va:value)+ SensorState(attributeName matches ("UI_ESTIMATED_TIME_DEPARTURE."+$e), $vd: value)thencommands.execute("UI_ESTIMATED_TIME_ARRIVAL", $va.toString());commands.execute("UI_ESTIMATED_TIME_DEPARTURE", $vd.toString());@@ -149,69 +147,69 @@ endrule "reset ACTUAL_TIME_ARRIVAL"when- SensorState(sensorName=="NEXT_EVENT", value=="Arrive")+ SensorState(attributeName=="currentScene", value=="Arrive")thencommands.execute("ACTUAL_TIME_ARRIVAL","-");endrule "reset ACTUAL_TIME_DEPARTURE"when- SensorState(sensorName=="ACTUAL_TIME_ARRIVAL",value=="-")+ SensorState(attributeName=="ACTUAL_TIME_ARRIVAL",value=="-")thencommands.execute("ACTUAL_TIME_DEPARTURE","-");endrule "reset action at midnight"when- SensorState(sensorName=="TIME_HH:MM:SS", value!="N/A", eval(value.toString().substring(0,5).equals("00:00"))) // more reliable than timer(cron: )+ SensorState(attributeName=="TIME_HH:MM:SS", value!="N/A", eval(value.toString().substring(0,5).equals("00:00"))) // more reliable than timer(cron: )then- commands.execute("NEXT_EVENT", "-");+ commands.execute("currentScene", "-");endrule "Move ETD to ET at arrive init"// This one is needed for cases when system is restart after arrive timewhen- SensorState(sensorName=="TIME_DAY_NAME_IN_WEEK",$e:value)- SensorState(sensorName=="TIME_HH:MM:SS",$h:value)- SensorState(sensorName matches ("UI_ESTIMATED_TIME_ARRIVAL."+$e), eval(util.parseTimestamp(value) <= util.parseTimestamp($h)), eval(value.toString().length()==5))- SensorState(sensorName matches ("UI_ESTIMATED_TIME_DEPARTURE."+$e), $vd:value, eval(value.toString().length()==5))-// SensorState(sensorName=="NEXT_EVENT", value=="-")+ SensorState(attributeName=="TIME_DAY_NAME_IN_WEEK",$e:value)+ SensorState(attributeName=="TIME_HH:MM:SS",$h:value)+ SensorState(attributeName matches ("UI_ESTIMATED_TIME_ARRIVAL."+$e), eval(util.parseTimestamp(value) <= util.parseTimestamp($h)), eval(value.toString().length()==5))+ SensorState(attributeName matches ("UI_ESTIMATED_TIME_DEPARTURE."+$e), $vd:value, eval(value.toString().length()==5))+// SensorState(attributeName=="currentScene", value=="-")thencommands.execute("UI_ESTIMATED_TIME_DEPARTURE", $vd.toString());- commands.execute("NEXT_EVENT", "Departure");+ commands.execute("currentScene", "Departure");endrule "Move ETD to ET at arrive"when- SensorState(sensorName=="TIME_DAY_NAME_IN_WEEK",$e:value)- SensorState(sensorName=="TIME_HH:MM:SS",$h:value)- SensorState(sensorName=="NEXT_EVENT", value=="Arrive")- SensorState(sensorName matches ("UI_ESTIMATED_TIME_DEPARTURE."+$e), $vd:value)- SensorState(sensorName=="UI_ESTIMATED_TIME_ARRIVAL", value != "-", value != ":--", value != "status")- SensorState(sensorName=="UI_ESTIMATED_TIME_ARRIVAL", eval(util.parseTimestamp(value) < util.parseTimestamp($h)))- SensorState(sensorName=="VACATION_DAYS", eval(Integer.parseInt(value.toString())==0))+ SensorState(attributeName=="TIME_DAY_NAME_IN_WEEK",$e:value)+ SensorState(attributeName=="TIME_HH:MM:SS",$h:value)+ SensorState(attributeName=="currentScene", value=="Arrive")+ SensorState(attributeName matches ("UI_ESTIMATED_TIME_DEPARTURE."+$e), $vd:value)+ SensorState(attributeName=="UI_ESTIMATED_TIME_ARRIVAL", value != "-", value != ":--", value != "status")+ SensorState(attributeName=="UI_ESTIMATED_TIME_ARRIVAL", eval(util.parseTimestamp(value) < util.parseTimestamp($h)))+ SensorState(attributeName=="VACATION_DAYS", eval(Integer.parseInt(value.toString())==0))then- commands.execute("NEXT_EVENT", "Departure");+ commands.execute("currentScene", "Departure");endrule "fetch actual arrival time"when- SensorState(sensorName=="TIME_HH:MM:SS", $h:value)- SensorState(sensorName=="ACTUAL_TIME_ARRIVAL", value == "-")- SensorState(sensorName=="NEXT_EVENT", value=="Departure")- SensorState(sensorName=="PRESENCE_AS_INTEGER", value=="1")+ SensorState(attributeName=="TIME_HH:MM:SS", $h:value)+ SensorState(attributeName=="ACTUAL_TIME_ARRIVAL", value == "-")+ SensorState(attributeName=="currentScene", value=="Departure")+ AssetEvent($id: id, type == AssetType.ROOM, $s:attributeName=="presenceDetectedInteger", value.asNumber==1)thencommands.execute("ACTUAL_TIME_ARRIVAL", $h.toString().substring(0,5));endrule "fetch earlier actual arrival time up to 30 min v02"when- SensorState(sensorName=="TIME_HH:MM:SS", $h:value, eval(util.parseTimestamp(value) >= util.parseTimestamp("04:30"))) // Earliest time of arrival is 4:30 AM- SensorState(sensorName=="ACTUAL_TIME_ARRIVAL", value == "-")- SensorState(sensorName=="NEXT_EVENT", value=="Arrive")- SensorState(sensorName=="PRESENCE_AS_INTEGER", value=="1")- SensorState(sensorName=="UI_ESTIMATED_TIME_ARRIVAL", value != "-", value != ":--")- SensorState(sensorName=="UI_ESTIMATED_TIME_ARRIVAL", eval(util.parseTimestamp(util.shiftTime(value,-30)) <= util.parseTimestamp($h)))+ SensorState(attributeName=="TIME_HH:MM:SS", $h:value, eval(util.parseTimestamp(value) >= util.parseTimestamp("04:30"))) // Earliest time of arrival is 4:30 AM+ SensorState(attributeName=="ACTUAL_TIME_ARRIVAL", value == "-")+ SensorState(attributeName=="currentScene", value=="Arrive")+ AssetEvent($id: id, type == AssetType.ROOM, $s:attributeName=="presenceDetectedInteger", value.asNumber==1)+ SensorState(attributeName=="UI_ESTIMATED_TIME_ARRIVAL", value != "-", value != ":--")+ SensorState(attributeName=="UI_ESTIMATED_TIME_ARRIVAL", eval(util.parseTimestamp(util.shiftTime(value,-30)) <= util.parseTimestamp($h)))thencommands.execute("ACTUAL_TIME_ARRIVAL", $h.toString().substring(0,5));end@@ -219,27 +217,27 @@ endrule "Move - to ET at end of work"timer(int: 1s) // avoid racingwhen- SensorState(sensorName=="TIME_HH:MM:SS",$h:value)- SensorState(sensorName=="UI_ESTIMATED_TIME_DEPARTURE", value != "-", value != ":--")- SensorState(sensorName=="UI_ESTIMATED_TIME_DEPARTURE", eval(util.parseTimestamp(value) < util.parseTimestamp($h)))- SensorState(sensorName=="NEXT_EVENT", value=="Departure")+ SensorState(attributeName=="TIME_HH:MM:SS",$h:value)+ SensorState(attributeName=="UI_ESTIMATED_TIME_DEPARTURE", value != "-", value != ":--")+ SensorState(attributeName=="UI_ESTIMATED_TIME_DEPARTURE", eval(util.parseTimestamp(value) < util.parseTimestamp($h)))+ SensorState(attributeName=="currentScene", value=="Departure")then- commands.execute("NEXT_EVENT", "-");+ commands.execute("currentScene", "-");endrule "Move - to ET on vacation"when- SensorState(sensorName=="VACATION_DAYS", eval(Integer.parseInt(value.toString())>0))+ SensorState(attributeName=="VACATION_DAYS", eval(Integer.parseInt(value.toString())>0))then- commands.execute("NEXT_EVENT", "-");+ commands.execute("currentScene", "-");endrule "fetch actual departure time"when- SensorState(sensorName=="ACTUAL_TIME_ARRIVAL", value != "-")- SensorState(sensorName=="ACTUAL_TIME_DEPARTURE", value == "-")- SensorState(sensorName=="NEXT_EVENT", value=="-")- SensorState(sensorName=="PRESENCE_LAST_DETECTED_TIME", $v:value, eval(value.toString().length()>4))+ SensorState(attributeName=="ACTUAL_TIME_ARRIVAL", value != "-")+ SensorState(attributeName=="ACTUAL_TIME_DEPARTURE", value == "-")+ SensorState(attributeName=="currentScene", value=="-")+ SensorState(attributeName=="PRESENCE_LAST_DETECTED_TIME", $v:value, eval(value.toString().length()>4))thencommands.execute("ACTUAL_TIME_DEPARTURE", $v.toString().substring(0,5));commands.execute("PRESENCE_LAST_DETECTED_TIME","-");@@ -247,15 +245,15 @@ endrule "fetch later departure time"when- SensorState(sensorName=="TIME_DAY_NAME_IN_WEEK",$e:value)- SensorState(sensorName=="NEXT_EVENT", value=="-")- SensorState(sensorName matches ("UI_ESTIMATED_TIME_DEPARTURE."+$e), $vd:value)- SensorState(sensorName=="ACTUAL_TIME_ARRIVAL", value != "-")- SensorState(sensorName=="ACTUAL_TIME_DEPARTURE", value != "-")- SensorState(sensorName=="PRESENCE_LAST_DETECTED_TIME", $v:value, eval(value.toString().length()>4),+ SensorState(attributeName=="TIME_DAY_NAME_IN_WEEK",$e:value)+ SensorState(attributeName=="currentScene", value=="-")+ SensorState(attributeName matches ("UI_ESTIMATED_TIME_DEPARTURE."+$e), $vd:value)+ SensorState(attributeName=="ACTUAL_TIME_ARRIVAL", value != "-")+ SensorState(attributeName=="ACTUAL_TIME_DEPARTURE", value != "-")+ SensorState(attributeName=="PRESENCE_LAST_DETECTED_TIME", $v:value, eval(value.toString().length()>4),eval(util.parseTimestamp(value)<=util.parseTimestamp(util.shiftTime($vd,15))), // Till 15 min after scheduled departureeval(util.parseTimestamp(value)<=util.parseTimestamp("23:55"))) // Till 23:55 - does not wrap on midnight- SensorState(sensorName=="VACATION_DAYS", eval(Integer.parseInt(value.toString())==0))+ SensorState(attributeName=="VACATION_DAYS", eval(Integer.parseInt(value.toString())==0))thencommands.execute("ACTUAL_TIME_DEPARTURE", $v.toString().substring(0,5));commands.execute("PRESENCE_LAST_DETECTED_TIME","-");@@ -263,13 +261,13 @@ endrule "adjust estimated times at midnight"when- SensorState(sensorName=="TIME_DAY_NAME_IN_WEEK", $e: value)- $timer: SensorState(sensorName=="TIME_HH:MM:SS", value!="N/A", eval(value.toString().substring(0,5).equals("23:58"))) // more reliable than timer(cron: )- SensorState(sensorName matches ("UI_ESTIMATED_TIME_DEPARTURE."+$e), $ved:value, this before [1m] $timer) // Loop cut- SensorState(sensorName matches ("UI_ESTIMATED_TIME_ARRIVAL."+$e), $vea:value, this before [1m] $timer)- SensorState(sensorName=="ACTUAL_TIME_DEPARTURE", $vd:value, value!="-")- SensorState(sensorName=="ACTUAL_TIME_ARRIVAL", $va:value, value!="-")- SensorState(sensorName=="VACATION_DAYS", eval(Integer.parseInt(value.toString())==0))+ SensorState(attributeName=="TIME_DAY_NAME_IN_WEEK", $e: value)+ $timer: SensorState(attributeName=="TIME_HH:MM:SS", value!="N/A", eval(value.toString().substring(0,5).equals("23:58"))) // more reliable than timer(cron: )+ SensorState(attributeName matches ("UI_ESTIMATED_TIME_DEPARTURE."+$e), $ved:value, this before [1m] $timer) // Loop cut+ SensorState(attributeName matches ("UI_ESTIMATED_TIME_ARRIVAL."+$e), $vea:value, this before [1m] $timer)+ SensorState(attributeName=="ACTUAL_TIME_DEPARTURE", $vd:value, value!="-")+ SensorState(attributeName=="ACTUAL_TIME_ARRIVAL", $va:value, value!="-")+ SensorState(attributeName=="VACATION_DAYS", eval(Integer.parseInt(value.toString())==0))thenDouble alpha = 2.0/(3+1); // 3 stays for 3 days exponential averagelong nt = (long) (alpha*util.parseTimestamp($va) + (1-alpha)*util.parseTimestamp($vea));@@ -279,7 +277,7 @@ thencommands.execute("UI_ESTIMATED_TIME_DEPARTURE."+$e.toString(), util.formatTimestamp(nt));LOG.fine("ED="+$ved.toString()+" AD="+$vd.toString()+" ND="+ util.formatTimestamp(nt));end-+*/// ##########################################################################################################