writing and reading values

hi! I have been using the Node-RED environment recently, there is a problem, I don't understand how to save the last value to a variable and then use this value later by condition???

I think you'll need to give us a bit more detail as there are many ways to do this.
I don't understand what you mean by 'use this value later by condition' (what does condition mean?)

You can, for example, save a variable to 'context' and load a variable from 'context'.
Can you share the Node-RED flow you have created thus far?

In the meantime, here's a simple example-flow to demonstrate the above suggestion.
save_restore

[{"id":"3dec13e5ee18aa39","type":"inject","z":"b1e86b3d06648f54","name":"21","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"21","payloadType":"num","x":170,"y":180,"wires":[["f003471d66f59037"]]},{"id":"f003471d66f59037","type":"change","z":"b1e86b3d06648f54","name":"Save to context","rules":[{"t":"set","p":"storage","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":180,"wires":[[]]},{"id":"f40cc3790227b2be","type":"inject","z":"b1e86b3d06648f54","name":"Trigger","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":170,"y":240,"wires":[["ef3d64aaec28533a"]]},{"id":"ef3d64aaec28533a","type":"change","z":"b1e86b3d06648f54","name":"Load from context","rules":[{"t":"set","p":"payload","pt":"msg","to":"storage","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":240,"wires":[["961ffb0c4a9f0c73"]]},{"id":"961ffb0c4a9f0c73","type":"debug","z":"b1e86b3d06648f54","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":550,"y":240,"wires":[]}]

thank you! and how is the recording module "change" configured? in your example

Ideally you should pass the data through as a message property rather than trying to use a variable.

thank you! sometimes it's important for me to keep something. Tell me how the person who answered me earlier set up the "change" node! I'm new to this.

You might want to do some reading of the docs as this is a fairly important and basic feature of Node-RED. :slight_smile:

But if you look in the change node, you will see settings in the drop-downs labelled "flow" and "global". These are two of the context variable options.

Working with context : Node-RED (nodered.org)

You can 'import' the flow I posted eariler and try it out (and also look inside the nodes and see the settings I used). As @TotallyInformation said, you might find it useful to read the 'docs' in the link he posted.

Good afternoon It's pretty superficial. This is how I understand where to get it, where to put it, where and how to write down the task, save it, look at the work of different options! Ideal in my opinion, and most importantly eliminates a lot of additional questions. And yesterday I did not deploy what you sent me, because I don’t know how it is done, but after reading this code it was not difficult to find the node settings and test it. I know some languages C, FBD. Therefore, knots are something new for me. I hope you don't mind my questions, they can be strange :slightly_smiling_face:. So what other ways can you catch variable data, store, change and use them in the conditions of the "function" node ???

Here's a basic example of saving/reading to/from flow 'context' using a function node. Hope it helps you?

[{"id":"d5a27b357204bdb5","type":"function","z":"b1e86b3d06648f54","name":"Save msg.payload in 'context'","func":"flow.set(\"storage\",msg.payload);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":380,"wires":[[]]},{"id":"787fefaddbb8faf1","type":"function","z":"b1e86b3d06648f54","name":"Read from 'context' put in msg.payload","func":"msg.payload = flow.get(\"storage\");\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":480,"y":440,"wires":[["12bbbc899a300c70"]]},{"id":"ca95ae81609c8b6e","type":"inject","z":"b1e86b3d06648f54","name":"Inject a number","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"21","payloadType":"num","x":200,"y":380,"wires":[["d5a27b357204bdb5"]]},{"id":"c03a50822ad419b8","type":"inject","z":"b1e86b3d06648f54","name":"Trigger","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":170,"y":440,"wires":[["787fefaddbb8faf1"]]},{"id":"12bbbc899a300c70","type":"debug","z":"b1e86b3d06648f54","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":750,"y":440,"wires":[]}]

Note:
In the above example I've injected a number (21) into the flow-context. This could have been text like "Hello World". Try it out by changing the settings in the Inject node.

The various flavours of context, along with MQTT, and pretty much any of the well known databases are the main options.

Thanks a lot! I appreciate your help! in the example, I understand that it is possible to catch and transfer any type of variable in this way by event! but for some reason, the example does not store data permanently, that is, it can be used once, how can it be stored permanently? so that this or any other, changed value is not deleted???

Have a look at the section titled 'Context stores' in this document as it explains how to implement it.
https://nodered.org/docs/user-guide/context#context-stores

thank you! Can you give an example of a common current one?

You don't have to do anything other than use it as described there. The examples show how to used file based persistent context, which is build in to node red. Then on a node-red deploy or restart the previous context values are automatically fetched from the files.

Thank you! and how to store boolean variables inside the "function" node???

The fact that it is a boolean is irrelevant.

In the node red doc Writing Functions in the section on Storing Data, the example headed "The following example maintains a count of how many times the function has been run" shows you how to do it using an integer counter, but using a boolean is no different.

I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

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