From 92c05faa8b29b87220b4a4a0593ccd0d618eb2fc Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sat, 9 May 2020 19:22:12 +0200 Subject: [PATCH] Add root level triggers as discussed on Discord - Add rule trigger ``root#`` to trigger on any root value like ``on root#loadavg<50 do power 2 endon`` after ``state`` command --- RELEASENOTES.md | 2 ++ tasmota/CHANGELOG.md | 3 ++- tasmota/xdrv_10_rules.ino | 23 ++++++++++++++++------- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index e86665a5d..00e1c4cf3 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -103,3 +103,5 @@ The following binary downloads have been compiled with ESP8266/Arduino library c - Add support for OpenTherm by Yuriy Sannikov (#8373) - 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 automatic compression of Rules to achieve ~60% compression, added ``SetOption93 1`` to control caching of rules +- Add rule trigger ``root#`` to trigger on any root value like ``on root#loadavg<50 do power 2 endon`` after ``state`` command diff --git a/tasmota/CHANGELOG.md b/tasmota/CHANGELOG.md index eeae3def4..5abf5c781 100644 --- a/tasmota/CHANGELOG.md +++ b/tasmota/CHANGELOG.md @@ -6,12 +6,13 @@ - Add support for analog anemometer by Matteo Albinola (#8283) - Add support for OpenTherm by Yuriy Sannikov (#8373) - 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 rule trigger ``root#`` to trigger on any root value like ``on root#loadavg<50 do power 2 endon`` after ``state`` command - Change flash access removing support for any Core before 2.6.3 - Change HAss discovery by Federico Leoni (#8370) - Change default PWM Frequency to 977 Hz from 223 Hz - Change minimum PWM Frequency from 100 Hz to 40 Hz - Change PWM updated to the latest version of Arduino PR #7231 -- Add automatic compression of Rules to achieve ~60% compression, added ``SetOption93 1`` to control caching of rules ### 8.2.0.5 20200425 diff --git a/tasmota/xdrv_10_rules.ino b/tasmota/xdrv_10_rules.ino index ba74c08f8..f83b23bb6 100644 --- a/tasmota/xdrv_10_rules.ino +++ b/tasmota/xdrv_10_rules.ino @@ -47,6 +47,7 @@ * on button1#state do publish cmnd/ring2/power %value% endon on button2#state do publish cmnd/strip1/power %value% endon * on switch1#state do power2 %value% endon * on analog#a0div10 do publish cmnd/ring2/dimmer %value% endon + * on root#loadavg<50 do power 2 endon * * Notes: * Spaces after , around and before are mandatory @@ -186,25 +187,25 @@ char rules_vars[MAX_RULE_VARS][33] = {{ 0 }}; * Add Unishox compression to Rules * * New compression for Rules, depends on SetOption93 - * + * * To avoid memory corruption when downgrading, the format is as follows: * - If `SetOption93 0` * Rule[x][] = 511 char max NULL terminated string (512 with trailing NULL) * Rule[x][0] = 0 if the Rule is empty * New: in case the string is empty we also enforce: * Rule[x][1] = 0 (i.e. we have two conseutive NULLs) - * + * * - If `SetOption93 1` * If the rule is smaller than 511, it is stored uncompressed. Rule[x][0] is not null. * If the rule is empty, Rule[x][0] = 0 and Rule[x][1] = 0; * If the rule is bigger than 511, it is stored compressed * The first byte of each Rule is always NULL. * Rule[x][0] = 0, if firmware is downgraded, the rule will be considered as empty - * + * * The second byte contains the size of uncompressed rule in 8-bytes blocks (i.e. (len+7)/8 ) * Maximum rule size si 2KB (2048 bytes per rule), although there is little chances compression ratio will go down to 75% * Rule[x][1] = size uncompressed in dwords. If zero, the rule is empty. - * + * * The remaining bytes contain the compressed rule, NULL terminated */ /*******************************************************************************************/ @@ -490,13 +491,21 @@ bool RulesRuleMatch(uint8_t rule_set, String &event, String &rule) 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; JsonObject &root = jsonBuf.parseObject(event); if (!root.success()) { return false; } // No valid JSON data - if (!root[rule_task].success()) { return false; } // No rule_task in JSON data - JsonObject &obj1 = root[rule_task]; - JsonObject *obj = &obj1; + JsonObject *obj; + if (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; uint32_t i = 0; while ((pos = rule_name.indexOf("#")) > 0) { // "SUBTYPE1#SUBTYPE2#CURRENT"