Page 1 of 1

First try Node-Red

Posted: Sunday 19 January 2020 17:05
by phoenixblue
Hello,
At this moment i have running MQTT, Node-Red and Domoticz.
And trying to connect those.

I have an sensor that is send data to MQTT that i can read with the "debug node" but I'm not able to handle that msg to Domoticz.
So i have made an msg inject to try if I can manual send text to Domoticz, but without any result.

'What do i wrong?

Code: Select all

[
    {
        "id": "3402e571.9a885a",
        "type": "function",
        "z": "e0840401.ca73f8",
        "name": "payload",
        "func": "msg.topic = msg.payload.name;\n\nmsg.payload = {};\nmsg.payload.idx = 168;\nmsg.payload.svalue = msg.payload.name;\nreturn msg; ",
        "outputs": 1,
        "noerr": 0,
        "x": 400,
        "y": 80,
        "wires": [
            [
                "387eaffe.4ce8",
                "b6dc8975.8aaf08"
            ]
        ]
    }
]

Re: First try Node-Red

Posted: Sunday 19 January 2020 21:15
by FireWizard
Hi,

You wrote,
At this moment i have running MQTT, Node-Red and Domoticz.
I have an sensor that is send data to MQTT that i can read with the "debug node"
The contents of your function node is as follows:

Code: Select all

msg.topic = msg.payload.name;

msg.payload = {};
msg.payload.idx = 168;
msg.payload.svalue = msg.payload.name;
return msg;
In general you decide first, what type of sensor it is.
It might be one of the standard sensors, available in Domoticz, such as Temperature, Humidity. etc.
Often it is special and then you need to add a virtual sensor of the type "Custom Sensor".

Each of those sensors require its own syntax.
See: https://www.domoticz.com/wiki/Domoticz_API/JSON_URL%27s

However, I like this complete overview: https://piandmore.wordpress.com/2019/02 ... -domoticz/

So, in case you have configured a "Custom Sensor", you have to create the following command: {"command":"udevice", "idx":1234, "svalue":"3.45"}.

Explanation: See also: https://www.domoticz.com/wiki/MQTT
"command":"udevice" is the default and in this case it may be omitted, but I always insert it for completeness.
"idx":1234 is the index number of the virtual device you just created. You will find this in the Setup > Devices tab.
"svalue":"3.45" is the string value you want the sensor to display. In this case "3.45"

I will gave an example of a very simple flow.

Screenshot_MQTT_ RPI3.png
Screenshot_MQTT_ RPI3.png (10.04 KiB) Viewed 8503 times
What does this flow do?

1. The MQTT-RPI3+ node receives some data from a Raspberry Pi. On this Pi I run some scripts, but not Domoticz.
These data are published under the following topics:

Home/rpi3plus/Temp
Home/rpi3plus/DiskUsagePercent
Home/rpi3plus/MemoryUsagePercent
Home/rpi3plus/CpuUsagePercent

2. I converted the received data to JSON

3. Prepare the data, so that Domoticz understands it.
In this case, I have created 4 virtual sensors. 1 of the type "Temperature" and 3 of the type "Percentage".
The contents of the function node is as follows:

Code: Select all

switch (msg.topic) {

    case "Home/rpi3plus/Temp":
    msg.payload = {"command":"udevice","idx":322,"nvalue":0,"svalue":((msg.payload).toString())};
    break;

    case "Home/rpi3plus/DiskUsagePercent":
    msg.payload = {"command":"udevice","idx":323,"nvalue":0,"svalue":((msg.payload).toString())};
    break;

    case "Home/rpi3plus/MemoryUsagePercent":
    msg.payload = {"command":"udevice","idx":324,"nvalue":0,"svalue":((msg.payload).toString())};
    break;

    case "Home/rpi3plus/CpuUsagePercent":
    msg.payload = {"command":"udevice","idx":325,"nvalue":0,"svalue":((msg.payload).toString())};
    break;
}
return msg;
The topic acts as a selector for the right sensor.
But look at the line, starting with msg.payload (4 times).

"command":"udevice", I already explained.
"idx":325, the 4 different index numbers of the sensors.
"nvalue":0, may be omitted as in this case it is not needed.
"svalue":((msg.payload).toString()), The payload received here is a numerical value and has to be converted to a string.

4. The MQTT output node sends the correct data to Domoticz (You should have MQTT Client Gateway with LAN interface have installed in Domoticz)
The sensors will be updated every time new input is received.

Can you publish the data that you want to have published to Domoticz? So this is what you see in the debug node.
Then it is easy to create the correct command to Domoticz.

Regards