openLuup: MQTT server
-
@akbooer this is what is in the logs surrounding the error, not sure if it could give som more clues:
2021-03-13 17:26:15.180 luup.register_handler:: global_function_name=Shelly_MQTT_Handler, request=mqtt:shellies/# 2021-03-13 17:26:15.180 openLuup.io.server:: starting MQTT:1883 server on port: 1883 tcp{server}: 0x55dba190f988 2021-03-13 17:26:15.180 openLuup.scheduler:: starting 2021-03-13 17:26:15.180 openLuup.scheduler:: [2] openLuup device startup 2021-03-13 17:26:15.180 luup_log:2: v21.3.11 2021-03-13 17:26:15.180 luup_log:2: sync in 104.8 s 2021-03-13 17:26:15.181 luup.variable_watch:: callback=housemode_watcher, watching=2.openLuup.HouseMode 2021-03-13 17:26:15.181 luup.register_handler:: global_function_name=openLuup_email, request=openLuup@openLuup.local 2021-03-13 17:26:15.181 luup.register_handler:: global_function_name=openLuup_images, request=images@openLuup.local 2021-03-13 17:26:15.181 luup.register_handler:: global_function_name=openLuup_events, request=events@openLuup.local 2021-03-13 17:26:15.181 luup.register_handler:: global_function_name=openLuup_mailbox, request=mail@openLuup.local 2021-03-13 17:26:15.181 luup.chdev.append:: [AltAppStore] Alternate App Store 2021-03-13 17:26:15.181 luup.chdev.sync:: [2] openLuup, syncing children 2021-03-13 17:26:15.181 openLuup.context_switch:: ERROR: [dev #2] ./openLuup/timers.lua:349: bad argument #1 to 'max' (number expected, got nil) 2021-03-13 17:26:15.182 openLuup.scheduler:: job aborted : ./openLuup/timers.lua:349: bad argument #1 to 'max' (number expected, got nil) 2021-03-13 17:26:15.182 openLuup.scheduler:: [3] Alternate UI device startup 2021-03-13 17:26:15.182 luup_log:3: ALTUI: initstatus(3) starting version: v2.53b 2021-03-13 17:26:15.182 openLuup.scheduler:: [3] Alternate UI device startup completed: status=, msg=, name=
-
Ah, excellent snippet from the log, giving context.
I've found it, and can fix it, but for the moment can you add to your MQTT configuration in Startup:
luup.attr_set ("openLuup.MQTT.PublishDeviceStatus", "0")
and see if that fixes it.
-
@akbooer There is still something that does not work, the device does not get updated.
This is what it says on the Tasmota device:
19:14:10 MQT: Connected 19:14:10 MQT: tele/tasmota_test/LWT = Online (retained) 19:14:10 MQT: cmnd/tasmota_test/POWER = 19:17:31 MQT: tele/tasmota_test/STATE = {"Time":"2021-03-13T19:17:31","Uptime":"0T00:15:09","UptimeSec":909,"Heap":27,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":19,"MqttCount":5,"Wifi":{"AP":1,"SSId":"BeachAC","BSSId":"FC:EC:DA:D1:7A:64","Channel":11,"RSSI":88,"Signal":-56,"LinkCount":1,"Downtime":"0T00:00:03"}} 19:17:31 MQT: tele/tasmota_test/SENSOR = {"Time":"2021-03-13T19:17:31","AM2301":{"Temperature":26.2,"Humidity":43.8,"DewPoint":12.9},"TempUnit":"C"}
This is what it says in MqttExplorer:
LuaStartup:
function MyMQTThandler (topic, message) luup.variable_set ("urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature", message, 216) end luup.register_handler ("MyMQTThandler", "mqtt:tele/tasmota_test/SENSOR/AM2301.Temperature")
It does connect and it seems as if everything in the Lua Startup matches the topic etc. Still no update of the device, strange.
I need to doubble check everything and see so that there are no typos... -
No, it’s fine. I’ve just not understood the previous syntax. The Explorer output gives exactly the required info. I’ll send some new code later (busy just now.)
-
In fact, just change the topic to tele/tasmota_test/SENSOR. It should update the temperature variable with the whole JSON string, which will actually need decoding.
-
@akbooer I changed the topic to tele/tasmota_test/SENSOR and now the temp variable is updated with the complete string:
{"Time":"2021-03-13T19:59:43","AM2301":{"Temperature":26.6,"Humidity":41.6,"DewPoint":12.5},"TempUnit":"C"}
which means that it actually gets updated.
One step in the right direction. -
The basic code, without error checking, would be:
local json = require "openLuup.json" function MyMQTThandler (topic, message) local info = json.decode (message) local t = info.AM2301.Temperature luup.variable_set ("urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature", t, 216) end luup.register_handler ("MyMQTThandler", "mqtt:tele/tasmota_test/SENSOR")
-
ArcherSreplied to akbooer on Mar 13, 2021, 7:27 PM last edited by ArcherS Mar 13, 2021, 3:00 PM
@akbooer thanks for the assistance and patience, now it works!
Edit: Added the humidity to the code:
local json = require "openLuup.json" function MyMQTThandler (topic, message) local info = json.decode (message) local t = info.AM2301.Temperature local h = info.AM2301.Humidity luup.variable_set ("urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature", t, 216) luup.variable_set ("urn:micasaverde-com:serviceId:HumiditySensor1", "CurrentLevel", h, 228) end luup.register_handler ("MyMQTThandler", "mqtt:tele/tasmota_test/SENSOR")
-
No need to have these as parameters... just make sure they’re in scope. The best way to do this is to put them in the same closure. You could even write a factory method to do this. There’s a trade-off between having multiple handlers for specific things and one which does many things. A lot of the choice will be governed by the topic structure. If, as is likely, a single handler manages a whole topic sub-tree, then you’ll probably want to subscribe to a wildcard topic of the form aaa/bbb/#.
-
@akbooer Yes but as you can see on the connect, the version is "Version: Unknown (132)"
This is what is causing the problem. After much searching and trying different configs, I stumbled on the following which solved the problem. From mosquitto.org
try_private [ true | false ] If try_private is set to true, the bridge will attempt to indicate to the remote broker that it is a bridge not an ordinary client. If successful, this means that loop detection will be more effective and that retained messages will be propagated correctly. Not all brokers support this feature so it may be necessary to set try_private to false if your bridge does not connect properly. Defaults to true.
So I set the attribute to false in my bridge config and immediately connected openLuup to the mosquitto broker. The connect packet shows the right version, and with a luup reload, all of my mosquitto broker topics populated in mqtt explorer that was pointed at openLuup. However, I don't see the topics in the mqtt console on openLuup?? Which is odd because I not only see the openLuup topics in explorer, but I see the topics actively changing.
I'm not a good one to suggest code changes, but since this mosquitto setting defaults to true, can you try to incorporate the try_private flag in openLuup's MQTT server.... It took a long time to track this down and I imagine anyone else that tries to connect the two servers will be in for a similar bug fix adventure.
-
-
-
@akbooer when you have a lot of devices, it could become a lot of code to do the same things. I found the design of callbacks for variables watches very powerful for the very same reason. Are you saying that instead of a string for a separate function, I could just pass a closure as an argument?
-
@therealdb said in openLuup: MQTT server:
Are you saying that instead of a string for a separate function, I could just pass a closure as an argument?
No, not that, although at the openLuup object-level API you can just pass a local function, rather than a string with the global function name.
I guess I’m not seeing your difficulty, can you supply a (pseudo-)code example of what you’d like to be able to do?
-
@buxton said in openLuup: MQTT server:
This is what is causing the problem. After much searching and trying different configs, I stumbled on the following which solved the problem.
Thanks SO MUCH for all the hard work, this is gold dust! I’ll see what I can do.
However, I don't see the topics in the mqtt console on openLuup?? Which is odd because I not only see the openLuup topics in explorer, but I see the topics actively changing.
Not an issue, I think. This is a list of SUBSCRIBED topics, not PUBLISHED ones. So I think it is working. A little test code with a wildcard subscription could confirm.
I'm not a good one to suggest code changes, but since this mosquitto setting defaults to true, can you try to incorporate the try_private flag in openLuup's MQTT server.... It took a long time to track this down and I imagine anyone else that tries to connect the two servers will be in for a similar bug fix adventure.
Absolutely... but I’m still not understanding what the difference is... I will dig further.
Many thanks again.
-
@akbooer something like this
function MyMQTThandler (topic, msg, state) local info = json.decode (message) local t = state.path luup.variable_set ("urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature", t, state.deviceID) end luup.register_handler ("MyMQTThandler", "mqtt:tele/#/SENSOR", {deviceID=255, description ="outdoor", path = "info.AM2301.Temperature"}) luup.register_handler ("MyMQTThandler", "mqtt:tele/#/SENSOR", {deviceID=266, description ="indoor", path = "info.DS18B20.Temperature"})
Passing a generic state will make handlers more compact and reusable.
-
akbooerreplied to akbooer on Mar 14, 2021, 1:14 PM last edited by akbooer Mar 14, 2021, 9:45 AM
@akbooer said in openLuup: MQTT server:
... I will dig further.
...and have now done so.
@Buxton, would it be at all possible to try this (latest development v21.3.14b) with the original bridging configuration? My implementation now, effectively, ignores the try_private flag, so will still behave as an ordinary client. Any magic of topic filtering, and the like, will still have to be configured on Mosquitto (if required.) It might be wise to limit the topics sent to those which are actually needed.
72/179