Setup MQTT support to connect external devices

跳转至: 导航搜索



MQTT [1] 是机器对机器的 (M2M)/"物联网"通信协议。它被设计的目的是成为一个非常轻量级的发布/订阅消息传输平台。 MQTT 提供发布/订阅消息模式,以提供一对多消息分发和应用程序解耦。

Node.js [2] 是基于Chrome的JavaScript运行时构建的平台,可轻松构建快速、可扩展的网络应用程序。 Node-RED [3] 是一个在Node.js平台上运行的工具,它提供了一个基于浏览器的流编辑器,可以很容易地将“流”连接起来。流量可以通过单击来部署到运行时中。

Domoticz-architecture 0.2.png

Domoticz 本地支持多种硬件设备(rfxtrx433, zwave, smartmeter etc.),在系统里还有大量其他设​​备和接口。使用它的原生MQTT接口Domoticz可以将事件从内部发布到外部。Domoticz还可以响应任何人请求的行为(并由MQT​​T代理传递)。只是需要注意创建和解释这些消息。


如果你不想使用MQTT,也可以使用Domoticz的内部LUA引擎(例如使用commandArray ['OpenURL']功能)。所以这也取决于你的需求和喜好。


  • 运行在Raspberry Pi上的Domoticz(支持Windows或Mac,但本教程重点介绍Pi)
  • Node.JS(我们将在稍后安装它)
  • Node-RED(我们将在稍后安装它)
  • MQTT代理(我们将在稍后安装它)
  • 几个Node-RED示例流程
  • 在Domoticz中添加'虚拟'硬件:“Create a MQTT Client Gateway with LAN interface”(在本地主机上)


请注意,目前的Domoticz SD卡镜像包含此项功能,您无需安装任何软件。

安装 Node.JS

  1. 注意:你是否需要这个,因为 instalation后来删除了旧的node.js和npm并安装了新的?
  2. 请按照 instructions 来安装 nodejs。
  3. 然后,使用Node Package Manager安装一些模块:
sudo npm install -g mqtt url request


sudo npm install -g nodemon


安装 Mosquitto

It is highly recommended that if you are using Debian or Rasbian that you use the additional Mosquitto repositories. This will ensure you get the latest stable version of Mosquitto as determined by the project itself, rather than an outdated version via the Debian or Raspbian repositories.

Rather than re-iterating the install procedure here, the guide given at is simple and easy to follow, and we encourage you to give their process a shot.

安装 Node-RED

Instructions can be found here: [4]

Node-RED should now be available on HTTP port 1880

添加硬件 "MQTT Client Gateway"

Domoticz needs to subscribe to the Mosquitto MQTT message broker that is now running locally. Under Setup/Hardware add a device of type: "MQTT Client Gateway with LAN interface" with settings:

  • Data Timeout: Disabled
  • Remote Address: localhost
  • Port: 1883
  • Username: empty
  • Password: empty

To finish press 'Add'.

To test if Domoticz receives data, create a Dummy Virtual temperature sensor.

  • Under Setup/Hardware add a device of type: "Dummy (Does nothing, use for virtual switches only)" with the name of your choice ("Dummy" for example).
  • In the corresponding entry of your just created "Dummy" hardware, click the "Create Virtual Sensors" button that appear following his type. Then enter the name of your choice (Let's say "Fictive Temp").
  • Under Setup/Devices, get the "idx" number of your "Fictive Temp" device (Let's say it's 1).

Then publish a temperature measurement to the virtual sensor idx noted above (example idx 1) like:

mosquitto_pub -h localhost -m '{ "idx" : 1, "nvalue" : 0, "svalue" : "25.0" }' -t 'domoticz/in'

If the mosquitto_pub command is not found install the mosquitto client:

sudo apt-get install mosquitto-clients

可用的 MQTT 应用


Owntracks ([]) is an app (both Android and iPhone) which periodicaly sends the location of a device to a mqtt-broker (e.g. mosquitto). A Node-RED script could subscribe to the owntracks-topic and republish the info in a Domoticz compatible message so Domoticz can use the location info.


与Domoticz之间的通信是通过JSON进行的。 默认MQTT中Domoticz信息进出的标题是:


Domoticz 到 MQTT

 "idx" : 5,
 "name" : "Internal Temperature",
 "id" : "00080A",
 "unit" : 1 
 "dtype" : "Temp",
 "stype" : "TFA 30.3133",
 "nvalue" : 0,
 "svalue1" : "41.2",
 "Battery" : 100,
 "RSSI" : 12,
 "idx" : 7,
 "name" : "S0 P1",
 "id" : "123456",
 "unit" : 0
 "dtype" : "Energy",
 "stype" : "CM119 / CM160",
 "nvalue" : 0,
 "svalue1" : "90",
 "svalue2" : "2975.00",
 "Battery" : 100,
 "RSSI" : 12,

该格式与Domoticz_API/JSON_URL's wiki页面中描述的格式兼容,除了“svalue”被拆分为多个部分以便于解析。

MQTT 到 Domoticz

消息格式与JSON wiki中的内容相同。


注意:如果 "command" 未设置,将使用 "udevice" 默认值,以下两条信息执行的效果是相同的:

 "command": "udevice",
 "idx" : 7,
 "nvalue" : 0,
 "svalue" : "90;2975.00"
 "idx" : 7,
 "nvalue" : 0,
 "svalue" : "90;2975.00"


{"command": "switchlight", "idx": 2450, "switchcmd": "On" }
{"command": "switchlight", "idx": 2450, "switchcmd": "Set Level", "level": 100 }
{"command": "setcolbrightnessvalue", "idx": 2450, "hue": 274, "brightness": 40, "iswhite"=false }
{"command": "setcolbrightnessvalue", "idx": 2450, "hex": "RRGGBB", "brightness": 100, "iswhite"=false }
{"command": "setcolbrightnessvalue", "idx": 2450, "color": {"m":3,"t":0,"r":0,"g":0,"b":50,"cw":0,"ww":0}, "brightness": 40}

The options for setcolbrightnessvalue are explained on Domoticz_API/JSON_URL's#Set_a_light_to_a_certain_color_or_color_temperature


{"command": "switchscene", "idx": 24, "switchcmd": "On" }
{"command": "switchscene", "idx": 24, "switchcmd": "Toggle" }


Optional: priority, sound

{"command": "sendnotification", "subject": "My Subject", "body": "My Body", "priority": 0, "sound": "mysound" }


{"command": "setuservariable", "idx": 1, "value": "12.3" }


{"command": "getdeviceinfo", "idx": 2450 }


{"command": "getsceneinfo", "idx": 1 }

Log 信息

{"command" : "addlogmessage", "message" : "MyMessage to log" }


  • Flows: This wiki-page is a collection of node-RED flows specific to Domoticz
  • here you'll find the official Node-RED library
  • MQTT Forum Topic (note: the topic also includes discussions/solutions when Domoticz did not yet have a native MQTT interface so ignore those)
  • MyMQTT for Android usefull for reading the MQTT messages during testing.

本地 MQTT 库

For interacting directly with Domoticz via MQTT.


sudo npm install node-domoticz-mqtt



You may use *paho-mqtt* library to communicate with domoticz MQTT client

sudo pip install paho-mqtt



Needed! Please Help.

Here is a good starting point:
This project contains an Arduino sketch for a sonoff device with MQTT/Domoticz compatibility: