Publish and Receive MQTT Messages

Send and receive metadata from the pipeline to a MQTT topic at a periodic interval or based on a trigger.

Overview

The Publish and Receive MQTT Messages node allows for sending and receiving metadata from the pipeline to a MQTT topic at a periodic interval or based on a trigger. This integration is useful for applications requiring real-time data exchange and monitoring.

Properties

PropertyDescriptionTypeDefaultRequired
publish_topicMQTT topic to publish to. Use Jinja2 template syntax to customize the topic using metadata.stringnullNo
publish_with_retainIf true, marks all published messages to be retained by MQTT broker.boolfalseNo
trigger_typeWhen to collect metadata. Options: custom_trigger (Collect metadata when a custom trigger is fired.), auto_trigger (Collect metadata when significant node properties change.), interval (Collect metadata periodically.), startup (Collect metadata on the first frame of the source.), shutdown (Collect metadata when the deployment shuts down.)enumauto_triggerYes
triggerSend a MQTT message when this condition evaluates to true. Conditional on trigger_type being "custom_trigger".trigger-conditionnullNo
intervalHow frequently to send message. Set to 0 to publish every time metadata is collected.number0Yes
formatMessage Format. Options: json (Dynamic keys) (Send metadata as JSON with dynamic keys. Use certain attributes, e.g. ROI names, as keys. Useful for JSON path operations.), json_static (Send metadata as JSON with static keys. Convert all attributes to a named static key.), custom_body (Send custom body.)enumjsonYes
custom_body_templateUse Jinja2 template syntax to customize the body of the message. Conditional on format being "custom_body".textYes
subscribe_topicComma separated list of MQTT topics to subscribe to. Use Jinja2 template syntax to customize the topic using metadata. Inserts metadata from topic into node metadata.stringnullNo
sanitize_messagesReplace invalid characters in topic name with underscores so you can use them in trigger conditions.booltrueNo
persist_last_messageIf true, retains latest message in node metadata for each topic.booltrueNo
mqtt_protocolMQTT Protocol. Options: MQTT, MQTT with SSL, MQTT over Websocket, MQTT over Websocket with SSLenummqttNo
mqtt_hostMQTT HoststringnullYes
mqtt_portPort number to connect to. Leave blank to use default port based on MQTT Protocol.stringnullNo
credential_typeCredential Type. Options: username_password, certificateenumusername_passwordNo
usernameUsername. Conditional on credential_type being "username_password".stringnullNo
passwordPassword. Conditional on credential_type being "username_password".stringnullNo
certificateCertificate. Conditional on credential_type being "certificate".textnullYes
certificate_keyCertificate Key. Conditional on credential_type being "certificate".textnullYes
validate_certValidate SSL Certificate?boolfalseNo
lastwill_messageLastwill MessagetextnullNo
lastwill_topicLastwill Topic. Use Jinja2 template syntax to customize the topic using metadata.stringnullNo
debugLog Debug Messages?boolfalseNo

Template Syntax

custom_body_template, lastwill_message, lastwill_topic, publish_topic, and subscribe_topic uses Jinja2 Template Syntax for customizing the body of the MQTT message.

Variables available to the template:

VariableDescription
metaFrame metadata. Contains keys like nodes, objects, and other custom metadata inserted by Custom function node.
application_idThe application ID of the deployment that produced this metadata.
application_nameThe application name of the deployment that produced this metadata.
deployment_idThe deployment ID of the deployment that produced this metadata.
deployment_nameThe deployment name of the deployment that produced this metadata.
gateway_idThe gateway ID of the deployment that produced this metadata.
gateway_nameThe gateway name of the deployment that produced this metadata.

Example templates

Static text content:

Text content

Reference specific metadata:

{{ deployment_id }}

Metadata reinserted as is:

{{ meta | tojson }}

Output Metadata

Metadata PropertyDescription
topicsThe MQTT topics subscribed to or published.
topics.<topic>The specific MQTT topic being referenced.
topics.<topic>.last_messageThe last message received for a topic.
topics.<topic>.messagesAll messages received for a topic.
topics.<topic>.topic_deltaIndicates if there has been a change in the topic.
topics.any_topic_deltaTrue if any topic has changed.
topics.all_topic_deltaTrue if all topics have changed.

Example JSON

{
    "nodes": {
        "mqtt1": {
            "type": "mqtt",
            "topics": {
                "topic1": {
                    "last_message": "message",
                    "messages": ["message"],
                    "topic_delta": true
                }
            },
            "any_topic_delta": true,
            "all_topic_delta": true
        }
    },
}