Rule did not been triggered if there's no comparation operator provided as trigger.
For example:
Rule1 ON EVENT#POWERON DO ... ENDON
The rule_name should be assigned as "POWERON" by default.
The major features of IF statement are:
- Support IF, ELSEIF, ELSE
- Support not only comparison but also using logical expression as condition
- Support run multiple commands
- Support nested IF statement
- All the commands run by IF statement will go to the BACKLOG!
- No limit for logical operators, parenthesis and nested IF statement. Depends on your RAM!
Extended Backus-Naur Form of IF statement:
<if-statement> ::=
IF "(" <logical-expression> ")" <statement-list> {ELSEIF "(" <logical-expression> ")" <statement-list>} [ELSE <statement-list>] ENDIF
<logical-expression> :=
<comparison-expression> |
(<comparison-expression> | <logical-expression>) {(AND | OR) <logical-expression>} |
"(" <logical-expression ")" {(AND | OR) <logical expression>}
<comparison-expression> ::=
<math-expression> ("=" | "<" | ">" | "|" | "==" | "<=" | ">=" | "!=") <math-expression>
<statement-list> ::=
<statement> {";" <statement>}
<statement> ::=
<Sonoff-Tasmota-command> | <if-statement>
In English:
If statement support 3 format:
1. IF (<condition>) <statement-list> ENDIF
2. IF (<condition>) <statement-list> ELSE <statement-list> ENDIF
3. IF (<condition>) <statement-list> [ELSEIF (<condition>) <statement-list> ]* ELSE <statement-list> ENDIF
<condition> is a logical expression which can be:
1. A comparison expression for example:
VAR1 >= 10
2. Multiple comparison expression with logical operator "AND" or "OR" between them. "AND" has higher priority than "OR". Fox example:
UPTIME > 100 AND MEM1 == 1 OR MEM2 == 1
3. Parenthesis can be used to change the priority of logical expression. For example:
UPTIME > 100 AND (MEM1 == 1 OR MEM2 == 1)
<statement-list> can be:
1. A Sonoff-Tasmota command. For example:
ledpower on
2. A IF statement ("IF .... ENDIF")
3. Multiple Sonoff-Tasmota command or IF statement split with ";". For example:
Power1 off; Ledpower on; if (mem1 == 0) Var1 Var1+1; mem1==1 endif; Delay 10; POWER1 on
4. Do not need to lead with "BACKLOG" for multiple commands.
Add rule support for single JSON value pair like {"SSerialReceived":"on"} by expanding it to {"SSerialReceived":{"Data":"on"}} allowing for trigger SSerialReceived#Data=on (#5638)
* Add rule Http#Initialized
* Add command WebColor to change non-persistent GUI colors on the fly
Use a rule like:
rule3 on http#initialized do webcolor {"webcolor":["#eeeeee","#181818","#4f4f4f","#000000","#dddddd","#008000","#222222","#ff0000","#008000","#ffffff","#1fa3ec","#0e70a4","#d43535","#931f1f","#47c266","#5aaf6f","#ffffff","#999999","#000000"]} endon
or
rule3 on http#initialized do webcolor {"webcolor":["#eee","#181818","#4f4f4f","#000","#ddd","#008000","#222"]} endon
to make color changes persistent)
Support subscribe/unsubscribe MQTT topics and trigger specified event with the subscribed MQTT topic.
You can subscribe a MQTT topic and assign an event name. Once we received subscribed MQTT message, an event will be automatically triggered. So you can set up a rule with "ON EVENT#<event_name> DO ..." to do whatever you want based on this MQTT message. The payload is passed as a parameter once the event been triggered. If the payload is in JSON format, you are able to get the value of specified key as parameter.
For example, if you have a Tasmota based thermostat and multiple temperature sensors in different place, usually you have to set up a centre home automation system like Domoticz to control the thermostat. Right now, with this new feature, you can write a rule to do this.
Two new commands in Rules:
1. Subscribe
Subscribe a MQTT topic (with or without key) and assign an event name to it.
Command format:
Subscribe [<event_name>, <topic> [, <key>]]
This command will subscribe a <topic> and give it an event name <event_name>.
The optional parameter <key> is for parse the specified key/value from MQTT message
payload with JSON format.
In order to parse value from two level JSON data, you can use one dot (".") to split the key into two section.
Subscribe command without any parameter will list all topics currently subscribed.
2. Unsubscribe
Unsubscribe specified MQTT event.
Command format:
Unsubscribe [<event_name>]
Unsubscribe a topic subscribed by specify the event name.
If no event specified, Unsubscribe all topics subscribed.
Examples:
1.
Subscribe BkLight, Tasmota/BackyardLight/stat/POWER
And define a rule like:
Rule1 on event#BkLight=ON do ruletimer4 60 endon
2.
Subscribe DnTemp, Tasmota/RoomSensor1/stat/SENSOR, DS18B20.Temperature
Define a rule to deal with the MQTT message like {"Time":"2017-02-16T10:13:52", "DS18B20":{"Temperature":20.6}}
Rule1 ON EVENT#DnTemp>=21 DO ... ENDON
Support use an expression as paramter in some rule commands, include Var<x>, Mem<x> and Ruletimer<x>.
Expression is constructed by constants (float number), variables (var<x>, mem<x>, Time, Uptime, Sunrise, Sunset), operators and round brackets.
Currently support 6 operators, order by priority from high to low:
^ (power)
% (modulo)
*, /
+, -
Commands examples:
Var1 3.14 * (MEM1 * (10 + VAR2 ^2) - 100) % 10 + uptime / (2 + MEM2)
Ruletimer4 Time - Sunrise + MEM2/2
Introduce new compare operators for rules and did some optimization as well.
The new "==" operator act as a real number comparison instead of the previous "=" operator which is doing string comparison which result in FALSE for "1 + 1 = 2". For example:
rule1 on event#test do backlog var1 1;add1 1; event CompareWith2=2 endon on event#CompareWith2=%var1% do ledpower on endon
ledpower off
rule1 on
event test
Added new triggers for rules: `VARx#STATE` and `MEMx#STATE`
These new triggers allows to execute commands when VARs or MEMs change their values.
This is useful for automations where the parameter sent to Tasmota by MQTT is a SetPoint for example.
Before these triggers, the approach was using an `EVENT` to store a value but some components in home automation softwares can't use `cmnd/sonofftopic/EVENT SETVAR=25`, where `cmnd/sonofftopic/EVENT` is the mqtt message and `SETVAR=25` is the payload.
With this new triggers, it will be as simple as `cmnd/sonofftopic/VAR 25` so any automation software can use it.
_(Using `cmnd/sonofftopic/EVENT SETVAR=` as mqtt message and `25` as payload is not understood by Tasmota)_
6.4.1.2 20181228
* Change switch driver making it modular and introduce input filter (#4665, #4724)
* Add define DS18B20_INTERNAL_PULLUP to select internal input pullup when only one DS18B20 sensor is connected eliminating external resistor (#4738)
* Add variable %timestamp% to rules (#4749)
This PR adds a new variable %timestamp% to be used in a rule to allow the user to include the Time Stamp as Tasmota has for Status, Sensors, etc, for example:
Command:
publish stat/topic/sensor {"Time":"%timestamp%","mysensor":"%var1%"}
Output:
{"Time":"2018-12-27T12:52:57","mysensor":"1"}
(https://github.com/arendst/Sonoff-Tasmota/issues/4734)
RULES: Added BREAK as an alternative ENDON that will stop the execution of the following rules.
If a rule that ends with BREAK, is triggered, then the following rules of that set will not be executed. This is useful for cases like: https://github.com/arendst/Sonoff-Tasmota/issues/4477