From a mqtt message extract and modify two parameters to create a new one

Just to be clear, I am not going to write you a function, but I'll be happy to guide you as you learn and write one yourself - or you could decide to do it all with nodes, that is your call. Also, I have no experience with Domoticz.

that said, in the two debugs above you have:
DEBUG 1:

{
  "Battery" : 255,
  "RSSI" : 12,
  "description" : "topic=tasmota_mcp\npayload=MCPINT_D0=",
  "dtype" : "Light/Switch",
  "hwid" : "6",
  "id" : "000140FE",
  "idx" : 174,
  "name" : "mcp_luce1",
  "nvalue" : 1,
  "stype" : "Switch",
  "svalue1" : "0",
  "switchType" : "On/Off",
  "unit" : 1
}

and DEBUG 2:

{
  "_msgid" : "1083342f.591abc",
  "due" : 1,
  "part1" : "topic=tasmota_mcp\npayload=MCPINT_D0=",
  "part2" : "topic=tasmota_mcp/eventpayload=MCPINT_D0=",
  "part3" : "cmnd/tasmota_mcp/eventpayload=MCPINT_D0=",
  "parts" : {
    "ch" : "payload=",
    "count" : 2,
    "id" : "94d4911b.ae57a",
    "index" : 1,
    "type" : "string"
  },
  "payload" : "MCPINT_D0=1",
  "payload_end" : 1,
  "topic" : "MCPINT_D0=",
  "uno" : "MCPINT_D0="
}

In the first one, you have a description but in the second you don't. Does that mean you would ignore the secondt msg?

that's clear, and apreciate that!

maybe better to try with function, i tryed with nodes but without success...try to change approach, but if you can suggest or guide using function it can help me.
regarding to domoticz, no knowledge are requested.
just be aware that i can write whaterever i want inside desctription that nodered will receve. ok?

no, this two output that i have are both not correct.

i need only one output that sent a response to the MQTT-out node with (in this case)
msg.topic = cmnd/tasmota_mcp/event
and
msg.payload = MCPINT_D0=1
if the description was

{
  "Battery" : 255,
  "RSSI" : 12,
  "description" : "topic=tasmota_mcp\npayload=MCPINT_D0=",
  "dtype" : "Light/Switch",
  "hwid" : "6",
  "id" : "000140FE",
  "idx" : 174,
  "name" : "mcp_luce1",
  "nvalue" : 1,
  "stype" : "Switch",
  "svalue1" : "0",
  "switchType" : "On/Off",
  "unit" : 1
}

and another example a response to the MQTT-out node with (in this new one case)
msg.topic = cmnd/light_kitchen/event
and
msg.payload = MCPINT_D6=0
if the description was

{
  "Battery" : 255,
  "RSSI" : 12,
  "description" : "topic=light_kitchen\npayload=MCPINT_D6=",
  "dtype" : "Light/Switch",
  "hwid" : "6",
  "id" : "000140FE",
  "idx" : 174,
  "name" : "mcp_luce1",
  "nvalue" : 0,
  "stype" : "Switch",
  "svalue1" : "0",
  "switchType" : "On/Off",
  "unit" : 1
}

Those are not outputs, those are the debug from the MQTT-in you provided. That is data coming from your Domoticz. So I'll ask again

In the first one, you have a description but in the second you don't. Does that mean you would ignore the second incoming msg?

not understand....
this output is at the end of the flow


the "msg" the only one that is activated after change: 2 rules node
the incoming message is only one, because now, for testing, i'm using the injection flow: "luce ON" or "luce OFF"
and to copy-paste here the debug, i first delete all debugs output, then click only one time on one injection flow and then posted here the debug output that was made by two messages

all right we have been mis-communicating. In this image attach a debug node (complete msg object) to the circled node. I want to see what is coming from DomoticzScreen Shot 2020-03-21 at 11.16.07 AM

{"Battery":255,"RSSI":12,"description":"topic=tasmota_mcp\npayload=MCPINT_D0=","dtype":"Light/Switch","hwid":"6","id":"000140FE","idx":174,"name":"mcp_luce1","nvalue":0,"stype":"Switch","svalue1":"0","switchType":"On/Off","unit":1}

{"Battery":255,"RSSI":12,"description":"topic=tasmota_mcp\npayload=MCPINT_D0=","dtype":"Light/Switch","hwid":"6","id":"000140FE","idx":174,"name":"mcp_luce1","nvalue":1,"stype":"Switch","svalue1":"0","switchType":"On/Off","unit":1}

{"Battery":255,"RSSI":12,"description":"topic=light_kitchen\npayload=MCPINT_D6=","dtype":"Light/Switch","hwid":"6","id":"000140FE","idx":174,"name":"mcp_luce3","nvalue":1,"stype":"Switch","svalue1":"0","switchType":"On/Off","unit":1}

{"Battery":255,"RSSI":12,"description":"topic=light_kitchen\npayload=MCPINT_D6=","dtype":"Light/Switch","hwid":"6","id":"000140FE","idx":174,"name":"mcp_luce3","nvalue":0,"stype":"Switch","svalue1":"0","switchType":"On/Off","unit":1}

{"Battery":255,"RSSI":12,"description":"topic=light_bathroom\npayload=MCPINT_D3=","dtype":"Light/Switch","hwid":"6","id":"000140FE","idx":174,"name":"mcp_luce2","nvalue":0,"stype":"Switch","svalue1":"0","switchType":"On/Off","unit":1}

from domoticz/out there are many massages, tremperature sensors, switches, blinds, and whatever.
here i reported only the message that i want to manipulate.
that's the reason why i filtered inside my flow using the switch node and filter all messagge that contain MCPINT inside description field.
then i start to manipulate only these messages, the others are skipped.
But again, the description field reports what i decide to fill inside domoticz to make the life inside nodered easiest possible

Look, I don’t think I can help you because you are not providing the information I’m asking. Hopefully someone else will be more successful at helping.

With that I am bowing out of this thread, sorry.

OK - it's a boring Saturday night... maybe this will help


// split the description into two parts
var parts = msg.payload.description.split('\n');

// make the topic part after the equal sign of the first part
msg.topic = parts[0].split("=")[1];

// grab the MCP part of the payload line
var p = parts[1].split("=")[1];

// rebuild the payload and append the nvalue
msg.payload = p + "=" + msg.payload.nvalue;
return msg;

Example to test

[{"id":"caf9c929.18d288","type":"inject","z":"3ead5d77.6a6672","name":"","topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"topic=tasmota_mcp\\npayload=MCPINT_D0=\",\"dtype\":\"Light/Switch\",\"hwid\":\"6\",\"id\":\"000140FE\",\"idx\":174,\"name\":\"mcp_luce1\",\"nvalue\":1,\"stype\":\"Switch\",\"svalue1\":\"0\",\"switchType\":\"On/Off\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":1820,"wires":[["4e0a89cb.59b048"]]},{"id":"4e0a89cb.59b048","type":"function","z":"3ead5d77.6a6672","name":"","func":"\n// split the description into two parts\nvar parts = msg.payload.description.split('\\n');\n\n// make the topic part after the equal sign of the first part\nmsg.topic = parts[0].split(\"=\")[1];\n\n// grab the MCP part of the payload line\nvar p = parts[1].split(\"=\")[1];\n\n// rebuild the payload and append the nvalue\nmsg.payload = p + \"=\" + msg.payload.nvalue;\nreturn msg;","outputs":1,"noerr":0,"x":390,"y":1820,"wires":[["dbf72f56.e230a"]]},{"id":"dbf72f56.e230a","type":"debug","z":"3ead5d77.6a6672","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":610,"y":1820,"wires":[]},{"id":"e635eb8a.ec11c8","type":"inject","z":"3ead5d77.6a6672","name":"","topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"topic=light_bathroom\\npayload=MCPINT_D3=\",\"dtype\":\"Light/Switch\",\"hwid\":\"6\",\"id\":\"000140FE\",\"idx\":174,\"name\":\"mcp_luce2\",\"nvalue\":0,\"stype\":\"Switch\",\"svalue1\":\"0\",\"switchType\":\"On/Off\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":1860,"wires":[["4e0a89cb.59b048"]]},{"id":"76ca7f94.ea6d","type":"inject","z":"3ead5d77.6a6672","name":"","topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"topic=light_kitchen\\npayload=MCPINT_D6=\",\"dtype\":\"Light/Switch\",\"hwid\":\"6\",\"id\":\"000140FE\",\"idx\":174,\"name\":\"mcp_luce3\",\"nvalue\":0,\"stype\":\"Switch\",\"svalue1\":\"0\",\"switchType\":\"On/Off\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":1900,"wires":[["4e0a89cb.59b048"]]}]

i'm very very very luck about your borin saturaday night !!!! :smiley:
joke apart, thanks for your help!
it's a very usefull sugestion, it woks at 90%
but i got the way, and i become it working at 100% :slight_smile:
i have some question when you have time....

perfectly understand the meening, very celar explanation!
the outpunt commadn should be:
msg.topic = cmnd/light_kitchen/event
and
msg.payload = MCPINT_D6=0

so i modify the firts instruction like:

// make the topic part after the equal sign of the first part
msg.topic = "cmnd/" + parts[0].split("=")[1] + "/event";

that's make your code working form 90% to 100% :slight_smile:
but joke apart, i apreciate very much your help, you understand 100% correctly, and give me a way to learn a lot!

now...just to learn more, i complicate a bit
with this example as description:
"description": "topic=tasmota_mcp\nMCPINT=D0",
i want an output like:
topic => cmnd/tasmota_mcp/sensor29
payload => 0,1
i modify your code, (function2) and i succeed on it. no question here.

now again a modification, want to modify a bit again.
with this example as description:
"description": "topic=tasmota_mcp\npayload=MCPINT=D0",
i want an output like:
topic => cmnd/tasmota_mcp/sensor29
payload => 0,1
i modify again your code (function3) but here i miss one command:
of course i can use :

// grab the MCP part of the payload line
var p = parts[1].split("D")[2];

instead of

// grab the MCP part of the payload line
var p = parts[1].split("=")[2];

but is it possible use:

// grab the MCP part of the payload line
var p = parts[1].split("=")[2];

and then a function that delete the carachter "D" to have only the number that is following the letter "D" ?

last question, i added at function3 outupt a debug node, because i want for example to see what there is inside var p
i do not suceed on monitoring var p in debug, what i should do?

here your flow modificated by me.

[{"id":"936b5343.1e99d","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"82b683b2.f2eaf","type":"inject","z":"936b5343.1e99d","name":"","topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"topic=tasmota_mcp\\npayload=MCPINT_D0=\",\"dtype\":\"Light/Switch\",\"hwid\":\"6\",\"id\":\"000140FE\",\"idx\":174,\"name\":\"mcp_luce1\",\"nvalue\":1,\"stype\":\"Switch\",\"svalue1\":\"0\",\"switchType\":\"On/Off\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":80,"wires":[["d4e444fc.b3f35"]]},{"id":"4e914d81.f30d1c","type":"function","z":"936b5343.1e99d","name":"function2","func":"\n// split the description into two parts\nvar parts = msg.payload.description.split('\\n');\n\n\n// make the topic part after the equal sign of the first part\nvar topic_middle = parts[0].split(\"=\")[1];\n\n//build message topic adding cmnd/<topic>/sensor29\nmsg.topic = \"cmnd/\" + topic_middle + \"/sensor29\"\n\n// grab the MCP part of the payload line\nvar p = parts[1].split(\"D\")[1];\n\n// rebuild the payload and append the nvalue\nmsg.payload = p + \",\" + msg.payload.nvalue;\nreturn msg;","outputs":1,"noerr":0,"x":320,"y":220,"wires":[["32fb53f1.1e7ef4","1755afce.ad7c9"]]},{"id":"32fb53f1.1e7ef4","type":"debug","z":"936b5343.1e99d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":510,"y":160,"wires":[]},{"id":"a46b1be5.29c708","type":"inject","z":"936b5343.1e99d","name":"","topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"topic=light_bathroom\\npayload=MCPINT_D3=\",\"dtype\":\"Light/Switch\",\"hwid\":\"6\",\"id\":\"000140FE\",\"idx\":174,\"name\":\"mcp_luce2\",\"nvalue\":0,\"stype\":\"Switch\",\"svalue1\":\"0\",\"switchType\":\"On/Off\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":120,"wires":[["d4e444fc.b3f35"]]},{"id":"47e1a706.c33258","type":"inject","z":"936b5343.1e99d","name":"","topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"topic=light_kitchen\\npayload=MCPINT_D6=\",\"dtype\":\"Light/Switch\",\"hwid\":\"6\",\"id\":\"000140FE\",\"idx\":174,\"name\":\"mcp_luce3\",\"nvalue\":0,\"stype\":\"Switch\",\"svalue1\":\"0\",\"switchType\":\"On/Off\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":160,"wires":[["d4e444fc.b3f35"]]},{"id":"1755afce.ad7c9","type":"debug","z":"936b5343.1e99d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"topic","targetType":"msg","x":510,"y":260,"wires":[]},{"id":"7a77a3b.55d7ddc","type":"inject","z":"936b5343.1e99d","name":"MCPINT=D8","topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"topic=light_kitchen\\nMCPINT=D8\",\"dtype\":\"Light/Switch\",\"hwid\":\"6\",\"id\":\"000140FE\",\"idx\":174,\"name\":\"mcp_luce3\",\"nvalue\":0,\"stype\":\"Switch\",\"svalue1\":\"0\",\"switchType\":\"On/Off\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":340,"wires":[["4e914d81.f30d1c"]]},{"id":"d4e444fc.b3f35","type":"function","z":"936b5343.1e99d","name":"function1","func":"\n// split the description into two parts\nvar parts = msg.payload.description.split('\\n');\n\n// make the topic part after the equal sign of the first part\nmsg.topic = \"cmnd/\" + parts[0].split(\"=\")[1] + \"/event\";\n\n// grab the MCP part of the payload line\nvar p = parts[1].split(\"=\")[1];\n\n// rebuild the payload and append the nvalue\nmsg.payload = p + \"=\" + msg.payload.nvalue;\nreturn msg;","outputs":1,"noerr":0,"x":320,"y":120,"wires":[["32fb53f1.1e7ef4","1755afce.ad7c9"]]},{"id":"8daa3ea6.ea5fc8","type":"inject","z":"936b5343.1e99d","name":"MCPINT=D0","topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"topic=tasmota_mcp\\nMCPINT=D0\",\"dtype\":\"Light/Switch\",\"hwid\":\"6\",\"id\":\"000140FE\",\"idx\":174,\"name\":\"mcp_luce1\",\"nvalue\":1,\"stype\":\"Switch\",\"svalue1\":\"0\",\"switchType\":\"On/Off\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":280,"wires":[["4e914d81.f30d1c"]]},{"id":"bfab8271.beee8","type":"inject","z":"936b5343.1e99d","name":"payload=MCPINT=D0","topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"topic=tasmota_mcp\\npayload=MCPINT=D0\",\"dtype\":\"Light/Switch\",\"hwid\":\"6\",\"id\":\"000140FE\",\"idx\":174,\"name\":\"mcp_luce1\",\"nvalue\":1,\"stype\":\"Switch\",\"svalue1\":\"0\",\"switchType\":\"On/Off\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":420,"wires":[["85ca653c.32746"]]},{"id":"801aa26f.b6cb58","type":"inject","z":"936b5343.1e99d","name":"payload=MCPINT=D8","topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"topic=light_kitchen\\npayload=MCPINT=D8\",\"dtype\":\"Light/Switch\",\"hwid\":\"6\",\"id\":\"000140FE\",\"idx\":174,\"name\":\"mcp_luce3\",\"nvalue\":0,\"stype\":\"Switch\",\"svalue1\":\"0\",\"switchType\":\"On/Off\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":480,"wires":[["85ca653c.32746"]]},{"id":"85ca653c.32746","type":"function","z":"936b5343.1e99d","name":"function3","func":"\n// split the description into two parts\nvar parts = msg.payload.description.split('\\n');\n\n\n// make the topic part after the equal sign of the first part\nvar topic_middle = parts[0].split(\"=\")[1];\n\n//build message topic adding cmnd/<topic>/sensor29\nmsg.topic = \"cmnd/\" + topic_middle + \"/sensor29\"\n\n// grab the MCP part of the payload line\nvar p = parts[1].split(\"=\")[2];\n\n// rebuild the payload and append the nvalue\nmsg.payload = p + \",\" + msg.payload.nvalue;\nreturn msg;","outputs":1,"noerr":0,"x":320,"y":360,"wires":[["32fb53f1.1e7ef4","1755afce.ad7c9","9072d9ed.d64a2"]]},{"id":"9072d9ed.d64a2","type":"debug","z":"936b5343.1e99d","name":"var p","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"var p","targetType":"jsonata","x":500,"y":420,"wires":[]},{"id":"181806e1.fc7ac9","type":"comment","z":"936b5343.1e99d","name":"","info":"    with this function i have as output example:\nmsg.topic = `cmnd/light_kitchen/event`\nand\nmsg.payload = `MCPINT_D6=0`\n\n","x":300,"y":60,"wires":[]},{"id":"3bab5363.81024c","type":"comment","z":"936b5343.1e99d","name":"","info":"    with this function i have as output example:\nmsg.topic = `cmnd/light_kitchen/sensor29`\nand\nmsg.payload = `6,0`\n\n\n\nfor description example please see the injection nodes that i modify","x":300,"y":180,"wires":[]},{"id":"fb2536fd.f5271","type":"comment","z":"936b5343.1e99d","name":"","info":"    with this function i have as output example:\nmsg.topic = `cmnd/light_kitchen/sensor29`\nand\nmsg.payload = `6,0`\n\n\n\nfor description example please see the injection nodes that i modify adding payload=.....","x":320,"y":320,"wires":[]}]

Hi
the output from the pin is only whatever you send it... (ie msg and its sub-properties like topic and payload)... If you want to see inside then by far the easiest is to add a node.warn("hello"); like statement - for example in your function

var p = parts[1].split("=")[2];
node.warn(p);

then it will appear as a yellow "warning" in the right hand debug tab
image

ok, i tried right now!
thanks

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