diff --git a/Expressions-in-Rules.md b/Expressions-in-Rules.md new file mode 100644 index 00000000..4151f486 --- /dev/null +++ b/Expressions-in-Rules.md @@ -0,0 +1,40 @@ +Starting Tasmota version 6.4.1.14, an optional feature for using mathematical expressions in Tasmota rules was introduced. This feature is disabled by default but can be enabled by compiling from source and removing the comment before #define USE_EXPRESSION in my_user_config.h (and of course, #define USE_RULES is also required). + +### Once the feature is enabled, the use of expressions is supported in the following commands: +* Var +* Mem +* Ruletimer +* If (only if the additional #define SUPPORT_IF_STATEMENT is set) + + +### Expressions can make use of the following operators, by the order of priority from higher to lower: +* ^ (power) +* % (modulo, division by zero returns modulo "0") +* \* and / (multiplication and division; division by zero returns "0") +* \+ and - (addition and substraction) + +**Subexpressions can be put into parentheses.** + +### Example for priorities: +* 1\+2\*2 results in 5.0 as the multiplication is done first due to its higher priority +* (1\+2)\*2 results in 6.0 + +### In addition to numeric constants, the following additional placeholders can be used: +* VARx : variable content (x = 1..MAX_RULE_VARS, expample VAR2) +* MEMx : mem-variable content (x = 1..MAX_RULE_MEMS, example MEM3) +* TIME : minutes past midnight +* UPTIME : minutes uptime +* UTCTIME : UTC time, UNIX timestamp, seconds since 01/01/1970 +* LOCALTIME : local time, UNIX timestamp +* SUNRISE : current sunrise time (minutes past midnight) +* SUNSET : current sunset time (minutes past midnight) + +### Calling convention: +To use expressions in the commands Var, Mem and Ruletimer, the "=" character has to be used after the command. If this is not the case, the old logic is applied which doesn't know about expressions. + +Examples: [resulting variable content in [] ] +* var1=42 [42] +* var1 1+1 [results in the string value "1+1"] +* var1=1+1 [2] +* var1=sunset-sunrise [duration of daylight in minutes] +* mem1=((0.5*var1)+10)*0.7