How-to
Checking MQTT connection status in an Ewon Flexy BASIC script
Short answer
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.

On this page
- How do you read the MQTT connection status in a Flexy BASIC script?
- Which returned value means the MQTT link is operational?
- Should the script force the MQTT reconnect itself?
- How do you log an overnight outage without flooding the log?
- What do you check when the Flexy never reconnects by itself?
- Does the status read tell you the MQTT QoS level?
- What should the script do for each status result?
- Questions asked
You do not need to infer the MQTT link state from failed publishes. On an Ewon Flexy running firmware 14.x or later, a BASIC script reads the state directly with _status% = MQTT "Status": the returned value 5 means the connection is operational, and any other value means it is not. Reconnection stays under firmware control, so the script's job is monitoring and logging. QoS is a separate publish setting that this read does not report.
How do you read the MQTT connection status in a Flexy BASIC script? #
From firmware 14.x onward, you read the state with the documented command _status% = MQTT "Status", which stores an integer result in a variable of your choice for use anywhere the script needs the link state. The command and its quoted word are the documented parts; the variable name is yours, and _status% is only the name the vendor's guidance uses.
- Pick an integer variable to hold the reading; any integer name works, and
_status%keeps the example recognizable. - Place the read where the script runs repeatedly, or anywhere a branch should depend on the link.
- Compare the stored value with a plain integer test; the operational value is fixed, and that comparison is the entire integration.
Which returned value means the MQTT link is operational? #
The test is binary: a returned value of 5 means the MQTT connection is operational at the instant of the call, and any other value means it is not, with no further meanings documented for the other numbers. Do not build a cause table from values you happen to observe, because the documentation defines two outcomes only.
| Returned value | Meaning | What it establishes |
|---|---|---|
| 5 | MQTT connection operational | The session to the broker is up at the instant of the read |
| Anything else | MQTT connection not operational | The session is down; the number does not encode the cause |
The vendor's support discussions show the identical test inside a function named ONMQTTSTATUS, checking IF $status% = 5 THEN, so the same binary comparison holds wherever you take the reading.
Should the script force the MQTT reconnect itself? #
No: the Flexy firmware manages MQTT disconnection and reconnection, the documented guidance is to do nothing about reconnection in the script, and a non-operational reading is a signal to record, not a trigger to act on. A script-side retry loop of sleep, re-check and republish duplicates work the firmware already does and can interleave with the firmware's own session setup.
Worse, a script that reconnects on its own masks the one fact you need for diagnosis: whether automatic recovery works. Leave the recovery to the firmware and let the script watch.
How do you log an overnight outage without flooding the log? #
Poll on a fixed interval, for example every 60 seconds, and write a log entry only when the returned value changes, so one night with a single drop produces three lines instead of hundreds.
- Call the status read where the script runs repeatedly, on the interval you chose.
- Keep the previous reading in an integer of your own, named
prevStatus%in this example and initialized to -1, a sentinel picked because the unit does not return it, so the first read always logs. - When the fresh reading differs from the stored one, log the new state with a timestamp and store it.
- Touch nothing about reconnection; the log exists to show when the link dropped and whether the firmware restored it.
_status% = MQTT "Status"
IF _status% <> prevStatus% THEN
IF _status% = 5 THEN
PRINT "MQTT link operational"
ELSE
PRINT "MQTT link not operational"
ENDIF
prevStatus% = _status%
ENDIF
_status% is an integer set by the documented read, prevStatus% is the illustrative integer holding the previous state, and the PRINT lines are example output.
Worked example with illustrative numbers: with a 60-second interval, an unattended window from 22:00 to 06:00 is 8 h × 3600 s/h = 28800 s, so the script performs 28800 s ÷ 60 s = 480 reads. Suppose the mobile provider drops the data path at 02:10 and the firmware restores the session at 02:25: the outage lasts 15 min × 60 s/min = 900 s, so 900 s ÷ 60 s = 15 consecutive reads return a non-operational value. Transition-only logging turns the 480 reads into three lines — up at the start, down at the first read after 02:10, up again at the first read after 02:25 — and the worst-case detection latency is one interval. Recovery time belongs to the firmware and the data path, not to the script.
Prove the monitor with a single interruption: pull the WAN link on a bench unit, or during a maintenance window on the production gateway, because the test stops live data. The console must show the not-operational line within one interval; restore the link, and the operational line returns with no script action. That sequence is the pass criterion.
What do you check when the Flexy never reconnects by itself? #
When the reading stays non-operational and the firmware does not restore the session, move off the script and check the internet path first, because that is the documented suspect when a Flexy on firmware 14.x never comes back on its own.
- Data path: confirm the Flexy's internet connection is up — on a mobile unit, the data SIM's session and the signal. If the data path is down, the MQTT symptom is a consequence; repair the connection and the firmware reconnects.
- Broker reachability: verify the configured address and port from the Flexy, and that the broker itself is running.
- Name resolution: if publishes reach the broker by IP address but fail by hostname, the fault is DNS, traced in why an Ewon Flexy 205 publishes MQTT by IP but not hostname.
- TLS: a status that parks at 3 right after you enable TLS is a distinct symptom with its own fix, described in why an Ewon MQTT status stays at 3 after enabling TLS.
- Script logic: only when the four checks above pass, examine the publish calls themselves.
Does the status read tell you the MQTT QoS level? #
No: QoS is a separate publish setting, the Flexy supports all three MQTT QoS levels 0, 1 and 2, and the status value reports only whether the connection is operational, never which level a message used. In MQTT, QoS 0 sends at most once, QoS 1 sends at least once against an acknowledgement, and QoS 2 sends exactly once through its full handshake; choose per tag, not per link state. If you rely on buffering to carry data through outages, a replay that freezes is its own failure with its own trace, covered in why buffered MQTT replay freezes on an edge gateway.
What should the script do for each status result? #
The decision has three cases: keep logging while the link is operational, log both transitions when the firmware recovers a drop on its own, and escalate to the internet-path checks when it never does.
| Reading over time | What it means | Script decision |
|---|---|---|
| Operational on every read | The link is healthy | Log state changes only |
| Non-operational, then operational again | The firmware reconnected after a drop | Log both transitions; take no action |
| Non-operational with no recovery | The firmware cannot restore the session | No script retries; run the data-path, broker, DNS and TLS checks |
That is the whole division of labor: the read gives you the fact, the firmware owns the recovery, and the script owns the record.
Questions asked
Does the MQTT Status command work on firmware older than 14?
The read is documented from firmware 14 onward, so an older build gives the script no supported way to query the link. The workable path is upgrading to a release in that series, after which the same command and the same value test apply; guessing the state from failed publishes is not a substitute.
Does an operational status prove the broker received my message?
No. The value reports the session state at the instant it is read, not the fate of any message. Delivery evidence comes from the QoS machinery instead: QoS 0 offers none, QoS 1 confirms with an acknowledgement, and QoS 2 completes its handshake. Use the status read for link health and the acknowledgements for delivery.
Does polling the status faster make the Flexy reconnect sooner?
No. The command is a passive query: it reports the state and plays no part in the reconnection, which the firmware runs on its own schedule. A shorter interval only narrows the detection latency in your log; it does not shorten the outage, and a one-minute interval is a sensible monitoring choice.


