Change root rule trigger

Add rule trigger at root level like ``on loadavg<50 do power 2 endon`` after ``state`` command
This commit is contained in:
Theo Arends 2020-05-10 11:09:34 +02:00
parent 2dac2836d8
commit 89b130b45a
3 changed files with 17 additions and 26 deletions

View File

@ -104,4 +104,4 @@ The following binary downloads have been compiled with ESP8266/Arduino library c
- Add support for Thermostat control by arijav (#8212) - Add support for Thermostat control by arijav (#8212)
- Add experimental basic support for Tasmota on ESP32 based on work by Jörg Schüler-Maroldt - Add experimental basic support for Tasmota on ESP32 based on work by Jörg Schüler-Maroldt
- Add automatic compression of Rules to achieve ~60% compression, added ``SetOption93 1`` to control caching of rules - Add automatic compression of Rules to achieve ~60% compression, added ``SetOption93 1`` to control caching of rules
- Add rule trigger ``#`` to trigger on any root value like ``on #loadavg<50 do power 2 endon`` after ``state`` command - Add rule trigger at root level like ``on loadavg<50 do power 2 endon`` after ``state`` command

View File

@ -7,7 +7,7 @@
- Add support for OpenTherm by Yuriy Sannikov (#8373) - Add support for OpenTherm by Yuriy Sannikov (#8373)
- Add support for Thermostat control by arijav (#8212) - Add support for Thermostat control by arijav (#8212)
- Add automatic compression of Rules to achieve ~60% compression, added ``SetOption93 1`` to control caching of rules - Add automatic compression of Rules to achieve ~60% compression, added ``SetOption93 1`` to control caching of rules
- Add rule trigger ``#`` to trigger on any root value like ``on #loadavg<50 do power 2 endon`` after ``state`` command - Add rule trigger at root level like ``on loadavg<50 do power 2 endon`` after ``state`` command
- Change flash access removing support for any Core before 2.6.3 - Change flash access removing support for any Core before 2.6.3
- Change HAss discovery by Federico Leoni (#8370) - Change HAss discovery by Federico Leoni (#8370)
- Change default PWM Frequency to 977 Hz from 223 Hz - Change default PWM Frequency to 977 Hz from 223 Hz

View File

@ -417,19 +417,20 @@ bool RulesRuleMatch(uint8_t rule_set, String &event, String &rule)
char stemp[10]; char stemp[10];
// Step1: Analyse rule // Step1: Analyse rule
int pos = rule.indexOf('#'); String rule_expr = rule; // "TELE-INA219#CURRENT>0.100"
if (pos == -1) { return false; } // No # sign in rule
String rule_task = rule.substring(0, pos); // "INA219" or "SYSTEM"
if (Rules.teleperiod) { if (Rules.teleperiod) {
int ppos = rule_task.indexOf("TELE-"); // "TELE-INA219" or "INA219" int ppos = rule_expr.indexOf("TELE-"); // "TELE-INA219#CURRENT>0.100" or "INA219#CURRENT>0.100"
if (ppos == -1) { return false; } // No pre-amble in rule if (ppos == -1) { return false; } // No pre-amble in rule
rule_task = rule.substring(5, pos); // "INA219" or "SYSTEM" rule_expr = rule.substring(5); // "INA219#CURRENT>0.100" or "SYSTEM#BOOT"
} }
String rule_expr = rule.substring(pos +1); // "CURRENT>0.100" or "BOOT" or "%var1%" or "MINUTE|5"
String rule_name, rule_param; String rule_name, rule_param;
int8_t compareOperator = parseCompareExpression(rule_expr, rule_name, rule_param); //Parse the compare expression.Return operator and the left, right part of expression int8_t compareOperator = parseCompareExpression(rule_expr, rule_name, rule_param); // Parse the compare expression.Return operator and the left, right part of expression
// rule_name = "INA219#CURRENT"
// rule_param = "0.100" or "%VAR1%"
//AddLog_P2(LOG_LEVEL_DEBUG, PSTR("RUL: expr %s, name %s, param %s"), rule_expr.c_str(), rule_name.c_str(), rule_param.c_str());
char rule_svalue[80] = { 0 }; char rule_svalue[80] = { 0 };
float rule_value = 0; float rule_value = 0;
@ -477,11 +478,12 @@ bool RulesRuleMatch(uint8_t rule_set, String &event, String &rule)
if (temp_value > -1) { if (temp_value > -1) {
rule_value = temp_value; rule_value = temp_value;
} else { } else {
rule_value = CharToFloat((char*)rule_svalue); // 0.1 - This saves 9k code over toFLoat()! rule_value = CharToFloat((char*)rule_svalue); // 0.1 - This saves 9k code over toFLoat()!
} }
} }
// Step2: Search rule_task and rule_name // Step2: Search rule_name
int pos;
int rule_name_idx = 0; int rule_name_idx = 0;
if ((pos = rule_name.indexOf("[")) > 0) { // "SUBTYPE1#CURRENT[1]" if ((pos = rule_name.indexOf("[")) > 0) { // "SUBTYPE1#CURRENT[1]"
rule_name_idx = rule_name.substring(pos +1).toInt(); rule_name_idx = rule_name.substring(pos +1).toInt();
@ -491,21 +493,10 @@ bool RulesRuleMatch(uint8_t rule_set, String &event, String &rule)
rule_name = rule_name.substring(0, pos); // "SUBTYPE1#CURRENT" rule_name = rule_name.substring(0, pos); // "SUBTYPE1#CURRENT"
} }
//AddLog_P2(LOG_LEVEL_DEBUG, PSTR("RUL: Find Task %s, Name %s"), rule_task.c_str(), rule_name.c_str());
StaticJsonBuffer<1024> jsonBuf; StaticJsonBuffer<1024> jsonBuf;
JsonObject &root = jsonBuf.parseObject(event); JsonObject &root = jsonBuf.parseObject(event);
if (!root.success()) { return false; } // No valid JSON data if (!root.success()) { return false; } // No valid JSON data
JsonObject *obj = &root;
JsonObject *obj;
if ((rule_task.length() == 0) || rule_task.startsWith("ROOT")) { // Support root level
obj = &root;
} else {
if (!root[rule_task].success()) { return false; } // No rule_task in JSON data
JsonObject &obj1 = root[rule_task];
obj = &obj1;
}
String subtype; String subtype;
uint32_t i = 0; uint32_t i = 0;
while ((pos = rule_name.indexOf("#")) > 0) { // "SUBTYPE1#SUBTYPE2#CURRENT" while ((pos = rule_name.indexOf("#")) > 0) { // "SUBTYPE1#SUBTYPE2#CURRENT"
@ -524,8 +515,8 @@ bool RulesRuleMatch(uint8_t rule_set, String &event, String &rule)
str_value = (*obj)[rule_name]; // "CURRENT" str_value = (*obj)[rule_name]; // "CURRENT"
} }
//AddLog_P2(LOG_LEVEL_DEBUG, PSTR("RUL: Task %s, Name %s, Value |%s|, TrigCnt %d, TrigSt %d, Source %s, Json %s"), //AddLog_P2(LOG_LEVEL_DEBUG, PSTR("RUL: Name %s, Value |%s|, TrigCnt %d, TrigSt %d, Source %s, Json %s"),
// rule_task.c_str(), rule_name.c_str(), rule_svalue, Rules.trigger_count[rule_set], bitRead(Rules.triggers[rule_set], Rules.trigger_count[rule_set]), event.c_str(), (str_value) ? str_value : "none"); // rule_name.c_str(), rule_svalue, Rules.trigger_count[rule_set], bitRead(Rules.triggers[rule_set], Rules.trigger_count[rule_set]), event.c_str(), (str_value) ? str_value : "none");
Rules.event_value = str_value; // Prepare %value% Rules.event_value = str_value; // Prepare %value%