TapHome has one script language for smart rules, notifications and device integrations. The syntax and the functions are the same everywhere; only the available variables, constants and functions depend on where the script runs. The app highlights the syntax in the script editor.
Where scripts are used
The same script language runs in smart rules, notifications and the Modbus and Packet parser integrations:
| Context | What the script does | Specific to the context |
|---|---|---|
| Equation smart rule | One expression over device values. TRUE runs the true actions, FALSE the false actions. | LastState, TriggerName |
| Formula smart rule | Computes a value that is written to every output device. | LastState, TriggerName |
| Script smart rule | Reads device values and assigns new ones; each assignment becomes a device action. | Read-write variables, TriggerName |
| Formula limiting condition | Decides whether a smart rule may run. | TRUE lets the rule run |
| Notification title and message | Builds the text of a notification. | Must return text; notification constants |
| Modbus module read script | Polls the module, typically to raise device errors. | MODBUS* functions |
| Modbus device read and write scripts | Converts registers into a device value and a requested value back into registers. | The read result becomes the device value |
| Modbus initialize script | Runs once after the module connects. | Typically MODBUSWNE |
| Packet parser read and write scripts | Sends a request or a command over HTTP, FTP, TCP, UDP or MQTT; for HTTP and FTP it also processes the answer. | SENDHTTPREQUEST, SENDDATA, MQTTPUBLISH |
| Packet parser listener script | Handles every incoming TCP or UDP packet and MQTT message. | RECEIVEDMSG |
| Service attributes and actions (Modbus, Packet parser) | Diagnostic rows and buttons with parameters in the app. | Action parameters by their abbreviation |
SLEEP is rejected in smart rules and notifications (the Formula limiting condition is the exception). ADDERROR, ADDWARNING and ADDINFO work only in device scripts. What each smart rule passes to its script is on Scripts in smart rules and notifications; the integrations have their own pages, Modbus functions and Packet parser functions.
Script result
A script is a list of statements. The value of the last evaluated statement is the result of the script. RETURN ends the script early with the given value.
VAR a := 1;
VAR b := 2;
a + b;
# Result: 3The result of a device read script must fit the device value, otherwise the script fails with Return value 'X' does not match required return type constraint:
| Device value | Allowed result |
|---|---|
| Switch state, reed contact | TRUE or FALSE (numbers are accepted), NaN, Null |
| Multi-value switch state | Number (a whole number selects the state; a decimal value is not rejected), NaN, Null |
| Any other value | Number, NaN, Null |
NaN means “the value is unknown”, Null means “there is no value”. How the two behave in expressions is described in Null and NaN.
Pages in this section
The script language reference has four groups of pages: a guide to the language, the functions by area, the integration functions and reference lists.
Guide
- Syntax and control flow — statements, comments, literals,
IF,SWITCH,WHILEandDOloops,RETURN. - Data types — numbers, text, structured and enum types, collections,
.Type, type promotion,NullandNaN. - Variables and constants — device variables, group values, module variables,
VARand typed declarations, properties, context constants. - Statistics and value info — statistics of a device, Value info with the time of the last change, the previous value and the values of finished periods.
- Operators — operator list, precedence, short-circuit evaluation, Excel equivalents.
- Scripts in smart rules and notifications — Equation, Formula, Script, the Formula limiting condition and notifications.
Functions
- Logic and diagnostics —
ISNULL,ISNAN,EQUALS,SLEEP,ADDERROR,ADDWARNING,ADDINFO,DEBOUNCE. - Mathematical functions —
MIN,MAX,AVG(they skip unknown values, the…STRICTvariants do not),ABS,SIGN,SQRT,POWER,MOD,LOG,LN,DEWPOINT. - Rounding and random numbers —
ROUND,CEIL,FLOOR,RAND,RANDINT. - Interpolation and thresholds —
LINEARmaps a value from one range to another (for example a dimming curve),HYSTERESIS. - Bit operations —
GETBIT,GETBITS,SETBIT,SETBITS,GETBYTE,SETBYTE,TOBCD,FROMBCD. - Text and collections —
LENGTH,GETAT,SETAT,APPEND,INSERT,REMOVEAT,COPY,INDEXOF,REPLACE,SPLIT,COMPARE,BYTECOLLECTION,UINT16COLLECTION,BOOLCOLLECTION. - Sorting —
ORDER,ORDERINDEXand their descending and strict variants. - Date and time —
NOW,DATETIME,DATETIMEADD,TIMESPAN,LOCALTIMEZONE. - Data type conversions —
TOSTRING,TODOUBLE,TOBYTES,ENCODE,DECODE. - Colours —
HSVTORGB,RGBTOHSV. - Parsing functions —
PARSETEXT,PARSEJSON(reads a value from JSON by its path),PARSEXML. - Binary data —
PACKVALUE,UNPACKVALUE. - Encryption and hashes —
AESENCRYPT,AESDECRYPT,HASH,HMAC,RANDBYTES. - Byte encodings and checksums —
TOBASE64,FROMBASE64,TOHEX,FROMHEX,CRC16,CRC32.
Integrations
- Modbus functions — register types,
MODBUSR,MODBUSW,MODBUSWNE,MODBUSRBLOCK,MODBUSWBLOCK,MODBUSWBLOCKNE. - Modbus data types — data types of the Modbus functions, byte order variants, 64-bit types, text.
- Modbus scripts and prefetch — which scripts a Modbus module and device have, when they run, prefetch and caching.
- Packet parser functions —
HTTPREQUEST,SENDHTTPREQUEST,SENDDATA,MQTTPUBLISH,FTPDOWNLOAD,FTPUPLOAD,COMPLETESERVICEATTRIBUTE,COMPLETESERVICEACTION. - Listener scripts — the listener script of a Packet parser module,
RECEIVEDMSGandRECEIVEDBYTES.
Reference
- Script examples — complete scripts from real device templates.
- Script errors and troubleshooting — parse and run-time error messages and how to fix them.
- Script version requirements — the Core build each function needs.
- Script functions A–Z — every function with one sentence and a link.