---
title: "Modbus scripts and prefetch"
description: "TapHome Modbus scripts: which script runs when, where to read and write registers, passing values in module variables, and how prefetch caches registers."
url: "https://taphome.com/en/docs/script/modbus-scripts/"
language: en
lastmod: 2026-10-13
generator: hugo
ai:
  topics: ["modbus","modbus-scripts","prefetch","scripting-language"]
  questions: ["Which Modbus scripts exist on a module and a device?","Where do I write MODBUSW for a thermostat set point?","How do I pass a value from a read script to a service attribute?","How does prefetch affect Modbus scripts?","Is it expensive to read the same register twice in one script?","When should I use No prefetch?"]
---

# Modbus scripts and prefetch

The scripts of a TapHome Modbus module and device — initialize, read, read value, write value, service attributes and service actions — and how prefetch and the register cache affect them.

Last updated: 2026-10-13

A Modbus module and each of its devices have several scripts, and the [Modbus functions](https://taphome.com/en/docs/script/modbus/) work in all of them. How often the scripts run and which registers they read from the bus depends on the poll interval and the prefetch mode of the device. When each script runs and which variables it sees is described in detail in [Modbus integration](https://taphome.com/en/docs/integration/modbus/#module-scripts-variables).

## Scripts of Modbus modules and devices

Each Modbus script has its own moment to run and its own typical content.

| Script | Runs | Typical content |
|---|---|---|
| Initialize (module, device) | Once after start | `MODBUSWNE` for configuration registers. |
| Read (module, device) | On every poll | Alarm registers and `ADDERROR`. |
| Read value (device) | On every poll | `MODBUSR` and a conversion. The last statement is the device value. |
| Write value (device) | When the value changes, and on every poll with Periodic write | `MODBUSW` of the device variable, for example the set point. |
| Service attribute | When the service settings open | `MODBUSR` turned into text. |
| Service action | When its button is pressed | `MODBUSW` with the action parameters, available by their abbreviation. |

**Remarks**

- Variables defined on the module or the device keep their value between script runs — use them to pass a value from a read script to a service attribute. Module variables of a collection type need TapHome Next 2026.1.39507+.
- `ADDERROR`, `ADDWARNING` and `ADDINFO` work only in some of these scripts — see [ADDERROR](https://taphome.com/en/docs/script/logic-and-diagnostics/#adderror).

**Example — read value script of a temperature sensor**

```taphome
MODBUSR(H, 20, Int16) / 10;
```

**Example — service action with a parameter `ID`**

```taphome
MODBUSW(H, 20, Float, ID);
```

## Prefetch and the register cache

The prefetch mode of a Modbus device decides whether its scripts read registers from the module's cache or from the bus.

| Mode | Effect on scripts |
|---|---|
| **Normal** | Registers used by the scripts are read in grouped requests before the scripts run. Reading the same register twice in one run costs one transaction. |
| **Isolated** | Like Normal, but the device's registers are not merged with registers of other devices. |
| **No prefetch** | No cache. Every `MODBUSR` and `MODBUSRBLOCK` is a real transaction, in the order written. |

**Remarks**

- Choose **No prefetch** for devices that need an exact sequence of transactions; then store a value read once in a variable instead of reading it again.
- Prefer one block read over a loop of single reads — `MODBUSRBLOCK(H, 100, 100)` replaces 100 transactions.
- `MODBUSWNE` and `MODBUSWBLOCKNE` compare with the cached value — see [MODBUSWNE](https://taphome.com/en/docs/script/modbus/#modbuswne).
- The size of grouped requests and the prefetch modes are set as described in [Prefetch](https://taphome.com/en/docs/integration/modbus/#prefetch).

**Example — one block instead of four reads**

```taphome
collection<uint16> data := MODBUSRBLOCK(H, 100, 4);
VAR voltage := UNPACKVALUE(data, 0, Uint16) / 10;
VAR current := UNPACKVALUE(data, 1, Uint16) / 100;
VAR power := UNPACKVALUE(data, 2, BigEndianFloat);
```

**See also** — [Modbus data types](https://taphome.com/en/docs/script/modbus-data-types/), [Examples](https://taphome.com/en/docs/script/examples/).
