Store Temporary Data

Hi everyone, It is posible I dont want using any database to store the data?

Now I tried to use the getter and setter to store the temporary data. I dont know why the node red cannot get the previous data. If current minute is 20 I would like to get also 15,10 and 5.

`[{"id":"e8ee6740.9d5ce8","type":"inject","z":"8debfc1f.64b44","name":"","topic":"","payload":"","payloadType":"date","repeat":"30","crontab":"","once":false,"onceDelay":0.1,"x":270,"y":1980,"wires":[["1afb4e8a.2cab81","a0637ea5.4bf94","43fdadf3.8783c4","24721b90.b4ffe4"]]},{"id":"1afb4e8a.2cab81","type":"function","z":"8debfc1f.64b44","name":"get data 15,35,55 (payload4)","func":"d = new Date();\nminute = d.getMinutes();\n\nif(minute%5 === 0){\nif(minute === 15 || minute === 35 || minute === 55){\ncontext.get('minutes15',minute)\nif(minute != 'minutes15'){\ncontext.set('minutes15',minute)\nmsg={payload4:{minute:minute}}\n}else{\ncontext.set('minutes15',minute)\nmsg={payload4:{minute}}\n}\n}\n}else return null\n\n/*context.set('hour',hour)\ncontext.set('minute',minute)\ncontext.set('second',second)\ncontext.set('year',year)\ncontext.set('month',month)\ncontext.set('day',day)\n\n\n\nif(minute === 15 || minute === 35 || minute ===55){\n    msg={payload4:{\n        hour:hour,\n        minute:minute,\n        second:second,\n        year:year,\n        month:month,\n        day:day,\n\n        \n        /*'hours':context.set(hour),\n        'minutes':context.set(minute),\n        'seconds':context.set(second),\n        'years':context.set(year),\n        'months':context.set(month),\n        'days':context.set(day),\n\n        \n    }}\n}else{\n    msg={payload4:{\n        hour:context.get('hour'),\n        minute:context.get('minute'),\n        second:context.get('second'),\n        year:context.get('year'),\n        month:context.get('month'),\n        day:context.get('day'),\n\n    }}\n}\n*/\n\n\nreturn msg;","outputs":1,"noerr":0,"x":540,"y":1940,"wires":[["a18e938c.609de"]]},{"id":"a0637ea5.4bf94","type":"function","z":"8debfc1f.64b44","name":"get data 20,40,0 (payload1)","func":"d = new Date();\nminute = d.getMinutes();\n\nif(minute%5 === 0){\nif(minute === 20 || minute === 40 || minute === 0){\ncontext.get('minutes15',minute)\nif(minute != 'minutes15'){\ncontext.set('minutes15',minute)\nmsg={payload1:{minute:minute}}\n}else{\ncontext.set('minutes15',minute)\nmsg={payload1:{minute}}\n}\n}\n}else return null\n\n/*context.set('hour',hour)\ncontext.set('minute',minute)\ncontext.set('second',second)\ncontext.set('year',year)\ncontext.set('month',month)\ncontext.set('day',day)\n\n\n\nif(minute === 15 || minute === 35 || minute ===55){\n    msg={payload4:{\n        hour:hour,\n        minute:minute,\n        second:second,\n        year:year,\n        month:month,\n\n        \n        /*'hours':context.set(hour),\n        'minutes':context.set(minute),\n        'seconds':context.set(second),\n        'years':context.set(year),\n        'months':context.set(month),\n        'days':context.set(day),\n\n        \n    }}\n}else{\n    msg={payload4:{\n        hour:context.get('hour'),\n        minute:context.get('minute'),\n        second:context.get('second'),\n        year:context.get('year'),\n        month:context.get('month'),\n        day:context.get('day'),\n\n    }}\n}\n*/\n\n\n\nreturn msg;","outputs":1,"noerr":0,"x":540,"y":1980,"wires":[["a18e938c.609de"]]},{"id":"43fdadf3.8783c4","type":"function","z":"8debfc1f.64b44","name":"get data 25,45,5 (payload2)","func":"d = new Date();\nminute = d.getMinutes();\n\nif(minute%5 === 0){\nif(minute === 25 || minute === 45 || minute === 5){\ncontext.get('minutes15',minute)\nif(minute != 'minutes15'){\ncontext.set('minutes15',minute)\nmsg={payload2:{minute:minute}}\n}else{\ncontext.set('minutes15',minute)\nmsg={payload2:{minute}}\n}\n}\n}else return null\n\n/*context.set('hour',hour)\ncontext.set('minute',minute)\ncontext.set('second',second)\ncontext.set('year',year)\ncontext.set('month',month)\ncontext.set('day',day)\n\n\n\nif(minute === 15 || minute === 35 || minute ===55){\n    msg={payload4:{\n        hour:hour,\n        minute:minute,\n        second:second,\n        year:year,\n        month:month,\n        day:day,\n\n        \n        /*'hours':context.set(hour),\n        'minutes':context.set(minute),\n        'seconds':context.set(second),\n        'years':context.set(year),\n        'months':context.set(month),\n        'days':context.set(day),\n\n        \n    }}\n}else{\n    msg={payload4:{\n        hour:context.get('hour'),\n        minute:context.get('minute'),\n        second:context.get('second'),\n        year:context.get('year'),\n        month:context.get('month'),\n        day:context.get('day'),\n\n    }}\n}\n*/\n\n\n\nreturn msg;","outputs":1,"noerr":0,"x":540,"y":2020,"wires":[["a18e938c.609de"]]},{"id":"24721b90.b4ffe4","type":"function","z":"8debfc1f.64b44","name":"get data 30,50,10 (payload3)","func":"d = new Date();\nminute = d.getMinutes();\n\nif(minute%5 === 0){\nif(minute === 30 || minute === 50 || minute === 10){\ncontext.get('minutes15',minute)\nif(minute != 'minutes15'){\ncontext.set('minutes15',minute)\nmsg={payload3:{minute:minute}}\n}else{\ncontext.set('minutes15',minute)\nmsg={payload3:{minute}}\n}\n}\n}else return null\n\n/*context.set('hour',hour)\ncontext.set('minute',minute)\ncontext.set('second',second)\ncontext.set('year',year)\ncontext.set('month',month)\ncontext.set('day',day)\n\n\n\nif(minute === 15 || minute === 35 || minute ===55){\n    msg={payload4:{\n        hour:hour,\n        minute:minute,\n        second:second,\n        year:year,\n        month:month,\n        day:day,\n\n        \n        /*'hours':context.set(hour),\n        'minutes':context.set(minute),\n        'seconds':context.set(second),\n        'years':context.set(year),\n        'months':context.set(month),\n        'days':context.set(day),\n\n        \n    }}\n}else{\n    msg={payload4:{\n        hour:context.get('hour'),\n        minute:context.get('minute'),\n        second:context.get('second'),\n        year:context.get('year'),\n        month:context.get('month'),\n        day:context.get('day'),\n\n    }}\n}\n*/\n\n\n\nreturn msg;","outputs":1,"noerr":0,"x":540,"y":2060,"wires":[["a18e938c.609de"]]},{"id":"a18e938c.609de","type":"debug","z":"8debfc1f.64b44","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":810,"y":2020,"wires":[]}]`

This is my code.

Thanks.

Hi @jason_tien

you will need to provide some more explanation for what your flow is doing (or what you think it is doing) and which parts you want us to look at. There is a lot of code in there with no comments.

1 Like

hi @knolleary

`[{"id":"e8ee6740.9d5ce8","type":"inject","z":"8debfc1f.64b44","name":"","topic":"","payload":"","payloadType":"date","repeat":"30","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":1960,"wires":[["1afb4e8a.2cab81","a0637ea5.4bf94","43fdadf3.8783c4","24721b90.b4ffe4"]]},{"id":"1afb4e8a.2cab81","type":"function","z":"8debfc1f.64b44","name":"get data 15,35,55 (payload4)","func":"d = new Date();\nminute = d.getMinutes();\n\nif(minute%5 === 0){\nif(minute === 15 || minute === 35 || minute === 55){\ncontext.get('minutes15',minute)\nif(minute != 'minutes15'){\ncontext.set('minutes15',minute)\nmsg={payload4:{minute:minute}}\n}else{\ncontext.set('minutes15',minute)\nmsg={payload4:{minute}}\n}\n}else{\ncontext.set('minutes15',minute)\nmsg={payload4:{minute}}\n}\n}else return null\n\n\nreturn msg;","outputs":1,"noerr":0,"x":360,"y":1920,"wires":[["a18e938c.609de"]]},{"id":"a0637ea5.4bf94","type":"function","z":"8debfc1f.64b44","name":"get data 20,40,0 (payload1)","func":"d = new Date();\nminute = d.getMinutes();\n\nif(minute%5 === 0){\nif(minute === 20 || minute === 40 || minute === 0){\ncontext.get('minutes20',minute)\nif(minute != 'minutes20'){\ncontext.set('minutes20',minute)\nmsg={payload1:{minute:minute}}\n}else{\ncontext.set('minutes20',minute)\nmsg={payload1:{minute}}\n}\n}else{\ncontext.set('minutes20',minute)\nmsg={payload1:{minute}}\n}\n}else return null\n\n\nreturn msg;","outputs":1,"noerr":0,"x":360,"y":1960,"wires":[["a18e938c.609de"]]},{"id":"43fdadf3.8783c4","type":"function","z":"8debfc1f.64b44","name":"get data 25,45,5 (payload2)","func":"d = new Date();\nminute = d.getMinutes();\n\nif(minute%5 === 0){\nif(minute === 25 || minute === 45 || minute === 5){\ncontext.get('minutes25',minute)\nif(minute != 'minutes25'){\ncontext.set('minutes25',minute)\nmsg={payload2:{minute:minute}}\n}else{\ncontext.set('minutes25',minute)\nmsg={payload2:{minute}}\n}\n}else{\ncontext.set('minutes25',minute)\nmsg={payload2:{minute}}\n}\n}else return null\n\n\nreturn msg;","outputs":1,"noerr":0,"x":360,"y":2000,"wires":[["a18e938c.609de"]]},{"id":"24721b90.b4ffe4","type":"function","z":"8debfc1f.64b44","name":"get data 30,50,10 (payload3)","func":"d = new Date();\nminute = d.getMinutes();\n\nif(minute%5 === 0){ //get the 5,10,15 min\nif(minute === 30 || minute === 50 || minute === 10){\ncontext.get('minutes30',minute)\nif(minute != 'minutes30'){\ncontext.set('minutes30',minute) \nmsg={payload3:{minute:minute}} //send the current minute (ex:30)\n}else{                         //if it is not 30/50/10\ncontext.set('minutes30',minute)\nmsg={payload3:{minute}}    //send the(30 again)\n}\n}else{                         //if it is not 30/50/10\ncontext.set('minutes30',minute)\nmsg={payload3:{minute}}     //send the(30 again)\n}\n}else return null\n\n\nreturn msg;","outputs":1,"noerr":0,"x":360,"y":2040,"wires":[["a18e938c.609de"]]},{"id":"a18e938c.609de","type":"debug","z":"8debfc1f.64b44","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":630,"y":2000,"wires":[]}]`

Example current minute is 30 I want the output is minute 30,25,20 and 15.
I use getter and setter tried to store the 25,20 and 15 but it cannot work.

Thanks.

Define Temporary please ? Do you not care about the data between restarts of node-red and/or the box it is running on - or do you need to keep it ?

You can use the local filesystem on whatever device you are running to store data by including something like this in your settings.js file

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

You can then use the change node to read and write variables into that storage space - or you can use function nodes with the same result.

Here is a good simple explanation and some examples

http://www.steves-internet-guide.com/node-red-variables/

Craig

1 Like

You are using context.set('xxx',nnn) - if you read the documentation you will see

In the Function node there are three predefined variables that can be used to access context:

  • context - the node’s local context
  • flow - the flow scope context
  • global - the global scope context

So any context varialbes you use are avaliable only in that node. So if node 'A' stores a variable using context, it will only be avaliable to node 'A'. Node 'B' will not have access to it.

You might want to try using flow variables.

2 Likes