---
title: "Variables and constants"
description: "TapHome script variables: device abbreviations, group values, module variables kept between runs, VAR and typed declarations, := and +=, properties, TriggerName, LastState, RECEIVEDMSG."
url: "https://taphome.com/en/docs/script/variables/"
language: en
lastmod: 2026-11-06
generator: hugo
ai:
  topics: ["scripting-language","variables","constants","properties"]
  questions: ["How do I read a device value in a TapHome script?","How do I declare a variable in a TapHome script?","How do I keep a value between two runs of a script?","What does a device group variable contain in a script?","Which constants does a notification script have?"]
---

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

Last updated: 2026-11-06

A TapHome script reads device values by their abbreviation, such as `Se`, and stores its own data in local variables declared with `VAR` or with a type and assigned with `:=`. It also works with group values, with module and device variables that keep their value between runs, and with read-only context constants such as `TriggerName`. The device values must first be picked in the script editor. This page shows how to read, declare, assign and use each of them.

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

```taphome
Se + 2;
```

Most scripts only read device values. A script can change a device value in two places:

- In the [Script smart rule](https://taphome.com/en/docs/script/smart-rules/#script), 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.

```taphome
IF Se > 25
    Mu := 1;
END
```

Anywhere 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](https://taphome.com/en/docs/script/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:

```taphome
# 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`:

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

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

```taphome
# declarations: VAR takes the type of its value
VAR    a := 5;
int    b := 5;
string s := 5;
# assignment is converted to the type of b
b := 2.6;
# Result: b = 3, converted to int, halves are rounded away from zero
# VAR keeps the type of its first value
VAR    t := "a";
t := 5;
# Result: t = "5", VAR keeps the type of its first value
# a value out of range of the type
uint16 r := 70000;
# Result: r = 4464, a value out of range wraps around silently
```

A 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`.

```taphome
datetime timestamp;
collection<uint16> block;
double ratio := 0.5;
```

Rules:

- A name starts with a letter and contains only letters and digits; `_` fails with `Only 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 `IF` is visible after `END`.

The type keywords are listed on [Data types](https://taphome.com/en/docs/script/data-types/).

## Assignment

`:=` assigns and `=` compares. The compound operators `+=`, `-=`, `*=`, `/=`, `<<=`, `>>=`, `&=`, `|=` and `^=` combine an operation with the assignment.

```taphome
# start value
VAR x := 5;
# compound operators change x in place
x += 1;
x *= 2;
x <<= 1;
# the last expression is the result
x;
# Result: 24
```

> [!WARNING]
> A compound operator has the precedence of its operation, so `x -= a - b` is evaluated as `(x -= a) - b`. Put the right side in parentheses: `x -= (a - b);`. See [Precedence](https://taphome.com/en/docs/script/operators/#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.

```taphome
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](https://taphome.com/en/docs/script/smart-rules/#overview). |
| `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 |
