From 181ac5539b584135cfd8db507106c8a975e0a647 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sun, 22 Sep 2019 16:14:34 +0200 Subject: [PATCH] Add JSON array index support to rules Add JSON array index support to rules evaluation allowing trigger on ENERGY#POWER[2]>0.60 from JSON ..,"Power":[0.00,0.68],.. (#6160) --- sonoff/_changelog.ino | 1 + sonoff/xdrv_10_rules.ino | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/sonoff/_changelog.ino b/sonoff/_changelog.ino index 640006c6a..36ff5c65c 100644 --- a/sonoff/_changelog.ino +++ b/sonoff/_changelog.ino @@ -3,6 +3,7 @@ * Add command EnergyReset4 x,x to initialize total usage for two tarrifs * Add command EnergyReset5 x,x to initialize total export (or production) for two tarrifs * Add command Sensor34 8,0 and Sensor34 8,1 to disable/enable JSON message on weight change over 4 gram + * Add JSON array index support to rules evaluation allowing trigger on ENERGY#POWER[2]>0.60 from JSON ..,"Power":[0.00,0.68],.. (#6160) * * 6.6.0.12 20190910 * Redesign command Tariff to now default to 0 (=disabled) and allowing to set both Standard Time (ST) and Daylight Savings Time (DST) start hour diff --git a/sonoff/xdrv_10_rules.ino b/sonoff/xdrv_10_rules.ino index ff0e3932a..877d6e47d 100644 --- a/sonoff/xdrv_10_rules.ino +++ b/sonoff/xdrv_10_rules.ino @@ -259,8 +259,17 @@ bool RulesRuleMatch(uint8_t rule_set, String &event, String &rule) JsonObject &root = jsonBuf.parseObject(event); if (!root.success()) { return false; } // No valid JSON data - float value = 0; - const char* str_value = root[rule_task][rule_name]; + const char* str_value; + if ((pos = rule_name.indexOf("[")) > 0) { // "CURRENT[1]" + int rule_name_idx = atoi(rule_name.substring(pos +1).c_str()); + if ((rule_name_idx < 1) || (rule_name_idx > 6)) { // Allow indexes 1 to 6 + rule_name_idx = 1; + } + rule_name = rule_name.substring(0, pos); // "CURRENT" + str_value = root[rule_task][rule_name][rule_name_idx -1]; // "ENERGY" and "CURRENT" and 0 + } else { + str_value = root[rule_task][rule_name]; // "INA219" and "CURRENT" + } //AddLog_P2(LOG_LEVEL_DEBUG, PSTR("RUL: Task %s, 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"); @@ -271,6 +280,7 @@ bool RulesRuleMatch(uint8_t rule_set, String &event, String &rule) Rules.event_value = str_value; // Prepare %value% // Step 3: Compare rule (value) + float value = 0; if (str_value) { value = CharToFloat((char*)str_value); int int_value = int(value);