Filter Scripts for Topics

By default, MQTT push notifications about received MQTT data are displayed directly in the “Messages” window, which is suitable for simple text messages. However, MQTT notifications can also be binary, XML, JSON, or otherwise encoded. Here, JavaScript-based filter scripts offer the possibility to analyze and format the notifications in order to display the desired data in the form of a text message.

Filter scripts can be accepted via text input or Copy/Paste. In addition, they are stored on the server so that all mobile devices of an account display the identical content. The filter scripts are executed on the mobile device during display. It is possible to test scripts in advance. Alternatively, a JavaScript can be developed, tested and adopted in a web browser.

To fiter messages, implement the filterMsg() function. The original message is contained in the “msg.text” parameter and was also assigned to the variable “content”.

The variable “content” is returned to the calling program and determines the content to be displayed.

Important:

The content of “content” must be of type string, i.e. if necessary, numeric values must be converted. Avoid complex code and actions that are not used to filter the message.

The result of the test run is displayed in the editor status line.

If you have already received news for your topic, the value of the last message will be preset but can be overwritten.

Additional filter scripts

The following examples show how data is extracted and formatted from messages with different structures. You can also use ( > Insert Example > <Examples> > Insert Example > <Examples>) to insert the examples into your code and customize them:

JSON

Split

Regular Expression

Binary

Hexdump

Variables

Variables that can be accessed from the filterMsg() function:

Parameter Description
msg.receivedDate Time at which the message was received (JavaScript Date object)
msg.topic Message topic
msg.text Message content (original message interpreted as UTF-8 string, non-displayable characters as well as invalid values are replaced or marked by the � character
msg.raw JavaScript ArrayBuffer object containing the original message
acc.user MQTT user name
acc.mqttServer MQTT server
acc.pushServer MQTT push server
view.setBackgroundColor(<value>) Message background color, e.g. using the color constant MQTT.Color.LT_GREEN (see details)
view.setTextColor(<value>) Message text color, e.g. 0xFF662233 (in hexadecimal ARGB notation with FF defining an alpha value of 100%)