How to calculate the difference in KWH values received every 5 min from modbus data and save it as a new values in csv

Hi ,

How to calculate the difference in KWH values received every 5 min from energy meter through modbus data and save the difference as a new values ( KWH_sys) in csv. Am trying this because the KWH received from Energy Meter are cumulative values , i like to save the energy used for 5 min .

I created a program like the below

When i tried this program , i received "NaN"

image

Please guide me

Thanks

If you want to post some code it is better to copy/paste the text here (use the </> button when pasting it in) so we can read it more easily and modify sections for you. However, after squinting at the screen from a few inches away it looks as if you are subtracting something from msg.payload, but msg.payload is an object not a number. Did you mean to subtract it from msg.payload.something?

2 Likes

What is the context value KWH_sys .. maybe you are trying to make a calculation between a string and a number ? parse it to a number first

[{"id":"2e1be04c.d9a8a","type":"debug","z":"bc0a35e9.972ca8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1150,"y":120,"wires":[]},{"id":"d7e6250e.ad0ec8","type":"function","z":"bc0a35e9.972ca8","name":"","func":"let KWH_SYS = global.get(\"KWH_SYS\") || 0;\n\nlet KWH_SYSi = global.get(\"KWH_SYSi\") || 0;\n\nKWH_SYS += 1;\n\n// check if energy provides a new value\n\nif (msg.payload > 0) {\n\nmsg.payload.KWH_SYS = (msg.payload - KWH_SYSi).toString();\n\n}\n\n// then we have to store last value \n\nglobal.set(\"KWH_SYSi\", msg.payload);\n\nnode.send(msg);\n\n\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1160,"y":180,"wires":[["127acca6.5e1bd3","9401a8.eee36e58"]]},{"id":"44f0bf8a.932c5","type":"inject","z":"bc0a35e9.972ca8","name":"energy","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"30","crontab":"","once":false,"onceDelay":"10","topic":"","payload":"","payloadType":"date","x":140,"y":180,"wires":[["7cd2c154.6eaee"]]},{"id":"7cd2c154.6eaee","type":"function","z":"bc0a35e9.972ca8","name":"address:1024","func":"msg.payload ={\n     value: msg.payload,\n    'fc':3,\n    'unitid':1,\n    'address':1024,\n    'quantity':36\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":360,"y":180,"wires":[["efafc1af.2b973"]]},{"id":"efafc1af.2b973","type":"modbus-flex-getter","z":"bc0a35e9.972ca8","name":"Modbus","showStatusActivities":false,"showErrors":false,"logIOActivities":false,"server":"d87e65b7.e945d8","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"keepMsgProperties":false,"x":520,"y":180,"wires":[["d767cc88.5da41"],[]]},{"id":"d767cc88.5da41","type":"buffer-parser","z":"bc0a35e9.972ca8","name":"Energy","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"bigint64be","name":"KWH_1i","offset":0,"length":1,"offsetbit":0,"mask":""},{"type":"bigint64be","name":"KWH_1e","offset":8,"length":1,"offsetbit":0,"mask":""},{"type":"bigint64be","name":"KWH_2i","offset":16,"length":1,"offsetbit":0,"mask":""},{"type":"bigint64be","name":"KWH_2e","offset":24,"length":1,"offsetbit":0,"mask":""},{"type":"bigint64be","name":"KWH_3i","offset":32,"length":1,"offsetbit":0,"mask":""},{"type":"bigint64be","name":"KWH_3e","offset":40,"length":1,"offsetbit":0,"mask":""},{"type":"bigint64be","name":"KWH_Sysi","offset":48,"length":1,"offsetbit":0,"mask":""},{"type":"bigint64be","name":"KWH_Syse","offset":56,"length":1,"offsetbit":0,"mask":""},{"type":"bigint64be","name":"KWH_BAL","offset":64,"length":1,"offsetbit":0,"mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"bigValues","msgPropertyType":"str","resultType":"object","resultTypeType":"output","multipleResult":false,"setTopic":true,"x":660,"y":180,"wires":[["d18793d2.cd7db"]]},{"id":"d18793d2.cd7db","type":"function","z":"bc0a35e9.972ca8","name":"ReadInt 64","func":"   msg.payload = \n   {\n\nKWH_1i : Number(msg.bigValues.KWH_1i.value),\nKWH_1e : Number(msg.bigValues.KWH_1e.value),\nKWH_2i : Number(msg.bigValues.KWH_2i.value),\nKWH_2e : Number(msg.bigValues.KWH_2e.value), \nKWH_3i : Number(msg.bigValues.KWH_3i.value),\nKWH_3e : Number(msg.bigValues.KWH_3e.value),\nKWH_Sysi : Number(msg.bigValues.KWH_Sysi.value),\nKWH_Syse : Number(msg.bigValues.KWH_Syse.value), \nKWH_BAL : Number(msg.bigValues.KWH_BAL.value),\n\n   }\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":810,"y":180,"wires":[["42f2ae88.e2f27"]]},{"id":"42f2ae88.e2f27","type":"function","z":"bc0a35e9.972ca8","name":"","func":"var date = new Date();\nvar time = date.toLocaleString();\nvar KWH_1i =msg.payload.KWH_1i;\nvar KWH_1e=msg.payload.KWH_1e;\nvar KWH_2i =msg.payload.KWH_2i;\nvar KWH_2e =msg.payload.KWH_2e;\nvar KWH_3i =msg.payload.KWH_3i;\nvar KWH_3e =msg.payload.KWH_3e;\nvar KWH_SYSi =msg.payload.KWH_SYSi;\nvar KWH_SYSe =msg.payload.KWH_SYSe;\nvar KWH_BAL =msg.payload.KWH_BAL;\nvar KWH_SYS = context.get('KWH_SYS')||0;\nKWH_SYS += 1;\nmsg.payload.KWH_SYS = msg.payload - KWH_SYSi;\n\nglobal.set(\"KWH_SYSi\", msg.payload)\n\n\nmsg.payload_1= {\"Timestamp\":time ,'KWH_1i':msg.payload.KWH_1i/10000 ,'KWH_2i':msg.payload.KWH_2i/10000,'KWH_3i':msg.payload.KWH_3i/10000,'KWH_1e':msg.payload.KWH_1e/10000,'KWH_2e':msg.payload.KWH_2e/10000,'KWH_3e':msg.payload.KWH_3e/10000,'KWH_Sysi':msg.payload.KWH_Sysi/10000,'KWH_Syse':msg.payload.KWH_Syse/10000,'KWH_SYS': msg.payload - KWH_SYS ,'KWH_BAL':msg.payload.KWH_BAL/10000\n\n};\n\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":980,"y":180,"wires":[["2e1be04c.d9a8a","d7e6250e.ad0ec8"]]},{"id":"127acca6.5e1bd3","type":"csv","z":"bc0a35e9.972ca8","name":"","sep":",","hdrin":true,"hdrout":"once","multi":"one","ret":"\\n","temp":"KWH_1i,KWH_1e,KWH_2i,KWH_2e,KWH_3i,KWH_3e,KWH_Sysi,KWH_Syse,KWH_SYS,KWH_BAL","skip":"0","strings":true,"include_empty_strings":false,"include_null_values":false,"x":1570,"y":180,"wires":[["f75f5086.74a1d","35244514.f2f43a"]]},{"id":"f75f5086.74a1d","type":"file","z":"bc0a35e9.972ca8","name":"Energylaundry","filename":"C:\\Users\\rexm1\\Documents\\NodeRed\\energy2_Analysis.csv","appendNewline":true,"createDir":false,"overwriteFile":"false","encoding":"none","x":1740,"y":180,"wires":[["1c1a2148.7fb75f"]]},{"id":"1c1a2148.7fb75f","type":"debug","z":"bc0a35e9.972ca8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1930,"y":180,"wires":[]},{"id":"35244514.f2f43a","type":"debug","z":"bc0a35e9.972ca8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1710,"y":240,"wires":[]},{"id":"9401a8.eee36e58","type":"debug","z":"bc0a35e9.972ca8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1330,"y":260,"wires":[]},{"id":"d87e65b7.e945d8","type":"modbus-client","z":"","name":"seneca","clienttype":"simpleser","bufferCommands":true,"stateLogEnabled":false,"queueLogEnabled":false,"tcpHost":"192.168.1.9","tcpPort":"502","tcpType":"DEFAULT","serialPort":"COM3","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","unit_id":"1","commandDelay":"1","clientTimeout":"1000","reconnectOnTimeout":true,"reconnectTimeout":"2000","parallelUnitIdsAllowed":true}]

Look at this node https://flows.nodered.org/node/node-red-contrib-delta, it will help a lot with things like this.

1 Like

While working on with modbus data ,I received output for all parameters from modbus through buffer-parser, stored in CSV file and sent to cloud db(Mongo).

Need to find difference between KWH_SYSi (imported energy) values(i.e , from first data and second data ) that gives KWH_SYS. .

Please guide me

Regards
Energycharges :smiley: :smiley:

Can anyone guide me and help to find solution

Appreciate , your effort in notifying the issue

thank you ! :smiley: :smiley:

Energycharges

Was the node linked to a couple of posts ago not helpful?

HI @Colin

Its looks similiar kind of issue. Not exactly but difference part of function

Appreciate , your effort in notifying the issue
Energy charges

With few knowledge in have in node-red, and with some references, I tried many ways (delta, delay ,Interval length... etc) , i couldn't reach the difference part alone.

I cannot figure out , which part im going wrong

Can you please guide me @Colin

I don't know whether it has already been suggested to you but if you haven't already done it then work right through the Node Red Essentials videos that are linked from the docs page. A couple of hours spent doing that will save all of us time in the long run.
When you have done that should see how you can use node-red-contrib-delta to give the difference. If not then show us what you have tried.