Simple sum gives wrong answer ... I am using get and set

Hi,
I have been trying to work out why this doesn't work correctly, but cannot see why it is wrong ?
I have a counter that increments by 1 every 5 seconds, that inputs to this function.
The answer on the output should be 1 every time but I get (see image below)
I think something to do with how i am storing it ?

var count_last = flow.get ('last') || 0 ;
var count_now = flow.get ('now') || 0 ;
var result = 0 ;

result = count_now - count_last ;
flow.set('last' ,result);
msg.payload = result;

return msg;

Not enough information.

  1. Is that the complete function?
  2. is the debug attached to the function?
  3. where do you set flow.now?
  4. how many nodes feed this function node?

Hi @questuk

where/how are you setting the now flow context property?

You say you are feeding messages to this function, but you don't refer to the incoming msg.payload anywhere - is that intentional?

Nick

Hi Zenofmud and Nick

I have a function that is a simple increment counter adds 1 every 5 seconds.
This function then inputs to the "store" function ...

flow.set('now',msg.payload);
return msg;

and then into the final node.

The full message debug outputs are

Here is the flow ...

[{"id":"796db0f4.2ef2b","type":"inject","z":"a13b9737.d91218","name":"trigger at start","topic":"","payload":"0","payloadType":"str","repeat":"","crontab":"","once":true,"x":240,"y":580,"wires":[["c3e34e7e.6df918"]]},{"id":"c3e34e7e.6df918","type":"function","z":"a13b9737.d91218","name":"counter","func":"msg.payload = parseInt(msg.payload) + 1;\nreturn msg;","outputs":"1","noerr":0,"x":420,"y":580,"wires":[["c246c126.1eb3d","3b60310a.af306e","2bd05d20.0ee19a"]]},{"id":"c246c126.1eb3d","type":"delay","z":"a13b9737.d91218","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":420,"y":520,"wires":[["c3e34e7e.6df918"]]},{"id":"3b60310a.af306e","type":"debug","z":"a13b9737.d91218","name":"","active":false,"console":false,"complete":false,"x":590,"y":580,"wires":[]},{"id":"2bd05d20.0ee19a","type":"function","z":"a13b9737.d91218","name":"Store","func":"\nflow.set('now',msg.payload);\nreturn msg;\n\n \n","outputs":1,"noerr":0,"x":710,"y":640,"wires":[["c5543d43.220758","cb7dabaf.d5e048"]]},{"id":"c5543d43.220758","type":"function","z":"a13b9737.d91218","name":"Compare","func":"var count_last = flow.get ('last') || 0 ;\nvar count_now = flow.get ('now') || 0 ;\nvar result = 0 ;\n\n\nresult = count_now - count_last ;\n\nflow.set('last' ,result);\n\nmsg.payload = result;\nreturn msg;\n","outputs":1,"noerr":0,"x":860,"y":640,"wires":[["795bfa0.b316408"]]},{"id":"795bfa0.b316408","type":"debug","z":"a13b9737.d91218","name":"END","active":true,"console":"false","complete":"true","x":1010,"y":640,"wires":[]},{"id":"cb7dabaf.d5e048","type":"debug","z":"a13b9737.d91218","name":"Store","active":true,"console":"false","complete":"true","x":850,"y":680,"wires":[]}]

So when/how do you reset the flow variables to zero when you want to re-test this??

Please read the this post How to share code or flow json and edit your last post to properly share your flow.

Hi
I have changed the flow from original as I wasn't resetting everything correctly and I have simplified it.

Wrapped in ''' at start and ''' at end, is this now correct ?

[{"id":"2bd05d20.0ee19a","type":"function","z":"a13b9737.d91218","name":"Store","func":"\nglobal.set('now',msg.payload);\nreturn msg;\n\n    \n","outputs":1,"noerr":0,"x":710,"y":640,"wires":[["c5543d43.220758","cb7dabaf.d5e048"]]},{"id":"c5543d43.220758","type":"function","z":"a13b9737.d91218","name":"Compare","func":"var count_last   = global.get ('last');\nvar count_now    = global.get ('now') ;\nvar result       = 0 ;\n\n\nresult = count_now - count_last ;\n\nglobal.set('last' ,result);\n\nmsg.payload = result;\nreturn msg;\n","outputs":1,"noerr":0,"x":860,"y":640,"wires":[["795bfa0.b316408"]]},{"id":"795bfa0.b316408","type":"debug","z":"a13b9737.d91218","name":"END","active":true,"console":"false","complete":"true","x":1010,"y":640,"wires":[]},{"id":"cb7dabaf.d5e048","type":"debug","z":"a13b9737.d91218","name":"Store","active":false,"console":"false","complete":"true","x":850,"y":680,"wires":[]},{"id":"edc09814.286e7","type":"debug","z":"a13b9737.d91218","name":"","active":true,"console":"false","complete":"payload","x":950,"y":820,"wires":[]},{"id":"c4768f85.ff805","type":"function","z":"a13b9737.d91218","name":"Set defaults","func":"if  (msg.payload === \"Reset variables\")   \n\n    {\n      \n    var count_last   = 0 ;\n    var count_now    = 0 ;\n    var result       = 0 ;\n    \n\n    global.set ('last', 0) ;\n    global.set ('now' , 0) ;\n    return msg;\n    \n    }\n","outputs":1,"noerr":0,"x":770,"y":820,"wires":[["edc09814.286e7"]]},{"id":"bd260558.4ec168","type":"inject","z":"a13b9737.d91218","name":"Auto + Manual reset","topic":"","payload":"Reset variables","payloadType":"str","repeat":"","crontab":"","once":true,"x":580,"y":820,"wires":[["c4768f85.ff805"]]},{"id":"b756d13e.8094f","type":"comment","z":"a13b9737.d91218","name":"Send whenever NodeRed restarts","info":"","x":580,"y":780,"wires":[]},{"id":"ef573165.718928","type":"inject","z":"a13b9737.d91218","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"x":510,"y":640,"wires":[["2bd05d20.0ee19a"]]},{"id":"7fc7b45d.5ea61c","type":"inject","z":"a13b9737.d91218","name":"","topic":"","payload":"2","payloadType":"num","repeat":"","crontab":"","once":false,"x":510,"y":680,"wires":[["2bd05d20.0ee19a"]]},{"id":"808f7d3b.6282c","type":"inject","z":"a13b9737.d91218","name":"","topic":"","payload":"3","payloadType":"num","repeat":"","crontab":"","once":false,"x":510,"y":720,"wires":[["2bd05d20.0ee19a"]]}]

Sadly no. The backticks need to be on separate lines, one before and one after. @knolleary you FAQ post is not entirely clear on this point.
Also are those backticks? They may be, but it is not clear looking at them in your post. You don't need to post it again, you can edit the previous post using the pen icon at the bottom of the post which will appear when you hover there. After you have done it you can check yourself that it is ok by trying to import it from here.

OK got it looked everywhere for back tick on UK keyboard, I have never used that symbol before !

Here it is for anyone else, marked in red square.

I don't know exactly what you are trying to achieve, but did you intend that in the Compare function you are setting global.last to the difference, not to the 'now' value?

To help debug code you can use node.warn() to get output to the debug pane. For example you could insert a line in the function something like
node.warn("now is " + count_now);

1 Like

Hi Colin,

I did not know how to send the variables to he debug window, thank you that has helped a lot.

All I am trying to do is receive a gas counter that gives a running total every 10 minutes.
After i receive it I want to get the actual gas usage for the last 10 minutes and save to a database.
Then I can create a graph showing how much gas was used every 10 minutes in a whole day .

I have realized that my code is wrong now.
I know it's easy to do but but my head is now scrambled :thinking:

I will have a break and look at it later !