Variables and constants
Variables in TapHome scripts: device values by abbreviation, group values, module and device variables, VAR and typed declarations, assignment, properties and context constants.
Device variables
Device values picked in the script editor are available under their abbreviation, for example Se, Mu or Te. A device that no longer exists reads as Null.
Se + 2;Most scripts only read device values. A script can change a device value in two places:
- In the Script smart rule, a read-write variable can be assigned. Each assignment becomes a device action.
- In a Packet parser listener script, assigning the abbreviation of a device value sets that value.
IF Se > 25
Mu := 1;
ENDAnywhere else an assignment stops the script with Can not directly set value to device variable.
A device value can also be picked as a statistic of the device, or as Value info with the time of its last change, its previous value and the values of finished periods — see Statistics and value info.
Group values
A variable bound to a device group holds a groupvalue:
| Property | Contents |
|---|---|
Calculated | The value calculated for the group by the calculation function set on the group — Min, Max, Avg, Sum, Or or And. Members with a NaN value are left out. Or and And treat a value above 0.5 as on and give 1 or 0. When no member has a valid value, the result is 0. |
Values | The values of the members, in the order of the members in the group. A member that is not started yet is Null, an unknown value is NaN. |
In arithmetic and comparisons a group value behaves as its Calculated value:
# Se = device group of room temperatures (calculation Avg)
IF Se > 22
RETURN(1);
END
0;After the group value is changed from a smart rule or by manual control, the variable can hold a plain number instead of a groupvalue for a while. Check .Type before reading Values; MIN and MAX skip the members that are Null or NaN:
# Se = device group of room temperatures
IF Se.Type = "GroupValue"
RETURN(MAX(Se.Values) - MIN(Se.Values));
END
NaN;Module and device variables
Variables defined on a Modbus or Packet parser module or device are available by name and keep their value between script runs. They pass data from a read script to service attributes, or from a listener script to device read scripts.
# Packet parser module read script: one request, the answer is kept for the device scripts
VAR response := SENDHTTPREQUEST("/status", "GET");
StatusJson := response.Content;Available — module variables of a collection type: TapHome Next 2026.1.39507+.
Local variables
A local variable lives for one run of the script. It is declared with VAR, which takes the type of the initial value, or with an explicit type. In both cases the type is fixed for the whole script and every later assignment is converted to it.
VAR a := 5;
int b := 5;
string s := 5;
b := 2.6;
# Result: b = 3, converted to int, halves are rounded away from zero
VAR t := "a";
t := 5;
# Result: t = "5", VAR keeps the type of its first value
uint16 r := 70000;
# Result: r = 4464, a value out of range wraps around silentlyA typed declaration may leave out the initial value; the variable then gets the default of its type (0, FALSE, empty text, an empty collection). VAR always needs a value, VAR y; fails with Variable must have initial value provided.
datetime timestamp;
collection<uint16> block;
double ratio := 0.5;Rules:
- A name starts with a letter and contains only letters and digits;
_fails withOnly letters or numbers are allowed in variable name. - A name must not repeat a constant, a variable or a device abbreviation.
- A variable must be declared above its first use. There is no block scope: a variable declared inside
IFis visible afterEND.
The type keywords are listed on Data types.
Assignment
:= assigns and = compares. The compound operators +=, -=, *=, /=, <<=, >>=, &=, |= and ^= combine an operation with the assignment.
VAR x := 5;
x += 1;
x *= 2;
x <<= 1;
x;
# Result: 24A compound operator has the precedence of its operation, so
x -= a - bis evaluated as(x -= a) - b. Put the right side in parentheses:x -= (a - b);. See Precedence.
Properties
A property is read with .. One level can be written, nested properties can only be read (Unable to assign a value to a nested property), and read-only properties cannot be written.
VAR t := NOW();
t.Minute := 0;
t.Hour;
Se.Current.LastKnownValue;Properties can be read only from a named variable, constant or device variable. NOW().Hour is a parse error; store the value first.
Context constants
Context constants are read-only values that a smart rule or a listener script gives to its script:
| Constant | Available in | Contents |
|---|---|---|
TriggerName | Equation, Formula and Script smart rules | The abbreviation of the variable whose change started the rule, or the name of the trigger that fired. In the Equation smart rule it needs TapHome Next 2026.1.36599+. |
LastState | Equation and Formula smart rules | The previous result; see Scripts in smart rules. |
RECEIVEDMSG | Packet parser listener script | The incoming message (receivedmessage). |
RECEIVEDBYTES | Packet parser listener script | Older form of the payload as a byte collection; prefer RECEIVEDMSG. |
Notification title and message scripts have these read-only constants:
| Constant | In a notification sent by |
|---|---|
SmartRuleName, LocationName | Any smart rule |
TriggerName | The true or false actions of an Equation smart rule |
CardName, DeviceName | An access event; CardName is Null for an unknown card |
DeviceName | A trigger action |
SensorNames | An alarm |
SensorNames, AlarmPhaseName | An alarm phase |
SensorNames, NumberOfSensors | A sensors action, for example open windows |