Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Isolated presence prediction rules for reimplementation

Christian Bauerb70ece1ac54d
diff --git a/manager/server/src/main/resources/demo/rules/DemoApartmentPresencePrediction.drl b/manager/server/src/main/resources/demo/rules/DemoApartmentPresencePrediction.drl
new file mode 100644
index 000000000..8bcd055d7
--- /dev/null
+++ b/manager/server/src/main/resources/demo/rules/DemoApartmentPresencePrediction.drl
@@ -0,0 +1,261 @@
+package org.openremote.test.rules;
+
+import org.openremote.model.*;
+import org.openremote.model.asset.*
+import elemental.json.Json;
+
+global java.util.logging.Logger LOG;
+global org.openremote.model.rules.Assets assets;
+
+/*
+ SENSOR_PRESENCE // "on"
+ PRESENCE_AS_INTEGER // 0, 1
+ PRESENCE_LAST_DETECTED_TIME // "-", hh:mm
+ PRESENCE_DETECTED_LATCHED // "No", Latches for 15mins
+
+ VACATION_DAYS // "0"
+
+ UI_ESTIMATED_TIME_ARRIVAL // This is today
+ UI_ESTIMATED_TIME_DEPARTURE // This is today
+ UI_ESTIMATED_TIME_ARRIVAL.mon // "9:00"
+ UI_ESTIMATED_TIME_DEPARTURE.mon // "17:00"
+ UI_ESTIMATED_TIME_ARRIVAL.tue // "9:00"
+ UI_ESTIMATED_TIME_DEPARTURE.tue // "17:00"
+ UI_ESTIMATED_TIME_ARRIVAL.wed // "9:00"
+ UI_ESTIMATED_TIME_DEPARTURE.wed // "17:00"
+ ...
+
+ ACTUAL_TIME_ARRIVAL // "-"
+ ACTUAL_TIME_DEPARTURE // "-"
+
+ NEXT_EVENT // "-", Possible values are "-", "Departure", "Arrive"
+*/
+
+
+// ##########################################################################################################
+
+
+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
+
+rule "person sense"
+when
+ SensorState($s:sensorName=="SENSOR_PRESENCE", value=="on")
+ SensorState(sensorName=="PRESENCE_AS_INTEGER", value!="1")
+then
+ commands.execute("PRESENCE_AS_INTEGER", "1");
+end
+
+rule "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"
+when
+ SensorState(sensorName == "PRESENCE_AS_INTEGER", value == "1")
+ SensorState(sensorName=="NEXT_EVENT", value=="Departure") // within expected attendance schedule
+then
+ commands.execute("PRESENCE_DETECTED_LATCHED","Yes");
+end
+
+rule "Absence when 30 min no movement"
+timer(int: 30m)
+when
+ SensorState(sensorName == "PRESENCE_AS_INTEGER", value == "0")
+ SensorState(sensorName=="NEXT_EVENT", value=="Departure") // within expected attendance schedule
+then
+ commands.execute("PRESENCE_DETECTED_LATCHED","No");
+end
+
+rule "Absence after 15 min v02"
+timer(int: 15m)
+when
+ SensorState(sensorName == "PRESENCE_AS_INTEGER", value == "0")
+then
+ commands.execute("PRESENCE_DETECTED_LATCHED","No");
+end
+
+rule "Absence after scheduled presence time"
+when
+ SensorState(sensorName=="NEXT_EVENT", value!="Departure") // outsidse expected attendance schedule
+then
+ commands.execute("PRESENCE_DETECTED_LATCHED","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))
+then
+ commands.execute("UI_ESTIMATED_TIME_ARRIVAL", $va.toString());
+ commands.execute("UI_ESTIMATED_TIME_DEPARTURE", $vd.toString());
+ commands.execute("NEXT_EVENT", "Arrive");
+end
+
+rule "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)
+then
+ commands.execute("UI_ESTIMATED_TIME_ARRIVAL", $va.toString());
+ commands.execute("UI_ESTIMATED_TIME_DEPARTURE", $vd.toString());
+end
+
+rule "reset ACTUAL_TIME_ARRIVAL"
+when
+ SensorState(sensorName=="NEXT_EVENT", value=="Arrive")
+then
+ commands.execute("ACTUAL_TIME_ARRIVAL","-");
+end
+
+rule "reset ACTUAL_TIME_DEPARTURE"
+when
+ SensorState(sensorName=="ACTUAL_TIME_ARRIVAL",value=="-")
+then
+ commands.execute("ACTUAL_TIME_DEPARTURE","-");
+end
+
+rule "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: )
+then
+ commands.execute("NEXT_EVENT", "-");
+end
+
+rule "Move ETD to ET at arrive init"
+// This one is needed for cases when system is restart after arrive time
+when
+ 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=="-")
+then
+ commands.execute("UI_ESTIMATED_TIME_DEPARTURE", $vd.toString());
+ commands.execute("NEXT_EVENT", "Departure");
+end
+
+rule "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))
+then
+ commands.execute("NEXT_EVENT", "Departure");
+end
+
+rule "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")
+then
+ commands.execute("ACTUAL_TIME_ARRIVAL", $h.toString().substring(0,5));
+end
+
+rule "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)))
+then
+ commands.execute("ACTUAL_TIME_ARRIVAL", $h.toString().substring(0,5));
+end
+
+rule "Move - to ET at end of work"
+timer(int: 1s) // avoid racing
+when
+ 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")
+then
+ commands.execute("NEXT_EVENT", "-");
+end
+
+rule "Move - to ET on vacation"
+when
+ SensorState(sensorName=="VACATION_DAYS", eval(Integer.parseInt(value.toString())>0))
+then
+ commands.execute("NEXT_EVENT", "-");
+end
+
+rule "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))
+then
+ commands.execute("ACTUAL_TIME_DEPARTURE", $v.toString().substring(0,5));
+ commands.execute("PRESENCE_LAST_DETECTED_TIME","-");
+end
+
+rule "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),
+ eval(util.parseTimestamp(value)<=util.parseTimestamp(util.shiftTime($vd,15))), // Till 15 min after scheduled departure
+ eval(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))
+then
+ commands.execute("ACTUAL_TIME_DEPARTURE", $v.toString().substring(0,5));
+ commands.execute("PRESENCE_LAST_DETECTED_TIME","-");
+end
+
+rule "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))
+then
+ Double alpha = 2.0/(3+1); // 3 stays for 3 days exponential average
+ long nt = (long) (alpha*util.parseTimestamp($va) + (1-alpha)*util.parseTimestamp($vea));
+ commands.execute("UI_ESTIMATED_TIME_ARRIVAL."+$e.toString(), util.formatTimestamp(nt));
+ LOG.fine("EA="+$vea.toString()+" AA="+$va.toString()+" NA="+ util.formatTimestamp(nt));
+ nt = (long) (alpha*util.parseTimestamp($vd) + (1-alpha)*util.parseTimestamp($ved));
+ commands.execute("UI_ESTIMATED_TIME_DEPARTURE."+$e.toString(), util.formatTimestamp(nt));
+ LOG.fine("ED="+$ved.toString()+" AD="+$vd.toString()+" ND="+ util.formatTimestamp(nt));
+end
+
+
+// ##########################################################################################################
+
+
+