Differentiate between message payloads

I'm relatively new to node-red but have managed to get several flows to function well, however I have come across a problem that I'm struggling with. My home automation system (Domoticz) will publish on only one topic. This wasn't a problem when I had only one device outputting to node red however I now have another device and I don't know how to differentiate between them. Output format from Domoticz to node red is in the following format;

object
Battery: 255
RSSI: 12
description: ""
dtype: "Light/Switch"
hwid: "18"
id: "000143F7"
idx: 935
name: "testmqtt"
nvalue: 1
stype: "Switch"
svalue1: "0"
switchType: "On/Off"
unit: 1

The "nvalue" is the value I send from node red to an ESP8266. I need to split the messages from Domoticz depending on their IDX value in order to forward them to their respective destinations. If anyone can assist I'd be extremely grateful.

You might want to start by putting in a change node at the front of your flow, where the Domoticz data comes in. The data you've shown has a number of identifying details, a change node will let you replace (or expand) the existing topic based on 1 or more of those fields.

Frankly though what good is Domoticz doing you here other than getting in the way? Why not get your ESP device to write to MQTT and consume that directly in Node-RED?

You can use msg.payload.idx to route messages or in a look up table to look up esp device id's (or values to add) etc

[{"id":"4cd7ce02.22cb","type":"inject","z":"5a245aa1.510164","name":"Off 2","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"\",\"dtype\":\"Light/Switch\",\"hwid\":\"18\",\"id\":\"000143F7\",\"idx\":836,\"name\":\"testmqtt\",\"nvalue\":0,\"stype\":\"Switch\",\"svalue1\":\"0\",\"switchType\":\"On/Off\",\"unit\":1}","payloadType":"json","x":121.33332824707031,"y":788.3333740234375,"wires":[["5242075a.1c74d"]]},{"id":"5242075a.1c74d","type":"function","z":"5a245aa1.510164","name":"","func":"var lookup = {\"_935\":0,\"_836\":2};\nmsg.payload = lookup[\"_\" + msg.payload.idx] + msg.payload.nvalue;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":310,"y":620,"wires":[["33d9788d.83b238"]]},{"id":"33d9788d.83b238","type":"debug","z":"5a245aa1.510164","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":550,"y":620,"wires":[]},{"id":"6707b592.81f91c","type":"inject","z":"5a245aa1.510164","name":"On","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"\",\"dtype\":\"Light/Switch\",\"hwid\":\"18\",\"id\":\"000143F7\",\"idx\":935,\"name\":\"testmqtt\",\"nvalue\":1,\"stype\":\"Switch\",\"svalue1\":\"0\",\"switchType\":\"On/Off\",\"unit\":1}","payloadType":"json","x":130,"y":620,"wires":[["5242075a.1c74d"]]},{"id":"c2ab7e91.ecfb58","type":"inject","z":"5a245aa1.510164","name":"Off","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"\",\"dtype\":\"Light/Switch\",\"hwid\":\"18\",\"id\":\"000143F7\",\"idx\":935,\"name\":\"testmqtt\",\"nvalue\":0,\"stype\":\"Switch\",\"svalue1\":\"0\",\"switchType\":\"On/Off\",\"unit\":1}","payloadType":"json","x":110,"y":680,"wires":[["5242075a.1c74d"]]},{"id":"ff2dd620.963bc","type":"inject","z":"5a245aa1.510164","name":"On 2","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"\",\"dtype\":\"Light/Switch\",\"hwid\":\"18\",\"id\":\"000143F7\",\"idx\":836,\"name\":\"testmqtt\",\"nvalue\":1,\"stype\":\"Switch\",\"svalue1\":\"0\",\"switchType\":\"On/Off\",\"unit\":1}","payloadType":"json","x":110,"y":740,"wires":[["5242075a.1c74d"]]}]

a simple lookup example.
[edit] output 0 , 1, 2 or 3 , dependant on idx and nvalue

Thanks for the reply, using the change node as suggested will do exactly what I'm after if only I knew how to accomplish it.
Just to expand on what I'm doing, or at least trying to! I'm sending a signal from Domoticz to the ESP8266, this in turn controls 2 relays. I can control the one by forwarding the 0 or 1 status of nvalue, for the second one I can take the nvalue of the second switch in domoticz and add 2 in a function node and forward that to the ESP to operate the second relay. Using the change node as you said would work fine if you can let me now how I can achieve that.

HI and thank you for the reply, that works and I can switch the relays independently. I can mostly see how that works however whenever a switch is pressed I get some error messages and don't seem to be able to get rid of them. the messages are the same for all conditions and are as follows;

3/02/2021, 22:51:09node: d75605f4.d407f8
msg : string[51]
"Cannot set property of non-object type: payload.idx"
13/02/2021, 22:51:09node: 33d9788d.83b238
domoticz/out/X/mqttDevices : msg.payload : number
NaN
13/02/2021, 22:51:09node: 16e53400.ae7914
msg : string[27]
"Failed to parse JSON string"
13/02/2021, 22:51:09node: a61f3e4d.c8cc28
msg : string[27]
"Failed to parse JSON string"

The errors aren't a show stopper, there are only 5-6 devices using mqtt at the moment. It would just be nice to understand what is going on a bit better and maybe get rid of them. Thanks for your help so far

Just check if lookup is defined.
e.g.

[{"id":"5242075a.1c74d","type":"function","z":"5a245aa1.510164","name":"","func":"var lookup = {\"_935\":0,\"_846\":2};\nlet addValue = lookup[\"_\" + msg.payload.idx];\nif (addValue !== undefined){\n    msg.payload =  addValue + msg.payload.nvalue;\n    return msg;\n}else{\n    return null;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":310,"y":620,"wires":[["33d9788d.83b238"]]}]

The other 3 devices are not defined in the lookup, so when it tries to look up the value, it can not find it and therefore creates errors.

Thank you so much, I understand better now and that works like a charm. Thanks again for your time.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.