Listen to mqtt message and send out a new modify/trasformed one

hi, i need to listen to mqtt message from 5 different devices:

  1. stat/tasmota_156/#
  2. stat/tasmota_157/#
  3. stat/tasmota_160/#
  4. stat/tasmota_161/#
  5. stat/tasmota_162/#

on listening to this i want to filter messages like this, example:
stat/tasmota_156/POWER2 OFF
(where topic=stat/tasmota_156/POWER2 ; payload=OFF)
or another example:
stat/tasmota_156/POWER2 OFF

here the output from a debug node set as complete message:
{"topic":"stat/tasmota_156/POWER9","payload":"ON","qos":0,"retain":false,"_msgid":"74a010a.335a3f"}

the variables are:

  1. POWERX where x can be from 1 to 18
  2. pyaload that can be OFF or ON

then i have to build an otput message like this for example:
domoticz/in {"idx":177,"nvalue":1,"svalue":"","Battery":100,"RSSI":6}
where the topic is= domoticz/in
and payload is= {"idx":177,"nvalue":1,"svalue":"","Battery":100,"RSSI":6}

here the variables are:

  1. (idx value) = 177
  2. (nvalue) = 1
    nvalue must be aligned with OFF or ON => 1=ON 0=OFF
    idx will be associated to POWERX.
    Here there is not a conresponding between X and idx, i mean POWER3 is not idx=3 but idx is a value that i will choose depens on listened topic(in this example topic is stat/tasmota_156/POWER2) and powerX of that topic. and it will be always the same for this topic and this power

i do not succeed on finding a best way to make this working, can someone help me?
thanks!

PRATICAL EXAMPLES
I try to clarify making examples:

example1:
topic1 to listen: stat/tasmota_156/#
so i'll receive: stat/tasmota_156/POWER7 ON
topic received=> stat/tasmota_156/POWER7
payload received=>ON
variable to be manipulate 7(=power) and ON

it should be transformed in => idx=xxx (xxx = value that i want to chose and will be always asociated to power7 and topic stat/tasmota_156/#) nvalue=1 (ON=1)
so message output will be: {"idx":xxx,"nvalue":1,"svalue":"","Battery":100,"RSSI":6}

example2:
topic2 to listen: stat/tasmota_157/#
so i'll receive: stat/tasmota_157/POWER7 ON
topic received=> stat/tasmota_156/POWER7
payload received=>ON
variable to be manipulate 7(=power) and *ON

it should be transformed in => idx=yyy* (yyy = value that i want to chose and will be always asociated to power7 and topic stat/tasmota_157/#) nvalue=1 (ON=1)
so message output will be: {"idx":yyy,"nvalue":1,"svalue":"","Battery":100,"RSSI":6}

example3:
topic2 to listen: stat/tasmota_157/#
so i'll receive: stat/tasmota_157/POWER13 OFF
topic received=> stat/tasmota_157/POWER13
payload received=>OFF
variable to be manipulate 13(=power) and OFF
it should be transformed in=> idx=zzz (zzz = a value that i want to chose and will be always asociated to power
13* and topic stat/tasmota_157/#) nvalue=0 (0=OFF)
so message output will be: {"idx":zzz,"nvalue":0,"svalue":"","Battery":100,"RSSI":6}

example4:
topic3 to listen: stat/tasmota_160/#
so i'll receive: stat/tasmota_160/POWER13 OFF
topic received=> stat/tasmota_160/POWER13
payload received=>OFF
it should be transformed in=> idx=kkk (kkk = a value that i want to chose and will be always asociated to power13 and topic stat/tasmota_160/#) nvalue=0 (0=OFF)
so message output will be: {"idx":kkk,"nvalue":0,"svalue":"","Battery":100,"RSSI":6}

and so on....

Couple of things:

xxx = value that i want to chose and will be always asociated to power 7

This xxx value, you have defined all these values in domoticz ?

{"idx":xxx,"nvalue":1,"svalue":"","Battery":100,"RSSI":6}

Basically you are only interested in the xxx and nvalue. Where do the battery and rssi come from, as they are not coming from the tasmota devices and therefore cannot be populated.

If you know the xxx,zzz etc values, you need to build some map to match against.

Example function:

t = msg.topic
p = msg.payload

idx = {
    "stat/tasmota_160/POWER13":245,
    "stat/tasmota_157/POWER13":129,
    "stat/tasmota_157/POWER7":492
    }
    

idxout = idx[t]
state = (p==="ON") ? 1:0

return {idx:idxout,nvalue:state}

Example flow:

[{"id":"238b2fbe.85e36","type":"function","z":"d5ec2cf.2857f5","name":"","func":"t = msg.topic\np = msg.payload\n\nidx = {\n    \"stat/tasmota_160/POWER13\":245,\n    \"stat/tasmota_157/POWER13\":129,\n    \"stat/tasmota_157/POWER7\":492\n    }\n    \n\nidxout = idx[t]\nstate = (p===\"ON\") ? 1:0\n\nreturn {idx:idxout,nvalue:state}","outputs":1,"noerr":0,"x":423,"y":264,"wires":[["a035b99e.15645"]],"l":false},{"id":"a035b99e.15645","type":"debug","z":"d5ec2cf.2857f5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":504,"y":264,"wires":[],"l":false},{"id":"9bb8105c.e14d6","type":"inject","z":"d5ec2cf.2857f5","name":"","topic":"stat/tasmota_157/POWER7","payload":"ON","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":202,"y":240,"wires":[["238b2fbe.85e36"]]},{"id":"ba70a1fe.f2e6a","type":"inject","z":"d5ec2cf.2857f5","name":"","topic":"stat/tasmota_160/POWER13","payload":"OFF","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":212,"y":288,"wires":[["238b2fbe.85e36"]]}]

hi, thanks so mych!!! that's fantastic!!
i tested and play a bit, it's working very good.

you are right, I'm only interested in the xxx and nvalue.
battery and rssi come from as they are and not coming from the tasmota devices and not needs be populated.
the line above {..... } is the payload
the topic to publish is: domoticz/in
so i modify your function to create msg.payload and msg.topic

can you please if i follow in the right way? thanks again!
i tested it and it works, but maybe could i wrote the function in a more "efficent" way? just to learn something, if you can, please check it :slight_smile:

now i implemented the part two: i wont to comunicate also in opposite direction from domoticz to tasmota.
i think to do this in this way.
domoticz publish all comands in topic=domoticz/out and send the payload like:

{"Battery":255,"RSSI":12,"description":"PCFtopic=esp_156_tasmota\npower=1","dtype":"Light/Switch","hwid":"6","id":"000140FE","idx":177,"name":"mcp_luce1","nvalue":1,"stype":"Switch","svalue1":"0","switchType":"On/Off","unit":1}

i have the freedom to fill the description field with whatever i want, so i think to write inside:
PCFtopic=esp_156_tasmota\npower=1
where \n is the separator
the topic is esp_156_tasmota
the power to trigger is = power1
and the status is nvalue = 1
also here the other parameters are fixed and not menaged.

so nodered have to catch this topic and create the output mqtt like:
cmnd/esp_156_tasmota/power1 1
topic=cmnd/esp_156_tasmota/power1
payload= 1

i used a switch to filter messagge that are contain PCF, so in this case i skip unwanted message of sensors and so on...
and i wrote a function to substitue words of description and build the output command.

it works, but i think that just because you wrote the great solution of mapping topics of tasmota and idx, my solution is a workaround because i have to wrote inside every idx of domoticz the conresponded topic and power of tasmota....but i have already inside your nodered function!!!
can i skip this repetition?
maybe using a common table and pointing to this table when message start to domoticz and also when message start from tasmota

what do you think?

i attach my flow, so you can see what i do.
i also put inside some injections so have some examples.
of course if it is feasible to use only one table inside nodered, the description field of domoticz will be empty.

here my tested flow:

[{"id":"75af6cea.260c04","type":"tab","label":"luci tasmota PCF con funzione","disabled":false,"info":""},{"id":"9ccd1a18.3c2a68","type":"function","z":"75af6cea.260c04","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];// con [1]prendo solo il primo carattere\n\n//topic_end = prendo solo la <X> di power=X\nvar topic_end = parts[1].split(\"=\")[1]\n//node.warn(topic_end); //per avere informazioni sulla var topic_end nel debug\n\n//build message topic adding cmnd/<topic>/sensor29\nmsg.topic = \"cmnd/\" + topic_middle + \"/power\" + topic_end\n\n\n// rebuild the payload and append the nvalue\nmsg.payload = msg.payload.nvalue;\nreturn msg;","outputs":1,"noerr":0,"x":440,"y":240,"wires":[["6e0f0714.4a80c8","dd20b413.c58378","3dc3bf97.828fc","93026d6e.fe728"]]},{"id":"6e0f0714.4a80c8","type":"debug","z":"75af6cea.260c04","name":"domot_payl","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":650,"y":180,"wires":[]},{"id":"dd20b413.c58378","type":"debug","z":"75af6cea.260c04","name":"domot_topic","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"topic","targetType":"msg","x":650,"y":140,"wires":[]},{"id":"e9febe24.caebd8","type":"inject","z":"75af6cea.260c04","name":"ON topic=esp_156_tasmota\\npower=1","topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"PCFtopic=esp_156_tasmota\\npower=1\",\"dtype\":\"Light/Switch\",\"hwid\":\"6\",\"id\":\"000140FE\",\"idx\":177,\"name\":\"mcp_luce1\",\"nvalue\":1,\"stype\":\"Switch\",\"svalue1\":\"0\",\"switchType\":\"On/Off\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":140,"wires":[["7a9e0ce1.bbb744"]]},{"id":"2182df07.5e00d8","type":"inject","z":"75af6cea.260c04","name":"OFF topic=esp_156_tasmota\\npower=1","topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"PCFtopic=esp_156_tasmota\\npower=1\",\"dtype\":\"Light/Switch\",\"hwid\":\"6\",\"id\":\"000140FE\",\"idx\":177,\"name\":\"mcp_luce3\",\"nvalue\":0,\"stype\":\"Switch\",\"svalue1\":\"0\",\"switchType\":\"On/Off\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":100,"wires":[["7a9e0ce1.bbb744"]]},{"id":"2f680bb8.7e10ec","type":"comment","z":"75af6cea.260c04","name":"","info":"    with this function i have as output example:\n\nDESCRIZIONE IN DOMOTICZ:    \ntopic=esp_156_tasmota\npower=1\n\nmsg.topic = `cmnd/esp_156_tasmota/power1`\nand\nmsg.payload = `1` oppure 0\n\n\n\n","x":440,"y":200,"wires":[]},{"id":"23f8e24f.0d2b6e","type":"mqtt in","z":"75af6cea.260c04","name":"","topic":"domoticz/out","qos":"2","datatype":"json","broker":"f0a64cae.6413e","x":90,"y":220,"wires":[["7a9e0ce1.bbb744"]]},{"id":"8cb7db72.a169b","type":"mqtt out","z":"75af6cea.260c04","name":"","topic":"","qos":"","retain":"","broker":"f0a64cae.6413e","x":1270,"y":480,"wires":[]},{"id":"3dc3bf97.828fc","type":"mqtt out","z":"75af6cea.260c04","name":"","topic":"","qos":"","retain":"","broker":"f0a64cae.6413e","x":630,"y":240,"wires":[]},{"id":"7a9e0ce1.bbb744","type":"switch","z":"75af6cea.260c04","name":"","property":"payload.description","propertyType":"msg","rules":[{"t":"cont","v":"PCF","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":270,"y":260,"wires":[["9ccd1a18.3c2a68"],[]]},{"id":"119be279.b68856","type":"mqtt in","z":"75af6cea.260c04","name":"","topic":"stat/tasmota_156/#","qos":"2","datatype":"auto","broker":"f0a64cae.6413e","x":110,"y":440,"wires":[["16dd5437.7ad2f4"]]},{"id":"7d27e65a.b7068","type":"debug","z":"75af6cea.260c04","name":"TASM_topic","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"topic","targetType":"msg","x":490,"y":440,"wires":[]},{"id":"16dd5437.7ad2f4","type":"function","z":"75af6cea.260c04","name":"","func":"t = msg.topic\np = msg.payload\n\nidx = {\n    \"stat/tasmota_156/POWER2\":178,\n    \"stat/tasmota_156/POWER1\":177,\n    \"stat/tasmota_156/POWER9\":179,\n    \"stat/esp_157_tasmota/POWER1\":108\n    }\n    \n\nidxout = idx[t]\nstate = (p===\"ON\") ? 1:0\n\npayload = {idx:idxout,nvalue:state,\"svalue\":\"\",\"Battery\":100,\"RSSI\":6} \n\nmsg.payload = payload ;\nmsg.topic = \"domoticz/in\";\nreturn msg;","outputs":1,"noerr":0,"x":355,"y":460,"wires":[["7d27e65a.b7068","5a4eb383.38ce7c","607fe357.0bdee4","ed1a2aab.5d0af8"]],"l":false},{"id":"3c4eed83.26d942","type":"inject","z":"75af6cea.260c04","name":"","topic":"stat/tasmota_156/POWER9","payload":"ON","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":500,"wires":[["16dd5437.7ad2f4"]]},{"id":"d9b25158.4aea68","type":"inject","z":"75af6cea.260c04","name":"","topic":"stat/tasmota_159/POWER13","payload":"OFF","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":540,"wires":[["16dd5437.7ad2f4"]]},{"id":"5a4eb383.38ce7c","type":"debug","z":"75af6cea.260c04","name":"TASM_payl","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":510,"y":480,"wires":[]},{"id":"607fe357.0bdee4","type":"mqtt out","z":"75af6cea.260c04","name":"","topic":"","qos":"","retain":"","broker":"f0a64cae.6413e","x":670,"y":460,"wires":[]},{"id":"cef10dba.a102f","type":"inject","z":"75af6cea.260c04","name":"","topic":"stat/tasmota_156/POWER9","payload":"OFF","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":580,"wires":[["16dd5437.7ad2f4"]]},{"id":"ed1a2aab.5d0af8","type":"debug","z":"75af6cea.260c04","name":"TASM_COMPLETE","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":470,"y":400,"wires":[]},{"id":"93026d6e.fe728","type":"debug","z":"75af6cea.260c04","name":"domot_COMPLETE","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":650,"y":100,"wires":[]},{"id":"95d95fc4.0aca98","type":"mqtt in","z":"75af6cea.260c04","name":"","topic":"stat/esp_157_tasmota/#","qos":"2","datatype":"auto","broker":"f0a64cae.6413e","x":130,"y":400,"wires":[["16dd5437.7ad2f4"]]},{"id":"f0a64cae.6413e","type":"mqtt-broker","z":"","name":"mosquitto","broker":"192.168.0.105","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

I hope you dont mind, I tidied up your post.

please post multiline code between three backticks

```

Like this

```

hey no no, glad that you make this! thanks so mutch!
....anyway i edited three time the post to try to put the flow in this way but i do not succeed on that.
maybe because i did in this way:
Like this

could be that the issue? (using theree of backaccent but on same line without space? )

No bother & yes - they must be on new line above AND below/

ok, thanks! good to always learn something :slight_smile:

You could use a variable, in which you define the "dictionary" with all the mappings, the variable you can call via

global.set('variablename',value)
...
global.get('variablename')

See this example flow:

[{"id":"7d05953.6ff41ec","type":"inject","z":"2d5afef1.17d73a","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":111,"y":648,"wires":[["95a18c28.4abd48"]],"l":false},{"id":"95a18c28.4abd48","type":"function","z":"2d5afef1.17d73a","name":"","func":"idx = {\n    \"stat/tasmota_156/POWER2\":178,\n    \"stat/tasmota_156/POWER1\":177,\n    \"stat/tasmota_156/POWER9\":179,\n    \"stat/esp_157_tasmota/POWER1\":108\n    }\n    \n    \nglobal.set(\"idx\",idx)\n\nreturn null","outputs":1,"noerr":0,"x":183,"y":648,"wires":[[]],"l":false},{"id":"e405b26.f2b395","type":"comment","z":"2d5afef1.17d73a","name":"Set global idx variable","info":"","x":172,"y":600,"wires":[]},{"id":"118fe087.414b9f","type":"inject","z":"2d5afef1.17d73a","name":"get idx - inject topic","topic":"stat/tasmota_156/POWER2","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":186,"y":720,"wires":[["8eceba0b.2fbc4"]]},{"id":"286c98b6.aa2b08","type":"function","z":"2d5afef1.17d73a","name":"","func":"\nidx = global.get(\"idx\")\ni = msg.payload\n\nlet topic = Object.keys(idx).find(k=>idx[k]===i);\n\n\n\nreturn {topic:topic}","outputs":1,"noerr":0,"x":327,"y":768,"wires":[["2ec1055b.e341ca"]],"l":false},{"id":"47ebdf7a.67dbf8","type":"inject","z":"2d5afef1.17d73a","name":"get topic - inject idx","topic":"","payload":"179","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":186,"y":768,"wires":[["286c98b6.aa2b08"]]},{"id":"2ec1055b.e341ca","type":"debug","z":"2d5afef1.17d73a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":434,"y":768,"wires":[]},{"id":"ca336c3a.1036d8","type":"debug","z":"2d5afef1.17d73a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":434,"y":720,"wires":[]},{"id":"8eceba0b.2fbc4","type":"function","z":"2d5afef1.17d73a","name":"","func":"\nidx = global.get(\"idx\")\ni = msg.topic\n\nidxout = idx[i]\n\nreturn {idx:idxout}","outputs":1,"noerr":0,"x":327,"y":720,"wires":[["ca336c3a.1036d8"]],"l":false}]

There is 1 inject node with a single function node, this is where you define the variable, every time you deploy or restart node-red the variable will be automatically set.

In the other function nodes I use idx = global.get('idx') and the object will be available in the function node.

sorry but this not work...maybe because i explained it not well
the firts part: comunication start from tasmota:
i receive topic = stat/tasmota_156/POWER2
payload = 1 or 0

i need to convert in:
topic = domoticz/in
payload = {"idx":xxx,"nvalue":value,"svalue":"","Battery":100,"RSSI":6}
where xxx will take from global table and value (nvalue) is the payload of tasmota (1 or 0)
so in the example the payload will be:
{"idx":178,"nvalue":1,"svalue":"","Battery":100,"RSSI":6}
in your function only the idx was setted, and not the nvalue and also the topic.
i succeed on modify it and make it working.
please have a look if i made it not a proper way :slight_smile:

second part: comunication start from domoticz:
i receive topic = domoticz/out
payload = {"Battery":255,"RSSI":12,"description":"PCFtopic=esp_156_tasmota\npower=1","dtype":"Light/Switch","hwid":"6","id":"000140FE","idx":178,"name":"pcf_luce1","nvalue":1,"stype":"Switch","svalue1":"0","switchType":"On/Off","unit":1}
i need to extract: idx 178 and nvalue 1
inside the second function you build the topic:
topic = stat/tasmota_156/POWER9
but not the payload that shuld be 0 or 1 (taking from nvalue)
you correctly link the stat/tasmota_156/POWER9 to idx 178
but is missing the payload 1 linked to nvalue
i do not succeed on implement it.

there is also another issue, that' my guilty, becaouse for sure i do not explain well: domoticz is publishing many diferent topics on domoticz/out
so i have first of all filter only the idx that's are inside table, if it's possible
so i think that a switch node is required, correct?

otherwise, if it's not possible to filter messages using idx wrote inside global table, we can do in another way, i can write something inside description field, that a switch node can detect: if metch something i will recognize and start to manipulate, if i do not receve nothing that's matching it's because the message is not for a light, so i have not to menage it.
so first thing is filtering messages on domoticz/out that are inside the table, if it's possible otherwise i will use to filter something that i will write inside description field.

here my flow.
i make working the first part: starting from tasmota
but not understand how to make working the second part: start from domoticz
inside the flow i set the examples injection.
is it correct how i wrote the exit topic and exit payload for first part: starting from tasmota?
is working but maybe not well written :slight_smile:

[{"id":"4e2e8cc.ec21b74","type":"tab","label":"Flow 7","disabled":false,"info":""},{"id":"45a0f5b.311d50c","type":"inject","z":"4e2e8cc.ec21b74","name":"get idx - inject topic","topic":"stat/tasmota_156/POWER2 ON","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":60,"wires":[["5c9061e1.9f8dc"]]},{"id":"ef249892.d542a","type":"inject","z":"4e2e8cc.ec21b74","name":"get topic - inject idx","topic":"","payload":"179","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":280,"wires":[["a5f5ecef.233508"]]},{"id":"5c9061e1.9f8dc","type":"function","z":"4e2e8cc.ec21b74","name":"","func":"p = msg.payload\nidx = global.get(\"idx\")\ni = msg.topic\n\nidxout = idx[i]\nstate = (p===\"ON\") ? 1:0\n\npayload = {\"idx\":idxout,\"nvalue\":state,\"svalue\":\"\",\"Battery\":100,\"RSSI\":6} \n\nmsg.payload = payload ;\nmsg.topic = \"domoticz/in\";\nreturn msg;\n","outputs":1,"noerr":0,"x":415,"y":140,"wires":[["157fab48.d48595","f5751c8d.95776","5c300e44.81c328","7f93020d.61db64"]],"l":false},{"id":"a5f5ecef.233508","type":"function","z":"4e2e8cc.ec21b74","name":"","func":"\nidx = global.get(\"idx\")\ni = msg.payload\n\nlet topic = Object.keys(idx).find(k=>idx[k]===i);\n\n\n\nreturn {topic:topic}","outputs":1,"noerr":0,"x":415,"y":280,"wires":[["fe603ca2.845cb8","4325376c.fc4268","65deb4ed.b97afc","7f93020d.61db64"]],"l":false},{"id":"97668794.f66ec8","type":"inject","z":"4e2e8cc.ec21b74","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":395,"y":80,"wires":[["37abdcf.31d2424"]],"l":false},{"id":"e8a62bf7.ce9c7","type":"comment","z":"4e2e8cc.ec21b74","name":"Set global idx variable","info":"","x":440,"y":40,"wires":[]},{"id":"37abdcf.31d2424","type":"function","z":"4e2e8cc.ec21b74","name":"","func":"idx = {\n    \"stat/tasmota_156/POWER2\":178,\n    \"stat/tasmota_156/POWER1\":177,\n    \"stat/tasmota_156/POWER9\":179,\n    \"stat/esp_157_tasmota/POWER1\":108\n    }\n    \n    \nglobal.set(\"idx\",idx)\n\nreturn null","outputs":1,"noerr":0,"x":495,"y":80,"wires":[[]],"l":false},{"id":"157fab48.d48595","type":"debug","z":"4e2e8cc.ec21b74","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":610,"y":240,"wires":[]},{"id":"fe603ca2.845cb8","type":"debug","z":"4e2e8cc.ec21b74","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":610,"y":300,"wires":[]},{"id":"4325376c.fc4268","type":"debug","z":"4e2e8cc.ec21b74","name":"domoticz_topic","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"topic","targetType":"msg","x":640,"y":340,"wires":[]},{"id":"65deb4ed.b97afc","type":"debug","z":"4e2e8cc.ec21b74","name":"domoticz_payl","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":640,"y":380,"wires":[]},{"id":"5c300e44.81c328","type":"debug","z":"4e2e8cc.ec21b74","name":"TASM_payl","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":630,"y":200,"wires":[]},{"id":"7f93020d.61db64","type":"mqtt out","z":"4e2e8cc.ec21b74","name":"","topic":"","qos":"","retain":"","broker":"f0a64cae.6413e","x":610,"y":160,"wires":[]},{"id":"f5751c8d.95776","type":"debug","z":"4e2e8cc.ec21b74","name":"TASM_topic","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"topic","targetType":"msg","x":630,"y":120,"wires":[]},{"id":"e93eeeae.187b98","type":"inject","z":"4e2e8cc.ec21b74","name":"fake commands not to considerate","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":160,"y":380,"wires":[["a5f5ecef.233508"]]},{"id":"3805870.b79467a","type":"inject","z":"4e2e8cc.ec21b74","name":"fake commands not to considerate","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":160,"y":420,"wires":[["a5f5ecef.233508"]]},{"id":"dc3b694f.e4732","type":"inject","z":"4e2e8cc.ec21b74","name":"fake commands not to considerate, idx=48 is not inside table","topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"\",\"dtype\":\"Light/Switch\",\"hwid\":\"6\",\"id\":\"000140FE\",\"idx\":48,\"name\":\"mcp_luce1\",\"nvalue\":0,\"stype\":\"Switch\",\"svalue1\":\"0\",\"switchType\":\"On/Off\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":520,"wires":[["a5f5ecef.233508"]]},{"id":"9688cf09.1908f","type":"comment","z":"4e2e8cc.ec21b74","name":"from domoticz, fake commands coming from other devices","info":"","x":210,"y":340,"wires":[]},{"id":"d3d8c4ce.efb8f","type":"comment","z":"4e2e8cc.ec21b74","name":"i light command but not to considerate because idx is not inside the table","info":"","x":260,"y":480,"wires":[]},{"id":"912c33ba.18446","type":"comment","z":"4e2e8cc.ec21b74","name":"commands to considerate because idx is inside table","info":"","x":200,"y":600,"wires":[]},{"id":"62012d90.744be4","type":"inject","z":"4e2e8cc.ec21b74","name":"commands to considerate, idx=177 is inside table","topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"PCFtopic=esp_156_tasmota\\npower=1\",\"dtype\":\"Light/Switch\",\"hwid\":\"6\",\"id\":\"000140FE\",\"idx\":178,\"name\":\"pcf_luce1\",\"nvalue\":1,\"stype\":\"Switch\",\"svalue1\":\"0\",\"switchType\":\"On/Off\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":640,"wires":[["a5f5ecef.233508"]]},{"id":"8369ebd5.fddb48","type":"comment","z":"4e2e8cc.ec21b74","name":"start from tasmota","info":"","x":110,"y":20,"wires":[]},{"id":"16c5304a.c91958","type":"comment","z":"4e2e8cc.ec21b74","name":"start from domoticz","info":"","x":90,"y":240,"wires":[]},{"id":"9e930edc.295d1","type":"inject","z":"4e2e8cc.ec21b74","name":"","topic":"stat/tasmota_156/POWER9","payload":"ON","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":100,"wires":[["5c9061e1.9f8dc"]]},{"id":"f72763b0.10a7b","type":"inject","z":"4e2e8cc.ec21b74","name":"","topic":"stat/tasmota_159/POWER13","payload":"OFF","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":140,"wires":[["5c9061e1.9f8dc"]]},{"id":"fd208c3d.f1316","type":"inject","z":"4e2e8cc.ec21b74","name":"","topic":"stat/tasmota_156/POWER9","payload":"OFF","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":180,"wires":[["5c9061e1.9f8dc"]]},{"id":"f0a64cae.6413e","type":"mqtt-broker","z":"","name":"mosquitto","broker":"192.168.0.105","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

It works, but you need to modify it.

[{"id":"34e083ba.ffd09c","type":"inject","z":"81db2ae6.0555b8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":135,"y":600,"wires":[["24b7e150.975ff6"]],"l":false},{"id":"24b7e150.975ff6","type":"function","z":"81db2ae6.0555b8","name":"","func":"idx = {\n    \"stat/tasmota_156/POWER2\":178,\n    \"stat/tasmota_156/POWER1\":177,\n    \"stat/tasmota_156/POWER9\":179,\n    \"stat/esp_157_tasmota/POWER1\":108\n    }\n    \n    \nglobal.set(\"idx\",idx)\n\nreturn null","outputs":1,"noerr":0,"x":207,"y":600,"wires":[[]],"l":false},{"id":"149faa32.d9a66e","type":"comment","z":"81db2ae6.0555b8","name":"Set global idx variable","info":"","x":196,"y":552,"wires":[]},{"id":"f0696f42.8d44e8","type":"inject","z":"81db2ae6.0555b8","name":"","topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"PCFtopic=esp_156_tasmota\\npower=1\",\"dtype\":\"Light/Switch\",\"hwid\":\"6\",\"id\":\"000140FE\",\"idx\":178,\"name\":\"pcf_luce1\",\"nvalue\":1,\"stype\":\"Switch\",\"svalue1\":\"0\",\"switchType\":\"On/Off\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":696,"wires":[["b7a73fb1.7db728"]]},{"id":"78f63897.80efb8","type":"debug","z":"81db2ae6.0555b8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":375,"y":720,"wires":[],"l":false},{"id":"424c2e75.f7b198","type":"debug","z":"81db2ae6.0555b8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":423,"y":672,"wires":[],"l":false},{"id":"b7a73fb1.7db728","type":"function","z":"81db2ae6.0555b8","name":"","func":"\nidx = global.get(\"idx\")\ni = msg.payload.idx\nstate = msg.payload.nvalue\n\nlet topic = Object.keys(idx).find(k=>idx[k]===i);\n\n\n\nreturn {topic:topic, payload:state}","outputs":1,"noerr":0,"x":279,"y":696,"wires":[["78f63897.80efb8","a3842e8f.9baa2"]],"l":false},{"id":"a3842e8f.9baa2","type":"function","z":"81db2ae6.0555b8","name":"","func":"\nidx = global.get(\"idx\")\ni = msg.topic\nstate = msg.payload\n\nidxout = idx[i]\n\nreturn {idx:idxout,nvalue:state}","outputs":1,"noerr":0,"x":375,"y":672,"wires":[["424c2e75.f7b198"]],"l":false}]

In this example it is injecting:

{"Battery":255,"RSSI":12,"description":"PCFtopic=esp_156_tasmota\npower=1","dtype":"Light/Switch","hwid":"6","id":"000140FE","idx":178,"name":"pcf_luce1","nvalue":1,"stype":"Switch","svalue1":"0","switchType":"On/Off","unit":1}

It goes through the first function node, output tasmota topic plus value, second function node, transforms it back to the domoticz idx value.

sorry, but i do not succeed on that.
i try to implement this way:

  1. start from tasmota:
    i will put all sniff mqtt topics like:
    stat/<topic_name>/#
    and the firtst one function
    but it not works because i see this message in domoticz logs:
2020-05-07 21:01:55.581 MQTT: Topic: domoticz/in, Message: {"svalue":"","Battery":100,"RSSI":6}
2020-05-07 21:01:55.682 MQTT: Topic: domoticz/in, Message: {"svalue":"","Battery":100,"RSSI":6}
2020-05-07 21:01:55.582 Error: MQTT: unknown idx received! (idx 0)
2020-05-07 21:01:55.683 Error: MQTT: unknown idx received! (idx 0)

it should be:
domoticz/in {"idx":179,"nvalue":1,"svalue":"","Battery":100,"RSSI":6}
it semans that is not publishing the topic and also the payload is partial

  1. try to use only the flow with two functions, because i understand that:

from domoticz it go hrough the first function node, output tasmota topic plus value, second function node, transforms it back to the domoticz idx value.

so i put there the sniff of tasmota: (/stat/.... )
and the sniff for domoticz/out

and at the end of functions the publish commands:

but in this case nothing is working from both side and i receive these errors contiuosly:

 2020-05-07 21:11:25.035 MQTT: Topic: domoticz/in, Message: {"svalue":"","Battery":100,"RSSI":6}
2020-05-07 21:11:25.136 MQTT: Topic: domoticz/in, Message: {"svalue":"","Battery":100,"RSSI":6}
2020-05-07 21:11:25.237 MQTT: Topic: domoticz/in, Message: {"svalue":"","Battery":100,"RSSI":6}
2020-05-07 21:11:25.340 MQTT: Topic: domoticz/in, Message: {"svalue":"","Battery":100,"RSSI":6}
2020-05-07 21:11:25.441 MQTT: Topic: domoticz/in, Message: {"svalue":"","Battery":100,"RSSI":6}
2020-05-07 21:11:25.542 MQTT: Topic: domoticz/in, Message: {"svalue":"","Battery":100,"RSSI":6}
2020-05-07 21:11:25.643 MQTT: Topic: domoticz/in, Message: {"svalue":"","Battery":100,"RSSI":6}
2020-05-07 21:11:25.744 MQTT: Topic: domoticz/in, Message: {"svalue":"","Battery":100,"RSSI":6}
2020-05-07 21:11:25.036 Error: MQTT: unknown idx received! (idx 0)
2020-05-07 21:11:25.137 Error: MQTT: unknown idx received! (idx 0)
2020-05-07 21:11:25.239 Error: MQTT: unknown idx received! (idx 0)
2020-05-07 21:11:25.340 Error: MQTT: unknown idx received! (idx 0)
2020-05-07 21:11:25.442 Error: MQTT: unknown idx received! (idx 0)
2020-05-07 21:11:25.543 Error: MQTT: unknown idx received! (idx 0)
2020-05-07 21:11:25.643 Error: MQTT: unknown idx received! (idx 0)
2020-05-07 21:11:25.744 Error: MQTT: unknown idx received! (idx 0)
2020-05-07 21:11:27.546 MQTT: Topic: domoticz/in, Message: {"svalue":"","Battery":100,"RSSI":6}
2020-05-07 21:11:27.547 Error: MQTT: unknown idx received! (idx 0)
2020-05-07 21:11:31.979 MQTT: Topic: domoticz/in, Message: {"svalue":"","Battery":100,"RSSI":6}
2020-05-07 21:11:31.979 Error: MQTT: unknown idx received! (idx 0)
2020-05-07 21:11:32.201 MQTT: Topic: domoticz/in, Message: {"svalue":"","Battery":100,"RSSI":6}
2020-05-07 21:11:32.231 (dummy ) Light/Switch (rele_caldaia_ACS)
2020-05-07 21:11:32.250 (dummy ) Light/Switch (Valvola_TreVie_ACS)

maybe it happen because it's necesary to filter the domoticz/out not only with function
let topic = Object.keys(idx).find(k=>idx[k]===i);
??

this is the flow

[{"id":"4e2e8cc.ec21b74","type":"tab","label":"Flow 7","disabled":false,"info":""},{"id":"9790dc66.f8104","type":"inject","z":"4e2e8cc.ec21b74","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":295,"y":80,"wires":[["37680434.435cd4"]],"l":false},{"id":"37680434.435cd4","type":"function","z":"4e2e8cc.ec21b74","name":"","func":"idx = {\n    \"stat/tasmota_156/POWER2\":178,\n    \"stat/tasmota_156/POWER1\":177,\n    \"stat/tasmota_156/POWER9\":179,\n    \"stat/esp_157_tasmota/POWER1\":108\n    }\n    \n    \nglobal.set(\"idx\",idx)\n\nreturn null","outputs":1,"noerr":0,"x":395,"y":80,"wires":[[]],"l":false},{"id":"dd0384fe.8e13c8","type":"comment","z":"4e2e8cc.ec21b74","name":"Set global idx variable","info":"","x":360,"y":40,"wires":[]},{"id":"bc47e4b8.f5cd7","type":"inject","z":"4e2e8cc.ec21b74","name":"","topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"\",\"dtype\":\"Light/Switch\",\"hwid\":\"6\",\"id\":\"000140FE\",\"idx\":177,\"name\":\"pcf_luce1\",\"nvalue\":1,\"stype\":\"Switch\",\"svalue1\":\"0\",\"switchType\":\"On/Off\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":360,"wires":[["a94fbc4f.43efd8"]]},{"id":"605acbcb.753a94","type":"debug","z":"4e2e8cc.ec21b74","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":515,"y":440,"wires":[],"l":false},{"id":"27a4a0c.9a827e","type":"debug","z":"4e2e8cc.ec21b74","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":595,"y":340,"wires":[],"l":false},{"id":"a94fbc4f.43efd8","type":"function","z":"4e2e8cc.ec21b74","name":"domoticz to tasmota","func":"\nidx = global.get(\"idx\")\ni = msg.payload.idx\nstate = msg.payload.nvalue\n\nlet topic = Object.keys(idx).find(k=>idx[k]===i);\n\n\n\nreturn {topic:topic, payload:state}","outputs":1,"noerr":0,"x":415,"y":340,"wires":[["605acbcb.753a94","be0be772.1ced1"]],"l":false},{"id":"be0be772.1ced1","type":"function","z":"4e2e8cc.ec21b74","name":"tasmota to domoticz","func":"\nidx = global.get(\"idx\")\ni = msg.topic\nstate = msg.payload\n\nidxout = idx[i]\n\n\npayload = {\"idx\":idxout,\"nvalue\":state,\"svalue\":\"\",\"Battery\":100,\"RSSI\":6} \n\n//return {idx:idxout,nvalue:state}\n\nmsg.payload = payload ;\nmsg.topic = \"domoticz/in\";\nreturn msg;\n\n","outputs":1,"noerr":0,"x":495,"y":340,"wires":[["27a4a0c.9a827e","c0abfe73.06ad7"]],"l":false},{"id":"2ebfc4f8.ccf78c","type":"function","z":"4e2e8cc.ec21b74","name":"","func":"p = msg.payload\nidx = global.get(\"idx\")\ni = msg.topic\n\nidxout = idx[i]\nstate = (p===\"ON\") ? 1:0\n\npayload = {\"idx\":idxout,\"nvalue\":state,\"svalue\":\"\",\"Battery\":100,\"RSSI\":6} \n\nmsg.payload = payload ;\nmsg.topic = \"domoticz/in\";\nreturn msg;\n","outputs":1,"noerr":0,"x":455,"y":200,"wires":[["fdaf103.7fcff7","97b3fdc3.73fe8","174d1ac8.a21f8d"]],"l":false},{"id":"c0abfe73.06ad7","type":"mqtt out","z":"4e2e8cc.ec21b74","name":"","topic":"","qos":"","retain":"","broker":"f0a64cae.6413e","x":650,"y":400,"wires":[]},{"id":"174d1ac8.a21f8d","type":"debug","z":"4e2e8cc.ec21b74","name":"TASM_payl","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":630,"y":200,"wires":[]},{"id":"fdaf103.7fcff7","type":"debug","z":"4e2e8cc.ec21b74","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":610,"y":240,"wires":[]},{"id":"97b3fdc3.73fe8","type":"debug","z":"4e2e8cc.ec21b74","name":"TASM_topic","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"topic","targetType":"msg","x":630,"y":120,"wires":[]},{"id":"f949f610.b0219","type":"mqtt in","z":"4e2e8cc.ec21b74","name":"","topic":"stat/tasmota_156/#","qos":"2","datatype":"auto","broker":"f0a64cae.6413e","x":230,"y":240,"wires":[["a94fbc4f.43efd8"]]},{"id":"ae1e1e37.de511","type":"mqtt in","z":"4e2e8cc.ec21b74","name":"","topic":"stat/esp_157_tasmota/#","qos":"2","datatype":"auto","broker":"f0a64cae.6413e","x":210,"y":200,"wires":[["a94fbc4f.43efd8"]]},{"id":"692d4f3f.8ed7e","type":"mqtt in","z":"4e2e8cc.ec21b74","name":"","topic":"domoticz/out","qos":"2","datatype":"auto","broker":"f0a64cae.6413e","x":230,"y":300,"wires":[["a94fbc4f.43efd8"]]},{"id":"d78b5ac7.450198","type":"inject","z":"4e2e8cc.ec21b74","name":"","topic":"stat/tasmota_156/POWER9","payload":"ON","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":226,"y":418,"wires":[["a94fbc4f.43efd8"]]},{"id":"2a798c46.ba0e64","type":"inject","z":"4e2e8cc.ec21b74","name":"","topic":"stat/tasmota_156/POWER9","payload":"OFF","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":460,"wires":[["a94fbc4f.43efd8"]]},{"id":"7a324515.d9fd84","type":"inject","z":"4e2e8cc.ec21b74","name":"commands to considerate, idx=177 is inside table","topic":"","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"PCFtopic=esp_156_tasmota\\npower=1\",\"dtype\":\"Light/Switch\",\"hwid\":\"6\",\"id\":\"000140FE\",\"idx\":178,\"name\":\"pcf_luce1\",\"nvalue\":1,\"stype\":\"Switch\",\"svalue1\":\"0\",\"switchType\":\"On/Off\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":260,"y":120,"wires":[["2ebfc4f8.ccf78c"]]},{"id":"f0a64cae.6413e","type":"mqtt-broker","z":"","name":"mosquitto","broker":"192.168.0.105","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

i understand why i recevie double message with idx 0
because when the commands statrt from domoticz tasmota trigger the device and replay with commands:
01:00:20 MQT: stat/tasmota_156/RESULT = {"POWER9":"ON"}
01:00:20 MQT: stat/tasmota_156/POWER9 = ON

so the second one is recongnized by your function in node-red, but the first one i think that is generating this:
{"svalue":"","Battery":100,"RSSI":6}
is it possible to filter the RESULT command of tasmota?
01:00:20 MQT: stat/tasmota_156/RESULT = {"POWER9":"ON"}

Instead of immediately connecting everything to mqtt, start with debug nodes only.

Check the inputs with inject nodes and directly a debug node and then check the outputs.

Perhaps the 2 function nodes confuses you, I used it to consolidate the flow and was hoping on your initiative to adapt it. Did you read up on working with messages ?

I have made it simpler. The point was that you receive ON OFF from tasmota and I assumed 0 1, so I have modified that for you.

[{"id":"f136831a.af7348","type":"inject","z":"981487d6.f28708","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":111,"y":96,"wires":[["c4ee72af.8ff"]],"l":false},{"id":"c4ee72af.8ff","type":"function","z":"981487d6.f28708","name":"","func":"idx = {\n    \"stat/tasmota_156/POWER2\":178,\n    \"stat/tasmota_156/POWER1\":177,\n    \"stat/tasmota_156/POWER9\":179,\n    \"stat/esp_157_tasmota/POWER1\":108\n    }\n    \n    \nglobal.set(\"idx\",idx)\n\nreturn null","outputs":1,"noerr":0,"x":159,"y":96,"wires":[[]],"l":false},{"id":"8d49adc7.57c8d8","type":"comment","z":"981487d6.f28708","name":"Set global idx variable","info":"","x":168,"y":40,"wires":[]},{"id":"70474b26.74e294","type":"debug","z":"981487d6.f28708","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":746,"y":240,"wires":[]},{"id":"fe08e928.b0afd8","type":"debug","z":"981487d6.f28708","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":746,"y":384,"wires":[]},{"id":"ff6f7087.4529b8","type":"function","z":"981487d6.f28708","name":"domoticz to tasmota","func":"\nidx = global.get(\"idx\")\ni = msg.payload.idx\nstate = msg.payload.nvalue\n\nlet topic = Object.keys(idx).find(k=>idx[k]===i);\n\n\n\nreturn {topic:topic, payload:state}","outputs":1,"noerr":0,"x":532,"y":240,"wires":[["70474b26.74e294"]]},{"id":"6c509724.2bd17","type":"function","z":"981487d6.f28708","name":"tasmota to domoticz","func":"\nidx = global.get(\"idx\")\ni = msg.topic\nstate = (msg.payload ==\"ON\") ? 1 : 0\n\nidxout = idx[i]\n\npayload = {\"idx\":idxout,\"nvalue\":state,\"svalue\":\"\",\"Battery\":100,\"RSSI\":6} \n\nmsg.payload = payload ;\nmsg.topic = \"domoticz/in\";\nreturn msg;\n\n","outputs":1,"noerr":0,"x":532,"y":384,"wires":[["fe08e928.b0afd8"]]},{"id":"cf90b715.c0911","type":"inject","z":"981487d6.f28708","name":"","topic":"stat/tasmota_156/POWER9","payload":"ON","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":226,"y":360,"wires":[["6c509724.2bd17"]]},{"id":"882de728.51c7e","type":"inject","z":"981487d6.f28708","name":"","topic":"stat/tasmota_156/POWER9","payload":"OFF","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":226,"y":408,"wires":[["6c509724.2bd17"]]},{"id":"a62f2198.bb0f1","type":"inject","z":"981487d6.f28708","name":"domoticz/out : 177 - nvalue 1 (ON)","topic":"domoticz/out","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"PCFtopic=esp_156_tasmota\\npower=1\",\"dtype\":\"Light/Switch\",\"hwid\":\"6\",\"id\":\"000140FE\",\"idx\":178,\"name\":\"pcf_luce1\",\"nvalue\":1,\"stype\":\"Switch\",\"svalue1\":\"0\",\"switchType\":\"On/Off\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":236,"y":216,"wires":[["ff6f7087.4529b8"]]},{"id":"896a1839.46889","type":"comment","z":"981487d6.f28708","name":"Tasmota to domoticz/in","info":"","x":172,"y":312,"wires":[]},{"id":"e2d6dc83.efaf38","type":"comment","z":"981487d6.f28708","name":"domoticz/out to tasmota","info":"","x":182,"y":168,"wires":[]},{"id":"e1234cbf.d08ad8","type":"inject","z":"981487d6.f28708","name":"domoticz/out : 177 - nvalue 0 (OFF)","topic":"domoticz/out","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"PCFtopic=esp_156_tasmota\\npower=1\",\"dtype\":\"Light/Switch\",\"hwid\":\"6\",\"id\":\"000140FE\",\"idx\":178,\"name\":\"pcf_luce1\",\"nvalue\":0,\"stype\":\"Switch\",\"svalue1\":\"0\",\"switchType\":\"On/Off\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":236,"y":264,"wires":[["ff6f7087.4529b8"]]}]

If this does not work for you answer all these questions:
What is the input from domoticz/out, what is the output to tasmota, what is the input from tasmota, what is the output to domoticz/in ?

hi, thanks so much for that!
but belive me, i tried so much...without suceed
anyway i tested your flow, it woks partially, and partially i solved, below my flow and explanations and answer at your questions

  1. input from domoticz to nodered -> topic=domoticz/out
    here the firts problem: domoticz is sended out many mqtt commands periodically, for every idx inside (so temperature sensor, light, thermostat, .....whatever)
    so without filtering it create a mess
    i solved using a switch, where i check if inside description there is written "PCF"
    if yes it's a message to manipulate for tasmota light, othewise we skip.
    it could be a solution having a control inside one function: if idx sended out from domoticz metch the table we manipulate otherwise not.
    i try but not succeed so i used a switch node and write inside description PCF to filter.

  2. the output from domoticz to tasmota
    in your function you send stat/<esp_topic_name>/POWER
    is not correct. stat/ is for receving, to command ee need to use cmnd/<esp_topic_name>/POWERx 1 or 0
    i change the function to manipulate the table and substitute stat/ with cmnd/
    ( /stat inside the table is correct because is used to listen. see next answer reply
    /cmnd is used to comand)
    so to command:
    topic = cmnd/tasmota_156/POWER9
    payload = 1 or 0
    variables in bold

  3. input from tasmota
    in this case we have stat/<<esp_topic_name>/POWER ON or OFF
    also here unfortunatly i see that tasmota when triggered reply with two different stat/

stat/tasmota_156/RESULT  {"POWER9":"ON"}
stat/tasmota_156/POWER9  ON

we are intereste only on second one:
stat/tasmota_156/POWER9 ON
topic = stat/tasmota_156/POWER9
payload = ON
variables = tasmota_159 and 9 and ON (or OFF)

also here i solved using a switch node that filtering if inside topic there is POWER
on first topic there is RESULT so i skip (in first command,that we not interested, POWER is inside payload)

  1. output to domoticz/in
    node-red should send to domoticz:
    topic= domoticz/in
    payload= {"idx":179,"nvalue":1,"svalue":"","Battery":100,"RSSI":6}
    where the variables are only idx and nvalue (1 or 0)

so your flow works correctly in terms of send / receive topic and payload.
but i add the parts described above to make it working. there is a way more robust to do this? expecially filtering from domoticz.

finally I find a bug that send to domoticz/in an empty message when see idx 0
so i insert inside the function from tasmota to domoticz an if statement
maybe it can be avoided in some way?

another issue, that one is completly not solved, because not understand why, is this one:
only when i started the command from domoticz to tasmota, the command is send two times:
as you can see in the logs below.
i al so see that for example startwith light off -> press on-> i see the icon on domoticz blink and then goes to on
same issue if i switch off from domoticz -> icon blink one time then goes to off


2020-05-08 15:41:30.372 (dummy ) Light/Switch (tasmota_PCF_9)
2020-05-08 15:41:30.519 MQTT: Topic: domoticz/in, Message: {"idx":179,"nvalue":0,"svalue":"","Battery":100,"RSSI":6}
2020-05-08 15:41:30.668 MQTT: Topic: domoticz/in, Message: {"idx":179,"nvalue":1,"svalue":"","Battery":100,"RSSI":6}
2020-05-08 15:41:30.352 Status: User: Admin initiated a switch command (179/tasmota_PCF_9/On)
2020-05-08 15:41:33.611 (dummy ) Light/Switch (tasmota_PCF_9)
2020-05-08 15:41:33.728 MQTT: Topic: domoticz/in, Message: {"idx":179,"nvalue":1,"svalue":"","Battery":100,"RSSI":6}
2020-05-08 15:41:33.880 MQTT: Topic: domoticz/in, Message: {"idx":179,"nvalue":0,"svalue":"","Battery":100,"RSSI":6}

here my flow with my implementations.
thanks for support

[{"id":"4e2e8cc.ec21b74","type":"tab","label":"Flow 7","disabled":false,"info":""},{"id":"c0abfe73.06ad7","type":"mqtt out","z":"4e2e8cc.ec21b74","name":"","topic":"","qos":"","retain":"","broker":"f0a64cae.6413e","x":770,"y":200,"wires":[]},{"id":"13b61fa5.488db8","type":"debug","z":"4e2e8cc.ec21b74","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":770,"y":244,"wires":[]},{"id":"c0b78f54.01cda8","type":"debug","z":"4e2e8cc.ec21b74","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":770,"y":388,"wires":[]},{"id":"5d272ebb.dc393","type":"function","z":"4e2e8cc.ec21b74","name":"domoticz to tasmota","func":"\nidx = global.get(\"idx\")\ni = msg.payload.idx\nstate = msg.payload.nvalue\n\nlet topic = Object.keys(idx).find(k=>idx[k]===i);\n\n// split the topic from global table into two parts\nvar parts = topic.split('/');\n//node.warn(parts); to see the parts splitted\nvar topic_end = parts[1] + \"/\" + parts[2];\n//node.warn(topic_end);\n\n//build message topic adding cmnd/<topic>\ntopic_cmnd = \"cmnd/\" + topic_end ;\n//node.warn(topic_cmnd);\n\nreturn {topic:topic_cmnd, payload:state}\n\n\n","outputs":1,"noerr":0,"x":556,"y":244,"wires":[["13b61fa5.488db8","1072ec38.ca021c","c0abfe73.06ad7"]]},{"id":"352a2a81.6181be","type":"function","z":"4e2e8cc.ec21b74","name":"tasmota to domoticz","func":"\nidx = global.get(\"idx\")\ni = msg.topic\nstate = (msg.payload ==\"ON\") ? 1 : 0\n\nidxout = idx[i]\n\n//to prevent to send out messages to domoticz with no IDX \nif (idxout > 0 ) {\npayload = {\"idx\":idxout,\"nvalue\":state,\"svalue\":\"\",\"Battery\":100,\"RSSI\":6}  ;\n}\nelse {\nt = msg.topic ;\np = msg.payload ;\n}\n\n\nmsg.payload = payload ;\nmsg.topic = \"domoticz/in\";\nreturn msg;\n\n","outputs":1,"noerr":0,"x":560,"y":400,"wires":[["c0b78f54.01cda8","3560f899.f9ab98"]]},{"id":"7ab4ba94.a992ec","type":"inject","z":"4e2e8cc.ec21b74","name":"","topic":"stat/tasmota_156/POWER9","payload":"ON","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":360,"wires":[["a737c969.c31d6"]]},{"id":"3bed1d1.26c0c62","type":"inject","z":"4e2e8cc.ec21b74","name":"","topic":"stat/tasmota_156/POWER9","payload":"OFF","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":400,"wires":[["a737c969.c31d6"]]},{"id":"426d5e8c.c2526","type":"inject","z":"4e2e8cc.ec21b74","name":"domoticz/out : 177 - nvalue 1 (ON)","topic":"domoticz/out","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"PCFtopic=esp_156_tasmota\\npower=1\",\"dtype\":\"Light/Switch\",\"hwid\":\"6\",\"id\":\"000140FE\",\"idx\":178,\"name\":\"pcf_luce1\",\"nvalue\":1,\"stype\":\"Switch\",\"svalue1\":\"0\",\"switchType\":\"On/Off\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":220,"wires":[["1d6ab91a.d86ff7"]]},{"id":"54a390bb.0be5e8","type":"comment","z":"4e2e8cc.ec21b74","name":"Tasmota to domoticz/in","info":"","x":196,"y":316,"wires":[]},{"id":"5e4239bd.d55e8","type":"comment","z":"4e2e8cc.ec21b74","name":"domoticz/out to tasmota","info":"","x":200,"y":140,"wires":[]},{"id":"6a5fbebf.1dcd3","type":"inject","z":"4e2e8cc.ec21b74","name":"domoticz/out : 177 - nvalue 0 (OFF)","topic":"domoticz/out","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"PCFtopic=esp_156_tasmota\\npower=1\",\"dtype\":\"Light/Switch\",\"hwid\":\"6\",\"id\":\"000140FE\",\"idx\":178,\"name\":\"pcf_luce1\",\"nvalue\":0,\"stype\":\"Switch\",\"svalue1\":\"0\",\"switchType\":\"On/Off\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":260,"wires":[["1d6ab91a.d86ff7"]]},{"id":"3e5dbbe8.b88624","type":"comment","z":"4e2e8cc.ec21b74","name":"Set global idx variable","info":"","x":600,"y":40,"wires":[]},{"id":"b2e5f7c6.1c399","type":"inject","z":"4e2e8cc.ec21b74","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":535,"y":80,"wires":[["4d328cba.902684"]],"l":false},{"id":"4d328cba.902684","type":"function","z":"4e2e8cc.ec21b74","name":"","func":"idx = {\n    \"stat/tasmota_156/POWER2\":178,\n    \"stat/tasmota_156/POWER1\":177,\n    \"stat/tasmota_156/POWER9\":179,\n    \"stat/esp_157_tasmota/POWER1\":108\n    }\n    \n    \nglobal.set(\"idx\",idx)\n\nreturn null","outputs":1,"noerr":0,"x":675,"y":80,"wires":[[]],"l":false},{"id":"1072ec38.ca021c","type":"debug","z":"4e2e8cc.ec21b74","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"node.warn","targetType":"msg","x":800,"y":300,"wires":[]},{"id":"1d6ab91a.d86ff7","type":"switch","z":"4e2e8cc.ec21b74","name":"","property":"payload.description","propertyType":"msg","rules":[{"t":"cont","v":"PCF","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":430,"y":180,"wires":[["5d272ebb.dc393"],[]]},{"id":"a737c969.c31d6","type":"switch","z":"4e2e8cc.ec21b74","name":"","property":"topic","propertyType":"msg","rules":[{"t":"cont","v":"POWER","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":450,"y":320,"wires":[["352a2a81.6181be"],[]]},{"id":"b2bc241c.04b108","type":"inject","z":"4e2e8cc.ec21b74","name":"domoticz/out : 179 - nvalue 1 (ON)","topic":"domoticz/out","payload":"{\"Battery\":255,\"RSSI\":12,\"description\":\"PCFtopic=tasmota_156\\npower=9\",\"dtype\":\"Light/Switch\",\"hwid\":\"6\",\"id\":\"00014103\",\"idx\":179,\"name\":\"tasmota_PCF_9\",\"nvalue\":1,\"stype\":\"Switch\",\"svalue1\":\"0\",\"switchType\":\"On/Off\",\"unit\":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":180,"wires":[["1d6ab91a.d86ff7"]]},{"id":"99bd39c0.379858","type":"mqtt in","z":"4e2e8cc.ec21b74","name":"","topic":"domoticz/out","qos":"2","datatype":"json","broker":"f0a64cae.6413e","x":230,"y":100,"wires":[["1d6ab91a.d86ff7"]]},{"id":"803c3fc7.a021e8","type":"mqtt in","z":"4e2e8cc.ec21b74","name":"","topic":"stat/esp_157_tasmota/#","qos":"2","datatype":"auto","broker":"f0a64cae.6413e","x":190,"y":460,"wires":[["352a2a81.6181be"]]},{"id":"1df18e46.dc8802","type":"mqtt in","z":"4e2e8cc.ec21b74","name":"","topic":"stat/tasmota_156/#","qos":"2","datatype":"auto","broker":"f0a64cae.6413e","x":170,"y":500,"wires":[["352a2a81.6181be"]]},{"id":"3560f899.f9ab98","type":"mqtt out","z":"4e2e8cc.ec21b74","name":"","topic":"","qos":"","retain":"","broker":"f0a64cae.6413e","x":770,"y":460,"wires":[]},{"id":"f0a64cae.6413e","type":"mqtt-broker","z":"","name":"mosquitto","broker":"192.168.0.105","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

To be honest, I am not really following you, there is a lot of text.

What is the input from domoticz/out, what is the output to tasmota, what is the input from tasmota, what is the output to domoticz/in ?

I meant, debug messages.

Did you click the link I posted above, how to work with messages ? That wil answer a lot of your questions, also watch the video playlist in that link.

ok, i was try to explain as much as possible what i find, and how i solve. did you look at my flow where i implement some solutions?

anyway the anser at these question are the 4 point list above.
here the messages:
1) input from domoticz/out
{"topic":"domoticz/out","payload":{"Battery":255,"RSSI":12,"description":"PCFtopic=tasmota_156\npower=9","dtype":"Light/Switch","hwid":"6","id":"00014103","idx":179,"name":"tasmota_PCF_9","nvalue":0,"stype":"Switch","svalue1":"0","switchType":"On/Off","unit":1},"qos":0,"retain":false,"_msgid":"d2510b38.3966b8"}

but as i wrote there are many other mqtt published by domoticz in domotic/out
so we need to filter, i used inside flow posted on message above a switch node.
if message contain "PCF" i will manipulate otherwise not.
question, is possible to eliminate the switch node and using global table?
if idx number is matching the idx insert inisde global table we process otherwise we do not nothing.

2) output for tasmota
{"topic":"cmnd/tasmota_156/POWER9","payload":"1","qos":0,"retain":false,"_msgid":"26fd3a8f.53a836"}

here we need to use cmnd/
and i modify your function to manipulate the global table and substitute stat/ with cmnd/

3) input from tasmota

{"topic":"stat/tasmota_156/RESULT","payload":"{\"POWER9\":\"ON\"}","qos":0,"retain":false,"_msgid":"d44f5924.1e2f78"}

{"topic":"stat/tasmota_156/POWER9","payload":"ON","qos":0,"retain":false,"_msgid":"26fd3a8f.53a836"}

as explainde above, i see that tasmota send out every tme two different messages. We are intereste only on message with POWER inside topic.
the message with RESULT we are not intereste; that's reason why i use a node switch to filter messages from tasmota that contain power inside topic

  1. output to domoticz/in

{"topic":"domoticz/in","payload":{"idx":179,"nvalue":0,"svalue":"","Battery":100,"RSSI":6},"qos":0,"retain":false,"_msgid":"65a21212.568f3c"}

at point 1) (when i start the command from domoticz, so domoticz/out)
i find also an issue that i'm not able to solve and not understand why is happening
every time i see a double commands.
if i click on icon swithc on, i see that icon become on, immediatly off, immediatly on again.
if it's on and i press off: it go immediatly to off, then immediatly on, finally off.
i posted the logs.

oh yes, thanks i read that article some days ago when i started with this flow, then read again and watch video...it helps a lot, still lacking something at me, but al least more clear thanks to your flow and forum reading in general searching for solutions :slight_smile:
pleas have a look at my flow if you have time so you can understand what i did and maybe correct what i did not in a proper way.
thanks again

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