Scripts in smart rules and notifications
What the Equation, Formula and Script smart rules, the Formula limiting condition and notification scripts pass to a script and do with its result: LastState, TriggerName, outputs, errors and SLEEP.
Overview
Smart rules give their script the values of the devices picked as variables, under their abbreviations, and use the result in different ways:
| Where the script runs | What the result does | LastState | TriggerName |
|---|---|---|---|
| Equation smart rule | TRUE runs the true actions, FALSE the false actions | Previous result, FALSE at first | Yes |
| Formula smart rule | Written to every output variable | Previous result, Null at first | Yes |
| Script smart rule | Assignments to read-write variables become device actions | No | Yes |
| Formula limiting condition | TRUE lets the smart rule run | No | No |
| Notification title and message | The text of the notification | No | Equation actions only |
SLEEP is rejected in all smart rule and notification scripts with SLEEP function not allowed in this context. The only exception is the Formula limiting condition, which accepts it; do not use it there either, because it holds up the evaluation of smart rules. ADDERROR, ADDWARNING and ADDINFO fail here with ADDERROR function not supported here.; they work only in device scripts.
Equation smart rule
The Equation smart rule evaluates its script whenever one of its variables changes, after it is saved and after the Core starts. The result is read as a boolean, where a non-zero number is TRUE. Actions run only when the result changes, so one-time actions and notifications fire once per change.
- A
Nullresult or a script error releases the devices the rule controls; the nextTRUEorFALSEresult applies its actions again. A text result is reported as a formula error. LastStateis the previous result of the script,FALSEon the first run and after aNullresult or an error.TriggerNameis the abbreviation of the variable whose change caused the evaluation, empty after a save or a restart. Available from Core 2026.1, build 35936.
A light that switches on below 20 lx and stays on until 60 lx:
(!LastState AND Li < 20) OR (LastState AND Li < 60);
# Result: TRUE below 20, then TRUE until Li reaches 60The same configuration step by step is in Configuration using LastState.
Formula smart rule
The Formula smart rule writes the result of its script to every output variable. In Evaluation mode you choose when it runs: When any of input variables changed, or On custom event, where only the triggers of the rule start it and the variables are just read.
- The result may be of any type the output accepts. A
Nullresult is written as well. LastStateis the previous result. It isNullon the first run and after a script error, so guard it before using it in arithmetic.TriggerNameis the abbreviation of the input variable that changed, or the name of the trigger that fired.- A device that is missing reads as
Null; check such inputs withISNULL.
Counting door openings with the custom event mode (the trigger is the door contact opening):
IF(ISNULL(LastState), 0, LastState) + 1;
# Result: 1 on the first run, then 2, 3 and so onA thermostat with HYSTERESIS uses the same guard:
HYSTERESIS(Se, 23, 21, 0, 1, IF(ISNULL(LastState), 0, LastState));Script smart rule
The Script smart rule has two lists of variables, Read-only variables and Read/write variables. Assigning a read-write variable creates a device action; several assignments to one device are merged into one action. The editor shows Script has to contain at least one assignment. until the script contains :=.
IF Se > 25
Mu := 1;
ELSE
Mu := 0;
END- It runs when Any of the read-only variables changes, or On custom event.
- Only the variables assigned in this run are written. When the rule holds its actions, a run that assigns only some read-write variables releases the others, and a run that assigns none releases all of them. A lower-priority rule or the default value then takes over. Assign every output in every branch, as in the example above.
- A script error releases every held output. A value of the wrong type for a device discards the whole run, including the assignments before it.
TriggerNameis the abbreviation of the read-only variable that changed. It stays empty when no read-only variable is marked as a trigger. With custom events it is the name of the trigger that fired. The script can also assign it.- There is no
LastState.
Formula limiting condition
The Formula type of a limiting condition evaluates a script over its own variables. TRUE lets the smart rule run; FALSE, Null or a script error blocks it.
Te < 22 AND Hu < 70;Notification scripts
The title and the message of a notification are scripts that must return text. They can read the devices picked for the notification and the notification constants listed in Context constants.
"Temperature in " + LocationName + " is " + TOSTRING(Te, "F1") + " °C";