(Last Updated: July 28, 2020)

MQTT Plugin



  • Update: I now have a kind of a working solution and it is interesting...
    The fundamental problem is that setting up an MQTT client to listen to the broker blocks the luasocket and therefore openLuup.
    The two libraries use different approaches to the problem.
    The older library used by the vera MQTT plugin uses a handler function which polls the broker and gets and closes immediately regardless of getting a message or not. It therefore won't block the luasocket but it comes back empty 99% of the time and really relies on luck to catch a message as it can miss them. Basically unusable.
    The newer lua library does the same but waits for a message back and blocks the luasocket while waiting. If one sets a high polling frequency without getting a message back, openLuup hangs. My solution to this has been to significantly increase the frequency of messages from the publishing client to unblock the socket. It really is no longer event based and this generates a lot of traffic.

    If anyone has better suggestions (@therealdb I look forward to your bridge). I am also thinking that openLuup could use moving all its http traffic from luasocket to libcurl...

    Edit:

    I ended up giving up on trying to make it work with luasocket and spent 2min setting up 4 MQTT subscriptions on Home Assistant and another 10 to build the bridges back into openLuup. I really hoped to be able to do have a direct pipe into openLuup...



  • @rafale77 right now I'm using it to process incoming messages and update variables on Vera/Openluup. Do you want to call service endpoints as well? I can make it really generic and call HTTP endpoints, if needed. So you can
    I don't use it so much to send messages, but I think we could add this later, if needed.
    I can also try to put a relay feature, to receive incoming messages and then publish them back to another broker.

    It's based on a JSON config file and you have to map it manually, but then it's transparent. Give me a couple of days to polish and I'll publish it on GitHub, with instructions on how to build, and install it.



  • @rafale77 said in MQTT Plugin:

    The fundamental problem is that setting up an MQTT client to listen to the broker blocks the luasocket and therefore openLuup.
    The two libraries use different approaches to the problem.

    This just sounds like a lack of understanding on behalf of the module authors, ...or possibly me. I can’t believe that it could possibly be so difficult.

    These are the two libraries you referenced st the start?



  • @akbooer
    Or it could very well be that I missed something but looking at reports from github, I am not the only one observing it. These library work very well for publishing to the broker. They don't work unless you want to dedicate an instance of lua to be the listener...
    @therealdb
    This could potentially be the base for a bridge to so many things for openLuup!!



  • @rafale77 I have a simple version almost ready. I'll debug later today or tomorrow, and put on GitHub. Here's an excerpt of the config:

    {
    	"MQTT": {
    		"Username": "vera",
    		"Password": "openluup",
    		"Port": 1883
    	},
    
    	"Devices": [
    		{
    			"Name": "Pool",
    			"ClientID": "sonoff-pool",
    
    			"TopicName": "tele/tasmota/SENSOR",
    			"TopicPath": "AM2301.Temperature",
    
    			"DeviceID": 12,
    			"Service": "urn:upnp-org:serviceId:TemperatureSensor1",
    			"Variable": "CurrentTemperature"
    		},
    
    		{
    			"Name": "DeHumidifier",
    			"ClientID": "tasmota-dehum",
    
    			"TopicName": "stat/sonoff/POWER",
    			"TopicValue": "ON",
    
    			"DeviceID": 12,
    			"Service": "urn:upnp-org:serviceId:HVAC_UserOperatingMode1",
    			"Variable": "ModeTarget",
    			"Value": "HeatOn"
    		},
    
    		{
    			"Name": "DeHumidifier",
    			"ClientID": "tasmota-dehum",
    
    			"TopicName": "stat/sonoff/POWER",
    			"TopicValue": "OFF",
    
    			"DeviceID": 12,
    			"Service": "urn:upnp-org:serviceId:HVAC_UserOperatingMode1",
    			"Variable": "ModeTarget",
    			"Value": "Off"
    		}
    	]
    }
    

    I think it's more than enough to understand how the things will gonna work 😄



  • @rafale77 hey Rafale,

    I went down the very same road a while back and threw in towel because the polling by the MQTT plugin created CPU drags that stopped openLuup from functioning "reliably". The instability was also in part because I use two other must-have plugins that rely on polling, and I imagine that the combination of the three was creating a scenario that caused intermittent failures. And I too ended up implementing MQTT in Home Assistant and then using RealDB's virtual HTTP plugin to send commands to my WiFi devices--albeit not knowing the status of the devices in openLuup after the send.

    I'm looking at RigPapa's socket proxy and WebSocket plugins to see if I can transform my polling plugins to Async. The MQTT plugin is too complex for me to convert though, so if you take a crack at it, and are successful, I would very much appreciate you publishing your results, as MQTT is becoming a must for me.



  • I was also thinking about using a proxy for this but it is really adding a layer of complication. For MQTT it might be work it though, I think @therealdb 's solution is likely a better approach... basically also kind of a proxy written in C to act as the client and seems to be very lightweight.



  • I had a brief health problem in the latest days that put me completely KO, but I'm recovering. Code is almost done, I just need some time to test it under load and write some doc. I hope to finish it later today.



  • here we are: https://github.com/dbochicchio/luup-mqtt

    Try it and let me know. It should be easy to acquire .NET Core SDK and build the app. It's very minimal, fast and should be easy to understand. Right now it's only updating variables (with virtual device I think it's more tha enough), but I could add other actions if needed.



  • @therealdb

    Thank you! I will be testing it shortly now that I am done with my object recognition component and fixed a couple of network issues.


Log in to reply