Updated Expressions in Rules (markdown)

Michael Ingraham 2019-10-08 14:45:03 -04:00
parent 4660f3ab55
commit ea8ba39cff
1 changed files with 34 additions and 30 deletions

@ -1,40 +1,44 @@
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 or even better in user_config_override.h (and of course, #define USE_RULES is also required).
Beginning with Tasmota version 6.4.1.14, an optional feature for using mathematical expressions in rules was introduced. This feature is disabled by default but can be enabled by compiling from source and adding `#define USE_EXPRESSION` to `user_config_override.h`. Of course, `#define USE_RULES` is also required (enabled by default).
### Once the feature is enabled, the use of expressions is supported in the following commands:
### Supported Commands
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)
* RuleTimer
* [If conditional statement](Rules---IF-ELSE-ELSEIF-and-AND-OR-Support) (requires `#define SUPPORT_IF_STATEMENT`)
### Syntax
Expressions can use of the following operators. They are listed by the order of operations priority, from higher to lower.
* `( )` (parentheses are used to explicitly control the order of operations)
* `^` (power)
* `%` (modulo, division by zero returns modulo "0")
* `*` and `/` (multiplication and division; division by zero returns "0")
* `+` and `-` (addition and subtraction)
### 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)
**Order of Operations Example**
* `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
**Subexpressions can be put into parentheses.**
In addition to numeric constants, the following symbolic values can be used:
### 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
Symbol|Description
-|-
|VAR\<x>|variable (\<x> = `1..MAX_RULE_VARS`, e.g., `VAR2`)
|MEM\<x>|persistent variable (\<x> = `1..MAX_RULE_MEMS`, e.g., `MEM3`)
|TIME|minutes past midnight
|UPTIME|uptime minutes
|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)
### 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)
Example: `Mem1=((0.5*Var1)+10)*0.7`
### 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.
To use expressions in the `Var`, `Mem` and `RuleTimer` commands, an equal sign (`=`) character has to be used after the command. If not, the traditional syntax interpretation is used.
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
Statement|Var1 Result
-|-
`Var1=42`|42
`Var1 1+1`|"1+1" (the literal string)
`Var1=1+1`|2
`Var1=sunset-sunrise`|duration of daylight in minutes