Skip to content

armgit / core

Public Imported from GitHub
0

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

Climate control rules compile

Christian Bauereee8ee5f40c5
diff --git a/container/src/main/java/org/openremote/container/Container.java b/container/src/main/java/org/openremote/container/Container.java
index c2d744bed..a6d082620 100644
--- a/container/src/main/java/org/openremote/container/Container.java
+++ b/container/src/main/java/org/openremote/container/Container.java
@@ -52,7 +52,17 @@ public class Container {
public static final String DEV_MODE = "DEV_MODE";
public static final boolean DEV_MODE_DEFAULT = true;
- public final ObjectMapper JSON;
+ public static final ObjectMapper JSON = new ObjectMapper()
+ .setSerializationInclusion(JsonInclude.Include.NON_NULL)
+ .configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false)
+ .configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, false)
+ .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
+ .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
+ .setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE)
+ .setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.NONE)
+ .setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE)
+ .setVisibility(PropertyAccessor.CREATOR, JsonAutoDetect.Visibility.NONE)
+ .registerModule(new ElementalJsonModule());
protected final ObjectNode config;
protected final boolean devMode;
@@ -85,18 +95,6 @@ public class Container {
}
public Container(Map<String, String> config, Stream<ContainerService> servicesStream) {
- JSON = new ObjectMapper();
- JSON.setSerializationInclusion(JsonInclude.Include.NON_NULL)
- .configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false)
- .configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, false)
- .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
- .setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
- .setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE)
- .setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.NONE)
- .setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.NONE)
- .setVisibility(PropertyAccessor.CREATOR, JsonAutoDetect.Visibility.NONE)
- .registerModule(new ElementalJsonModule());
-
this.config = JSON.createObjectNode();
for (Map.Entry<String, String> entry : config.entrySet()) {
this.config.put(entry.getKey(), entry.getValue());
diff --git a/controller/src/main/java/org/openremote/controller/rules/RuleEngine.java b/controller/src/main/java/org/openremote/controller/rules/RuleEngine.java
index f7acbd383..d40fe3505 100644
--- a/controller/src/main/java/org/openremote/controller/rules/RuleEngine.java
+++ b/controller/src/main/java/org/openremote/controller/rules/RuleEngine.java
@@ -20,19 +20,22 @@
*/
package org.openremote.controller.rules;
-import org.drools.core.builder.conf.impl.DecisionTableConfigurationImpl;
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.annotation.PropertyAccessor;
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
-import org.kie.api.builder.KieModule;
import org.kie.api.builder.Message;
import org.kie.api.builder.model.KieBaseModel;
import org.kie.api.builder.model.KieModuleModel;
import org.kie.api.builder.model.KieSessionModel;
import org.kie.api.conf.EqualityBehaviorOption;
import org.kie.api.io.Resource;
-import org.kie.api.io.ResourceConfiguration;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
@@ -41,9 +44,8 @@ import org.kie.api.runtime.conf.TimedRuleExectionOption;
import org.kie.api.runtime.rule.FactHandle;
import org.kie.internal.builder.DecisionTableConfiguration;
import org.kie.internal.builder.DecisionTableInputType;
-import org.kie.internal.builder.KnowledgeBuilder;
import org.kie.internal.builder.KnowledgeBuilderFactory;
-import org.kie.internal.io.ResourceTypeImpl;
+import org.openremote.container.Container;
import org.openremote.controller.event.CommandFacade;
import org.openremote.controller.event.Event;
import org.openremote.controller.event.EventContext;
@@ -55,7 +57,6 @@ import org.openremote.controller.statuscache.SwitchFacade;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
-import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;
@@ -72,6 +73,8 @@ public abstract class RuleEngine extends EventProcessor {
private SwitchFacade switchFacade;
private LevelFacade levelFacade;
private RangeFacade rangeFacade;
+ private RulePersistence rulePersistence;
+ private RuleUtil ruleUtil;
@Override
public String getName() {
@@ -170,6 +173,8 @@ public abstract class RuleEngine extends EventProcessor {
switchFacade = new SwitchFacade();
rangeFacade = new RangeFacade();
levelFacade = new LevelFacade();
+ rulePersistence = new RulePersistence();
+ ruleUtil = new RuleUtil();
try {
knowledgeSession.setGlobal("execute", commandFacade);
@@ -192,6 +197,26 @@ public abstract class RuleEngine extends EventProcessor {
} catch (Throwable t) {
}
+ try {
+ knowledgeSession.setGlobal("persistence", rulePersistence);
+ } catch (Throwable t) {
+ }
+
+ try {
+ knowledgeSession.setGlobal("util", ruleUtil);
+ } catch (Throwable t) {
+ }
+
+ try {
+ knowledgeSession.setGlobal("JSON", Container.JSON);
+ } catch (Throwable t) {
+ }
+
+ try {
+ knowledgeSession.setGlobal("LOG", LOG);
+ } catch (Throwable t) {
+ }
+
try {
RuleExecutionLogger ruleExecutionLogger = new RuleExecutionLogger();
knowledgeSession.addEventListener(ruleExecutionLogger);
diff --git a/controller/src/main/java/org/openremote/controller/rules/RulePersistence.java b/controller/src/main/java/org/openremote/controller/rules/RulePersistence.java
new file mode 100644
index 000000000..88c4e0fdd
--- /dev/null
+++ b/controller/src/main/java/org/openremote/controller/rules/RulePersistence.java
@@ -0,0 +1,34 @@
+package org.openremote.controller.rules;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.logging.Logger;
+
+public class RulePersistence {
+
+ private static final Logger LOG = Logger.getLogger(RulePersistence.class.getName());
+
+ // TODO: This should be persistent on disk
+ final protected Map<String, String> data = new HashMap<>();
+
+ public void writeData(String key, Object value) {
+ LOG.fine("Writing '" + key + "': " + value);
+ data.put(key, value != null ? value.toString() : null);
+ }
+
+ public String readData(String key) {
+ LOG.fine("Reading '" + key + "'");
+ return data.get(key);
+ }
+
+ public String readData(String key, String defaultValue) {
+ LOG.fine("Reading '" + key + "', default: " + defaultValue);
+ return data.containsKey(key) ? data.get(key) : defaultValue;
+ }
+
+ public void deleteData(String key) {
+ LOG.fine("Deleting '" + key + "'");
+ data.remove(key);
+ }
+
+}
diff --git a/controller/src/main/java/org/openremote/controller/rules/RuleUtil.java b/controller/src/main/java/org/openremote/controller/rules/RuleUtil.java
new file mode 100644
index 000000000..8b61b9269
--- /dev/null
+++ b/controller/src/main/java/org/openremote/controller/rules/RuleUtil.java
@@ -0,0 +1,105 @@
+package org.openremote.controller.rules;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+public class RuleUtil {
+
+ /**
+ * @param o A timestamp string as 'HH:mm:ss' or 'HH:mm'.
+ * @return Epoch time or 0 if there is a problem parsing the timestamp string.
+ */
+ public long parseTimestamp(Object o) {
+ String timestamp = o.toString();
+ SimpleDateFormat sdf;
+ if (timestamp.length() == 8) {
+ sdf = new SimpleDateFormat("HH:mm:ss");
+ } else if (timestamp.length() == 5) {
+ sdf = new SimpleDateFormat("HH:mm");
+ } else {
+ return (0L);
+ }
+ try {
+ return (sdf.parse(timestamp).getTime());
+ } catch (ParseException e) {
+ return (0L);
+ }
+ }
+
+ /**
+ * @param timestamp Epoch time
+ * @return The timestamp formatted as 'HH:mm' or <code>null</code> if the timestamp is <= 0.
+ */
+ public String formatTimestamp(long timestamp) {
+ if (timestamp <= 0)
+ return null;
+ SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
+ return (sdf.format(new Date(timestamp)));
+ }
+
+ /**
+ * @param timestamp Epoch time
+ * @return The timestamp formatted as 'HH:mm:ss' or <code>null</code> if the timestamp is <= 0.
+ */
+ public String formatTimestampWithSeconds(long timestamp) {
+ if (timestamp <= 0)
+ return null;
+ SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
+ return (sdf.format(new Date(timestamp)));
+ }
+
+ /**
+ * @param o A timestamp string as 'HH:mm' or '-'.
+ * @param minutes The minutes to increment/decrement from timestamp.
+ * @return Timestamp string as 'HH:mm', modified with the given minutes or the current time + 60 minutes if
+ * the given timestamp was '-' or the given timestamp couldn't be parsed.
+ */
+ public String shiftTime(Object o, int minutes) {
+ String timestamp = o.toString();
+ SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
+ Date date = null;
+ if (timestamp != null && timestamp.length() >= 1 && timestamp.substring(0, 1).equals("-")) {
+ date = new Date();
+ date.setTime(date.getTime() + 60 * 60000);
+ } else {
+ try {
+ date = sdf.parse(timestamp);
+ date.setTime(date.getTime() + minutes * 60000);
+ } catch (ParseException ex) {
+ date = new Date();
+ date.setTime(date.getTime() + 60 * 60000);
+ }
+ }
+ return (sdf.format(date));
+ }
+
+ /**
+ * @param o A string representation of a double value.
+ * @return The parsed value or 0.0 if the string couldn't be parsed.
+ */
+ public Double parseDouble(Object o) {
+ String s = o.toString();
+ try {
+ if (s.length() >= 1) {
+ return (Double.parseDouble(s.substring(0, s.length() - 1)));
+ } else {
+ return (0.0);
+ }
+ } catch (NumberFormatException e) {
+ return (0.0);
+ }
+ }
+
+ /**
+ *
+ * @param o A string representation of a double value.
+ * @param shift Increments or decrements the parsed value.
+ * @param suffix A string appended to the result.
+ */
+ public String shiftDouble(Object o, double shift, String suffix) {
+ Double d = parseDouble(o);
+ d = d + shift;
+ return (String.format("%.1f", d) + suffix);
+ }
+}
diff --git a/controller/src/test/groovy/org/openremote/test/rules/ClimateControlTest.groovy b/controller/src/test/groovy/org/openremote/test/rules/ClimateControlTest.groovy
new file mode 100644
index 000000000..a6f8b5f9e
--- /dev/null
+++ b/controller/src/test/groovy/org/openremote/test/rules/ClimateControlTest.groovy
@@ -0,0 +1,72 @@
+package org.openremote.test.rules
+
+import org.kie.api.KieServices
+import org.kie.api.io.Resource
+import org.openremote.controller.ControllerService
+import org.openremote.controller.command.ExecutableCommand
+import org.openremote.controller.command.ExecutableCommandFactory
+import org.openremote.controller.event.CommandFacade
+import org.openremote.controller.event.CustomStateEvent
+import org.openremote.controller.event.EventProcessorChain
+import org.openremote.controller.event.SwitchEvent
+import org.openremote.controller.model.CommandDefinition
+import org.openremote.controller.rules.RuleEngine
+import org.openremote.test.ContainerTrait
+import org.openremote.test.util.EventGrabProcessor
+import spock.lang.Specification
+
+import java.util.stream.Stream
+
+class ClimateControlTest extends Specification implements ContainerTrait {
+
+ def "Climate control basic test"() {
+
+ given: "event processors and rules"
+ def ruleEngineProcessor = new RuleEngine() {
+ @Override
+ protected Stream<Resource> getResources(KieServices kieServices) {
+ Stream.of(
+ kieServices.getResources().newClassPathResource(
+ "org/openremote/test/rules/climatecontrol/ClimateControl.drl"
+ )
+ )
+ }
+ }
+ def grabProcessor = new EventGrabProcessor();
+
+ String lastCommandValue = null
+ int totalCommandCount = 0
+ def commandFactory = new ExecutableCommandFactory() {
+ @Override
+ protected ExecutableCommand buildCommand(CommandDefinition commandDefinition) {
+ return new ExecutableCommand() {
+ @Override
+ void send(String value) {
+ lastCommandValue = value
+ totalCommandCount++
+ }
+ }
+ }
+ }
+ def commandDefinitions = [
+ new CommandDefinition(commandFactory, 11, "test", [(CommandDefinition.NAME_PROPERTY): "temp"])
+ ]
+ def commandFacade = new CommandFacade(commandDefinitions)
+ def eventProcessorChain = new EventProcessorChain(
+ commandFacade,
+ ruleEngineProcessor,
+ grabProcessor
+ )
+
+ and: "the started controller server"
+ def controllerService = new ControllerService(eventProcessorChain)
+ def services = Stream.of(controllerService)
+ def serverPort = findEphemeralPort()
+ def container = startContainer(defaultConfig(serverPort), services)
+
+ // TODO Write tests
+
+ cleanup: "the server should be stopped"
+ stopContainer(container)
+ }
+}
diff --git a/controller/src/test/resources/org/openremote/test/rules/climatecontrol/ClimateControl.drl b/controller/src/test/resources/org/openremote/test/rules/climatecontrol/ClimateControl.drl
new file mode 100644
index 000000000..3cb52c1ad
--- /dev/null
+++ b/controller/src/test/resources/org/openremote/test/rules/climatecontrol/ClimateControl.drl
@@ -0,0 +1,1522 @@
+package org.openremote.controller.test.rules;
+
+import java.util.concurrent.*;
+import java.util.Date;
+import java.text.SimpleDateFormat;
+import java.io.*;
+import org.openremote.controller.model.*;
+import org.openremote.controller.event.*;
+import org.openremote.controller.event.*;
+
+global org.openremote.controller.event.CommandFacade execute;
+global org.openremote.controller.rules.RulePersistence persistence;
+global org.openremote.controller.rules.RuleUtil util;
+global com.fasterxml.jackson.databind.ObjectMapper JSON;
+global java.util.logging.Logger LOG;
+
+declare Event
+ @role(event)
+end
+
+rule "-PSB: Init"
+salience 10
+then
+ execute.command("VR1.ET", persistence.readData("VR1.ET","--:--"));
+ execute.command("VR1.ET.inc", "OFF");
+ execute.command("VR1.ET.dec", "OFF");
+ execute.command("VETA.inc", "OFF");
+ execute.command("VETA.dec", "OFF");
+ execute.command("VETD.inc", "OFF");
+ execute.command("VETD.dec", "OFF");
+ execute.command("VR1.COMFORT",persistence.readData("VR1.COMFORT","20.0\u00B0"));
+ execute.command("VR1.COMFORT.inc","OFF");
+ execute.command("VR1.COMFORT.dec","OFF");
+ execute.command("VR1.TEMPERATURE","19.5\u00B0");
+ execute.command("VR1.TEMPERATURE.inc","OFF");
+ execute.command("VR1.TEMPERATURE.dec","OFF");
+ execute.command("VNEXTACTION",persistence.readData("VNEXTACTION","-")); // Earlier arrive with reset
+ execute.command("VPERSONSENSETIME","-");
+ execute.command("VLEAVES", "1.0");
+ execute.command("VSCORE", "0");
+ execute.command("VHEATINGSETPOINT", "16");
+ execute.command("VWINDOW","Closed");
+ execute.command("VADVICEDONE", "OFF");
+ execute.command("VSUMMER", "No");
+ execute.command("VADVICE", "You're doing great!");
+ execute.command("VACATION.inc", "OFF");
+ execute.command("VACATION.dec", "OFF");
+ execute.command("VACATION", persistence.readData("VACATION", "0"));
+ execute.command("VTOTALSCORE",persistence.readData("VTOTALSCORE", "0"));
+ execute.command("VLEVEL", persistence.readData("VLEVEL", "0"));
+ execute.command("VATA", persistence.readData("VATA","-"));
+ execute.command("VATD", persistence.readData("VATD","-"));
+ execute.command("VPRESENCE","No");
+ execute.command("GVconfig",persistence.readData("GVconfig", "OpenRemote"));
+ execute.command("GVtestsPassed",persistence.readData("GVtestsPassed", "0"));
+ execute.command("GVtestsFailed",persistence.readData("GVtestsFailed", "0"));
+ execute.command("VArrivalBackground", "on");
+ execute.command("VDepartureBackground", "off");
+end
+
+rule "PSB: Init ETA when empty"
+salience 9
+when
+ Event(source matches "VETA.*", $s: source, value=="")
+then
+ execute.command($s, persistence.readData($s, "09:00"));
+end
+
+rule "PSB: Init ETD when empty"
+salience 8
+when
+ Event(source matches "VETD.*", $s: source, value=="")
+then
+ execute.command($s, persistence.readData($s, "17:00"));
+end
+
+rule "PSB: store values"
+timer(int: 2s)
+when
+(
+ Event($s:source matches "VET.*", $v:value!="", eval(!persistence.readData($s,"").equals($v))) ||
+ Event($s:source=="VACATION", $v:value, eval(!persistence.readData($s,"").equals($v))) ||
+ Event($s:source=="VTOTALSCORE", $v:value, eval(!persistence.readData($s,"").equals($v))) ||
+ Event($s:source=="VLEVEL", $v:value, eval(!persistence.readData($s,"").equals($v))) ||
+ Event($s:source=="VR1.COMFORT", $v:value, eval(!persistence.readData($s,"").equals($v))) ||
+ Event($s:source=="VATA", $v:value, eval(!persistence.readData($s,"").equals($v))) ||
+ Event($s:source=="VATD", $v:value, eval(!persistence.readData($s,"").equals($v))) ||
+ Event($s:source=="VNEXTACTION", $v:value, eval(!persistence.readData($s,"").equals($v))) ||
+ Event($s:source=="VR1.ET", $v:value, eval(!persistence.readData($s,"").equals($v))) ||
+ Event($s:source=="VETA", $v:value, eval(!persistence.readData($s,"").equals($v))) ||
+ Event($s:source=="VETD", $v:value, eval(!persistence.readData($s,"").equals($v))) ||
+ Event($s:source matches "^GV.*", $v:value, eval(!persistence.readData($s,"").equals($v)))
+)
+then
+ LOG.fine($s+" old value: "+persistence.readData($s,"||")+"; new value:"+$v.toString());
+ persistence.writeData($s, $v);
+end
+
+rule "-PSB: VR1.ET Inc"
+ timer(int:300ms) // debounce & protect double click
+when
+ Event(source == "VR1.ET", $v: value)
+ Event(source == "VR1.ET.inc" , value == "ON")
+then
+ execute.command("VR1.ET", util.shiftTime($v.toString(), 5));
+ execute.command("VR1.ET.inc", "off");
+ execute.command("VR1.ET.dec", "OFF");
+end
+
+rule "-PSB: VETA Inc"
+ timer(int:300ms) // debounce & protect double click
+when
+ Event(source == "VETA", $v: value)
+ Event(source == "VETA.inc" , value == "ON")
+then
+ execute.command("VETA", util.shiftTime($v.toString(), 5));
+ execute.command("VETA.inc", "off");
+ execute.command("VETA.dec", "OFF");
+end
+
+rule "-PSB: VETD Inc"
+ timer(int:300ms) // debounce & protect double click
+when
+ Event(source == "VETD", $v: value)
+ Event(source == "VETD.inc" , value == "ON")
+then
+ execute.command("VETD", util.shiftTime($v.toString(), 5));
+ execute.command("VETD.inc", "off");
+ execute.command("VETD.dec", "OFF");
+end
+
+rule "-PSB: VR1.ET Dec"
+ timer(int:300ms)
+when
+ Event(source == "VR1.ET", $v: value)
+ Event(source == "VR1.ET.dec" , value == "ON")
+then
+ execute.command("VR1.ET", util.shiftTime($v.toString(), -15));
+ execute.command("VR1.ET.inc", "OFF");
+ execute.command("VR1.ET.dec", "off");
+end
+
+rule "-PSB: VETA Dec"
+ timer(int:300ms)
+when
+ Event(source == "VETA", $v: value)
+ Event(source == "VETA.dec" , value == "ON")
+then
+ execute.command("VETA", util.shiftTime($v.toString(), -15));
+ execute.command("VETA.inc", "OFF");
+ execute.command("VETA.dec", "off");
+end
+
+rule "-PSB: VETD Dec"
+ timer(int:300ms)
+when
+ Event(source == "VETD", $v: value)
+ Event(source == "VETD.dec" , value == "ON")
+then
+ execute.command("VETD", util.shiftTime($v.toString(), -15));
+ execute.command("VETD.inc", "OFF");
+ execute.command("VETD.dec", "off");
+end
+
+rule "PSB: VUSERSCHEDULECHANGES change counter"
+ timer(int: 30s) // Wait 30s to mark user's schedule change
+when
+(
+ Event($s:source == "VR1.ET.inc" , value == "off") ||
+ Event($s:source == "VR1.ET.dec" , value == "off") ||
+ Event($s:source == "VETA.inc" , value == "off") ||
+ Event($s:source == "VETA.dec" , value == "off") ||
+ Event($s:source == "VETD.inc" , value == "off") ||
+ Event($s:source == "VETD.dec" , value == "off")
+)
+then
+ // off - changed by user
+ // OFF - idle state after restart
+ execute.command($s, "OFF");
+ int cnt = 0;
+ try{
+ cnt = Integer.parseInt(persistence.readData("VUSERSCHEDULECHANGES","0"));
+ }finally{
+ cnt += 1;
+ persistence.writeData("VUSERSCHEDULECHANGES", cnt);
+ }
+end
+
+rule "PSB: set departure time on manual VR1.ET change"
+ salience 10
+ timer(int: 300ms)
+when
+ Event(source == "VR1.ET", $v: value)
+(
+ Event(source == "VR1.ET.inc" , value == "ON") ||
+ Event(source == "VR1.ET.dec" , value == "ON") ||
+ Event(source == "VETD.inc", value == "ON") ||
+ Event(source == "VETD.dec", value == "ON")
+)
+ Event(source == "VNEXTACTION", value == "-")
+then
+ execute.command("VNEXTACTION", "Departure");
+end
+
+rule "PSB: VR1.COMFORT inc"
+ timer(int:300ms)
+when
+ Event(source == "VR1.COMFORT", $v: value, eval(util.parseDouble(value) < 24))
+ Event(source == "VR1.COMFORT.inc" , value == "ON")
+then
+ execute.command("VR1.COMFORT.inc","off");
+ execute.command("VR1.COMFORT.dec","OFF");
+ execute.command("VR1.COMFORT", util.shiftDouble($v.toString(), 0.5, "\u00B0"));
+end
+
+rule "PSB: VR1.COMFORT dec"
+ timer(int:300ms)
+when
+ Event(source == "VR1.COMFORT", $v: value, eval(util.parseDouble(value) > 18))
+ Event(source == "VR1.COMFORT.dec" , value == "ON")
+then
+ execute.command("VR1.COMFORT.inc","OFF");
+ execute.command("VR1.COMFORT.dec","off");
+ execute.command("VR1.COMFORT", util.shiftDouble($v.toString(), -0.5, "\u00B0"));
+end
+
+rule "PSB: VR1.COMFORT INC/dec" // needed when change on the boundary
+ salience -10
+ timer(int:300ms)
+when
+ Event(source == "VR1.COMFORT.inc" , value == "ON")
+then
+ execute.command("VR1.COMFORT.inc","off");
+ execute.command("VR1.COMFORT.dec","OFF");
+end
+
+rule "PSB: VR1.COMFORT inc/DEC" // needed when change on the boundary
+ salience -10
+ timer(int:300ms)
+when
+ Event(source == "VR1.COMFORT.dec" , value == "ON")
+then
+ execute.command("VR1.COMFORT.inc","OFF");
+ execute.command("VR1.COMFORT.dec","off");
+end
+
+rule "PSB: VR1.TEMPERATURE inc"
+ timer(int:300ms)
+when
+ Event(source=="VSUMMER", value=="No")
+ Event(source == "VR1.TEMPERATURE", $v: value, eval(util.parseDouble(value) <26))
+ Event(source == "VR1.TEMPERATURE.inc" , value == "ON")
+then
+ execute.command("VR1.TEMPERATURE.inc","off");
+ execute.command("VR1.TEMPERATURE.dec","OFF"); // Do not increase change counter
+ execute.command("VR1.TEMPERATURE", util.shiftDouble($v.toString(), 0.5, "\u00B0"));
+end
+
+rule "PSB: VR1.TEMPERATURE dec"
+ timer(int:300ms)
+when
+ Event(source=="VSUMMER", value=="No")
+ Event(source == "VR1.TEMPERATURE", $v: value, eval(util.parseDouble(value) > 16)) // Lower temp limit
+ Event(source == "VR1.TEMPERATURE.dec" , value == "ON")
+then
+ execute.command("VR1.TEMPERATURE.inc","OFF");
+ execute.command("VR1.TEMPERATURE.dec","off");
+ execute.command("VR1.TEMPERATURE", util.shiftDouble($v.toString(), -0.5, "\u00B0"));
+end
+
+rule "PSB: VR1.TEMPERATURE INC/dec" // needed when on the boundary and in summer
+ salience -10
+ timer(int:300ms)
+when
+ Event(source == "VR1.TEMPERATURE.inc" , value == "ON")
+then
+ execute.command("VR1.TEMPERATURE.inc","off");
+ execute.command("VR1.TEMPERATURE.dec","OFF");
+end
+
+rule "PSB: VR1.TEMPERATURE inc/DEC" // needed when on the boundary and in summer
+ salience -10
+ timer(int:300ms)
+when
+ Event(source == "VR1.TEMPERATURE.dec" , value == "ON")
+then
+ execute.command("VR1.TEMPERATURE.inc","OFF");
+ execute.command("VR1.TEMPERATURE.dec","off");
+end
+
+rule "PSB: VR1.TEMPERATURE change counter"
+ timer(int: 30s) // Wait 30s to mark user's temperature change
+when
+(
+ Event($s:source == "VR1.TEMPERATURE.inc" , value == "off") ||
+ Event($s:source == "VR1.TEMPERATURE.dec" , value == "off") ||
+ Event($s:source == "VR1.COMFORT.inc" , value == "off") ||
+ Event($s:source == "VR1.COMFORT.dec" , value == "off")
+)
+then
+ // off - changed by user
+ // OFF - idle state after restart
+ execute.command($s, "OFF");
+ int cnt = 0;
+ try{
+ cnt = Integer.parseInt(persistence.readData("VSETTEMPCHANGES","0"));
+ }finally{
+ cnt += 1;
+ persistence.writeData("VSETTEMPCHANGES", cnt);
+ }
+end
+
+rule "PSB: VR1.TEMPERATURE change summer"
+salience 10
+when
+ Event(source=="VSUMMER", value=="Yes")
+(
+ Event(source == "VR1.TEMPERATURE.inc" , $s: source, value == "ON") ||
+ Event(source == "VR1.TEMPERATURE.dec", $s: source, value == "ON")
+)
+then
+ execute.command($s, "OFF");
+end
+
+rule "PSB: set departure on manual TEMPERATURE change after expected attendance"
+salience 10
+ timer(int:300ms)
+when
+ Event(source=="VSUMMER", value=="No")
+ Event(source == "VR1.ET", $et: value)
+(
+ Event(source == "VR1.TEMPERATURE.inc" , value == "ON") ||
+ Event(source == "VR1.TEMPERATURE.dec" , value == "ON")
+)
+ Event(source == "VNEXTACTION", value == "-")
+then
+ // Set departure time when temp manually adjusted after leave
+ execute.command("VNEXTACTION", "Departure");
+ execute.command("VR1.ET", util.shiftTime($et.toString(), 60));
+ execute.command("VETD", util.shiftTime($et.toString(), 60));
+end
+
+rule "PSB: Move ETA to ET at midnight"
+when
+ Event(source=="TimerEEE", $e:value)
+ Event(source=="TimerHH",$h:value,eval(util.parseTimestamp(value) >= util.parseTimestamp("00:02"))) // Wait 2 minutes to be sure that $e is OK
+ Event(source matches ("VETA."+$e), eval(util.parseTimestamp(value) > util.parseTimestamp($h)), $va:value)
+ Event(source matches ("VETD."+$e), $vd: value)
+ Event(source=="VNEXTACTION", value=="-")
+ Event(source=="VACATION", eval(Integer.parseInt(value.toString())==0))
+then
+ execute.command("VR1.ET", $va.toString());
+ execute.command("VETA", $va.toString());
+ execute.command("VETD", $vd.toString());
+ execute.command("VNEXTACTION", "Arrive");
+end
+
+rule "Move ETA ETD for current day"
+when
+ Event(source=="TimerEEE", $e:value)
+ Event(source matches ("VETA."+$e), $va:value)
+ Event(source matches ("VETD."+$e), $vd: value)
+then
+ execute.command("VETA", $va.toString());
+ execute.command("VETD", $vd.toString());
+end
+
+rule "PSB: reset VATA"
+when
+ Event(source=="VNEXTACTION", value=="Arrive")
+then
+ execute.command("VATA","-");
+end
+
+rule "PSB: reset VATD"
+when
+ Event(source=="VATA",value=="-")
+then
+ execute.command("VATD","-");
+end
+
+rule "PSB: decrease vacation counter at midnight"
+when
+ $timer: Event(source=="TimerHH", eval(value.toString().substring(0,5).equals("00:00"))) // more reliable than timer(cron: )
+ Event(source=="VACATION", $v: value, eval(Integer.parseInt(value.toString())>0), this before $timer)
+then
+ execute.command("VACATION", Integer.parseInt($v.toString())-1);
+end
+
+rule "PSB: reset action at midnight"
+when
+ Event(source=="TimerHH", eval(value.toString().substring(0,5).equals("00:00"))) // more reliable than timer(cron: )
+then
+ execute.command("VR1.ET", "-");
+ execute.command("VNEXTACTION", "-");
+end
+
+rule "PSB: Move ETD to ET at arrive init"
+// This one is needed for cases when system is restart after arrive time
+when
+ Event(source=="TimerEEE",$e:value)
+ Event(source=="TimerHH",$h:value)
+ Event(source matches ("VETA."+$e), eval(util.parseTimestamp(value) <= util.parseTimestamp($h)), eval(value.toString().length()==5))
+ Event(source matches ("VETD."+$e), $vd:value, eval(value.toString().length()==5))
+// Event(source=="VNEXTACTION", value=="-")
+ Event(source=="VR1.ET", value == "--:--")
+then
+ execute.command("VR1.ET", $vd.toString());
+ execute.command("VETD", $vd.toString());
+ execute.command("VNEXTACTION", "Departure");
+end
+
+rule "PSB: Move ETD to ET at arrive"
+when
+ Event(source=="TimerEEE",$e:value)
+ Event(source=="TimerHH",$h:value)
+ Event(source=="VNEXTACTION", value=="Arrive")
+ Event(source matches ("VETD."+$e), $vd:value)
+ Event(source=="VETA", value != "-", value != "--:--")
+ Event(source=="VETA", eval(util.parseTimestamp(value) < util.parseTimestamp($h)))
+ Event(source=="VACATION", eval(Integer.parseInt(value.toString())==0))
+then
+ execute.command("VR1.ET", $vd.toString());
+ execute.command("VNEXTACTION", "Departure");
+end
+
+rule "PSB: actual arrival time earlier when temperature increased and person detected"
+timer(int: 10s) // Prevent glitch at midnight reset
+when
+ // Before attendance schedule
+ Event(source=="TimerHH",$h:value)
+ Event(source=="VNEXTACTION", value=="Arrive")
+ // Temperature increased manually (higher than ECO)
+ Event(source=="VSUMMER", value=="No")
+ Event(source=="VR1.COMFORT", $vc: value)
+ Event(source=="VR1.TEMPERATURE", eval(util.parseDouble(value) > java.lang.Math.max(util.parseDouble($vc)-5.0, 16.0)))
+ // Person detected
+ Event(source=="VPERSONSENSE", value == "1")
+ Event(source=="VATA",value=="-")
+then
+ execute.command("VR1.ET", $h.toString().substring(0,5));
+end
+
+rule "PSB: fetch actual arrival time"
+when
+ Event(source=="TimerHH", $h:value)
+ Event(source=="VATA", value == "-")
+ Event(source=="VNEXTACTION", value=="Departure")
+ Event(source=="VPERSONSENSE", value=="1")
+then
+ execute.command("VATA", $h.toString().substring(0,5));
+end
+
+rule "PSB: fetch earlier actual arrival time up to 30 min v02"
+when
+ Event(source=="TimerHH", $h:value, eval(util.parseTimestamp(value) >= util.parseTimestamp("04:30"))) // Earliest time of arrival is 4:30 AM
+ Event(source=="VATA", value == "-")
+ Event(source=="VNEXTACTION", value=="Arrive")
+ Event(source=="VPERSONSENSE", value=="1")
+ Event(source=="VETA", value != "-", value != "--:--")
+ Event(source=="VETA", eval(util.parseTimestamp(util.shiftTime(value,-30)) <= util.parseTimestamp($h)))
+then
+ execute.command("VATA", $h.toString().substring(0,5));
+end
+
+rule "PSB: Move - to ET at end of work"
+timer(int: 1s) // avoid racing
+when
+ Event(source=="TimerHH",$h:value)
+ Event(source=="VETD", value != "-", value != "--:--")
+ Event(source=="VETD", eval(util.parseTimestamp(value) < util.parseTimestamp($h)))
+ Event(source=="VNEXTACTION", value=="Departure")
+then
+ execute.command("VR1.ET", "-");
+ execute.command("VNEXTACTION", "-");
+end
+
+rule "PSB: Move - to ET on vacation"
+when
+ Event(source=="VACATION", eval(Integer.parseInt(value.toString())>0))
+then
+ execute.command("VR1.ET", "-");
+ execute.command("VNEXTACTION", "-");
+end
+
+rule "PSB: fetch actual departure time"
+when
+ Event(source=="VATA", value != "-")
+ Event(source=="VATD", value == "-")
+ Event(source=="VNEXTACTION", value=="-")
+ Event(source=="VPERSONSENSETIME", $v:value, eval(value.toString().length()>4))
+then
+ execute.command("VATD", $v.toString().substring(0,5));
+ execute.command("VPERSONSENSETIME","-");
+end
+
+rule "PSB: fetch later departure time"
+when
+ Event(source=="TimerEEE",$e:value)
+ Event(source=="VNEXTACTION", value=="-")
+ Event(source matches ("VETD."+$e), $vd:value)
+ Event(source=="VATA", value != "-")
+ Event(source=="VATD", value != "-")
+ Event(source=="VPERSONSENSETIME", $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
+ Event(source=="VACATION", eval(Integer.parseInt(value.toString())==0))
+then
+ execute.command("VATD", $v.toString().substring(0,5));
+ execute.command("VPERSONSENSETIME","-");
+end
+
+rule "PSB: adjust estimated times at midnight"
+when
+ Event(source=="TimerEEE", $e: value)
+ $timer: Event(source=="TimerHH", eval(value.toString().substring(0,5).equals("23:58"))) // more reliable than timer(cron: )
+ Event(source matches ("VETD."+$e), $ved:value, this before $timer) // Loop cut
+ Event(source matches ("VETA."+$e), $vea:value, this before $timer)
+ Event(source=="VATD", $vd:value, value!="-")
+ Event(source=="VATA", $va:value, value!="-")
+ Event(source=="VACATION", 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));
+ execute.command("VETA."+$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));
+ execute.command("VETD."+$e.toString(), util.formatTimestamp(nt));
+ LOG.fine("ED="+$ved.toString()+" AD="+$vd.toString()+" ND="+ util.formatTimestamp(nt));
+end
+
+rule "--PSB: move sense"
+when
+ Event($s:source=="FS.PIR", value=="on")
+ Event(source=="TimerHH", $h:value)
+then
+ LOG.fine("Move sense on "+$s);
+ execute.command("VPERSONSENSETIME", $h.toString());
+end
+
+rule "-PSB: person sense"
+when
+ Event($s:source=="FS.PIR", value=="on")
+ Event(source=="VPERSONSENSE", value!="1")
+then
+ execute.command("VPERSONSENSE", "1");
+end
+
+rule "-PSB: no person sense"
+timer(int: 2s)
+when
+ Event(source=="FS.PIR", value!="on")
+ Event(source=="VPERSONSENSE", value!="0")
+then
+ execute.command("VPERSONSENSE", "0");
+end
+
+rule "-PSB: window open sense"
+when
+ Event(source=="FS.Window", value=="on")
+then
+ execute.command("VWINDOW","Open");
+end
+
+rule "-PSB: window closed sense"
+when
+ Event(source=="FS.Window", value=="off")
+then
+ execute.command("VWINDOW","Closed");
+end
+
+rule "--FS outside temperature sense"
+when
+ Event(source=="FS.Toutside", $v: value)
+then
+ execute.command("VOUTSIDE", $v.toString() );
+ LOG.fine("Fake outside temperature: "+$v.toString() );
+end
+
+rule "--FS inside temperature sense"
+when
+ Event(source=="FS.Tinside", $v: value)
+then
+ LOG.fine("Fake inside temperature: "+ $v.toString() );
+ execute.command("VROOMTEMPERATURE", $v.toString() );
+end
+
+rule "PSB: Presence"
+when
+ Event(source == "VPERSONSENSE", value == "1")
+ Event(source=="VNEXTACTION", value=="Departure") // within expected attendance schedule
+then
+ execute.command("VPRESENCE","Yes");
+end
+
+rule "PSB: Absence when 30 min no movement"
+timer(int: 30m)
+when
+ Event(source == "VPERSONSENSE", value == "0")
+ Event(source=="VNEXTACTION", value=="Departure") // within expected attendance schedule
+then
+ execute.command("VPRESENCE","No");
+end
+
+rule "PSB: Absence after 15 min v02"
+timer(int: 15m)
+when
+ Event(source == "VPERSONSENSE", value == "0")
+then
+ execute.command("VPRESENCE","No");
+end
+
+rule "PSB: Absence after scheduled presence time"
+when
+ Event(source=="VNEXTACTION", value!="Departure") // outsidse expected attendance schedule
+then
+ execute.command("VPRESENCE","No");
+end
+
+rule "-PSB: heating ECO"
+when
+(
+ Event(source=="VNEXTACTION", value!="Departure") || // outsidse expected attendance schedule
+ Event(source=="VSUMMER", value=="Yes")
+)
+then
+ execute.command("VHEATING","ECO");
+end
+
+rule "-PSB: heating Stand-by from ECO"
+when
+ Event(source=="VSUMMER", value=="No")
+ Event(source=="VNEXTACTION", value=="Departure") // within expected attendance schedule
+ Event(source=="VPRESENCE", value=="No")
+ Event(source=="VHEATING", value == "ECO")
+ Event(source=="VR1.COMFORT", $vc: value)
+then
+ execute.command("VR1.TEMPERATURE", $vc.toString());
+ execute.command("VHEATING", "Stand-by");
+end
+
+rule "-PSB: heating Stand-by from ECO preheating 30 min v02"
+when
+ Event(source=="VSUMMER", value=="No")
+ Event(source=="VNEXTACTION", value=="Arrive") // within expected attendance schedule
+ Event(source=="VHEATING", value == "ECO")
+ Event(source=="VR1.COMFORT", $vc: value)
+
+ Event(source=="TimerHH", $h:value, eval(util.parseTimestamp(value) >= util.parseTimestamp("04:30"))) // Earliest time of arrival is 4:30 AM
+ Event(source=="VETA", value != "-", value != "--:--")
+ Event(source=="VETA", eval(util.parseTimestamp(util.shiftTime(value,-30)) <= util.parseTimestamp($h)))
+then
+ execute.command("VR1.TEMPERATURE", $vc.toString());
+ execute.command("VHEATING", "Stand-by");
+end
+
+rule "-PSB: heating Stand-by from Comfort"
+when
+ Event(source=="VSUMMER", value=="No")
+ Event(source=="VNEXTACTION", value=="Departure") // within expected attendance schedule
+ Event(source=="VPRESENCE", value=="No")
+ Event(source=="VHEATING", value == "Comfort")
+ Event(source=="VR1.COMFORT", $vc: value)
+ Event(source=="VR1.TEMPERATURE", value==$vc) // Only when no manual temperature increase
+then
+ execute.command("VHEATING","Stand-by");
+end
+
+rule "-PSB: heating Comfort on presence"
+when
+ Event(source=="VSUMMER", value=="No")
+ Event(source=="VPRESENCE", value=="Yes")
+ Event(source=="VR1.COMFORT", $v: value)
+then
+ execute.command("VHEATING","Comfort");
+ execute.command("VR1.TEMPERATURE", $v.toString());
+end
+
+rule "-PSB: heating Comfort on temperature increase"
+timer(int: 1s) // debounce
+when
+ Event(source=="VSUMMER", value=="No")
+ Event(source=="VR1.COMFORT", $vc: value)
+ Event(source=="VR1.TEMPERATURE", eval(util.parseDouble(value) > util.parseDouble($vc)))
+ Event(source=="VHEATING", value != "Comfort")
+then
+ execute.command("VHEATING","Comfort");
+end
+
+rule "-PSB: heating setpoint ECO"
+when
+ Event(source=="VSUMMER", value=="No")
+ Event(source=="VHEATING", value=="ECO")
+ Event(source=="VR1.COMFORT", $v: value)
+then
+ String s = $v.toString();
+ Double t;
+ try{
+ t = java.lang.Math.max(16.0, Double.parseDouble(s.substring(0,s.length()-1))-5); // 5 degrees below comfort
+ } catch (NumberFormatException e){
+ t = 16.0;
+ }
+ execute.command("VHEATINGSETPOINT", String.format("%.1f",t));
+ execute.command("VR1.TEMPERATURE", String.format("%.1f\u00B0",t));
+end
+
+rule "-PSB: heating setpoint ECO summer"
+when
+ Event(source=="VSUMMER", value=="Yes")
+ Event(source=="VHEATING", value=="ECO")
+ Event(source=="VR1.COMFORT", $v: value)
+then
+ String s = $v.toString();
+ Double t = java.lang.Math.max(16.0, Double.parseDouble(s.substring(0,s.length()-1))-5); // 5 degrees below comfort
+ execute.command("VHEATINGSETPOINT", String.format("%.1f",t));
+ execute.command("VR1.TEMPERATURE", "-");
+end
+
+rule "-PSB: heating setpoint Stand-by"
+when
+ Event(source=="VSUMMER", value=="No")
+ Event(source=="VHEATING", value=="Stand-by")
+ Event(source=="VR1.COMFORT", $v: value)
+ Event(source=="VR1.TEMPERATURE", value==$v) // When temperature changed manually keep it till ECO
+then
+ String s = $v.toString();
+ Double t = java.lang.Math.max(16.0, Double.parseDouble(s.substring(0,s.length()-1))-1); // 1 degree below comfort
+ execute.command("VHEATINGSETPOINT", String.format("%.1f",t));
+ execute.command("VR1.TEMPERATURE", s);
+end
+
+rule "-PSB: heating setpoint Comfort"
+timer(int: 1s) // avoid race between "VHEATING" & "VR1.TEMPERATURE"
+when
+ Event(source=="VSUMMER", value=="No")
+ Event(source=="VHEATING", value=="Comfort")
+ Event(source=="VR1.TEMPERATURE", $v: value) // set by rule "PSB: heating Comfort" to VR1.COMFORT or manually
+then
+ String s = $v.toString();
+ Double t = java.lang.Math.max(16.0, Double.parseDouble(s.substring(0,s.length()-1)));
+ execute.command("VHEATINGSETPOINT", String.format("%.1f",t));
+end
+
+rule "-PSB: leaves 0"
+when
+ Event(source=="VLEAVES", value=="0.0")
+then
+ execute.command("VLEAF1", "l0.png");
+ execute.command("VLEAF2", "l0.png");
+ execute.command("VLEAF3", "l0.png");
+end
+
+rule "-PSB: leaves 0.5"
+when
+ Event(source=="VLEAVES", value=="0.5")
+then
+ execute.command("VLEAF1", "l1.png");
+ execute.command("VLEAF2", "l0.png");
+ execute.command("VLEAF3", "l0.png");
+end
+
+rule "-PSB: leaves 1"
+when
+ Event(source=="VLEAVES", value=="1.0")
+then
+ execute.command("VLEAF1", "l2.png");
+ execute.command("VLEAF2", "l0.png");
+ execute.command("VLEAF3", "l0.png");
+end
+
+rule "-PSB: leaves 1.5"
+when
+ Event(source=="VLEAVES", value=="1.5")
+then
+ execute.command("VLEAF1", "l2.png");
+ execute.command("VLEAF2", "l1.png");
+ execute.command("VLEAF3", "l0.png");
+end
+
+rule "-PSB: leaves 2"
+when
+ Event(source=="VLEAVES", value=="2.0")
+then
+ execute.command("VLEAF1", "l2.png");
+ execute.command("VLEAF2", "l2.png");
+ execute.command("VLEAF3", "l0.png");
+end
+
+rule "-PSB: leaves 2.5"
+when
+ Event(source=="VLEAVES", value=="2.5")
+then
+ execute.command("VLEAF1", "l2.png");
+ execute.command("VLEAF2", "l2.png");
+ execute.command("VLEAF3", "l1.png");
+end
+
+rule "-PSB: leaves 3"
+when
+ Event(source=="VLEAVES", value=="3.0")
+then
+ execute.command("VLEAF1", "l2.png");
+ execute.command("VLEAF2", "l2.png");
+ execute.command("VLEAF3", "l2.png");
+end
+
+rule "-PSB: energy efficiency 1"
+when
+ Event(source=="VSUMMER", value=="No")
+ Event(source=="VHEATINGSETPOINT", eval(Double.parseDouble(value.toString()) >= 23)) // Comfort+
+ Event(source=="VHEATING", value!="ECO") // needed because of race
+ Event(source=="VPRESENCE", value=="No")
+then
+ execute.command("VLEAVES", "0.0");
+ execute.command("VSCORE", 0);
+end
+
+rule "-PSB: energy efficiency 2"
+when
+ Event(source=="VSUMMER", value=="No")
+ Event(source=="VHEATINGSETPOINT", eval(Double.parseDouble(value.toString()) >= 23)) // Comfort+
+ Event(source=="VHEATING", value!="ECO") // needed because of race
+ Event(source=="VPRESENCE", value=="Yes")
+ Event(source=="VWINDOW", value=="Open")
+then
+ execute.command("VLEAVES", "0.5");
+ execute.command("VSCORE", 0);
+end
+
+// v02
+rule "-PSB: energy efficiency 2-1 v02"
+when
+ Event(source=="VSUMMER", value=="No")
+ Event(source=="VWINDOW", value=="Open")
+ Event(source=="VHEATINGSETPOINT", eval(Double.parseDouble(value.toString()) < 23)) // Comfort, Comfort-, Comfort--
+ Event(source=="VHEATING", value!="ECO") // needed because of race
+ Event(source=="VPRESENCE", value=="No")
+ Event(source=="VOUTSIDE", $t: value)
+ Event(source=="VR1.TEMPERATURE", eval(util.parseDouble($t) < util.parseDouble(value))) // Lower outside temperature
+then
+ execute.command("VLEAVES", "0.5");
+ execute.command("VSCORE", 0);
+end
+
+rule "-PSB: energy efficiency 2-2 v02"
+when
+ Event(source=="VSUMMER", value=="No")
+ Event(source=="VWINDOW", value=="Closed")
+ Event(source=="VHEATINGSETPOINT", eval(Double.parseDouble(value.toString()) >= 23)) // Comfort+
+ Event(source=="VHEATING", value!="ECO") // needed because of race
+ Event(source=="VPRESENCE", value=="No")
+then
+ execute.command("VLEAVES", "0.5");
+ execute.command("VSCORE", 0);
+end
+
+rule "-PSB: energy efficiency 2-3 v02"
+when
+ Event(source=="VSUMMER", value=="No")
+ Event(source=="VWINDOW", value=="Closed")
+ Event(source=="VHEATINGSETPOINT", eval(Double.parseDouble(value.toString()) < 23), eval(Double.parseDouble(value.toString()) >= 18)) // Comfort, Comfort-, Comfort--, not eco
+ Event(source=="VHEATING", value!="ECO") // needed because of race
+ Event(source=="VPRESENCE", value=="No")
+then
+ execute.command("VLEAVES", "1.0");
+ execute.command("VSCORE", 0);
+end
+// v02
+
+rule "-PSB: energy efficiency 3"
+when
+ Event(source=="VSUMMER", value=="No")
+ Event(source=="VHEATINGSETPOINT", eval(Double.parseDouble(value.toString()) >= 21), eval(Double.parseDouble(value.toString()) < 23)) // Comfort
+ Event(source=="VHEATING", value!="ECO") // needed because of race
+ Event(source=="VPRESENCE", value=="No")
+then
+ execute.command("VLEAVES", "1.0");
+ execute.command("VSCORE", 0);
+end
+
+rule "-PSB: energy efficiency 4"
+when
+ Event(source=="VSUMMER", value=="No")
+ Event(source=="VWINDOW", value=="Open") // Added because otherwise it overrules efficiency 11
+ Event(source=="VHEATINGSETPOINT", eval(Double.parseDouble(value.toString()) >= 21), eval(Double.parseDouble(value.toString()) < 23)) // Comfort
+ Event(source=="VHEATING", value!="ECO") // needed because of race
+ Event(source=="VPRESENCE", value=="Yes")
+then
+ execute.command("VLEAVES", "1.0");
+ execute.command("VSCORE", 0);
+end
+
+rule "-PSB: energy efficiency 5"
+when
+ Event(source=="VSUMMER", value=="No")
+ Event(source=="VHEATING", value=="Stand-by")
+ Event(source=="VPRESENCE", value=="No")
+ Event(source=="VWINDOW", value=="Open")
+then
+ execute.command("VLEAVES", "1.0");
+ execute.command("VSCORE", 0);
+end
+
+rule "-PSB: energy efficiency 6"
+when
+ Event(source=="VSUMMER", value=="No")
+ Event(source=="VHEATINGSETPOINT", eval(Double.parseDouble(value.toString()) >= 23)) // Comfort+
+ Event(source=="VHEATING", value!="ECO") // needed because of race
+ Event(source=="VPRESENCE", value=="Yes")
+ Event(source=="VWINDOW", value=="Open")
+then
+ execute.command("VLEAVES", "1.0");
+ execute.command("VSCORE", 0);
+end
+
+rule "-PSB: energy efficiency 7"
+when
+ Event(source=="VSUMMER", value=="No")
+ Event(source=="VWINDOW", value=="Open")
+ Event(source=="VHEATING", value=="Stand-by")
+ Event(source=="VPRESENCE", value=="No")
+ Event(source=="VOUTSIDE", $t: value)
+ Event(source=="VR1.TEMPERATURE", eval(util.parseDouble($t) < util.parseDouble(value))) // Lower outside temperature
+then
+ execute.command("VLEAVES", "1.5");
+ execute.command("VSCORE", 1);
+end
+
+rule "-PSB: energy efficiency 8"
+when
+ Event(source=="VSUMMER", value=="No")
+ Event(source=="VWINDOW", value=="Open")
+ Event(source=="VHEATING", value=="ECO")
+ Event(source=="VPRESENCE", value=="No")
+ Event(source=="VOUTSIDE", $t: value)
+ Event(source=="VR1.TEMPERATURE", eval(util.parseDouble($t) < util.parseDouble(value))) // Lower outside temperature
+then
+ execute.command("VLEAVES", "1.5");
+ execute.command("VSCORE", 1);
+end
+
+rule "-PSB: energy efficiency 9"
+when
+ Event(source=="VSUMMER", value=="No")
+ Event(source=="VWINDOW", value=="Closed")
+ Event(source=="VHEATINGSETPOINT", eval(Double.parseDouble(value.toString()) >= 23)) // Comfort+
+ Event(source=="VHEATING", value!="ECO") // needed because of race
+ Event(source=="VPRESENCE", value=="Yes")
+then
+ execute.command("VLEAVES", "1.5");
+ execute.command("VSCORE", 1);
+end
+
+rule "-PSB: energy efficiency 10"
+when
+ Event(source=="VSUMMER", value=="No")
+ Event(source=="VWINDOW", value=="Open")
+ Event(source=="VHEATINGSETPOINT", eval(Double.parseDouble(value.toString()) >= 20), eval(Double.parseDouble(value.toString()) < 21)) // Comfort-
+ Event(source=="VHEATING", value!="ECO") // needed because of race
+ Event(source=="VPRESENCE", value=="Yes")
+ Event(source=="VOUTSIDE", $t: value)
+ Event(source=="VR1.TEMPERATURE", eval(util.parseDouble($t) < util.parseDouble(value))) // Lower outside temperature
+then
+ execute.command("VLEAVES", "1.5");
+ execute.command("VSCORE", 1);
+end
+
+rule "-PSB: energy efficiency 11"
+when
+ Event(source=="VSUMMER", value=="No")
+ Event(source=="VWINDOW", value=="Closed")
+ Event(source=="VHEATINGSETPOINT", eval(Double.parseDouble(value.toString()) >= 21), eval(Double.parseDouble(value.toString()) < 23)) // Comfort
+ Event(source=="VHEATING", value!="ECO") // needed because of race
+ Event(source=="VPRESENCE", value=="Yes")
+then
+ execute.command("VLEAVES", "2.0");
+ execute.command("VSCORE", 2);
+end
+
+rule "-PSB: energy efficiency 12 v02"
+when
+ Event(source=="VSUMMER", value=="No")
+ Event(source=="VWINDOW", value=="Closed")
+ Event(source=="VHEATINGSETPOINT", eval(Double.parseDouble(value.toString()) >= 20), eval(Double.parseDouble(value.toString()) < 21)) // Comfort-
+ Event(source=="VHEATING", value!="ECO") // needed because of race
+ Event(source=="VPRESENCE", value=="Yes") // v02
+then
+ execute.command("VLEAVES", "2.5");
+ execute.command("VSCORE", 3);
+end
+
+rule "-PSB: energy efficiency 13"
+when
+ Event(source=="VSUMMER", value=="No")
+ Event(source=="VWINDOW", value=="Open")
+ Event(source=="VHEATINGSETPOINT", eval(Double.parseDouble(value.toString()) < 20)) // Comfort--
+ Event(source=="VHEATING", value!="ECO") // needed because of race
+ Event(source=="VPRESENCE", value=="Yes")
+ Event(source=="VOUTSIDE", $t: value)
+ Event(source=="VR1.TEMPERATURE", eval(util.parseDouble($t) >= util.parseDouble(value))) // Higher outside temperature
+then
+ execute.command("VLEAVES", "3.0");
+ execute.command("VSCORE", 4);
+end
+
+rule "-PSB: energy efficiency 14"
+when
+ Event(source=="VSUMMER", value=="No")
+ Event(source=="VWINDOW", value=="Closed")
+ Event(source=="VHEATING", value=="ECO")
+ Event(source=="VPRESENCE", value=="No")
+then
+ execute.command("VLEAVES", "3.0");
+ execute.command("VSCORE", 4);
+end
+
+rule "-PSB: energy efficiency 15"
+when
+ Event(source=="VSUMMER", value=="No")
+ Event(source=="VWINDOW", value=="Closed")
+ Event(source=="VHEATINGSETPOINT", eval(Double.parseDouble(value.toString()) < 20)) // Comfort--
+ Event(source=="VPRESENCE", value=="Yes") // No important IMHO
+then
+ execute.command("VLEAVES", "3.0");
+ execute.command("VSCORE", 4);
+end
+
+rule "-PSB: energy efficiency Summer"
+when
+ Event(source=="VSUMMER", value=="Yes")
+then
+ execute.command("VLEAVES", "3.0");
+ execute.command("VSCORE", 4);
+end
+
+rule "--PSB: increase total score every 15m"
+when
+ $timer: Event(source=="TimerHH", eval(Integer.parseInt(value.toString().substring(3,5)) % 15 == 0)) // more reliable than timer(cron: )
+ Event(source=="VSCORE", $s: value)
+ Event(source=="VTOTALSCORE", $ts: value, this before $timer)
+then
+ execute.command("VTOTALSCORE", Integer.parseInt($ts.toString())+Integer.parseInt($s.toString()));
+end
+
+rule "PSB: reward"
+when
+ $ts: Event(source=="VTOTALSCORE", $s: value, eval(Integer.parseInt(value.toString()) >= 1000))
+ Event(source=="VLEVEL", $l: value, this before $ts)
+then
+ Integer i = Integer.parseInt($s.toString());
+ execute.command("VTOTALSCORE", i-1000); // Reset score
+ execute.command("VLEVEL", Integer.parseInt($l.toString())+1);
+end
+
+rule "--PSB: display totalscore"
+when
+ Event(source=="VTOTALSCORE", $ts: value)
+then
+ execute.command("VTOTALSCOREdisp", $ts+" / 1000");
+end
+
+rule "PSB: summer"
+timer(int: 1h)
+when
+// Event(source=="VR1.COMFORT", $sp: value)
+// Event(source=="VOUTSIDE", value!="", eval(util.parseDouble($sp) <= util.parseDouble(value) )) // Outside temp higher than set point
+ Event(source=="VOUTSIDE", $o:value) // Only for logging
+ Event(source=="YT.summer", value=="0") // Y-con test
+ Event(source=="VSUMMER", value!="Yes")
+then
+ LOG.fine("Summer is ON; outside="+$o.toString());
+ execute.command("VSUMMER", "Yes");
+end
+
+rule "PSB: no summer"
+timer(int: 1h)
+when
+// Event(source=="VR1.COMFORT", $sp: value)
+// Event(source=="VOUTSIDE", value!="", eval(util.parseDouble($sp) > util.parseDouble(value) )) // Outside temp lower than set point
+ Event(source=="VOUTSIDE", $o:value) // Only for logging
+ Event(source=="YT.summer", value=="1") // Y-con test
+ Event(source=="VSUMMER", value!="No")
+then
+ LOG.fine("Summer is OFF; outside="+$o.toString());
+ execute.command("VSUMMER", "No");
+end
+
+rule "-PSB: advice 1"
+when
+ Event(source=="VWINDOW", value=="Open")
+ Event(source=="VSUMMER", value=="No")
+ Event(source=="VHEATINGSETPOINT", $sp: value)
+ Event(source=="VOUTSIDE", value!="", eval(Double.parseDouble($sp.toString()) > util.parseDouble(value) )) // Outside temp lower than set point
+then
+ execute.command("VADVICE", "It's more energy efficient to close the windows when it is colder outside than it is inside");
+ execute.command("VADVICEDONE", "OFF");
+ insert(new AdviceGiven("It's more energy efficient to close the windows when it is colder outside than it is inside"));
+end
+
+rule "-PSB: advice 1 done"
+when
+ Event(source=="VHEATINGSETPOINT", $sp: value)
+(
+ Event(source=="VWINDOW", value=="Closed") ||
+ Event(source=="VOUTSIDE", value!="", eval(Double.parseDouble($sp.toString()) <= util.parseDouble(value) )) // Outside temp lower than set point
+)
+ Event(source=="VADVICE",eval(value.toString().substring(0,9).equals("It's more")))
+then
+ execute.command("VADVICE", "You're doing great!");
+ execute.command("VADVICEDONE", "OFF");
+end
+
+rule "-PSB: advice 2"
+when
+ Event(source=="VSUMMER", value=="No")
+ Event(source=="VR1.COMFORT", eval(util.parseDouble(value) > 20.5)) // Average is 20 hardcoded or from sensor?
+then
+ execute.command("VADVICE", "Your comfort temperature is set higher than average. You can adjust it on the settings page.");
+ execute.command("VADVICEDONE", "OFF");
+ insert(new AdviceGiven("Your comfort temperature is set higher than average. You can adjust it on the settings page."));
+end
+
+rule "-PSB: advice 2 done"
+when
+ Event(source=="VR1.COMFORT", eval(util.parseDouble(value) <= 20.5)) // Average is 20 hardcoded or from sensor?
+ Event(source=="VADVICE",eval(value.toString().substring(0,12).equals("Your comfort")))
+then
+ execute.command("VADVICE", "You're doing great!");
+ execute.command("VADVICEDONE", "OFF");
+end
+
+rule "-PSB: advice 3"
+when
+// irregular attendence
+then
+ execute.command("VADVICEDONE", "OFF");
+end
+
+rule "-PSB: advice 4"
+when
+(
+ Event(source=="VADVICEDONE", value=="ON") ||
+ Event(source=="VSUMMER", value=="Yes")
+)
+then
+ execute.command("VADVICE", "You're doing great!");
+ execute.command("VADVICEDONE", "OFF");
+ insert(new AdviceGiven("You're doing great!"));
+end
+
+declare AdviceGiven
+ @role(event)
+ @expires(10m)
+ advice : String
+end
+
+rule "-PSB: advices given counter"
+when
+ Event(source=="VADVICE", value!="", value!="You're doing great!")
+ //AdviceGiven(advice!="You're doing great!") // This one fires with the same advice
+then
+ int cnt = 0;
+ try{
+ cnt = Integer.parseInt(persistence.readData("VADVICEGIVEN","0"));
+ }finally{
+ cnt += 1;
+ persistence.writeData("VADVICEGIVEN", cnt);
+ }
+end
+
+rule "-PSB: vacation inc"
+ timer(int:300ms)
+when
+ Event(source=="VACATION", $v: value)
+ Event(source=="VACATION.inc", value=="ON")
+then
+ execute.command("VACATION", Integer.parseInt($v.toString())+1);
+ execute.command("VACATION.inc", "OFF");
+end
+
+rule "-PSB: vacation dec"
+ timer(int:300ms)
+when
+ Event(source=="VACATION", $v: value, eval(Integer.parseInt(value.toString()) > 0))
+ Event(source=="VACATION.dec", value=="ON")
+then
+ execute.command("VACATION", Integer.parseInt($v.toString())-1);
+ execute.command("VACATION.dec", "OFF");
+end
+
+rule "-PSB: vacation inc/dec" // needed so the ON value does not hang
+ salience -10
+ timer(int:300ms)
+when
+ Event(source matches "VACATION...c", $s: source, value=="ON")
+then
+ execute.command($s, "OFF");
+end
+
+rule "PSB: start next week stats"
+ timer(cron: 0 0 0 ? * 1) // Every Sunday morning at midnight
+when
+ // Time scheduled
+ Number($arrive: longValue>0) from accumulate(Event(source matches "VETA.*", $a:value), sum(util.parseTimestamp($a)))
+ Number($departure: longValue>0) from accumulate(Event(source matches "VETD.*", $d:value), sum(util.parseTimestamp($d)))
+then
+ persistence.writeData("VSETTEMPCHANGES4" ,persistence.readData("VSETTEMPCHANGES3","0"));
+ persistence.writeData("VSETTEMPCHANGES3" ,persistence.readData("VSETTEMPCHANGES2","0"));
+ persistence.writeData("VSETTEMPCHANGES2" ,persistence.readData("VSETTEMPCHANGES1","0"));
+ persistence.writeData("VSETTEMPCHANGES1" ,persistence.readData("VSETTEMPCHANGES","0"));
+ persistence.writeData("VSETTEMPCHANGES" , "0");
+
+ persistence.writeData("VUSERSCHEDULECHANGES4" ,persistence.readData("VUSERSCHEDULECHANGES3","0"));
+ persistence.writeData("VUSERSCHEDULECHANGES3" ,persistence.readData("VUSERSCHEDULECHANGES2","0"));
+ persistence.writeData("VUSERSCHEDULECHANGES2" ,persistence.readData("VUSERSCHEDULECHANGES1","0"));
+ persistence.writeData("VUSERSCHEDULECHANGES1" ,persistence.readData("VUSERSCHEDULECHANGES","0"));
+ persistence.writeData("VUSERSCHEDULECHANGES" , "0");
+
+ persistence.writeData("VADVICEGIVEN4" ,persistence.readData("VADVICEGIVEN3","0"));
+ persistence.writeData("VADVICEGIVEN3" ,persistence.readData("VADVICEGIVEN2","0"));
+ persistence.writeData("VADVICEGIVEN2" ,persistence.readData("VADVICEGIVEN1","0"));
+ persistence.writeData("VADVICEGIVEN1" ,persistence.readData("VADVICEGIVEN","0"));
+ persistence.writeData("VADVICEGIVEN" , "0");
+
+ // Time scheduled
+ long minutes = ($departure-$arrive)/60000;
+ long hours = minutes/60;
+
+ persistence.writeData("VHOURSPRESENT4" ,persistence.readData("VHOURSPRESENT3","0:00"));
+ persistence.writeData("VHOURSPRESENT3" ,persistence.readData("VHOURSPRESENT2","0:00"));
+ persistence.writeData("VHOURSPRESENT2" ,persistence.readData("VHOURSPRESENT1","0:00"));
+ persistence.writeData("VHOURSPRESENT1" , String.format("%2d:%02d", hours, minutes%60));
+end
+
+// New StrijpS 2016
+
+rule "-PSB16: Full Timer"
+when
+ Event(source=="TimerEEEE", $EEEE:value)
+ Event(source=="Timerd", $d: value)
+ Event(source=="TimerMMM", $MMM:value)
+then
+ execute.command("VTimer", $EEEE.toString()+" "+$d.toString()+" "+$MMM.toString()+". I will be:");
+end
+
+rule "-PSB16: background arrival on"
+when
+ Event(source=="TimerHH", $h:value)
+ Event(source=="VETA", eval(util.parseTimestamp(value) > util.parseTimestamp($h)) )
+ Event(source=="VArrivalBackground", value!="on")
+then
+ execute.command("VArrivalBackground", "on");
+end
+
+rule "-PSB16: background departure on"
+when
+ Event(source=="TimerHH", $h:value)
+ Event(source=="VETD", eval(util.parseTimestamp(value) > util.parseTimestamp($h)) )
+ Event(source=="VDepartureBackground", value!="on")
+then
+ execute.command("VDepartureBackground", "on");
+end
+
+rule "-PSB16: background arrival off"
+when
+ Event(source=="TimerHH", $h:value)
+ Event(source=="VETA", eval(util.parseTimestamp(value) <= util.parseTimestamp($h)) )
+ Event(source=="VArrivalBackground", value!="off")
+then
+ execute.command("VArrivalBackground", "off");
+end
+
+rule "-PSB16: background departure off"
+when
+ Event(source=="TimerHH", $h:value)
+ Event(source=="VETD", eval(util.parseTimestamp(value) <= util.parseTimestamp($h)) )
+ Event(source=="VDepartureBackground", value!="off")
+then
+ execute.command("VDepartureBackground", "off");
+end
+
+// PID controller start
+
+rule "-PID: init"
+then
+ execute.command("GV.PID.Kp",persistence.readData("GV.PID.Kp", "0.6"));
+ execute.command("GV.PID.Ki",persistence.readData("GV.PID.Ki", "0.2"));
+ execute.command("GV.PID.Kd",persistence.readData("GV.PID.Kd", "0.1"));
+ execute.command("GV.PID.Db",persistence.readData("GV.PID.Db", "0.0"));
+ execute.command("GV.PID.Sp",persistence.readData("GV.PID.Sp", "1.0"));
+end
+
+// PID UI
+
+rule "-PID: increase Kp"
+ timer(int:300ms)
+when
+ Event(source=="GV.PID.Kp", $v: value)
+ Event($s:source=="PID.kp.inc", value=="ON")
+then
+ execute.command("GV.PID.Kp", String.format("%.1f", Double.parseDouble($v.toString())+0.1) );
+ execute.command($s, "off");
+end
+
+rule "-PID: decrease Kp"
+ timer(int:300ms)
+when
+ Event(source=="GV.PID.Kp", $v: value)
+ Event($s:source=="PID.kp.dec", value=="ON")
+then
+ execute.command("GV.PID.Kp", String.format("%.1f", Double.parseDouble($v.toString())-0.1) );
+ execute.command($s, "off");
+end
+
+rule "-PID: increase Ki"
+ timer(int:300ms)
+when
+ Event(source=="GV.PID.Ki", $v: value)
+ Event($s:source=="PID.ki.inc", value=="ON")
+then
+ execute.command("GV.PID.Ki", String.format("%.1f", Double.parseDouble($v.toString())+0.1) );
+ execute.command($s, "off");
+end
+
+rule "-PID: decrease Ki"
+ timer(int:300ms)
+when
+ Event(source=="GV.PID.Ki", $v: value)
+ Event($s:source=="PID.ki.dec", value=="ON")
+then
+ execute.command("GV.PID.Ki", String.format("%.1f", Double.parseDouble($v.toString())-0.1) );
+ execute.command($s, "off");
+end
+
+rule "-PID: increase Kd"
+ timer(int:300ms)
+when
+ Event(source=="GV.PID.Kd", $v: value)
+ Event($s:source=="PID.kd.inc", value=="ON")
+then
+ execute.command("GV.PID.Kd", String.format("%.1f", Double.parseDouble($v.toString())+0.1) );
+ execute.command($s, "off");
+end
+
+rule "-PID: decrease Kd"
+ timer(int:300ms)
+when
+ Event(source=="GV.PID.Kd", $v: value)
+ Event($s:source=="PID.kd.dec", value=="ON")
+then
+ execute.command("GV.PID.Kd", String.format("%.1f", Double.parseDouble($v.toString())-0.1) );
+ execute.command($s, "off");
+end
+
+rule "-PID: increase Db"
+ timer(int:300ms)
+when
+ Event(source=="GV.PID.Db", $v: value)
+ Event($s:source=="PID.db.inc", value=="ON")
+then
+ execute.command("GV.PID.Db", String.format("%.1f", Double.parseDouble($v.toString())+0.1) );
+ execute.command($s, "off");
+end
+
+rule "-PID: decrease Db"
+ timer(int:300ms)
+when
+ Event(source=="GV.PID.Db", $v: value)
+ Event($s:source=="PID.db.dec", value=="ON")
+then
+ execute.command("GV.PID.Db", String.format("%.1f", Double.parseDouble($v.toString())-0.1) );
+ execute.command($s, "off");
+end
+
+rule "-PID: increase Sp"
+ timer(int:300ms)
+when
+ Event(source=="GV.PID.Sp", $v: value)
+ Event($s:source=="PID.sp.inc", value=="ON")
+then
+ execute.command("GV.PID.Sp", String.format("%.1f", Double.parseDouble($v.toString())+0.5) );
+ execute.command($s, "off");
+end
+
+rule "-PID: decrease Sp"
+ timer(int:300ms)
+when
+ Event(source=="GV.PID.Sp", $v: value)
+ Event($s:source=="PID.sp.dec", value=="ON")
+then
+ execute.command("GV.PID.Sp", String.format("%.1f", Double.parseDouble($v.toString())-0.5) );
+ execute.command($s, "off");
+end
+
+rule "-PID: reset inc/dec"
+ salience -10
+ timer(int: 1s)
+when
+ Event($s: source matches "^PID......c$", value!="off")
+then
+ execute.command($s, "off");
+end
+
+// PID algorithm
+
+declare PIDVars
+// Input==Output;
+ output: double
+ iterm: double
+ prevInput: double
+ outMin: double
+ outMax: double
+ active: Boolean
+end
+
+rule "PID: algo init"
+then
+ PIDVars pv = new PIDVars();
+ pv.setActive(true);
+ pv.setOutMax(100);
+ pv.setOutMin(-100);
+ insert(pv);
+end
+
+rule "--PID: compute"
+ timer(int: 0 1s)
+when
+ $pv: PIDVars(active==true)
+ Event(source=="GV.PID.Kp", $kp: value)
+ Event(source=="GV.PID.Ki", $ki: value)
+ Event(source=="GV.PID.Kd", $kd: value)
+ Event(source=="GV.PID.Sp", $sp: value)
+ Event(source=="GV.PID.Db", $db: value)
+then
+ double error = Double.parseDouble($sp.toString() ) - $pv.getOutput();
+ double kp = Double.parseDouble($kp.toString());
+ double ki = Double.parseDouble($ki.toString()); // * sample time in sec
+ double kd = Double.parseDouble($kd.toString()); // / sample time in sec
+ double sp = Double.parseDouble($sp.toString());
+ double db = Double.parseDouble($db.toString());
+
+ double iterm = $pv.getIterm() + (ki * error);
+ if(iterm > $pv.getOutMax() ) iterm = $pv.getOutMax();
+ else if(iterm < $pv.getOutMin() ) iterm = $pv.getOutMin();
+ $pv.setIterm( iterm );
+
+ //double dInput = (Input - lastInput);
+ double dInput = ($pv.getOutput() - $pv.getPrevInput() );
+ $pv.setPrevInput($pv.getOutput());
+
+ // Compute PID Output
+ double output = kp * error + $pv.getIterm()- kd * dInput;
+ if(output > $pv.getOutMax()) output = $pv.getOutMax();
+ else if(output < $pv.getOutMin()) output = $pv.getOutMin();
+ $pv.setOutput(output);
+
+ execute.command("PID.Se", String.format("%.4f", error) );
+ execute.command("PID.Output", String.format("%.4f", output) );
+// LOG.fine($pv.toString() + " error: "+String.format("%f", Double.parseDouble($sp.toString() ) - $pv.getOutput()));
+end
+
+// PID end
+
+// Fake sensors start
+
+rule "Fake sensors init"
+then
+ execute.command("FS.Toutside", "15\u00B0");
+ execute.command("FS.Tinside", "19.5\u00B0");
+ execute.command("FS.PIR", "off");
+ execute.command("FS.Window", "off");
+end
+
+rule "--Fake sensors Toutside inc"
+ timer(int:300ms)
+when
+ Event(source == "FS.Toutside", $v: value, eval(util.parseDouble(value) < 50))
+ Event(source == "FS.Toutside.inc" , value == "on")
+then
+ execute.command("FS.Toutside.inc","off");
+ execute.command("FS.Toutside", util.shiftDouble($v.toString(), 0.1, "\u00B0"));
+end
+
+rule "--Fake sensors Toutside dec"
+ timer(int:300ms)
+when
+ Event(source == "FS.Toutside", $v: value, eval(util.parseDouble(value) > -40))
+ Event(source == "FS.Toutside.dec" , value == "on")
+then
+ execute.command("FS.Toutside.dec","off");
+ execute.command("FS.Toutside", util.shiftDouble($v.toString(), -0.1, "\u00B0"));
+end
+
+rule "--Fake sensors Tinside inc"
+ timer(int:300ms)
+when
+ Event(source == "FS.Tinside", $v: value, eval(util.parseDouble(value) < 40))
+ Event(source == "FS.Tinside.inc" , value == "on")
+then
+ execute.command("FS.Tinside.inc","off");
+ execute.command("FS.Tinside", util.shiftDouble($v.toString(), 0.1, "\u00B0"));
+end
+
+rule "--Fake sensors Tinside dec"
+ timer(int:300ms)
+when
+ Event(source == "FS.Tinside", $v: value, eval(util.parseDouble(value) > 0))
+ Event(source == "FS.Tinside.dec" , value == "on")
+then
+ execute.command("FS.Tinside.dec","off");
+ execute.command("FS.Tinside", util.shiftDouble($v.toString(), -0.1, "\u00B0"));
+end
+
+rule "--Fake sensors PIR off"
+ timer(int: 2s)
+when
+ Event(source=="FS.PIR", value=="on")
+then
+ execute.command("FS.PIR", "off");
+end
+
+// Fake sensors end
\ No newline at end of file