Skip to content

feat(victron_mqtt): send the Venus MQTT keep-alive (1.2.0) - #268

Merged
frahlg merged 2 commits into
mainfrom
fix-256
Nov 22, 2026
Merged

frahlg merged 2 commits into
mainfrom
fix-256

Conversation

@frahlg

@frahlg frahlg commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Builds on #266 (victron_mqtt 1.1.1, the battery sign), now on main.

What was wrong

Venus OS publishes MQTT values only while some client sends a read request
to R/<portal ID>/keepalive. victron_mqtt sent none, so on a GX with no
other MQTT client its values stopped arriving 60 s after the last
keep-alive from anyone, and the driver went stale.

Sources

  • victronenergy/dbus-flashmq README, "Keep-alive" (in the manifest's
    upstream_docs): the topic R/<portal ID>/keepalive; a 60 s timeout; an
    empty payload asks for a full republish, which ends with
    N/<portal ID>/full_publish_completed; the option
    {"keepalive-options": ["suppress-republish"]} keeps the GX awake without
    one. Victron suggests that option on a timer and an empty payload "when
    the state of the program requires it". Its example sends one every 30 s.
  • dbus-flashmq source: Item::should_be_retained publishes
    N/<portal ID>/system/0/Serial retained, the one topic a client gets
    before any keep-alive. State::handle_keepalive keeps the GX awake for
    60 s per keep-alive.
  • FTW origin/master, go/internal/drivers/lua.go and host.go:
    host.mqtt_publish(topic, payload) returns nothing when it sends and an
    error text when it refuses. mqtt_publish is in the linux-edge profile
    in spec/host-api-profile.json, so Blixt has it too.

Change (victron_mqtt 1.2.0)

  • Subscribes to N/+/system/0/Serial and N/+/full_publish_completed, and
    takes the portal ID from the topics it receives. When system/0/Serial
    names another portal ID, it follows the new GX, forgets the old one's
    values and asks the new one for a full republish.
  • Sends R/<portal ID>/keepalive every 30 s, also on polls that bring no
    messages. The payload is suppress-republish, except at first, when the
    GX speaks again after 60 s of silence, and every 120 s. Then it is empty,
    which asks for a full republish.
  • With that republish in place, the system's SoC, voltage, current and
    battery temperature now have a bound, as the driver's comment asked for.
    A full republish resends every value the GX has, so a value that arrived
    well before the latest full_publish_completed was not in it and is left
    out. Each poll has a stamp (its clock and its number); a value counts as
    resent when it arrived within two polls or 5 s of the completion, so a
    republish that straddles a poll on a host polling every 5 s or slower is
    not misread. Before any full republish has completed, they behave as
    before.
    The BMS limits keep their own 60 s rule.
  • When the host refuses to publish, the driver logs one warning, keeps
    reading and tries again every 30 s, never faster.

Minor version, since the driver now publishes to the GX.

Tests

drivers/tests/test_victron_mqtt_keepalive.py runs 13 harness scenarios:
subscriptions, no keep-alive before a portal ID, the first one full, the
30 s and 120 s timing on idle polls, a full republish when the GX speaks
after a silent link, refusal (one warning, retry after 30 s, recovery), no
bound without a completed republish, a value a republish left out, a value
every republish resends, a republish straddling a 5.5 s poll, a changed
portal ID, and no telemetry from an idle poll. The harness
replaces the mock's mqtt_publish with FTW's return convention. Seven of
the first eleven fail on #266's driver; the four that pass there check
behaviour that must not change. The 5.5 s poll and portal ID scenarios fail
on this PR's first commit and pass now.

  • make check on main c9ac63d: 5243 passed, 928 skipped.

FTW refuses this publish for a channel install

FTW's allowWrite refuses every mqtt.publish from a driver that runs
under a runtime policy, and FTW binds one to each control: false driver
installed from the signed channel ("read-only driver cannot write"). The
bundled copy runs without a policy and can send it. So once this version
reaches a site through the channel, FTW will refuse the keep-alive and the
GX keeps publishing only while another client keeps it awake, as today.
The driver handles that, but the fix only fully works on FTW after a Core
change, for example letting a read-only MQTT driver send a read request
(R/...) the way a read-only cloud driver may send its sign-in POST.
That is a decision for Core, not made here.

Not verified on hardware

Not run against a real GX. To check on one with no other MQTT client
(no Node-RED, Home Assistant or VRM window open): watch FTW's Victron
values for several minutes. Before this change they stop after about a
minute; with it they keep coming. If FTW's log shows "refused the
keep-alive", the host blocked it as described above.

Refs #256

🤖 Generated with Claude Code

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-22T12:24:25.269954Z 3fa6fd0 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3fa6fd050f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread drivers/lua/victron_mqtt.lua Outdated
Comment on lines +214 to +215
if at_ms == nil or at_ms < last_full_publish_ms - REPUBLISH_SLACK_MS then
return nil

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Track republish membership instead of completion-relative age

If a full republish spans more than five seconds across polls, values received near its beginning are incorrectly treated as absent when full_publish_completed arrives. Reproducing this with the Lua harness—receiving SoC at 2 s and completion plus battery power at 8 s—drops the freshly republished SoC from battery telemetry. An unchanged SoC or temperature can then remain missing until the next full republish, normally 120 seconds later. Track whether each value arrived during the republish, rather than assuming every included value arrived within five seconds of completion.

Useful? React with 👍 / 👎.

@frahlg
frahlg changed the base branch from fix-255 to main November 22, 2026 12:39
frahlg and others added 2 commits November 22, 2026 13:39
Venus OS publishes MQTT values only while some client sends a read
request to R/<portal ID>/keepalive (dbus-flashmq README). The driver sent
none, so on a GX with no other client its values stopped after 60 s.

The driver learns the portal ID from the retained
N/<portal ID>/system/0/Serial, sends a keep-alive every 30 s with
suppress-republish, and an empty payload (a full republish) at first,
when the GX speaks again after a silent link, and every 120 s. A
system SoC, voltage, current or temperature that the last completed full
republish did not resend is left out. When the host refuses to publish,
as FTW does for a read-only driver from the signed channel, the driver
logs once, keeps reading and tries again every 30 s.

Harness tests cover the topic, payloads, timing, refusal and the
republish rule. Not checked on a real GX.

Refs #256

Signed-off-by: Fredrik Ahlgren <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The republish rule assumed a poll about every second: a value that came one
poll before full_publish_completed on a host polling every 5 s or slower was
dropped as if the GX no longer had it. Each poll now has a stamp, its clock
and its number, and a value counts as resent when it arrived within two
polls or 5 s of the completion.

When N/<portal ID>/system/0/Serial names another portal ID, the driver now
follows the new GX, forgets the old one's values and asks the new one for a
full republish.

Harness tests: a republish straddling a 5.5 s poll, and a changed portal ID.
Both fail on the previous commit.

Refs #256

Signed-off-by: Fredrik Ahlgren <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant