Getting Started Cameras & Video Detection & Recording Automation & Events Actions Integration & Connectivity Network & Discovery AI & Remote Control MQTT Modbus ZeroMQ System & Administration Comparisons Use Cases Troubleshooting About & Legal
Home / Documentation / MQTT Publish Action
Knowledge base

MQTT Publish Action

MQTTPublishAction (renamed from MQTTSetStateAction) publishes messages to MQTT topics through a configured MQTT connector. Both the target topic and the payload are evaluated as Spring SpEL template expressions against the action ExecutionContext, so runtime values such as the triggering event can be interpolated into the published topic and payload.

Publish Banalytics decisions to MQTT topics

The action takes a target topic, QoS, retain flag, and a SpEL-based message template. On start it parses the topic and message templates; on each invocation it evaluates them against the current execution context, publishes the UTF-8 payload through an MQTT v3 or v5 connector, and waits for publish completion.

PUB

Publish command

Send device commands such as ON, OFF, open, close, or JSON control payloads.

RET

Retained state

Use retained messages for topics that represent latest known state or desired state.

RULE

Event bridge

Attach the action to Event Manager rules to forward Banalytics events to MQTT systems.

Adding an MQTT publishing action

01

Select the MQTT connector

Choose an MQTT v3 or v5 connector that is connected to the broker where the message should be published.

02

Set a specific topic

Use stable, explicit topics such as site/gate/mode or home/light/1/set. Avoid broad shared command topics unless all subscribers expect the same payload.

03

Choose QoS and retain

Use QoS 1 for most commands and state changes. Enable retain only for desired-state or latest-state topics.

04

Enter the SpEL message template

Plain text outside #{...} markers is published verbatim. Inside markers, expressions are evaluated against the current execution context, for example #{ #event.sourceTitle } or {"src":"#{ #event.sourceTitle }","ts":#{ #event.dateTime?.time }}.

Common MQTT publishing scenarios

CMD

Device commands

Publish simple commands to lights, relays, gates, alarm panels, PLC gateways, or other MQTT-controlled devices.

STATE

Desired state

Use retained messages for topics where new subscribers should receive the latest target state immediately.

BRG

Automation bridge

Forward Banalytics events to Node-RED, Home Assistant, cloud brokers, industrial middleware, or a local MQTTServerThing.

Retain carefully: retained commands can be delivered again when a device reconnects. This is useful for desired state but risky for one-shot actions like reset, unlock, or pulse.

QoS and retain choices

QoS 0
Use for frequent, non-critical updates where occasional loss is acceptable.
QoS 1
Good default for commands and state changes where at-least-once delivery is acceptable.
QoS 2
Use only when duplicate handling is more dangerous than the extra protocol overhead.
Retain
Enable for state topics, avoid for transient button presses, alarms, or one-shot commands.

Configuration parameters

ParameterRequiredDescriptionDefault
Title
YesDisplay name of the action inside Banalytics.None
MQTT Connector
YesReference to an MQTT connector Thing. It can be MQTTV3ConnectorThing or MQTTV5ConnectorThing.None
Topic
YesTarget MQTT topic. Evaluated as a Spring SpEL template expression; plain text is published as-is, while #{...} markers are evaluated against the action ExecutionContext.None
QoS
YesPublish quality of service: 0, 1, or 2.1
Retain Message
YesStores the last message on the broker so new subscribers receive it immediately.false
Message Template
YesPayload content. Evaluated as a Spring SpEL template expression against the action ExecutionContext, so values such as #event.sourceTitle or context variables can be interpolated into the published payload.None

Operational notes

RUN

Connector must be connected

The action fails if the selected connector is not connected when the action runs.

WAIT

Publish wait time

The publish call waits up to 10 seconds for completion. Broker outages, authentication problems, TLS issues, or network timeouts can make the action fail.

TPL

SpEL template parsing

Both Topic and Message Template are parsed on action start. A syntax error in either template prevents the action from initializing; fix the expression before re-enabling the action.

CTX

Execution context variables

Templates evaluate against a SpEL StandardEvaluationContext whose root object is the action ExecutionContext. Variables registered upstream with ctx.setVar("name", value) are accessible as #name; in event-driven rules, Event Manager exposes the triggering event as #event. Use safe-navigation (?.) for optional fields.

NULL

Null-safe evaluation

A template that evaluates to null is treated as an empty string for both the topic and the payload, so the connector still receives a publish call.

REN

Renamed from MQTTSetStateAction

The class was renamed from MQTTSetStateAction to MQTTPublishAction and the obsolete templateType selector was removed. Existing knowledge-base URLs continue to point to this page.

Related MQTT pages

Related tasks and pages