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

**URL:** https://discourse.nodered.org/t/how-to-calculate-the-difference-in-kwh-values-received-every-5-min-from-modbus-data-and-save-it-as-a-new-values-in-csv/33274
**Category:** General
**Created:** [24 September 2020 09:40 UTC](https://discourse.nodered.org/t/how-to-calculate-the-difference-in-kwh-values-received-every-5-min-from-modbus-data-and-save-it-as-a-new-values-in-csv/33274 "2020-09-24T09:40:16Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Energycharges](https://avatars.discourse-cdn.com/v4/letter/e/e47c2d/32.png) [@Energycharges](https://discourse.nodered.org/u/Energycharges)
#### Post date: [24 September 2020 09:40 UTC](https://discourse.nodered.org/t/how-to-calculate-the-difference-in-kwh-values-received-every-5-min-from-modbus-data-and-save-it-as-a-new-values-in-csv/33274/1 "2020-09-24T09:40:17Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/2/125b9c2044ed9dacb2a7dbd55797156da520a984.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/b/cbbcb6d467f2cab05a27a2d10f434e498288ce03.png)

When i tried this program , i received "NaN"

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/0/7035ae55c544fb50412f67908c060a2e53f95187.png)

Please guide me

Thanks

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [24 September 2020 10:08 UTC](https://discourse.nodered.org/t/how-to-calculate-the-difference-in-kwh-values-received-every-5-min-from-modbus-data-and-save-it-as-a-new-values-in-csv/33274/2 "2020-09-24T10:08:59Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [24 September 2020 10:13 UTC](https://discourse.nodered.org/t/how-to-calculate-the-difference-in-kwh-values-received-every-5-min-from-modbus-data-and-save-it-as-a-new-values-in-csv/33274/3 "2020-09-24T10:13:46Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Energycharges](https://avatars.discourse-cdn.com/v4/letter/e/e47c2d/32.png) [@Energycharges](https://discourse.nodered.org/u/Energycharges)
#### Post date: [24 September 2020 15:28 UTC](https://discourse.nodered.org/t/how-to-calculate-the-difference-in-kwh-values-received-every-5-min-from-modbus-data-and-save-it-as-a-new-values-in-csv/33274/4 "2020-09-24T15:28:55Z")

</div>

```auto
[{"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}]

```

---

<div class="post-metadata">

### Author: ![gregor-samosir](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregor-samosir/32/27537_2.png) [@gregor-samosir](https://discourse.nodered.org/u/gregor-samosir)
#### Post date: [24 September 2020 15:30 UTC](https://discourse.nodered.org/t/how-to-calculate-the-difference-in-kwh-values-received-every-5-min-from-modbus-data-and-save-it-as-a-new-values-in-csv/33274/5 "2020-09-24T15:30:21Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Energycharges](https://avatars.discourse-cdn.com/v4/letter/e/e47c2d/32.png) [@Energycharges](https://discourse.nodered.org/u/Energycharges)
#### Post date: [24 September 2020 19:04 UTC](https://discourse.nodered.org/t/how-to-calculate-the-difference-in-kwh-values-received-every-5-min-from-modbus-data-and-save-it-as-a-new-values-in-csv/33274/6 "2020-09-24T19:04:52Z")

</div>

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 😃 😃

---

<div class="post-metadata">

### Author: ![Energycharges](https://avatars.discourse-cdn.com/v4/letter/e/e47c2d/32.png) [@Energycharges](https://discourse.nodered.org/u/Energycharges)
#### Post date: [26 September 2020 09:30 UTC](https://discourse.nodered.org/t/how-to-calculate-the-difference-in-kwh-values-received-every-5-min-from-modbus-data-and-save-it-as-a-new-values-in-csv/33274/7 "2020-09-26T09:30:40Z")

</div>

Can anyone guide me and help to find solution

Appreciate , your effort in notifying the issue

thank you ! 😃 😃

Energycharges

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [26 September 2020 10:01 UTC](https://discourse.nodered.org/t/how-to-calculate-the-difference-in-kwh-values-received-every-5-min-from-modbus-data-and-save-it-as-a-new-values-in-csv/33274/8 "2020-09-26T10:01:11Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Energycharges](https://avatars.discourse-cdn.com/v4/letter/e/e47c2d/32.png) [@Energycharges](https://discourse.nodered.org/u/Energycharges)
#### Post date: [26 September 2020 10:04 UTC](https://discourse.nodered.org/t/how-to-calculate-the-difference-in-kwh-values-received-every-5-min-from-modbus-data-and-save-it-as-a-new-values-in-csv/33274/9 "2020-09-26T10:04:12Z")

</div>

HI @Colin

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

Appreciate , your effort in notifying the issue  
Energy charges

---

<div class="post-metadata">

### Author: ![Energycharges](https://avatars.discourse-cdn.com/v4/letter/e/e47c2d/32.png) [@Energycharges](https://discourse.nodered.org/u/Energycharges)
#### Post date: [26 September 2020 10:08 UTC](https://discourse.nodered.org/t/how-to-calculate-the-difference-in-kwh-values-received-every-5-min-from-modbus-data-and-save-it-as-a-new-values-in-csv/33274/10 "2020-09-26T10:08:38Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [26 September 2020 10:17 UTC](https://discourse.nodered.org/t/how-to-calculate-the-difference-in-kwh-values-received-every-5-min-from-modbus-data-and-save-it-as-a-new-values-in-csv/33274/11 "2020-09-26T10:17:15Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [25 November 2020 10:17 UTC](https://discourse.nodered.org/t/how-to-calculate-the-difference-in-kwh-values-received-every-5-min-from-modbus-data-and-save-it-as-a-new-values-in-csv/33274/12 "2020-11-25T10:17:20Z")

</div>

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