Compare current data with data from the last 2 days

Hello guys,

I want to count how many requests I receive per day. For this I use the node-red-contrib-count NODE. I would like to always compare the value with the last 2 days.
I can write the value in the variable "previous_day". However, I can’t write the value of the previous day in the variable "2days_before".

[{"id":"7ed14a93.175e34","type":"counter","z":"26c470dc.f99018","name":"","init":"0","step":"1","lower":"","upper":"","mode":"increment","outputs":"1","x":300,"y":620,"wires":[["51597868.5de2d"]]},{"id":"e17348b.078b6b8","type":"inject","z":"26c470dc.f99018","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":130,"y":660,"wires":[["7ed14a93.175e34"]]},{"id":"c22f84c4.c8712","type":"inject","z":"26c470dc.f99018","name":"reset","props":[{"p":"reset","v":"1","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"20","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":110,"y":560,"wires":[["7ed14a93.175e34"]]},{"id":"d6186338.9a8aa","type":"debug","z":"26c470dc.f99018","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":610,"y":620,"wires":[]},{"id":"51597868.5de2d","type":"function","z":"26c470dc.f99018","name":"","func":"var end_of_day = msg.count //time when override\nif (end_of_day == 19) {\n    msg.prev_day = msg.count; \n}\n// how can I write the data from msg.prev_day into msg.2days_before after the next end of day? \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":460,"y":620,"wires":[["d6186338.9a8aa"]]}]

Can someone help me?

Thanks
Georg

It is easier if you don't start names with digits (as javascript tends to think they are numbers). Call it two_days_before instead and life will be easier. You might be better making it an array though, one element for each day.

As a simplistic way - take it out of the function node

Have an inject node that runs once per day at a given time - have it input to a change node

First step in the change node - move Previous_day to two_day_sold_Previous_Day

2nd step Move Today to Previous_Day

Craig

Like this

[{"id":"7ed14a93.175e34","type":"counter","z":"b3576501.1541d8","name":"","init":"0","step":"1","lower":null,"upper":null,"mode":"increment","outputs":"1","x":400,"y":260,"wires":[["51597868.5de2d"]]},{"id":"e17348b.078b6b8","type":"inject","z":"b3576501.1541d8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":230,"y":300,"wires":[["7ed14a93.175e34"]]},{"id":"c22f84c4.c8712","type":"inject","z":"b3576501.1541d8","name":"reset","props":[{"p":"reset","v":"1","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"20","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":210,"y":200,"wires":[["7ed14a93.175e34"]]},{"id":"d6186338.9a8aa","type":"debug","z":"b3576501.1541d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":710,"y":260,"wires":[]},{"id":"51597868.5de2d","type":"function","z":"b3576501.1541d8","name":"","func":"var end_of_day = msg.count //time when override\nif (end_of_day == 19) {\n    msg.prev_day = msg.count; \n}\n// how can I write the data from msg.prev_day into msg.2days_before after the next end of day? \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":560,"y":260,"wires":[["d6186338.9a8aa"]]},{"id":"800cf741.8de9a8","type":"change","z":"b3576501.1541d8","name":"Move the stuff","rules":[{"t":"move","p":"PreviousDay","pt":"flow","to":"Two_Days_Previous","tot":"flow"},{"t":"move","p":"Today","pt":"flow","to":"PreviousDay","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":420,"wires":[[]]},{"id":"569429fd.b0a188","type":"inject","z":"b3576501.1541d8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"59 23 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":260,"y":440,"wires":[["800cf741.8de9a8"]]}]

Sorry, maybe I don´t check the part with:
move Today to move previousDay, ....

Can you please give me a little bit more information / help :slight_smile:

Did you import the flow that i posted ?

What is the problem?

Show us some screenshots and open the debug window and show the COntext variables in there.

Craig

yes i imported your flow.

This is what the debug node shows

grafik

Context:

grafik

For me it´s not clear, how variable connection is.
how does the flow node, what is flow.Today, flow.PreviousDay and flow.Two_Days_Previous?

Thanks for help :slight_smile:

@craigcurtin: is it possible that you help me. This would be great :slight_smile:

When you say you want to compare the last two days does that mean on Tuesday you will compare Sunday and Monday? What about holidays?

when it´s tuesday I will compare Monday and Sunday.
What do you mean with holidays? The application should run the complete year

I just wanted to make sure that it was running every day of the week. Sometimes when people say “I want it to run every day” they mean every weekday so I just wanted to make sure.

Personally I think I’d store the data in a database so each day you select the data from the past two days to compare the current day with.

But that is just one possible way.

ok :slight_smile:

Isn´t it possible to make this all in Node-RED without any other tool or external database?

Sure it is and @craigcurtin offered you a method. Did you try it?

You should read about context Working with context : Node-RED

So explain what you have done with the flow that i posted and what is not working please ?

Craig

I just went back and looked at my flow i posted.

There is an inject node that runs at 11:59PM every day - it takes the counter that you have been incrementing throughout the day and then moves it into a context variable so you now have the total count for today stored in a context variable - at the same time the flow moves the value from the previous days total to a new context variable

so like this

Variable 1 = todays count
at 11:59PM - move Yesterdays count to a new varaible - 2 days ago count
then move todays count to Yesterdays count

This was an example only - i would then assume that you would (for example) Zero out todays count as the time rolled over to 00:00:01 seconds and start your counting routine again.

Craig

The context menu shows the following variables, every time.
grafik

I don´t understand this.
Also I do not know how I can use the values off the context variable to show it in the dashboard.
How can I get the variables from the context. With a change node it does not work.

George

  1. You have both a global variable and a flow variable - you need to read up on Context variables to understand they are 2 completely different things

  2. Spelling and capitalisation etc - must match exactly

  3. The change node definitely does work to access context variables - post up your flow so we can see what you hav done

Craig

I started node-red again and now there is no context data anymore.
I also played around with the flow. But no result, so I changed it back to yours.

[{"id":"7ed14a93.175e34","type":"counter","z":"26c470dc.f99018","name":"","init":"0","step":"1","lower":"","upper":"","mode":"increment","outputs":"1","x":480,"y":800,"wires":[["51597868.5de2d"]]},{"id":"e17348b.078b6b8","type":"inject","z":"26c470dc.f99018","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":310,"y":840,"wires":[["7ed14a93.175e34"]]},{"id":"c22f84c4.c8712","type":"inject","z":"26c470dc.f99018","name":"reset","props":[{"p":"reset","v":"1","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"20","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":290,"y":740,"wires":[["7ed14a93.175e34"]]},{"id":"d6186338.9a8aa","type":"debug","z":"26c470dc.f99018","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":790,"y":800,"wires":[]},{"id":"51597868.5de2d","type":"function","z":"26c470dc.f99018","name":"","func":"var end_of_day = msg.count //time when override\nif (end_of_day == 19) {\n    msg.prev_day = msg.count; \n}\n// how can I write the data from msg.prev_day into msg.2days_before after the next end of day? \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":640,"y":800,"wires":[["d6186338.9a8aa"]]},{"id":"800cf741.8de9a8","type":"change","z":"26c470dc.f99018","name":"Move the stuff","rules":[{"t":"move","p":"PreviousDay","pt":"flow","to":"Two_Days_Previous","tot":"flow"},{"t":"move","p":"Today","pt":"flow","to":"PreviousDay","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":960,"wires":[[]]},{"id":"569429fd.b0a188","type":"inject","z":"26c470dc.f99018","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"10","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":340,"y":980,"wires":[["800cf741.8de9a8"]]},{"id":"63bcf78f.6005","type":"change","z":"26c470dc.f99018","name":"Move the stuff","rules":[{"t":"move","p":"Today","pt":"flow","to":"Count","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":660,"y":700,"wires":[[]]}]

I have created now two differnet flows.
This is a very simple flow to understand how the context variable works.

[{"id":"3331f309.b0c294","type":"inject","z":"26c470dc.f99018","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"30","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"10:46:23","payloadType":"str","x":200,"y":540,"wires":[["b6666d13.02dc18"]]},{"id":"b6666d13.02dc18","type":"function","z":"26c470dc.f99018","name":"","func":"flow.set (\"time\",msg.payload);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":390,"y":540,"wires":[[]]},{"id":"5e454f58.f966e8","type":"inject","z":"26c470dc.f99018","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"2","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":290,"y":660,"wires":[["7111d870.5ce068"]]},{"id":"7111d870.5ce068","type":"function","z":"26c470dc.f99018","name":"","func":"var time = flow.get(\"time\");\nmsg.payload = time; \nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":460,"y":660,"wires":[["866f336e.370de"]]},{"id":"866f336e.370de","type":"debug","z":"26c470dc.f99018","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":770,"y":640,"wires":[]}]

The second is to created the wished function.

[{"id":"7c1cd3fb.993404","type":"inject","z":"26c470dc.f99018","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":150,"y":880,"wires":[["3ceda765.0341d"]]},{"id":"3ceda765.0341d","type":"counter","z":"26c470dc.f99018","name":"","init":"0","step":"1","lower":"","upper":"","mode":"increment","outputs":"1","x":330,"y":880,"wires":[["8e3706a6.fca648"]]},{"id":"8e3706a6.fca648","type":"function","z":"26c470dc.f99018","name":"","func":"var count = msg.count; \n\nif (msg.topic ==\"1\") {\n    flow.set(\"today\",count); \n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":640,"y":880,"wires":[[]]},{"id":"c389515b.cf4ea","type":"change","z":"26c470dc.f99018","name":"","rules":[{"t":"move","p":"twoDaysbefore","pt":"flow","to":"previousDay","tot":"flow"},{"t":"move","p":"previousDay","pt":"flow","to":"today","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":980,"wires":[[]]},{"id":"f02fbc09.f83a88","type":"inject","z":"26c470dc.f99018","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"30","crontab":"","once":false,"onceDelay":0.1,"topic":"1","payload":"","payloadType":"date","x":170,"y":960,"wires":[["c389515b.cf4ea","8e3706a6.fca648","6de68fd.7729c7"]]},{"id":"7d852e78.6af2a8","type":"inject","z":"26c470dc.f99018","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"60","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":1080,"wires":[["eea86d53.33b75"]]},{"id":"eea86d53.33b75","type":"function","z":"26c470dc.f99018","name":"","func":"msg.today = flow.get(\"today\");\nmsg.previousDay = flow.get(\"previousDay\");\nmsg.twoDaysbefore = flow.get(\"twoDaysbefore\");\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":360,"y":1080,"wires":[["92f4b266.7b7688"]]},{"id":"92f4b266.7b7688","type":"debug","z":"26c470dc.f99018","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":550,"y":1080,"wires":[]},{"id":"6de68fd.7729c7","type":"debug","z":"26c470dc.f99018","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":610,"y":820,"wires":[]}]

I don´t understand why the context variables in the second flow will not be stored and shown in context?

grafik

Did you change the settings.js file to store the context to the local file system?

This thread might help you understand persistent context ; A guide to understanding 'Persistent Context' - #88 by Colin

I am not sure if this is neccessary, for the first flow the context variable takes the value but for the second not. I don´t understand this