TapHome

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 runsWhat the result doesLastStateTriggerName
Equation smart ruleTRUE runs the true actions, FALSE the false actionsPrevious result, FALSE at firstYes
Formula smart ruleWritten to every output variablePrevious result, Null at firstYes
Script smart ruleAssignments to read-write variables become device actionsNoYes
Formula limiting conditionTRUE lets the smart rule runNoNo
Notification title and messageThe text of the notificationNoEquation 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 Null result or a script error releases the devices the rule controls; the next TRUE or FALSE result applies its actions again. A text result is reported as a formula error.
  • LastState is the previous result of the script, FALSE on the first run and after a Null result or an error.
  • TriggerName is 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 60

The 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 Null result is written as well.
  • LastState is the previous result. It is Null on the first run and after a script error, so guard it before using it in arithmetic.
  • TriggerName is 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 with ISNULL.

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 on

A 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.
  • TriggerName is 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";