Store msg alone without needing to have excel ,SQLite table just by topic to payload it again to node red when needed

Hello guys. I need your help to solve this problem. I'm running node red from my PC and I'm using node red to calculate and count the payment on my brother store. I'm really happy that node red is on my life it helps me too much. I have to calculate >> count the total payment for each staff (how much does he collect on his work hours) and view the total amount for each of the staffs at the end of week. for example (staff1) collect today on five hours work 300$ then on his next five hours work he collect 200$ so the total will be 300$+200$=500$. and same goes to the other staffs. the things that I need is to store the total of each of them on different time. to view them at the end of the week. the problem that when I use function.


msg.payload = (flow.get("staff1") || 0) + msg.payload;
flow.set("staff1", msg.payload);

it works fine to store the data for each staff but when I Shut down my PC the node red stop as usual, so all the data that stored on the function well be delete. So is there way to store the last data collected until the next day ,,,, until the end of the week. I tried to store the data on excel but the problem that excel will not store the total collected for each staff1 alone on a time it will ask to store also the total collected for the other staffs on the same time. it goes also to the SQLite.

Thanks all your help will be appreciated and respected. :slight_smile:

Hello ..

if you already have the functionality working with Context and it suits your needs ..
you could set up Context to be saved in files instead of memory.

Read this article from official Node-red documentation on how to achieve that.
Working with context

especially the section that describes how to setup context to use localfilesystem

1 Like

@UnborN Thanks for your time to reply. incase I watched some tutorials on that and all goes as it must be, but when I try it on my node red the change node doesn't have a property to chose a way to save whether to only memory or file.

Did you make the changes to the settings.js file that will enable saving to the file system as is explained in the documentation and stop/start Node-RED?

If you have and it is not working, please start node-red And copy and paste the startup log to a reply.

@zenofmud Thanks for your reply. I already read your explanation on that on your node red post to change a function on the {.node-red/settings.js} file. I need to make sure is this function that I must to enable it on the file. then change it to the function that you shared.

    contextStorage: {
    	default    : { module: "memory" },
		storeInFile: { module: "localfilesystem"},
		memoryOnly : { module: "memory" }
    },

This is my settings.js file

yes .. contextStorage is the part of the settings.js file that you have to un-comment and change.

and as you mentioned above .. if you want the option in your Change node to choose whether you want something to be stored on disk or memory .. then the code you posted will do just that.

@UnborN Thanks I did it. as I already I can store the data on file so is there a way to request the data by function node example,

msg.payload = (flow.get("cust1")

the flow that I stored on file because I'm using function node the sum the data as it shown bellow.

image

In the case of using Context in a Function node ..
you have to pass in an additional parameter to the get and set methods to specify which context store you want to read or write.

If you read carefully the previous link till the end, it points you to another section of the NR documentation for functions.

Multiple context stores

ps. if you are setting flow context with code in a function .. why you need a Change node after it that seems to be setting context as well ?

1 Like

Thanks I got it. :slight_smile: and here is the function that I changed to call the file.

1 Like

@UnborN @zenofmud I was a little bit confused. I deleted the change node and all goes fine. I have one more question is there a way that I can export all the flow of node red from node red folder while my site is not working it keeps loading. Maybe because the space is not enough on the ubuntu 18.04. So maybe I'll need to reinstall my ubuntu 18.04. Thanks.

When you start node-red it shows the name of the flow file that is loaded.
for Example on my PC it's flows_NODE.json

1 Aug 21:03:09 - [info] Node-RED version: v2.0.5
1 Aug 21:03:09 - [info] Node.js  version: v14.16.0
1 Aug 21:03:09 - [info] Windows_NT 10.0.19043 x64 LE
1 Aug 21:03:11 - [info] Loading palette nodes
1 Aug 21:03:16 - [info] Dashboard version 2.30.0 started at /ui
1 Aug 21:03:17 - [info] Settings file  : \Users\User\.node-red\settings.js
1 Aug 21:03:17 - [info] Context store  : 'default' [module=localfilesystem]
1 Aug 21:03:17 - [info] Context store  : 'memoryOnly' [module=memory]
1 Aug 21:03:17 - [info] User directory : \Users\User\.node-red
1 Aug 21:03:17 - [warn] Projects disabled : editorTheme.projects.enabled=false
1 Aug 21:03:17 - [warn] Flows file name not set. Generating name using hostname.
1 Aug 21:03:17 - [info] Flows file     : \Users\User\.node-red\flows_NODE.json
1 Aug 21:03:17 - [info] Server now running at http://127.0.0.1:1880/

check the line Flows file : <your path>\<your file>.json and backup the json file.
but thats backing up only the flow file. For good measure better backup the whole .node-red folder because there are other important files in there also. Like the context folder (now that you are using the file system to save the context on disk)

@UnborN for the ubuntu maybe the file direction is deferent than PC even when you stop node red then you start it again it doesn't show any direction even to node red. I tried to find it from the node-red.service status I found this but is doesn't fit.
image

if you running node-red as a service you can start, stop and check the log with the commands described here

  • node-red-start - this starts the Node-RED service and displays its log output. Pressing Ctrl-C or closing the window does not stop the service; it keeps running in the background
  • node-red-stop - this stops the Node-RED service
  • node-red-restart - this stops and restarts the Node-RED service
  • node-red-log - this displays the log output of the service

open a terminal, stop node-red, start it again an see the log so you can investigate why your node-red
is not loading. what did you change ? is it really because of no space on disk ? did you confirm that ?

You only pass two parameters when ‘set’ - then you pass the name of the context variable and the second parameter is the data (actual value, a variable, object, etc).

The ‘get’ only needs the name of the context variable.

If you have two different types of storage for the context, then you may need to specify the name of in addition.

Here is a write up explaining all this

1 Like

@UnborN Yah it is because there is no space on disk. I tried to find solution on site it says that maybe node red needs upgrades so I followed this commend.


after that my node red website didn't even refresh
image
maybe all flows erased.

on this way the default storing data will be (storeName) is there a deferent between you function that you shared above and the next function on your post (storeInFile). as shown bellow.

    contextStorage: {
    	storeName    : { module: "storeModule" }
    },
contextStorage: {
		storeInFile: { module: "localfilesystem"},
    	default    : { module: "memory" }
    },

you are right .. i didnt explain it very clearly
i bookmarked your Guide and will share the link

and you think by upgrading node-red will fix the problem of no disk space ? :innocent:

@UnborN I thought that maybe the problem on the version.
Is there a command to run node red service again because I already tried node-red start and enable but it didn’t work.

@badr_al_moragab
It looks (from the startup log you provided earlier) like you are running Node-RED on a windows machine. Is this correct?

Now you say there is no disk space. Where are you seeing that there is no disk space?

Also why do you have a user called 'user'?