Troubleshooting
Why an MQTT string tag value loses its first and last characters
Short answer
An MQTT receive script that maps JSON members to gateway tags by name strips one character from each end of every value it writes to a string tag, because that trim exists to remove JSON quote marks. Publish string values wrapped in double quotes, keep numeric and Boolean values bare, and every tag receives exactly what the publisher sent.

On this page
- Why does an MQTT string tag arrive shorter than the value I published?
- Which part of the receive script removes the characters?
- How do I confirm quote stripping with a test payload?
- Is the colon character in my time string the fault?
- How do I publish string values so nothing is cut?
- Will quoting strings break my numeric and Boolean tags?
- What payload shapes does this script not support?
- Questions asked
A time or date string published over MQTT reaches the gateway two characters short: 03:09:2021 lands in the string tag as 3:09:202. The broker, the topic and the colon characters are all innocent. The receive script that maps JSON members to tags by name trims one character from each end of every value it writes to a string tag, because that trim exists to remove the JSON quote marks around a string. When the publisher sends the value bare, the trim takes real data instead. Wrap every value destined for a string tag in double quotes — {"TimeFrom":"03:09:2021"} — leave numeric and Boolean members bare, and the tag receives the full text with no change to the script.
Why does an MQTT string tag arrive shorter than the value I published? #
The receive script's string-tag branch trims one character from each end of every value it writes to a string tag, because that trim exists to remove the JSON quote marks around a string; an unquoted value loses real data instead, and 03:09:2021 arrives as 3:09:202.
The failure pattern is specific. A system that has published numeric and Boolean values for a long time adds its first string destination — usually a time or date field for dispatch or scheduling — and only that tag comes back short. Nothing in the transport shortens a payload: the MQTT read hands the script the full message, and the message reaches the parse loop complete. The loss happens in the last step before the write, in a branch that runs only for string tags and applies a fixed two-character correction that assumes a quote mark at each end of the value. Any string destination is affected the same way: a shift code, a batch label or a text alarm arrives short by exactly its first and last characters.
Which part of the receive script removes the characters? #
Two lines of the value handling do it: after GETSYS TAG, "Type" reports 6 for a string tag, the script re-cuts TagValueToWrite$ with the offsets Pos1stColumn%+1 and Pos2stBraket%-2, taking one extra character off each end before the SETIO write.
For every member the script works with position searches rather than a JSON library. The SplitString$ helper splits the payload into members, and INSTR finds the first and second double quote to cut the key into TagNameToWrite$, so the key side is safe: valid JSON always quotes its keys, and those offsets always find real quote marks.
The value side starts with a universal cut. Pos1stColumn% is the position of the first colon plus one and Pos2stBraket% is the position of the first closing brace after it, so TagValueToWrite$ first receives everything between the separator and the brace. The script then loads the destination tag with SETSYS Tag, "load", TagNameToWrite$ and reads its type with GETSYS TAG, "Type". When myType$ reads "6" — the script's string tag — a second cut re-assigns TagValueToWrite$ with one more character removed at each end, and the comment on that line states the purpose: trim off quote marks. SETIO TagNameToWrite$, TagValueToWrite$ then writes it. Every other type skips the second cut and is written as SETIO TagNameToWrite$, VAL TagValueToWrite$.
How do I confirm quote stripping with a test payload? #
Publish one test member to a scratch string tag and follow the two cuts by character position: unquoted, the first cut holds the full ten characters and the second holds eight; quoted, the second cut holds the complete value. That difference is the whole fault.
Count positions from the opening brace of the member. In {"TimeFrom":03:09:2021} the separator colon sits at position 12 and the closing brace at position 23, so the first cut takes positions 13 to 22 — the full ten characters — and the string-branch cut takes 14 to 21, eight characters, dropping the leading zero at position 13 and the trailing one at position 22.
| Published member | First cut | String-branch cut | Tag receives |
|---|---|---|---|
{"TimeFrom":03:09:2021} |
positions 13–22: 03:09:2021 |
positions 14–21: 3:09:202 |
3:09:202 |
{"TimeFrom":"03:09:2021"} |
positions 13–24: "03:09:2021" |
positions 14–23: 03:09:2021 |
03:09:2021 |
The quoted member moves the closing brace to position 25 and puts quote marks at positions 13 and 24, so the first cut carries the quotes and the second cut removes exactly those two marks. The script also prints TagNameToWrite$ and the first-cut value for every member, so a console line showing the full value beside a tag holding the short one is the signature of this branch.
Is the colon character in my time string the fault? #
No. The cut positions are measured from the separator colon and the closing brace, so they remove exactly one character at each end whatever the value contains; the interior colons survive even in the truncated tag contents, which is why the damage looks like chopped ends.
Only the first colon after the start of the member is ever a search target, and it sits before the value begins, so quoting the value does not disturb the delimiter search. Colons are legal characters inside a quoted JSON string and the broker forwards them unchanged, so no escaping is needed. Keep the separator colon exactly where it is — the script depends on it — and treat every colon after it as payload.
How do I publish string values so nothing is cut? #
Wrap the value in double quotes inside the published JSON object — {"TimeFrom":"03:09:2021"} — and change nothing in the script: its string branch is built to strip exactly those two quote marks before writing, so the tag receives 03:09:2021 complete.
Only the value side changes. The member becomes an ordinary JSON string, so the broker and every other subscriber on the topic see nothing unusual. Because the correction is two fixed offsets, the same publisher-side change covers every string destination on every gateway that runs this example, and the trim finally finds the quote marks it exists to remove. If you are building or auditing the member-to-tag mapping itself, the walkthrough on writing tags on an edge gateway from an MQTT JSON payload uses the same key-matches-tag-name pattern.
Will quoting strings break my numeric and Boolean tags? #
They do not, as long as you quote only the string destinations: numeric and Boolean members bypass the trimming branch and are written through SETIO TagNameToWrite$, VAL TagValueToWrite$, which removes no characters, so the form your publisher already sends for those tags keeps working.
The trimming lives entirely in the string branch; the numeric path never re-cuts TagValueToWrite$. Keep every numeric and Boolean member in the bare form the publisher already uses, then run this verification before returning the system to service:
- Wrap every string-destination value in double quotes at the publisher; leave numeric and Boolean members bare.
- Publish one string member and read the console line that pairs
TagNameToWrite$with the first-cut value, confirming the parse reached the right member. - Read the destination tag and confirm it holds the full published text, first and last characters included.
- Publish one numeric member and one Boolean member and confirm both tags still update through the
VALbranch. - Leave the script untouched; the offsets are correct for quoted JSON strings and need no edit.
What payload shapes does this script not support? #
Two shapes are outside what this parser proves: a quoted string that contains a comma, and a nested object or array. SplitString$ divides members at every comma and the value cut ends at the first } after the colon, so both shapes break before any quote handling runs.
Both consequences follow from the parsing shown, but the original fault did not exercise them, so check each on a scratch tag before relying on the result. SplitString$ splits at every comma without looking at quotes, so a member like {"Msg":"alarm, line 3"} becomes two fragments and the fragment after the comma carries no complete key — expect a lost or corrupt write, and keep commas out of string values in this payload family.
The value cut also stops at the first closing brace after the colon, so a nested object ends the value early; publish flat members, one per tag. If the application genuinely needs nesting or embedded commas, replace the substring parser with a JSON-aware one rather than adjusting the offsets. Payload faults of this kind — the receiver mangling what the publisher meant — are the same family as extra characters in a PLC MQTT JSON payload, and they are fixed at the publisher side.
Questions asked
What happens to a JSON member whose tag does not exist on the gateway?
The script wraps the tag load in RESUMENEXT and reads the LSTERR code afterwards. When that load fails with error -4, meaning no tag carries the name from the payload, the script skips the member and continues with the next one, so a misspelled key is dropped without any write and without stopping the receive loop.
Why does publishing an empty string value not clear the tag?
The write is guarded by a check that TagValueToWrite$ is greater than an empty string, so a member with no value never reaches SETIO and the tag keeps its previous contents. To force a blank, publish a single space as the value, or handle the empty case in your own copy of the script.
Can I fix this in the script instead of in the publisher?
You can remove the extra trimming from the string branch, but then every quoted value arrives wrapped in quote marks inside the tag, and any other publisher feeding string tags on the topic has to stop quoting at the same time. Changing the payload costs nothing on the receiver side and keeps the script's existing contract intact.
Do I need to touch the numeric and Boolean handling after the change?
No. Those members run through the VAL branch, which contains no trimming, so whatever form the publisher already uses for them is the form those tags have been receiving. Change only the string members, then confirm one numeric and one Boolean tag still update before returning the system to service.


