Modbus contrib with hex commands through TCP

Hello, i am reading the values of 3 modbus RTU energy meters, i am using a modbus RTU <> modbus TCP gateway and everything is working fine, however, i need to reset the measured energy daily, to do that i need to send a custom HEX command to the modbus RTU meters, which is: slave-id 0x42 CRCH CRCL.

i have done it sucessfull with a tcp-request node, however, if i have the modbus-flex-getter nodes enabled the request fails, i guess is because there is an active connection with the modbus server due the modbus-flex-getter node.

the question is, is there a way to send a custom hex command with nodered-contrib-modbus? if no, can i pause/stop/kill/close the connection in the modbus-flex-getter node? so in this way i can reset the energy meter counter and restart the connection again.

here are the energy meters that i am using:

this is my flow:

This is my actual code in node-red:

[{"id":"e2071299.201fe","type":"tab","label":"Monitor de energia Modbus","disabled":false,"info":""},{"id":"aa51672d.68f1d8","type":"function","z":"e2071299.201fe","name":"Decode payload","func":"a = msg.payload[0]\nb = msg.payload[1]\nc = msg.payload[2]\nd = msg.payload[3]\ne = msg.payload[4]\nf = msg.payload[5]\ng = msg.payload[6]\nh = msg.payload[7]\ni = msg.payload[8]\n\nvar msg1 = {payload:a/10};\nvar msg2 = {payload:b/1000};\nvar msg3 = {payload:c};\nvar msg4 = {payload:d/10000};\nvar msg5 = {payload:e/10000};\nvar msg6 = {payload:f};\nvar msg7 = {payload:g};\nvar msg8 = {payload:h/10};\nvar msg9 = {payload:i/100};\n\nreturn [msg1,msg2,msg3,msg4,msg5,msg6,msg7,msg8,msg9];","outputs":9,"noerr":0,"x":440,"y":620,"wires":[["5be36a07.511074"],["46f6c75c.1c8718"],[],["d557fd64.62ce5"],[],["c851c16f.279c2"],[],["7e1e9b8b.bcee44"],["b6f4c01d.1faf9"]]},{"id":"2b45a040.776f8","type":"function","z":"e2071299.201fe","name":"Decode payload","func":"a = msg.payload[0]\nb = msg.payload[1]\nc = msg.payload[2]\nd = msg.payload[3]\ne = msg.payload[4]\nf = msg.payload[5]\ng = msg.payload[6]\nh = msg.payload[7]\ni = msg.payload[8]\n\nvar msg1 = {payload:a/10};\nvar msg2 = {payload:b/1000};\nvar msg3 = {payload:c};\nvar msg4 = {payload:d/10000};\nvar msg5 = {payload:e/10000};\nvar msg6 = {payload:f};\nvar msg7 = {payload:g};\nvar msg8 = {payload:h/10};\nvar msg9 = {payload:i/100};\n\nreturn [msg1,msg2,msg3,msg4,msg5,msg6,msg7,msg8,msg9];","outputs":9,"noerr":0,"x":440,"y":1040,"wires":[["7fd94b56.795414"],["dd43e46a.d22348"],[],["cb71ee9b.4138d"],[],["14606d18.71cbe3"],[],["8a3a18f6.c962c8"],["6d618b04.643714"]]},{"id":"fb60f02.7d94d1","type":"function","z":"e2071299.201fe","name":"Decode payload","func":"a = msg.payload[0]\nb = msg.payload[1]\nc = msg.payload[2]\nd = msg.payload[3]\ne = msg.payload[4]\nf = msg.payload[5]\ng = msg.payload[6]\nh = msg.payload[7]\ni = msg.payload[8]\n\nvar msg1 = {payload:a/10};\nvar msg2 = {payload:b/1000};\nvar msg3 = {payload:c};\nvar msg4 = {payload:d/10000};\nvar msg5 = {payload:e/10000};\nvar msg6 = {payload:f};\nvar msg7 = {payload:g};\nvar msg8 = {payload:h/10};\nvar msg9 = {payload:i/100};\n\nreturn [msg1,msg2,msg3,msg4,msg5,msg6,msg7,msg8,msg9];","outputs":9,"noerr":0,"x":460,"y":200,"wires":[["bcafb111.4c24c"],["e3a06505.fbfc98"],[],["ed43336c.e71a1"],[],["a35fef4b.84577"],[],["85ea2bba.10a6e8"],["87f1bfa3.3b51a"]]},{"id":"bcafb111.4c24c","type":"ha-entity","z":"e2071299.201fe","name":"voltsr","server":"c44ef758.293d48","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"voltsr"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:power-socket-eu"},{"property":"unit_of_measurement","value":"V"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":650,"y":160,"wires":[[]]},{"id":"5be36a07.511074","type":"ha-entity","z":"e2071299.201fe","name":"voltss","server":"c44ef758.293d48","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"voltss"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:power-socket-eu"},{"property":"unit_of_measurement","value":"V"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":630,"y":580,"wires":[[]]},{"id":"7fd94b56.795414","type":"ha-entity","z":"e2071299.201fe","name":"voltst","server":"c44ef758.293d48","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"voltst"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:power-socket-eu"},{"property":"unit_of_measurement","value":"V"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":630,"y":1000,"wires":[[]]},{"id":"e3a06505.fbfc98","type":"ha-entity","z":"e2071299.201fe","name":"ampsr","server":"c44ef758.293d48","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"ampsr"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:flash"},{"property":"unit_of_measurement","value":"A"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":650,"y":220,"wires":[[]]},{"id":"46f6c75c.1c8718","type":"ha-entity","z":"e2071299.201fe","name":"ampss","server":"c44ef758.293d48","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"ampss"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:flash"},{"property":"unit_of_measurement","value":"A"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":630,"y":640,"wires":[[]]},{"id":"dd43e46a.d22348","type":"ha-entity","z":"e2071299.201fe","name":"ampst","server":"c44ef758.293d48","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"ampst"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:flash"},{"property":"unit_of_measurement","value":"A"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":630,"y":1060,"wires":[[]]},{"id":"ed43336c.e71a1","type":"ha-entity","z":"e2071299.201fe","name":"powerr","server":"c44ef758.293d48","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"powerr"},{"property":"device_class","value":"power"},{"property":"icon","value":"mdi:transmission-tower"},{"property":"unit_of_measurement","value":"kW"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"msg","inputOverride":"allow","x":650,"y":280,"wires":[[]]},{"id":"d557fd64.62ce5","type":"ha-entity","z":"e2071299.201fe","name":"powers","server":"c44ef758.293d48","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"powers"},{"property":"device_class","value":"power"},{"property":"icon","value":"mdi:transmission-tower"},{"property":"unit_of_measurement","value":"kW"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":640,"y":700,"wires":[[]]},{"id":"cb71ee9b.4138d","type":"ha-entity","z":"e2071299.201fe","name":"powert","server":"c44ef758.293d48","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"powert"},{"property":"device_class","value":"power"},{"property":"icon","value":"mdi:transmission-tower"},{"property":"unit_of_measurement","value":"kW"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":630,"y":1120,"wires":[[]]},{"id":"a35fef4b.84577","type":"ha-entity","z":"e2071299.201fe","name":"whr","server":"c44ef758.293d48","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"whr"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:poll"},{"property":"unit_of_measurement","value":"Wh"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":650,"y":340,"wires":[[]]},{"id":"85ea2bba.10a6e8","type":"ha-entity","z":"e2071299.201fe","name":"frequencyr","server":"c44ef758.293d48","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"frequencyr"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:current-ac"},{"property":"unit_of_measurement","value":"Hz"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":670,"y":400,"wires":[[]]},{"id":"c851c16f.279c2","type":"ha-entity","z":"e2071299.201fe","name":"whs","server":"c44ef758.293d48","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"whs"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:poll"},{"property":"unit_of_measurement","value":"Wh"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":630,"y":760,"wires":[[]]},{"id":"7e1e9b8b.bcee44","type":"ha-entity","z":"e2071299.201fe","name":"frequencys","server":"c44ef758.293d48","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"frequencys"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:current-ac"},{"property":"unit_of_measurement","value":"Hz"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":650,"y":820,"wires":[[]]},{"id":"14606d18.71cbe3","type":"ha-entity","z":"e2071299.201fe","name":"wht","server":"c44ef758.293d48","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"wht"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:poll"},{"property":"unit_of_measurement","value":"Wh"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":630,"y":1180,"wires":[[]]},{"id":"8a3a18f6.c962c8","type":"ha-entity","z":"e2071299.201fe","name":"frequencyt","server":"c44ef758.293d48","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"frequencyt"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:current-ac"},{"property":"unit_of_measurement","value":"Hz"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":650,"y":1240,"wires":[[]]},{"id":"f037bd10.ebb14","type":"modbus-response","z":"e2071299.201fe","name":"","registerShowMax":20,"x":690,"y":480,"wires":[]},{"id":"3ca3f58.05f660a","type":"modbus-flex-getter","z":"e2071299.201fe","name":"Modbus Flex Getter R","showStatusActivities":false,"showErrors":false,"logIOActivities":false,"server":"dab1c578.63ef48","useIOFile":false,"ioFile":"","useIOForPayload":false,"x":340,"y":400,"wires":[["fb60f02.7d94d1"],["f037bd10.ebb14"]]},{"id":"8977c6f5.8b02c8","type":"function","z":"e2071299.201fe","name":"Lectura F4","func":"msg.payload = \n    { \n        'fc': 4, \n        'unitid': 3, \n        'address': 0 , \n        'quantity': 9 \n    } \nreturn msg\nreturn msg;","outputs":1,"noerr":0,"x":150,"y":320,"wires":[["3ca3f58.05f660a"]]},{"id":"872f50eb.4b109","type":"inject","z":"e2071299.201fe","name":"","topic":"","payload":"","payloadType":"date","repeat":"5","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":200,"wires":[["8977c6f5.8b02c8","52c1db1.814e924","d75b9a2b.5a13f8"]]},{"id":"5484377c.62c678","type":"function","z":"e2071299.201fe","name":"Lectura F4","func":"msg.payload = \n    { \n        'fc': 4, \n        'unitid': 2, \n        'address': 0 , \n        'quantity': 9 \n    } \nreturn msg\nreturn msg;","outputs":1,"noerr":0,"x":170,"y":620,"wires":[["ae073569.8e9b78"]]},{"id":"ae073569.8e9b78","type":"modbus-flex-getter","z":"e2071299.201fe","name":"","showStatusActivities":false,"showErrors":false,"logIOActivities":false,"server":"dab1c578.63ef48","useIOFile":false,"ioFile":"","useIOForPayload":false,"x":350,"y":780,"wires":[["aa51672d.68f1d8"],["bba95c88.8697c"]]},{"id":"bba95c88.8697c","type":"modbus-response","z":"e2071299.201fe","name":"","registerShowMax":20,"x":670,"y":900,"wires":[]},{"id":"1ff644e.6a6ecbb","type":"function","z":"e2071299.201fe","name":"Lectura F4","func":"msg.payload = \n    { \n        'fc': 4, \n        'unitid': 1, \n        'address': 0 , \n        'quantity': 9 \n    } \nreturn msg\nreturn msg;","outputs":1,"noerr":0,"x":170,"y":1020,"wires":[["59d633ee.89e09c"]]},{"id":"59d633ee.89e09c","type":"modbus-flex-getter","z":"e2071299.201fe","name":"","showStatusActivities":false,"showErrors":false,"logIOActivities":false,"server":"dab1c578.63ef48","useIOFile":false,"ioFile":"","useIOForPayload":false,"x":330,"y":1240,"wires":[["2b45a040.776f8"],["7a26f2b1.d003ac"]]},{"id":"7a26f2b1.d003ac","type":"modbus-response","z":"e2071299.201fe","name":"","registerShowMax":20,"x":690,"y":1360,"wires":[]},{"id":"52c1db1.814e924","type":"delay","z":"e2071299.201fe","name":"","pauseType":"delay","timeout":"330","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":210,"y":500,"wires":[["5484377c.62c678"]]},{"id":"d75b9a2b.5a13f8","type":"delay","z":"e2071299.201fe","name":"","pauseType":"delay","timeout":"660","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":150,"y":940,"wires":[["1ff644e.6a6ecbb"]]},{"id":"87f1bfa3.3b51a","type":"ha-entity","z":"e2071299.201fe","name":"pfr","server":"c44ef758.293d48","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"pfr"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:current-ac"},{"property":"unit_of_measurement","value":"FP"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":810,"y":440,"wires":[[]]},{"id":"b6f4c01d.1faf9","type":"ha-entity","z":"e2071299.201fe","name":"pfs","server":"c44ef758.293d48","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"pfs"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:current-ac"},{"property":"unit_of_measurement","value":"FP"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":790,"y":840,"wires":[[]]},{"id":"6d618b04.643714","type":"ha-entity","z":"e2071299.201fe","name":"pft","server":"c44ef758.293d48","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"pft"},{"property":"device_class","value":""},{"property":"icon","value":"mdi:current-ac"},{"property":"unit_of_measurement","value":"FP"}],"state":"payload","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":670,"y":1300,"wires":[[]]},{"id":"ed6306ab.056e48","type":"debug","z":"e2071299.201fe","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":430,"y":940,"wires":[]},{"id":"2f98dc1e.966ed4","type":"inject","z":"e2071299.201fe","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":100,"y":1480,"wires":[["67289a85.3e4174","5adfe465.a65c0c"]]},{"id":"67289a85.3e4174","type":"function","z":"e2071299.201fe","name":"Reset energy UnitID 3","func":"msg.payload  = Buffer.from('00010000000403428171', 'hex');\nreturn msg;","outputs":1,"noerr":0,"x":520,"y":1540,"wires":[["cfe65a6b.360708"]]},{"id":"bf4f865b.167988","type":"debug","z":"e2071299.201fe","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1030,"y":1600,"wires":[]},{"id":"424b5e10.d04d1","type":"function","z":"e2071299.201fe","name":"Reset energy UnitID 2","func":"msg.payload  = Buffer.from('000100000004024280E1', 'hex');\nreturn msg;","outputs":1,"noerr":0,"x":520,"y":1680,"wires":[["cfe65a6b.360708"]]},{"id":"2a1413ae.f756cc","type":"function","z":"e2071299.201fe","name":"Reset energy UnitID 1","func":"msg.payload  = Buffer.from('00010000000401428011', 'hex');\nreturn msg;","outputs":1,"noerr":0,"x":520,"y":1820,"wires":[["cfe65a6b.360708"]]},{"id":"5adfe465.a65c0c","type":"delay","z":"e2071299.201fe","name":"","pauseType":"delay","timeout":"330","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":310,"y":1680,"wires":[["a0bb35a1.4138f8","424b5e10.d04d1"]]},{"id":"a0bb35a1.4138f8","type":"delay","z":"e2071299.201fe","name":"","pauseType":"delay","timeout":"330","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":330,"y":1820,"wires":[["2a1413ae.f756cc"]]},{"id":"cfe65a6b.360708","type":"tcp request","z":"e2071299.201fe","server":"172.19.6.25","port":"502","out":"time","splitc":"0","name":"","x":780,"y":1600,"wires":[["bf4f865b.167988"]]},{"id":"d8b33cec.98ddc","type":"debug","z":"e2071299.201fe","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":430,"y":1380,"wires":[]},{"id":"f502ccfd.524ef","type":"debug","z":"e2071299.201fe","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":430,"y":520,"wires":[]},{"id":"c44ef758.293d48","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true},{"id":"dab1c578.63ef48","type":"modbus-client","z":"","name":"modbusgateway","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":true,"tcpHost":"172.19.6.25","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1000","clientTimeout":"1000","reconnectOnTimeout":true,"reconnectTimeout":"2000","parallelUnitIdsAllowed":true}]

Thanks you! :slight_smile:

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