---
title: "Scripts in smart rules and notifications"
description: "How TapHome smart rules run scripts: Equation true and false actions, Formula outputs, Script read-write variables, LastState, TriggerName, the Formula limiting condition and notification text."
url: "https://taphome.com/en/docs/script/smart-rules/"
language: en
lastmod: 2026-11-06
generator: hugo
ai:
  topics: ["scripting-language","smart-rules","notifications","laststate","triggername"]
  questions: ["What is the difference between the Equation, Formula and Script smart rules?","What does LastState contain on the first run?","How do I find out which device started a smart rule?","Why does a Script smart rule release a device it does not assign?","Can I use SLEEP in a smart rule?"]
---

# 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.

Last updated: 2026-11-06

A smart rule gives a script the values of the devices you pick as variables and uses the result of the script: an Equation smart rule runs its true or false actions, a Formula smart rule writes the result to its output variables, and a Script smart rule turns assignments to read-write variables into device actions. The Formula limiting condition and the title and message of a notification use scripts in the same way. You need a smart rule, limiting condition or notification with devices picked as variables, which the script reads under their abbreviations. This page shows what each of them passes to the script, such as `LastState` and `TriggerName`, and what a `Null` result, an error or `SLEEP` does.

## 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](https://taphome.com/en/docs/script/smart-rules/#equation) | `TRUE` runs the true actions, `FALSE` the false actions | Previous result, `FALSE` at first | Yes |
| [Formula smart rule](https://taphome.com/en/docs/script/smart-rules/#formula) | Written to every output variable | Previous result, `Null` at first | Yes |
| [Script smart rule](https://taphome.com/en/docs/script/smart-rules/#script) | Assignments to read-write variables become device actions | No | Yes |
| [Formula limiting condition](https://taphome.com/en/docs/script/smart-rules/#formula-condition) | `TRUE` lets the smart rule run | No | No |
| [Notification](https://taphome.com/en/docs/script/smart-rules/#notifications) 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 `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. It needs TapHome Next 2026.1.36599+.

A light that switches on below 20 lx and stays on until 60 lx:

```taphome
(!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](https://taphome.com/en/docs/configuration/hysteresis-equation/#last-state).

## 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`](https://taphome.com/en/docs/script/logic-and-diagnostics/#isnull).

Counting door openings with the custom event mode (the trigger is the door contact opening):

```taphome
IF(ISNULL(LastState), 0, LastState) + 1;
# Result: 1 on the first run, then 2, 3 and so on
```

A thermostat with [`HYSTERESIS`](https://taphome.com/en/docs/script/interpolation-and-thresholds/#hysteresis) uses the same guard:

```taphome
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 `:=`.

```taphome
IF Se > 25
    # above 25: switch Mu on
    Mu := 1;
ELSE
    # otherwise: switch Mu off
    Mu := 0;
END   # Mu is assigned in both branches
```

- 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](https://taphome.com/en/docs/smart-rules/limiting-condition/) evaluates a script over its own variables. `TRUE` lets the smart rule run; `FALSE`, `Null` or a script error blocks it.

```taphome
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](https://taphome.com/en/docs/script/variables/#context-constants).

```taphome
"Temperature in " + LocationName + " is " + TOSTRING(Te, "F1") + " °C";
```
