Troubleshooting
Why an MQTT receive script updates only the first JSON attribute
Short answer
The IF/ELSE chain in the receive script is mutually exclusive: once the first key matches, the second branch is never evaluated, so only one tag attribute updates. Cache each parse result in a variable, test each cached value with an independent IF statement, and exit the indexed loop on the missing-element sentinel.
On this page
Your script is not failing to parse: the IF/ELSE chain is mutually exclusive, so once the first key matches, the second branch is never evaluated and only that first attribute updates. Cache each parse result in a string variable and test each cached value with its own independent IF statement, and exit the indexed loop when the parser reports the element does not exist.
Confirm the symptom pattern #
The report shows the signature of an exclusive branch, not a broken setter. The script receives payloads shaped like { "name" : "R1", "value" : 0 } and { "name" : "AI1", "AlHiHi" : "160" }. Inside the topic handler it parses the name, then tests the alarm-limit key in the IF branch and the value key in the ELSE branch. Whichever attribute is tested first works, and swapping the order moves the failure with it. That tells you the parse and setter functions themselves are fine; the branching structure allows exactly one action per message.
An IF/ELSE chain is by definition mutually exclusive: after one condition succeeds, the remaining branches are not evaluated at all. That is correct when each message must trigger exactly one action, and wrong when a single message may carry several attributes you all want applied. The same trap appears outside scripting, for example in ladder logic where mutually exclusive conditions silently swallow a reset; the reasoning and the fix are the same.
The defect, step by step #
- The first inner IF tests the alarm-limit key. On the value-only payload it fails, so control goes to the ELSE branch and the value is written. Correct.
- On the alarm-limit payload the first IF succeeds and the alarm limit is written. Correct.
- On a message that carries both keys, only the alarm-limit branch runs; the value branch is never reached. This is where updates are silently lost.
- Worse: on the value-only payloads the roles depend purely on branch order. If the alarm-limit check had been placed after the value check in the same exclusive chain, the alarm limit could never be applied to a message that also carries a value.
- The indexed loop increments its counter after one dispatch and keeps going to its fixed limit even when the parser has already said the element does not exist, so exhausted input burns thousands of no-op iterations.
Cache parse results before testing them #
Call the JSON parse function once per key per index, store the result, and test the stored string. Do not call the parse function in the condition and again in the assignment: apart from doubling the work, the second call makes the log show something different from what the branch actually tested. Then replace the exclusive chain with independent IF statements so a message containing both supported attributes invokes both setters:
MQTTRECEIVEMSG:
MessageQty% = Mqtt "READ"
IF MessageQty% > 0 THEN
MsgTopic$ = MQTT "MSGTOPIC"
MsgData$ = MQTT "MSGDATA"
@Log("[MQTT SCRIPT] Message '" + MsgData$ + "' on topic " + MsgTopic$)
IF MsgTopic$ = SetIOTopic$ THEN
InputStr$ = MsgData$
indexElement% = 0
Loop:
ParsedName$ = @ParseSimpleJson$(InputStr$, "name", indexElement%)
ParsedAlHiHi$ = @ParseSimpleJson$(InputStr$, "AlHiHi", indexElement%)
ParsedValue$ = @ParseSimpleJson$(InputStr$, "value", indexElement%)
IF ParsedName$ = "ELEMENT DOES NOT EXIST" THEN GOTO EndLoop
TagName$ = ParsedName$
IF ParsedAlHiHi$ <> "ELEMENT DOES NOT EXIST" THEN
@SETAlHiHi(TagName$, ParsedAlHiHi$)
ENDIF
IF ParsedValue$ <> "ELEMENT DOES NOT EXIST" THEN
@SETTAGVALUE(TagName$, ParsedValue$)
ENDIF
indexElement% = indexElement% + 1
IF indexElement% < 1000 THEN GOTO Loop
EndLoop:
ENDIF
GOTO MQTTRECEIVEMSG
ENDIF
END
Keep the topic check separate from the payload dispatch: match the configured write topic first, then parse. SetIOTopic$ stands for the SETIO topic string you already assemble from the device identifier and serial number in your script.
Worked example: how the loop now ends #
Take the payload { "name" : "AI1", "AlHiHi" : "160" }. At index 0 the cached name result is the string AI1, so the loop continues; the cached alarm-limit result is 160, so that setter runs, and the cached value result is the missing-element text, so the value setter is skipped by its own test rather than by an ELSE. At index 1 the cached name result is the missing-element text, so the loop exits.
Count the iterations: 2 elements produce 3 parse rounds (index 0, index 1, and the exit test) instead of running to the 1000 cap — a reduction from 1000 iterations to 3, about 99.7% less work on this payload. The same arithmetic holds for any payload: iterations equal the element count plus 1, because the sentinel ends the loop instead of the fixed limit.
Scaling the attribute dispatch #
There is no predefined function that discovers an arbitrary property name and automatically routes it to the matching tag operation, so each supported key still needs an explicit branch that maps it to its setter. You can write a helper that extracts a key by delimiter position, but a delimiter-based extractor is not a general JSON parser: it breaks on nested objects, repeated keys or values containing the delimiter, so do not treat it as universal. Adding a new attribute means one more cached result and one more independent IF — linear, explicit and verifiable.
Verify each update #
- Send the value-only payload and confirm the cached value result differs from the missing-element text before checking the tag value in the runtime.
- Send the alarm-limit payload and confirm the cached alarm-limit result differs from the missing-element text before checking the limit.
- Send a combined payload with both keys and confirm both setters ran — this is the case the old chain could never handle.
- Log the topic, the raw message and the three cached strings each iteration. If parsing succeeds but the tag does not change, the fault is in the setter or its accepted input type, not in MQTT reception or branch selection.
One caution on types: the sample payloads show the value unquoted and the alarm limit quoted as a string, so do not infer conversion rules from the examples — confirm the accepted input type of each setter in your runtime before passing a cached string to it.
Decision: if a message may update more than one attribute, use cached parse results with independent IF statements and a sentinel-based loop exit. If your contract truly allows exactly one attribute per message, an exclusive chain is defensible — but still cache the parse results so your log shows exactly what each branch evaluated.
Questions asked
How many loop iterations should the indexed parse loop use?
Do not rely on a large fixed limit. Parse the name key at the current index and exit the loop as soon as the parser returns the missing-element text. For a payload with two elements this ends the loop on the third iteration instead of running thousands of pointless iterations on malformed or exhausted input.
Why cache the parse result in a variable instead of calling the parser inside the IF condition?
Calling the parser once per key and storing the result guarantees the value you test is exactly the value you pass to the setter and write to the log. Parsing twice doubles the work and can leave the log showing something different from what the branch actually evaluated.
Can I write one generic branch that handles any JSON key automatically?
No. There is no predefined function that discovers an arbitrary property name and routes it to the matching tag operation, and a delimiter-based extractor is not a general JSON parser because it breaks on nested objects, repeated keys or values containing the delimiter. Each supported key needs its own cached result and its own independent IF that calls its setter.