MQTT Client Plugin

Great work Bruce!

Thanks for sharing :slight_smile:

Hello ! Great work !
It works well.

As I needed to be able to manage MQTT message from the broker, I’ve added subscribe action :

Topic and payload can be retrieved in variables “mqttLastReceivedTopic” and “mqttLastReceivedPayload”.

Great work! This plugin made my need for advanced scenes in Vera less important!

This plugin with Node red, influxdb and Grafana has made my home automation system much better and easier to extend features to.
Currently verifying that my SMS modem works correctly with the node Red flows, when this is verified I have 5000 SMS / month to use with Node Red and Vera!

I made an RPI tutorial that now includes this plugin for Vera :slight_smile:
Currently I made:
Influxdb
Grafana (v4)
Node red
Mosquitto
This MQTT plugin
The guide is in Swedish, but google translate works “OK”

[url=http://www.automatiserar.se/guide-raspberry-pi/]http://www.automatiserar.se/guide-raspberry-pi/[/url]

Great job on the plugin!

// Ispep

Just came across this thread and it was exactly what I was looking for. Thanks for the great plug-in.

Two related questions:

  1. Should this work if I use a hostname and not an IP address for the message broker? It isn’t for me.
  2. If the broker is disconnected, will there be an issues in my general Vera usage if the broker can’t be connected to?

Related to #1 I noticed that if I ssh into my vera I can’t ping the raspberry pi by hostname but I can by IP, I’m guessing that is the underlying issue.

Some modifications :

You can subscribe to a topic by creating a child device :

1/ Add a new device and choose a type (e.g. “D_BinaryLight1.xml” or “D_TemperatureSensor1.xml”).
2/ Reload LUUP engine.
3/ Change the attribut “id_parent” with the id of the MQTT plugin device.
4/ Reload LUUP engine and refresh your browser.
5/ You should see variables “mqttTarget” and “mqttTopic” in your newly created device.
6/ Set the topic you want to subcribe to and the target (format: service,variable=(formula in LUA)).
7/ Reload LUUP engine.

If the payload of the received message is in JSON, the plugin will try to decode it and put it in the variable “payload” in the context of the LUA formula.

examples :

Topic : Test/#
Target: urn:upnp-org:serviceId:SwitchPower1,Status=payload.value and ((payload.value==“alarm”) and “1” or “0”)

On a message from topic ‘Test/Something’ with payload ‘{“value”:“alarm”}’, the switch will be powered on.

Topic : Test/+/Sensor
Target: urn:upnp-org:serviceId:TemperatureSensor1,CurrentTemperature=payload.temperature and (tonumber(payload.temperature) or “0”)

On a message from topic ‘Test/Something/Sensor’ with payload "{"temperature “:“15.2”, “hygrometry”:“80”}”, the temperature will be set to 15,2.

[quote=“vosmont, post:85, topic:190658”]You can subscribe to a topic by creating a child device :

1/ Add a new device and choose a type (e.g. “D_BinaryLight1.xml” or “D_TemperatureSensor1.xml”).
2/ Reload LUUP engine.
3/ Change the attribut “id_parent” with the id of the MQTT plugin device.
4/ Reload LUUP engine and refresh your browser.
5/ You should see variables “mqttTarget” and “mqttTopic” in your newly created device.
6/ Set the topic you want to subcribe to and the target (format: service,variable=(formula in LUA)).
7/ Reload LUUP engine.

If the payload of the received message is in JSON, the plugin will try to decode it and put it in the variable “payload” in the context of the LUA formula.

examples :

Topic : Test/#
Target: urn:upnp-org:serviceId:SwitchPower1,Status=payload.value and ((payload.value==“alarm”) and “1” or “0”)

On a message from topic ‘Test/Something’ with payload ‘{“value”:“alarm”}’, the switch will be powered on.

Topic : Test/+/Sensor
Target: urn:upnp-org:serviceId:TemperatureSensor1,CurrentTemperature=payload.temperature and (tonumber(payload.temperature) or “0”)

On a message from topic ‘Test/Something/Sensor’ with payload "{"temperature “:“15.2”, “hygrometry”:“80”}”, the temperature will be set to 15,2.[/quote]

I have successfully started MQTT on OpenLuup installation, and sensors are reported to Mosquitto.
however I’m not able to read anything via child devices.
creating child devices does not create variables which you mention so I use workaround:

do -- MQTT
local dev = luup.create_device ('', "System", "System", "D_TemperatureSensor1.xml")
local sid = "urn:schemas-upnp-org:device:TemperatureSensor:1"
luup.variable_set (sid, "CurrentTemperature", "", dev)
luup.variable_set (sid, "mqttTarget", "", dev)
luup.variable_set (sid, "mqttTopic", "", dev)
end

and I have device with two variables (nothing more)
how can I show temperature from MQTT?
message from other client

{"CurrentTemperature":28.3,"DeviceId":88,"DeviceName":"Temp.Kaloryfery","DeviceType":"urn:schemas-micasaverde-com:device:TemperatureSensor:1","OldCurrentTemperature":28.4,"RoomId":5,"RoomName":"KotƂownia","ServiceId":"urn:upnp-org:serviceId:TemperatureSensor1","Time":1483171789,"Variable":"CurrentTemperature"}

Could you please help in understanding your example on my specific case?

Hello Tommi,

[quote=“Tommi, post:86, topic:190658”]I have successfully started MQTT on OpenLuup installation, and sensors are reported to Mosquitto.
however I’m not able to read anything via child devices. [/quote]

I think you haven’t linked the newly created device to the MQTT plugin.

You can try this :

do -- MQTT
   local parentId = 999 -- The id of the device of the MQTT plugin
   local internalId = "System" -- Has to be unique if you create several child devices
   local dev = luup.create_device ('', internalId, "System", "D_TemperatureSensor1.xml", nil, nil, nil, nil, nil, parentId)
end

Reload your Luup engine, refresh your browser and you should see the new variables.

Then you can set these variables :

Topic (e.g.) : Vera/+/TemperatureSensor1/88
Target: urn:upnp-org:serviceId:TemperatureSensor1,CurrentTemperature=payload.CurrentTemperature and (tonumber(payload.CurrentTemperature) or “0”)

I’ve installed this using the great instructions provided but it is failing
 anyone have any idea what this means:

01 01/12/17 22:33:26.347 LuaInterface::CallFunction_Startup-1 device 105 function startup failed [string “module(“L_SensorMqtt1”, package.seeall)
”]:107: attempt to index field ‘client’ (a nil value) <0x2bc43680>
01 01/12/17 22:33:26.347 LuImplementation::StartLua running startup code for 105 I_SensorMqtt1.xml failed <0x2bc43680>

Thanks!

[quote=“castrov, post:88, topic:190658”]I’ve installed this using the great instructions provided but it is failing
 anyone have any idea what this means:

01 01/12/17 22:33:26.347 LuaInterface::CallFunction_Startup-1 device 105 function startup failed [string “module(“L_SensorMqtt1”, package.seeall)
”]:107: attempt to index field ‘client’ (a nil value) <0x2bc43680>
01 01/12/17 22:33:26.347 LuImplementation::StartLua running startup code for 105 I_SensorMqtt1.xml failed <0x2bc43680>

Thanks![/quote]

I think that the MQTT library is not installed.
You have to copy the files “JSON.lua”, “mqtt_library.lua” and “utility.lua” in “/usr/lib/lua”.

If you get my modifications, you don’t have to put the “JSON.lua” file.

I’ve added action “Publish”, to be able to publish from LUA code.

The files were in there originally
 but I still receive this error.

Any other ideas? What do you mean by your modifications? I grabbed all files from GitHub - vosmont/vera-mqtt

Thanks!

root@MiOS:/usr/lib/lua# ls -l | grep “Jan 12”
-rw-r–r-- 1 root root 49510 Jan 12 20:56 JSON.lua
-rw-r–r-- 1 root root 272996 Jan 12 20:56 mqtt_library.lua
-rw-r–r-- 1 root root 93149 Jan 12 20:56 utility.lua

[quote=“vosmont, post:89, topic:190658”][quote=“castrov, post:88, topic:190658”]I’ve installed this using the great instructions provided but it is failing
 anyone have any idea what this means:

01 01/12/17 22:33:26.347 LuaInterface::CallFunction_Startup-1 device 105 function startup failed [string “module(“L_SensorMqtt1”, package.seeall)
”]:107: attempt to index field ‘client’ (a nil value) <0x2bc43680>
01 01/12/17 22:33:26.347 LuImplementation::StartLua running startup code for 105 I_SensorMqtt1.xml failed <0x2bc43680>

Thanks![/quote]

I think that the MQTT library is not installed.
You have to copy the files “JSON.lua”, “mqtt_library.lua” and “utility.lua” in “/usr/lib/lua”.

If you get my modifications, you don’t have to put the “JSON.lua” file.[/quote]

New version on github (check the loading of the MQTT library)

@castrov, can you try to copy again the dependencies ?
The file sizes are weird.

on my Vera :

root@MiOS_xx:/# ls -lrt /usr/lib/lua

...

lrwxrwxrwx    1 root     root            28 Sep 24 21:23 dkjson.lua -> /mios/usr/lib/lua/dkjson.lua
-rw-r--r--    1 root     root          5989 Dec  5 23:28 utility.lua
-rw-r--r--    1 root     root         29819 Dec  5 23:28 mqtt_library.lua

[quote=“vosmont, post:85, topic:190658”]Some modifications :

You can subscribe to a topic by creating a child device :

1/ Add a new device and choose a type (e.g. “D_BinaryLight1.xml” or “D_TemperatureSensor1.xml”).
2/ Reload LUUP engine.
3/ Change the attribut “id_parent” with the id of the MQTT plugin device.
4/ Reload LUUP engine and refresh your browser.
5/ You should see variables “mqttTarget” and “mqttTopic” in your newly created device.
6/ Set the topic you want to subcribe to and the target (format: service,variable=(formula in LUA)).
7/ Reload LUUP engine.

If the payload of the received message is in JSON, the plugin will try to decode it and put it in the variable “payload” in the context of the LUA formula.

examples :

Topic : Test/#
Target: urn:upnp-org:serviceId:SwitchPower1,Status=payload.value and ((payload.value==“alarm”) and “1” or “0”)

On a message from topic ‘Test/Something’ with payload ‘{“value”:“alarm”}’, the switch will be powered on.

Topic : Test/+/Sensor
Target: urn:upnp-org:serviceId:TemperatureSensor1,CurrentTemperature=payload.temperature and (tonumber(payload.temperature) or “0”)

On a message from topic ‘Test/Something/Sensor’ with payload "{"temperature “:“15.2”, “hygrometry”:“80”}”, the temperature will be set to 15,2.[/quote]
Does the device type have to correspond to the Vera service we want to use? (Ex. If we want to trigger a light switch we have to use D_BinaryLight1.xml).

Also thanks for your work!

You take the device type you want. When a message is received, the plugin updates the given service/variable without checking that it was already defined in your device.

 so you can do what you want ! :slight_smile:

Hello, fabulous work on this client, it is amazing! I just added my first child device using your temperature sensor example (I have a remote temp sensor reporting via MQTT with a payload of “65.73” only (whatever the temp is). The temperature is not showing up on the child device, but it does show up under the Variables tab under the variable CurrentTemperature. At the top of the page I now have the blue bar and it says MQTT Client[111]: Running Lua Startup and it never goes away (it survives Lua reload and device restart). Any ideas?

EDIT: Checked through the LUA log and realized that temperature sensors do not have an implementation file, removed that and the blue bar went away, but the sensor is not showing the temperature on the main devices screen or anywhere inside the device. The parent (111, MQTT Client plugin) is receiving the temperature and tossing it in a variable. Should I just wait and see if it figures itself out?

I’ve checked on my VeraEdge and on openLuup : it’s ok.

Have you refreshed your browser ?
Check that target is “urn:upnp-org:serviceId:TemperatureSensor1,CurrentTemperature=something”

Anyone try getting this to interface with Osram’s Lightify system? I saw someone posted Python code for MQTT for Lightify (GitHub - owagner/lightify2mqtt: OSRAM Lightify to MQTT gateway, following the mqtt-smarthome architecture)

[quote=“vosmont, post:96, topic:190658”]I’ve checked on my VeraEdge and on openLuup : it’s ok.

Have you refreshed your browser ?
Check that target is “urn:upnp-org:serviceId:TemperatureSensor1,CurrentTemperature=something”[/quote]

Vosmont,

my mqttTopic is “/ESP_Office/Office TPH/Temperature” and the mqttTarget is “urn:upnp-org:serviceId:TemperatureSensor1,CurrentTemperature=payload.temperature and (tonumber(payload.temperature) or “0”)”. currentTemperature is currently populated with the value 66.76, which happened a long time ago (the most recent value in the parent plugin is mqttLastReceivedTopic “/ESP_Office/Office TPH/Temperature” and mqttLastReceivedPayload “69.08”. It seems that the parent plugin just isn’t handing this down to the child. It has worked successfully at least once though, as that temperature is displayed on the devices page now.

The LUA formula is made for handling complex data in JSON.

If the message (payload) is just a string, you don’t have to use the formula.

your setting could be :
mqttTopic : “/ESP_Office/Office TPH/Temperature” (it should be “ESP_Office/Office TPH/Temperature”)
mqttTarget : “urn:upnp-org:serviceId:TemperatureSensor1,CurrentTemperature=”

I went ahead and removed the forward slash from the parent and the child. (is it necessary to click the checkbox below to make the edit successfully? I never understood what it does
) I hope I’m just doing something dumb. I went ahead and attached the variables page from the client and the child. Thank you for your help!