IMD SolutionComputing LibraryShop main store

Troubleshooting

Flexy BASIC ThingsBoard RPC invalid parameter 5 explained

Edge computing and IIoT Published 5 min read By IMD Solution engineering team

Short answer

A ThingsBoard RPC command arrives on MQTT as one JSON object carrying method and params. Search for a command name that is not a key and INSTR returns zero; the next INSTR call takes that zero as its start position and raises invalid parameter (5). Parse method at collection index 0, compare the returned string against each command you support, and read params only inside the matching branch.

On this page
  1. What one RPC message contains
  2. Why the parser raises invalid parameter (5)
  3. Worked example: routing the tbsvSW1 command with params 60.17
  4. The receive loop around the parser
  5. When ELEMENT DOES NOT EXIST still appears
  6. Verify the fix
  7. Questions asked

A ThingsBoard RPC command reaches the Flexy over MQTT as one small JSON object: {"method":"tbsvSW1","params":60.17}. The command name is the value of method and the value to apply sits in params, so the script reads two keys from one object at collection index 0. The invalid parameter (5) failure comes from a different approach: searching the payload for the command name as though the name were a key. The name is not there, INSTR returns 0, and the next INSTR call receives that 0 as its start position, which stops the script. Parse method, compare its value with each command the script supports, and read params inside the matching branch, with no loop over the object.

What one RPC message contains #

Each command the dashboard sends produces one JSON object with the same two keys:

  • method — the command name the sending widget chose. In the tested setup the analog command arrives as tbsvAI1 and the switch command as tbsvSW1.
  • params — the value the command carries, sitting after the colon of its key.

The command name travels as the value of method, never as a key of its own. One command gives one object with one method and one params, so a payload that carries the analog command cannot also contain a key named tbsvSW1.

Why the parser raises invalid parameter (5) #

When the script searches for the command name directly, the failure runs in four steps:

  1. The search asks INSTR for the position of tbsvSW1 inside a payload that actually carries {"method":"tbsvAI1","params":60.17}.
  2. That string is not present, so INSTR returns 0.
  3. The next INSTR call takes the returned 0 as its start position.
  4. The interpreter rejects a start position of 0 and stops the script with invalid parameter (5).

The error therefore reports the second search, while the real fault is the first: the script asked for a key the payload does not carry. Routing on the value of method removes the failed search, because the script never looks for a command name as a key again.

Worked example: routing the tbsvSW1 command with params 60.17 #

Inputs, taken from the message flow the dashboard produces:

  • MsgData$ = {"method":"tbsvSW1","params":60.17} — the string MQTT "MSGDATA" returns for one switch command; 35 characters long.
  • Supported commands: tbsvAI1 and tbsvSW1.
  • Collection index 0, because the payload holds a single object.

The rule in words: find the first opening brace and the first closing brace, take the characters between them as the element, locate the requested key, then read the characters between the quotes after its colon. Read method first, compare the returned text with each supported command, and read params only in the branch that matches.

The arithmetic on the 35 characters:

  • INSTR 1, MsgData$, '{' returns 1, because the opening brace is the first character.
  • INSTR 1, MsgData$, '}' returns 35, because the closing brace is the last character, so the element spans characters 1 TO 35.
  • The key method starts at position 3.
  • Its value sits between the quotes at positions 12 TO 18, so method$ becomes tbsvSW1.
  • The colon in front of the params value sits at position 29, and the characters at positions 30 TO 34 read 60.17, which converts to the number 60.17.
MsgData$ = '{"method":"tbsvSW1","params":60.17}'

method$ = @ParseSimpleJson$(MsgData$,"method",0)
params$ = ""

IF method$ = "tbsvAI1" THEN
   params$ = @ParseSimpleJson$(MsgData$,"params",0)
   PRINT "tbsvAI1 value is " + params$
ENDIF

IF method$ = "tbsvSW1" THEN
   params$ = @ParseSimpleJson$(MsgData$,"params",0)
   PRINT "tbsvSW1 value is " + params$
ENDIF

Result: method$ equals tbsvSW1, exactly one branch runs, and the console prints the switch value as 60.17 while the analog branch stays silent. The decision that follows: a payload holding one object is always parsed at collection index 0, and you raise the index by one per additional opening brace only if a message ever stacks several objects, which a single RPC command never does. If any key search returns 0, have the parser return ELEMENT DOES NOT EXIST and stop there; never pass that 0 on as a start position. The parser returns text, so convert the params value to a number with the numeric conversion your BASIC dialect provides before it drives an output or a setpoint.

The receive loop around the parser #

Mqtt "READ" returns the number of pending messages into MessageQty%. Read a message only when that count is greater than 0: take the topic with MQTT "MSGTOPIC", the payload with MQTT "MSGDATA", hand the payload to the parser, and jump back while messages remain.

MQTTRECEIVEMSG:
MessageQty% = Mqtt "READ"

IF MessageQty% > 0 THEN
   MsgTopic$ = MQTT "MSGTOPIC"
   MsgData$  = MQTT "MSGDATA"
   PRINT "RPC on " + MsgTopic$ + ": " + MsgData$
   GOTO MQTTRECEIVEMSG
ENDIF

Each pass consumes one message, so the loop runs at most MessageQty% times before the count reaches 0 and the script falls through. If the count stays at 0 while the dashboard is definitely sending, check the MQTT connection status in an Ewon Flexy BASIC script before touching the parser.

When ELEMENT DOES NOT EXIST still appears #

Work through these checks before changing the routing logic:

  • Log the raw MsgData$ and confirm this message really carries method and params. A payload that no client can read points back at the sender, the same way a JSON parse error on a Sparkplug B NBIRTH payload traces to the format the publisher emits.
  • Confirm no extra characters ride along outside the braces; the fix for extra characters in a PLC MQTT JSON payload shows what that looks like and how to strip them.
  • Confirm the collection index passed to the parser is 0 and was not left incremented by an earlier message.
  • Confirm the compared text matches the method string character for character. The dashboard sends whatever method name its widget is configured with, so take the expected names from the logged payload rather than from a default list.

The mirror-image fault on the same MQTT path is a receive script that updates only the first JSON attribute; there as here, the fix is to parse the keys the payload actually carries.

Verify the fix #

Run this sequence after the change:

  1. Send one RPC command for each supported method from the dashboard, the analog command first and the switch command second.
  2. Read the logged payload line and confirm the string matches what the widget sent.
  3. Confirm exactly one branch runs per command: the switch command must leave the analog branch untouched.
  4. Confirm the printed value equals the number carried in params for that command.
  5. Send a method name the script does not list and confirm no branch runs and no error appears.

When all five pass, the script routes every command the dashboard can send and never builds an INSTR call from a failed search again.

Questions asked

Why does my Flexy BASIC parser report invalid parameter (5)?

The script searched the payload for a command name as if the name were a key. That name is absent, INSTR returns zero, and the following INSTR call receives zero as its start position, which the interpreter rejects as invalid parameter (5). Parse the keys method and params at collection index 0 and compare the returned method value with each command name instead.

How do I get the value out of a ThingsBoard RPC message?

Read the payload with MQTT MSGDATA, extract method at collection index 0 to identify the command, then extract params at index 0 inside the branch that matches that method. The parser returns text, so convert the value to a number before it drives an output. A single RPC command is one JSON object, so no loop over the collection is needed.

What does ELEMENT DOES NOT EXIST from the parser mean?

The parser could not find the requested element in the payload string. Log the raw message and check that method and params are present in it, that no stray characters sit outside the braces, and that the collection index passed was 0. A payload that never parses points at the sending side, not at the routing logic.

Why does my command branch never run?

The text compared against method must match the method name the dashboard widget actually sends, character for character. A widget configured with a different method name than the one the script tests skips every branch without raising an error. Take the expected names from the logged payload line and send one test command per method name.

Keep reading

  • How-to

    Checking MQTT connection status in an Ewon Flexy BASIC script

    On an Ewon Flexy with firmware 14.x or later, a BASIC script reads the MQTT link state with the MQTT Status command: a returned 5 means the connection is operational and any other value means it is not. The firmware owns disconnection and reconnection, so the script only monitors and logs, and you investigate the internet path when the broker never comes back on its own.

    Edge computing and IIoT

  • Troubleshooting

    Why a Flexy BASIC script publishes MQTT once then stops

    The SENDDATA timer handler publishes only when MQTT STATUS returns 5, and the script never calls MQTT CONNECT again after a failure, so one LSTERR 28 at startup or any later drop silences every following cycle. Trace STATUS on each cycle, add a controlled reconnect to the connect label, and move the SUBSCRIBE loop after CONNECT.

    Edge computing and IIoT

  • How-to

    Buffering MQTT messages while the broker connection is down

    From firmware 14 the Flexy can detect its own MQTT disconnection from BASIC: the script publishes live while the connection is up, appends each payload as a line to /usr/mqttmsg_db.txt while it is down, and replays the file at QoS 2 when the connection returns. The 5000000-byte cap covers roughly a day of outage at a 5-second publish rate for 300-byte payloads.

    Edge computing and IIoT

Fix InfluxDB invalid boolean from a NAS write