Extract two values from mqtt message(json type) and create a new mqtt with these two

Hi, i would like to implement something like this:
receiving messages from mqtt subscription at: domoticz/out
of course many messages in json format will arrive but i want to filter some messages inparticular.
from this messages i need to take two value: name and svalue
and with this two parameters create a new mqtt message.
is it possible?
i'm trying to do this since days, but not succeed.

some example:

this is some topic that i receive with first block mqtt where i add a debug to see that:

"{↵ "Battery" : 255,↵ "RSSI" : 7,↵ "description" : "",↵ "dtype" : "Temp",↵ "id" : "82116",↵ "idx" : 116,↵ "name" : "Ritorno_Riscaldamento",↵ "nvalue" : 0,↵ "stype" : "LaCrosse TX3",↵ "svalue1" : "19.30",↵ "unit" : 1↵}↵"

2/3/2020, 17:37:46[node: 9badb8d6.0b475](http://192.168.0.105:1880/#)domoticz/out : msg.payload : string[282]

"{↵ "Battery" : 255,↵ "RSSI" : 12,↵ "description" : "",↵ "dtype" : "Light/Switch",↵ "id" : "000140F8",↵ "idx" : 168,↵ "name" : "tap_tasmota_1",↵ "nvalue" : 2,↵ "stype" : "Switch",↵ "svalue1" : "70",↵ "switchType" : "Blinds Percentage Inverted",↵ "unit" : 1↵}↵"

2/3/2020, 17:37:46[node: 9badb8d6.0b475](http://192.168.0.105:1880/#)domoticz/out : msg.payload : string[305]

"{↵ "Battery" : 255,↵ "RSSI" : 12,↵ "description" : "",↵ "dtype" : "Temp + Humidity",↵ "id" : "82014",↵ "idx" : 14,↵ "name" : "Corridoio",↵ "nvalue" : 0,↵ "stype" : "THGN122/123/132, THGR122/228/238/268",↵ "svalue1" : "21.00",↵ "svalue2" : "37.1",↵ "svalue3" : "0",↵ "unit" : 1↵}↵"

2/3/2020, 17:37:47[node: 9badb8d6.0b475](http://192.168.0.105:1880/#)domoticz/out : msg.payload : string[282]

"{↵ "Battery" : 255,↵ "RSSI" : 12,↵ "description" : "",↵ "dtype" : "Light/Switch",↵ "id" : "000140F9",↵ "idx" : 169,↵ "name" : "tap_tasmota_2",↵ "nvalue" : 2,↵ "stype" : "Switch",↵ "svalue1" : "38",↵ "switchType" : "Blinds Percentage Inverted",↵ "unit" : 1↵}↵"

2/3/2020, 17:37:49[node: 9badb8d6.0b475](http://192.168.0.105:1880/#)domoticz/out : msg.payload : string[282]

"{↵ "Battery" : 255,↵ "RSSI" : 12,↵ "description" : "",↵ "dtype" : "Light/Switch",↵ "id" : "000140FA",↵ "idx" : 170,↵ "name" : "tap_tasmota_3",↵ "nvalue" : 2,↵ "stype" : "Switch",↵ "svalue1" : "44",↵ "switchType" : "Blinds Percentage Inverted",↵ "unit" : 1↵}↵"

2/3/2020, 17:37:51[node: 9badb8d6.0b475](http://192.168.0.105:1880/#)domoticz/out : msg.payload : string[305]

"{↵ "Battery" : 255,↵ "RSSI" : 12,↵ "description" : "",↵ "dtype" : "Temp + Humidity",↵ "id" : "82014",↵ "idx" : 14,↵ "name" : "Corridoio",↵ "nvalue" : 0,↵ "stype" : "THGN122/123/132, THGR122/228/238/268",↵ "svalue1" : "21.00",↵ "svalue2" : "36.9",↵ "svalue3" : "0",↵ "unit" : 1↵}↵"

2/3/2020, 17:37:53[node: 9badb8d6.0b475](http://192.168.0.105:1880/#)domoticz/out : msg.payload : string[303]

"{↵ "Battery" : 255,↵ "RSSI" : 9,↵ "description" : "",↵ "dtype" : "Temp + Humidity",↵ "id" : "82021",↵ "idx" : 21,↵ "name" : "Mansarda",↵ "nvalue" : 0,↵ "stype" : "THGN122/123/132, THGR122/228/238/268",↵ "svalue1" : "21.70",↵ "svalue2" : "40.1",↵ "svalue3" : "1",↵ "unit" : 1↵}↵"

2/3/2020, 17:37:54[node: 9badb8d6.0b475](http://192.168.0.105:1880/#)

domoticz/out : msg.payload : string[235]

"{↵ "Battery" : 255,↵ "RSSI" : 6,↵ "description" : "",↵ "dtype" : "Temp",↵ "id" : "82095",↵ "idx" : 95,↵ "name" : "Temperatura_Camino",↵ "nvalue" : 0,↵ "stype" : "LaCrosse TX3",↵ "svalue1" : "55.60",↵ "unit" : 1↵}↵"

from here what i'm interested are only these:

"{↵ "Battery" : 255,↵ "RSSI" : 12,↵ "description" : "",↵ "dtype" : "Light/Switch",↵ "id" : "000140F8",↵ "idx" : 168,↵ "name" : "tap_tasmota_1",↵ "nvalue" : 2,↵ "stype" : "Switch",↵ "svalue1" : "70",↵ "switchType" : "Blinds Percentage Inverted",↵ "unit" : 1↵}↵"

or

"{↵ "Battery" : 255,↵ "RSSI" : 12,↵ "description" : "",↵ "dtype" : "Light/Switch",↵ "id" : "000140FA",↵ "idx" : 170,↵ "name" : "tap_tasmota_3",↵ "nvalue" : 2,↵ "stype" : "Switch",↵ "svalue1" : "44",↵ "switchType" : "Blinds Percentage Inverted",↵ "unit" : 1↵}↵"

or

"{↵ "Battery" : 255,↵ "RSSI" : 12,↵ "description" : "",↵ "dtype" : "Light/Switch",↵ "id" : "000140F9",↵ "idx" : 169,↵ "name" : "tap_tasmota_2",↵ "nvalue" : 2,↵ "stype" : "Switch",↵ "svalue1" : "38",↵ "switchType" : "Blinds Percentage Inverted",↵ "unit" : 1↵}↵"

so all message that as :
"name" : "tap_#######
so that name start with this charachers: tap_

when i recognize these messages i need also to extract the svalue1

so for example in:
"{↵ "Battery" : 255,↵ "RSSI" : 12,↵ "description" : "",↵ "dtype" : "Light/Switch",↵ "id" : "000140F9",↵ "idx" : 169,↵ "name" : "tap_tasmota_2",↵ "nvalue" : 2,↵ "stype" : "Switch",↵ "svalue1" : "38",↵ "switchType" : "Blinds Percentage Inverted",↵ "unit" : 1↵}↵"

i need name and svalue1:
tap_tasmota_2
38

from the name i need to extract the number: in this case: 2

now with this indormation i need to build a mqtt command that publish this:

cmnd/blind_tasmota/shutterposition #number# space #svalue1#
so in this example the output will be:
cmnd/blind_tasmota/shutterposition2 38

is it possible to make somthing like this?
can someone help me?
thanks!

It is possible indeed. First thing is to find a way to get clean JavaScript objects from your inputs (by removing the cr+lf from middle of the strings).

Second step is filtering the input. One way (among many) is using a jsonata expression inside a switch node.

payload[$substring(name, 0,4) = "tap_"]

Testing flow:

[{"id":"f60c85b7.0ddda8","type":"change","z":"eb539f15.4d14e","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload[$substring(name, 0,4) = \"tap_\"]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":280,"wires":[["54bdddc7.f72a74"]]},{"id":"54bdddc7.f72a74","type":"debug","z":"eb539f15.4d14e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":790,"y":280,"wires":[]},{"id":"2d1dacde.480094","type":"inject","z":"eb539f15.4d14e","name":"tap_tasmota_1","topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"\",\"dtype\":\"Light/Switch\",\"id\":\"000140F9\",\"idx\":169,\"name\":\"tap_tasmota_1\",\"nvalue\":2,\"stype\":\"Switch\",\"svalue1\":\"38\",\"switchType\":\"Blinds Percentage Inverted\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":280,"y":260,"wires":[["f60c85b7.0ddda8"]]},{"id":"4895f75d.2f3308","type":"inject","z":"eb539f15.4d14e","name":"tap_tasmota_2","topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"\",\"dtype\":\"Light/Switch\",\"id\":\"000140F9\",\"idx\":169,\"name\":\"tap_tasmota_2\",\"nvalue\":2,\"stype\":\"Switch\",\"svalue1\":\"38\",\"switchType\":\"Blinds Percentage Inverted\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":280,"y":320,"wires":[["f60c85b7.0ddda8"]]},{"id":"797409cc.c1ce18","type":"inject","z":"eb539f15.4d14e","name":"fake_tasmota_2","topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"\",\"dtype\":\"Light/Switch\",\"id\":\"000140F9\",\"idx\":169,\"name\":\"fake_tasmota_2\",\"nvalue\":2,\"stype\":\"Switch\",\"svalue1\":\"38\",\"switchType\":\"Blinds Percentage Inverted\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":280,"y":380,"wires":[["f60c85b7.0ddda8"]]}]

Edit: a template node can build your command.

cmnd/blind_tasmota/shutterposition {{payload.name}} {{payload.svalue1}}

Testing flow:

[{"id":"9632ddbf.8658b","type":"change","z":"b3412710.dacd38","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload[$substring(name, 0,4) = \"tap_\"]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":240,"wires":[["87b6bb87.0ddee8"]]},{"id":"e7d1fd85.5c596","type":"debug","z":"b3412710.dacd38","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":870,"y":240,"wires":[]},{"id":"a9070bb2.1f1f78","type":"inject","z":"b3412710.dacd38","name":"tap_tasmota_1","topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"\",\"dtype\":\"Light/Switch\",\"id\":\"000140F9\",\"idx\":169,\"name\":\"tap_tasmota_1\",\"nvalue\":2,\"stype\":\"Switch\",\"svalue1\":\"38\",\"switchType\":\"Blinds Percentage Inverted\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":220,"wires":[["9632ddbf.8658b"]]},{"id":"81152ba0.06e0a8","type":"inject","z":"b3412710.dacd38","name":"tap_tasmota_2","topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"\",\"dtype\":\"Light/Switch\",\"id\":\"000140F9\",\"idx\":169,\"name\":\"tap_tasmota_2\",\"nvalue\":2,\"stype\":\"Switch\",\"svalue1\":\"38\",\"switchType\":\"Blinds Percentage Inverted\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":280,"wires":[["9632ddbf.8658b"]]},{"id":"a4947919.6805b8","type":"inject","z":"b3412710.dacd38","name":"fake_tasmota_2","topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"\",\"dtype\":\"Light/Switch\",\"id\":\"000140F9\",\"idx\":169,\"name\":\"fake_tasmota_2\",\"nvalue\":2,\"stype\":\"Switch\",\"svalue1\":\"38\",\"switchType\":\"Blinds Percentage Inverted\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":340,"wires":[["9632ddbf.8658b"]]},{"id":"87b6bb87.0ddee8","type":"template","z":"b3412710.dacd38","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"cmnd/blind_tasmota/shutterposition {{payload.name}} {{payload.svalue1}}","output":"str","x":680,"y":240,"wires":[["e7d1fd85.5c596"]]}]

What does the data look like if you put a debug node direct on the mqtt node?
What have you got the Output field in the MQTT node set to?
What does the data look like if you set Output to Parsed JSON object?

Hi Andrei and thanks for help me!
i need some more explanation, please.
if i'm not wrong, readin your flow (thanks for time dedicated and sharing it!!) i understand that thanks to template it will publish message like:
cmnd/blind_tasmota/shutterposition tap_tasmota_1 38
but it should not publicate the name of the "name" field inside the json, but only the number attached to the name.
in this case (because the name is: tap_tasmota_1
the message should be:
cmnd/blind_tasmota/shutterposition1 38
so i guess that it's needed a function that "remove the name" and take only the final part of name.

but to make it more customable, i see that there is a field name description and i can use it to store two important thing related to every blind: the number and the topic where to public

for example, if i fill the description fiel like this:
topic=blind_tasmota
blind=1

{ "Battery" : 255, "RSSI" : 12, "description" : "topic=tasmota_blind\nblind=1", "dtype" : "Light/Switch", "hwid" : "6", "id" : "000140F9", "idx" : 169, "name" : "tap_tasmota_1", "nvalue" : 2, "stype" : "Switch", "svalue1" : "59", "switchType" : "Blinds Percentage Inverted", "unit" : 1 }

in this case i will publish:
cmnd/"topcic"/shutteposition"blind" "svalue1"
so
cmnd/tasmota_blind/shutterposition1 59

another example:
{ "Battery" : 255, "RSSI" : 12, "description" : "topic=tasmota_blind2\nblind=3", "dtype" : "Light/Switch", "hwid" : "6", "id" : "000140FB", "idx" : 171, "name" : "tamp_tasmota_3", "nvalue" : 2, "stype" : "Switch", "svalue1" : "85", "switchType" : "Blinds Percentage Inverted", "unit" : 1 }
i should publish:
cmnd/tasmota_blind2/shutterposition3 85

sorry, but i think out this escamotage right now, and i ive more flexibility on namin device. sorry

another question is related to flow, i guess that it is done for test, right?
because i see that you use "inject node"
i guess that i should use as first node "mqtt in" but in this case i will receive all the messages that is publised in domoticz/out, and from all this messages (temperature, status, lights and so on....) i would like to filter only the json that has name that contain carachers wildcard: "tap_XXXXXX"
or even better, now that we can use description field, we can filter for json that contain "description" : "topic=" (only this first character that will be common for all device that i want to cntrol
what do you think?
how i can do it?

last question just for my learning
i see that you use "change node"
and you insert here: payload[$substring(name, 0,4) = "tap_"]
i interrupt the flow after the change note, and put a debug here.
i see that if use tap_tasmota_1 inijection box
on debug i see:

object

Battery: 255

RSSI: 12

description: ""

dtype: "Light/Switch"

id: "000140F9"

idx: 169

name: "tap_tasmota_1"

nvalue: 2

stype: "Switch"

svalue1: "38"

switchType: "Blinds Percentage Inverted"

unit: 1

same for tap_tasmota_2

and an error if i use tap_tasmota_fake.

so you will use this node to filter the mqtt messages?
thanms so much for your patience and help me solving issue and learning

hi, thanks for reply!
.....but i put the debug immediatly after mqtt....
you can see on my picture of flow posted
i posted also what's the outcome json that i have, that's full of messages, and i write also on only what kind of message i'm interested

OK, it wasn't clear to me which debug o/p you were showing. What happens if you set the Output to Parsed JSON. I don't expect it to be useful but it would be interesting to know.

no problem Colin, i'm newbee so maybe i'm not understand request in correct way :slight_smile:
anyway, on my flow, before suggestion of Andrei, and i think that he is on the right way to solve my issue, i used a json, you can see here:

immagine

how to set it as parsed, sorry, but i do not know.
i search on google, and try but i do not succeed on find/do it

Sure, no problem. I will reply each point in separate.

I overlooked that requirement. It is easy to fix though. Like you said I will add a code to take only the final part of the name. Let´s do it with the help of JSONata.

The expression below, also used inside the change node, will store in the property newname only the final part of the string.

payload.$substringAfter(name, "tap_tasmota_")

Please test and let me know if it is all right.

[{"id":"9632ddbf.8658b","type":"change","z":"b3412710.dacd38","name":"JSONata","rules":[{"t":"set","p":"newname","pt":"msg","to":"payload.$substringAfter(name, \"tap_tasmota_\")","tot":"jsonata"},{"t":"set","p":"payload","pt":"msg","to":"payload[$substring(name, 0,4) = \"tap_\"]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":280,"wires":[["87b6bb87.0ddee8"]]},{"id":"e7d1fd85.5c596","type":"debug","z":"b3412710.dacd38","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":730,"y":280,"wires":[]},{"id":"a9070bb2.1f1f78","type":"inject","z":"b3412710.dacd38","name":"tap_tasmota_1","topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"\",\"dtype\":\"Light/Switch\",\"id\":\"000140F9\",\"idx\":169,\"name\":\"tap_tasmota_1\",\"nvalue\":2,\"stype\":\"Switch\",\"svalue1\":\"38\",\"switchType\":\"Blinds Percentage Inverted\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":220,"wires":[["9632ddbf.8658b"]]},{"id":"81152ba0.06e0a8","type":"inject","z":"b3412710.dacd38","name":"tap_tasmota_2","topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"\",\"dtype\":\"Light/Switch\",\"id\":\"000140F9\",\"idx\":169,\"name\":\"tap_tasmota_2\",\"nvalue\":2,\"stype\":\"Switch\",\"svalue1\":\"38\",\"switchType\":\"Blinds Percentage Inverted\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":280,"wires":[["9632ddbf.8658b"]]},{"id":"a4947919.6805b8","type":"inject","z":"b3412710.dacd38","name":"fake_tasmota_2","topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"\",\"dtype\":\"Light/Switch\",\"id\":\"000140F9\",\"idx\":169,\"name\":\"fake_tasmota_2\",\"nvalue\":2,\"stype\":\"Switch\",\"svalue1\":\"38\",\"switchType\":\"Blinds Percentage Inverted\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":220,"y":340,"wires":[["9632ddbf.8658b"]]},{"id":"87b6bb87.0ddee8","type":"template","z":"b3412710.dacd38","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"cmnd/blind_tasmota/shutterposition{{newname}} {{payload.svalue1}}","output":"str","x":580,"y":280,"wires":[["e7d1fd85.5c596"]]}]

In the meantime I will check the other points.

Thank Andrei, it seams works perfectly!
now as i explained in the other points is it better to take the information inside description field :slight_smile:
sorry to have this idea only now.
i hope that i exppained well what i thought

Understood. Probably the code for this part will be very similar to the previous one. I will see and reply you back.

Well, looks like this part of the problem could be solved with a sequence of string substitutions. Once again I did it using JSONata. You will get the command inside msg.payload.

r-02

Code for testing:

[{"id":"1cba6873.653cd8","type":"inject","z":"b3412710.dacd38","name":"topic=tasmota_blind\\nblind=1","topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"topic=tasmota_blind\\nblind=1\",\"dtype\":\"Light/Switch\",\"hwid\":\"6\",\"id\":\"000140F9\",\"idx\":169,\"name\":\"tap_tasmota_1\",\"nvalue\":2,\"stype\":\"Switch\",\"svalue1\":\"59\",\"switchType\":\"Blinds Percentage Inverted\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":280,"y":480,"wires":[["20cb6803.5e7558"]]},{"id":"b8ad3b58.01d158","type":"inject","z":"b3412710.dacd38","name":"topic=tasmota_blind2\\nblind=3","topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"topic=tasmota_blind2\\nblind=3\",\"dtype\":\"Light/Switch\",\"hwid\":\"6\",\"id\":\"000140FB\",\"idx\":171,\"name\":\"tamp_tasmota_3\",\"nvalue\":2,\"stype\":\"Switch\",\"svalue1\":\"85\",\"switchType\":\"Blinds Percentage Inverted\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":280,"y":540,"wires":[["20cb6803.5e7558"]]},{"id":"20cb6803.5e7558","type":"change","z":"b3412710.dacd38","name":"JSONata","rules":[{"t":"set","p":"part1","pt":"msg","to":"payload.description","tot":"jsonata"},{"t":"set","p":"part2","pt":"msg","to":"$replace(part1,\"\\n\", \"/\")","tot":"jsonata"},{"t":"set","p":"part3","pt":"msg","to":"$replace(part2,\"topic=\", \"cmnd/\")","tot":"jsonata"},{"t":"set","p":"newname2","pt":"msg","to":"$replace(part3,\"blind=\", \"shutterposition\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":500,"wires":[["acdce1e6.95f8a"]]},{"id":"7c889f74.fe399","type":"debug","z":"b3412710.dacd38","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":870,"y":500,"wires":[]},{"id":"acdce1e6.95f8a","type":"template","z":"b3412710.dacd38","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{{newname2}}} {{payload.svalue1}}","output":"str","x":700,"y":500,"wires":[["7c889f74.fe399"]]}]
1 Like

Again you are right. We are breaking down the problem in smaller pieces. At the end we will be able to join everything together. The reason I am using JSONata is to make easier to filter (later on) the incoming message.

understood, thanks for your patience and very good support and explanation!
now i will test immediatly your code, before go to sleep :slight_smile:

thanks, i tested your flow, the output is perfect for both inputs!!!
fantastic!!!

now i would like to understand better what you did to learn something.
what is completly not clear to me is JSONata
inside i see:
immagine
starting from the 1st one, if i click on the 3dots i see this:


you wrote: payload.description
but inside label "test" i do not see any output.
how i can understand what this command do?

same question related to all of 4 comands inside jsonata

what do: $replace(part1,"\n", "/")
??
and: $replace(part2,"topic=", "cmnd/")
??
last one: $replace(part3,"blind=", "shutterposition")
???

of course i guess that here you did the operation to extract value that we need, and then use the "template" box to combine:
{{{newname2}}} {{payload.svalue1}}
but totaly lost on understand code behind you are able to take/filter the json of injection boxes.

apart of that, now that we have the flow that is working with injection tests
we should insert mqtt that listen to topic domoticz/out, but i guess that we need also another block that filter only json messages that contain description with "topic=" and "blind="
or it's enought the JSONata that will separe/filter only Json message that has description with wildcards "topic=" and "blind=" ?
thanks so much!!!

Good. I am leaving for dinner now. I reply you late tonight or tomorrow morning. Cheers.

1 Like

Great, learning is the most important.

My approach to the problem was to handle it as a string substitution problem.

We want to transform this input:
topic=tasmota_blind\nblind=1

into this output:
cmnd/tasmota_blind/shutterposition1

So, it would require:

1- tranform topic= into cmnd/
2- tranform \n into /
3- transform blind= into shutterposition

Each rule in the change node does a part of the transformation.

The template node just combine two parts: cmnd/tasmota_blind/shutterposition1 (space) value of property svalue1

Missing to work out the last part that will read the mqtt messages and clean them up before submitting to the flow we already have.

As a matter of fact this is part of the jsonata editor. I did not write it. This is an embedded capability to test jsonata within Node-RED.

In order to finish the flow I will need to ask the same question asked before by @Colin.

Can you change the configuration of the MQTT node (tapparelle) to "a parsed JSON object"and share a screenshot of what a debug node connected to the MQTT node will display ?

I wonder if you are really receiving all those line feeds in the middle of the strings.

hi, thansk for very good explanation.
now i understand better, thanks!
just two comments from my side:

  1. i use the description filed to pass to nodered two parameters:
    the topic (in this example is "tasmota_blind") and blind (that in this example is 1 and i need it to build the mqtt output with sending the number of blind to be moved).

of course in tap_tasmota_4 for example i can use a different topic (for example tasmota_blind2)
it will be manage by your flow?
i try to answer by myself and i edit the json that we used for test, and it works.
the node that will take care of if is the template node?
right? i do not understand who will take care about it, but it will be asked in second question:-)

In this point i just want to brainstorming a bit on how we can fill better the description field.
Maybe, due to an "ethernal" compatibility (domoticz in newer version can pass a different value when in the descriptio filed there are two rows one above the other).
Maybe is it better divide topic and blind using ";" and not in two different lines.

Sorry to think about this only now, but i'm "under developping" and every time that i understand something new in nodered (mainly thans your suggestion flow and explanations) i discover better way to do things.

Anyway i'm open to sugestion for this!

For you, what's the best option to write this two parameters: tipic and blind number?

now we have:
topic=bla_bla_bla
and in a new line:
blind=number of blind to pass after shutterposition

is it better to avoid problems, and "ethernal" compatibility using:
topic=bla_bla_bla;blind_number=13 (or whatever number: can be only one number from 1 to 9 or two numbers from 10 to 99)
better in this way not using spaces? or better with spaces?

or again better using a json notation?
{"topic":"bla_bla_bla","blind_number":"42"}

open to every suggestion that can make easly the recognition and template construction.

  1. second question

the template, as you wrote, will complete all the parts transformed by jsonata
{{{newname2}}} {{payload.svalue1}}

newname2 is made by: part3=cmnd/
and newname2 that is made by: shutterposition
so it become
cmnd/shutterposition
then there is: {{payload.svalue1}}
that i guess is able to take value from json injection
two sub questions:
1a) just to learn.
if i used for example: {{payload.stype}} instead of {{payload.svalue1}}
i will find: Switch as result ?
because inside injection json i have as "stype" : "Switch"
correct?

2a) thanks to template block:
{{{newname2}}} {{payload.svalue1}}
at command we have:
cmnd/shutterposition
that's newname2
but the righ comand is: cmnd/topic_bla_bla_bla/shutterposition8 43 (for example)
where you take the number 8 of the example above? i do not understand
and where you take the topic_bla_bla_bla ??
and how you build the construction of commands output?
thanks

sure, sorry to not understand request from the beginnig

this is the output.
p.s. maybe as i write inside the above post reply, is it better to think about using description field parameters in a better way, due to avoid two lines and so avoid characher like
that as i can see is present inside this json and we elaborate finding \n
open to every kind of suggestion to write parameters in a realible way inside description field. thanks!

object
Battery: 255
RSSI: 12
description: "topic=now_change_topi_to_test↵blind=46"
dtype: "Light/Switch"
hwid: "6"
id: "000140F9"
idx: 169
name: "tap_tasmota_1"
nvalue: 2
stype: "Switch"
svalue1: "58"
switchType: "Blinds Percentage Inverted"
unit: 1
3/3/2020, 07:58:07node: 9badb8d6.0b475domoticz/out : msg.payload : Object
object
Battery: 255
RSSI: 12
description: "topic=tasmota_blind↵blind=2"
dtype: "Light/Switch"
hwid: "6"
id: "000140FA"
idx: 170
name: "tap_tasmota_2"
nvalue: 2
stype: "Switch"
svalue1: "69"
switchType: "Blinds Percentage Inverted"
unit: 1
3/3/2020, 07:58:07node: 9badb8d6.0b475domoticz/out : msg.payload : Object
object
Battery: 255
RSSI: 2
description: ""
dtype: "Temp"
hwid: "6"
id: "82117"
idx: 117
name: "Temp_ACS"
nvalue: 0
stype: "LaCrosse TX3"
svalue1: "59.20"
unit: 1
3/3/2020, 07:58:08node: 9badb8d6.0b475domoticz/out : msg.payload : Object
object
Battery: 255
RSSI: 12
description: ""
dtype: "Light/Switch"
hwid: "6"
id: "000140C7"
idx: 119
name: "rele_caldaia_ACS"
nvalue: 0
stype: "Switch"
svalue1: "0"
switchType: "On/Off"
unit: 1
3/3/2020, 07:58:09node: 9badb8d6.0b475domoticz/out : msg.payload : Object
object
Battery: 255
RSSI: 12
description: ""
dtype: "Light/Switch"
hwid: "6"
id: "000140C2"
idx: 114
name: "Valvola_TreVie_ACS"
nvalue: 0
stype: "Switch"
svalue1: "0"
switchType: "On/Off"
unit: 1
3/3/2020, 07:58:10node: 9badb8d6.0b475domoticz/out : msg.payload : Object
object
Battery: 255
RSSI: 12
description: "topic=tasmota_blind↵blind=3"
dtype: "Light/Switch"
hwid: "6"
id: "000140FB"
idx: 171
name: "tamp_tasmota_3"
nvalue: 2
stype: "Switch"
svalue1: "33"
switchType: "Blinds Percentage Inverted"
unit: 1

I would still be interested to know this. If you open the MQTT node you will see a field labelled Output. One of the options is Parsed JSON (which means it expects the value to be a JSON string and it attempts to parse it into a javascript object.
Where is this strangely formatted mqtt message coming from? This would be much easier if the data were sensibly formatted in the first place.