Queued message delivery for scripts or plugins

Posted on
Wed Aug 07, 2019 7:00 pm
FlyingDiver offline
User avatar
Posts: 7217
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Queued message delivery for scripts or plugins

Just released 0.0.7, and the major feature added is what I refer to as queued message delivery. This allows Python scripts, or other plugins, to get MQTT messages asynchronously. So here's an example.

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.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Wed Aug 07, 2019 7:11 pm
FlyingDiver offline
User avatar
Posts: 7217
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Queued message delivery for scripts or plugins

Here's the external script version:

Code: Select all
#! /usr/bin/env python
# -*- coding: utf-8 -*-
####################

import json
import time
import indigo

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:
            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))
        else:
            time.sleep(1.0)

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Wed Aug 07, 2019 7:36 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Queued message delivery for scripts or plugins

Cool! Almost makes me want to go find some MQTT device just to play with... ;)

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed Aug 07, 2019 7:37 pm
FlyingDiver offline
User avatar
Posts: 7217
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Queued message delivery for scripts or plugins

jay (support) wrote:
Cool! Almost makes me want to go find some MQTT device just to play with... ;)


I have an Amazon order in for some toys...

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Thu Aug 08, 2019 1:09 am
autolog offline
Posts: 3988
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Re: Queued message delivery for scripts or plugins

Thanks for the update. :)

fyi, the version number of Release .7 is still it at .6 :)

Posted on
Thu Aug 08, 2019 3:42 am
FlyingDiver offline
User avatar
Posts: 7217
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Queued message delivery for scripts or plugins

autolog wrote:
Thanks for the update. :)

fyi, the version number of Release .7 is still it at .6 :)


Oops.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Thu Aug 08, 2019 4:13 am
AndyVirus offline
Posts: 257
Joined: Mar 18, 2014
Location: Newport Pagnell, UK

Re: Queued message delivery for scripts or plugins

jay (support) wrote:
Cool! Almost makes me want to go find some MQTT device just to play with... ;)


Go look at the Sonoff devices. The most intresting one is the new Sonoff Mini. So for about $5 you get an on/off module that can sit behind a normal light switch. Its basically an ESP8285 and a relay in a little box.
https://www.itead.cc/sonoff-mini.html
https://fccid.io/2APN5-MINI/Internal-Photos/Internal-Photos-4291787

Put Tasmota firmware on there and you get MQTT AND Homekit with out the need for a bridge (homekit bridge that is). Very good firmware.
https://github.com/arendst/Sonoff-Tasmota

It really makes me wonder if I will buy another Z-Wave module again at 10x the cost of this. Yes there are wifi/security/mesh/dimmer/power consumption reporting considerations but I already use a seperate vlan for IoT devices that can not speck to each other or the internet (unless I specify they can) and can only speak to the MQTT broker on 1883. Mesh... not a massive issue for me as i have 3 APs.... I only use dimmer in 2 rooms (Living room and Bedroom) so the remaining x rooms have dimmer function but i never use it (mainly due to LED Bulbs and lack of requirement to do so). Power consumpton.... Ill bet that comes in a later version as there are inline modules that can do it just not i this mini form at the moment.

I would be very hard pressed to recommend Z-Wave modules for lights or sockets for a new comer when these things cost $5 each and can handle 10A loads. ($5 if ordered from china shopping sites and can wait a month, or $7-9 on Amazon)

If i had a clue how (must sit down a make time at some point), i would create a Tasmota plugin to handle the OTA updates as using the web interface is fine if you have a few but if you had 50-100.... that could be tedious.

If you dont want to spend any wing wangs then there are quite a few something to MQTT bridges like i use to read temp from my Weber BBQ probes which get sent to MQTT via a pi i have near by. Bluetooth range is meh, so being able to see temp (and set an action when temp reaches desired) is great.

Sorry for the hijack of the thread. MQTT is epic for HA, just excited to see some focus on MQTT!

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 4 guests