Pick a topic/payload that you want to handle in a script. Set up a trigger for that topic, matching topic components as necessary. Use the "Queue Last Message for Dispatch" MQTT Action. You will need to provide a message type. That's probably a bad name for this. More like "Unique Identifier". Something that applies to this message only, or any other message that you want to handle identically. That's all for the Trigger.
Now you need a script. This can be embedded or external. I recommend an embedded script for low-frequency messages, or any message that will take more than a second or two to process. Embedded scripts will need to be triggered by an event or a schedule. Or it could be a plugin. Here's an example:
- Code: Select all
import json
mqttPlugin = indigo.server.getPlugin("com.flyingdiver.indigoplugin.mqtt")
if mqttPlugin.isEnabled():
props = {
'message_type':"#Test#"
}
while True:
message_data = mqttPlugin.executeAction("fetchQueuedMessage", deviceId=1867973662, props=props, waitUntilDone=True)
if message_data == None:
return
indigo.server.log("Queue Fetch, version = {}, message_type = {}, topic_string = {}".format(message_data["version"], message_data["message_type"], message_data["topic_string"]))
device_data = json.loads(message_data["payload"])
indigo.server.log("Queue Fetch, device_data = {}".format(device_data))
This script will process all pending queued messages for the specified message type. It exits when the queue is empty. If you're running this as an external script, you would want to change it around a little so that it just does a short sleep and then checks the queue again.
In this case, it knows the payload is JSON, so it process it as such. After that, it would do whatever it wants to with the payload data.
This was designed to decouple the incoming message triggers from processing them, to ensure that the plugin doesn't get hung up and miss incoming data.