High in the previous lecture, he learned about messages, which is the primary method by which a note
will pass data to another note.
There's another way to do that as well.
And that is done by our variables with variables.
It's possible to pass data between notes in the same flow, but also notes across different flows.
So I'm going to give you a few examples, a few demonstrations that use messages that use variables
to do that.
I want to point out that there is a very good page, a the competition page in the note read website,
which is this one here.
And I encourage you, of course, to have a look at this.
It will give you a lot of details in this lecture.
I'm going to focus on examples.
So a central concept in note read is the context, which has got the same meaning as a context in programming
and programming language.
So when it comes to variables in note read, the context can be the node or a flow or global.
So node, of course, means that you can create fireballs that are only accessible inside the node in
particular.
That is true for function nodes where you write small javascript flow means the context within a flow.
OK, within a tab.
All of the nodes inside ATAP, which is a flow, can access variables that have been created within
this flow.
And then for the global variables, we are talking about variables that can be accessed and can be shared
across multiple flows to demonstrate the different kinds of variables, especially flow and global variables.
I'm going to use my DHT 22 example flow as the starting point.
So this is the flow that you are already familiar with from the previous lecture.
What I'll do is I'm going to disable those debug messages and I've just actually added another debug
node here so that I can print out the temperature and humidity to the back window.
So if I trigger it, actually should deploy first or trigger this flow.
And this is what you get.
The idea here is that instead of printing out the temperature and humidity, connecting the debug nodes
directly to the DHT 22 node, what I'll do is I'm going to store these two values in flow and in global
variables and then access them by triggering a different part of the flow or by getting them in a totally
different flow and presenting them into the deep window.
Again, I'm going to start with local flow variables.
There's a couple of different ways to work with variables, whether they are global or flow, and that
is by creating them and accessing them programmatically inside a function node or by using one of the
compatible nodes to first I'm going to work with a compatible node.
And then I'm also going to show you the second way of doing that through functions, which is obviously
a lot more flexible.
So before we can read a flow variable, we need to create it.
And to do that will use the change node and grab a node and place it up here and then connect Mattituck
22 node to the change node inside the change node.
I'm going to create two rooms I'm going to set.
A flu.
A variable called.
Temperature.
To whatever message DOD payload contains, as you know, payload is the variable that contains the temperature,
and then I'm going to do the same thing for humidity.
I'm going to set the humidity flow.
To message dot.
Humidity.
And that's it, I might put another debug message here so that.
Let's see, I'm going to add another message and set the payload that will come out of the change node
to say something like variables set.
All right, so then that output will go into the debug node here and I'm going to give it a name, Farabaugh
Verbose set confirmation.
All right.
So what I've got now is that the G20 to take a measurement, it will pass the data onto the change through
the change, is going to set the Flotard temperature and Flotard humidity variables.
And then it's also going to set the message payload vulnerable to this string so that in the next note,
the debug note, I'm just going to print out that message that the variables have been set and confirmation.
Now, let's see how that works so far.
I'm going to deploy and then trigger this flu and you can see that virus said.
So we want to be able to read them.
Right.
So let's go ahead and do the reading of those flu variables to do that.
I'm going to use an inject note and in that inject note, I am going to read a flu virus first the temperature.
I'll give it a name, read SLU.
Temperature, I read, read low temperature variable underneath the second topic.
Done and then printed out to the back window, I just say show temperature, actually low temperature
Vudu.
Like that, and I'll do the same thing for humidity, so I just copy and paste.
The original note and change this to humidity, and now I want to get the humidity flow variable, variable.
Like that.
I can actually connect this node to the same debug node if I want, and I can just say show flow values.
Like this sense for both of these inject notes, the message is printed or is stored inside the message
payload vulnerable and that should work.
I'm going to test that.
And if it doesn't work, I'm going to split up into two debug nodes.
All right.
So let's deploy that and clean up.
And first, we want to get a reading from the data twenty to sensor.
So there's the reading.
And next, I want to read the flow variables first, the temperature one.
There it is.
And then second, the slow bearable for humidity and there it is so they can see these two sub flows
are not connected through wires or no messages are being passed around directly, but they do communicate
via flow variables.
And to stop flooding the debug window with messages every 10 seconds from the day to I'm just going
to disable those to debug notes.
All right.
One thing to notice here is that every time that the teacher, 22, is triggered and then the flow variables
are changed or updated, I get confirmation that variables have been set from this dybbuk.
Now, let's do the same thing.
But past those variables over to a second flow in a different tab.
So to do that, we need to use global variables.
So let's go ahead and do that first.
I'm going to go back into my change mode and add another two sets.
So the first one is going to go to a global temperature.
Vargas is going to set the global temperature variable and of course, we want to grab the value from
message payload for this.
And the second one, you can probably guess it will do the same thing.
But for global.
So I'm just going to copy humidity and then.
The value for the global humidity variable will come from.
And g humidity, so the message object to humidity and done, then I'm going to copy these three modes.
So command to see, to copy, I'm going to place them in my new flow, in the verbose flow, and I'm
just going to change.
Flow to global.
And again, flew to grovel for the payload, and that should be enough.
I'm just going to change this to show global values, global values and.
Deploy.
All right.
Let's trigger the DHT 22 sensor variables set and then go into the variables and context example flow
and take a reading.
So the second one, the humidity works, the first one does not, why's that global temperature so much
of miswired something?
Let's go back in my rules and let's see.
Humidity did not work.
So this is interesting, so I'll just show you again the problem here.
So let's have a look at the global variables.
I'm going to trigger the temperature variable first.
Hmm.
And it's giving me the variable set, which is not temperature.
I'm going to look into this in a moment.
And what about the humidity?
Yes.
And humidity works.
So there's a little bug here.
Let's try and figure out why this is happening.
So I'm going to go back to my DHT.
Twenty two example Fluked and then inside the change node and let's see.
I think the problem is right here, as you can see, that in this step, in this rule, I should say,
we said the payload to be viable set just a string of text, and then in the next one, setting temperature
to be the payload, which is what I've set of reset to be the string, Viterbo said.
So this is where the bug is to fix it.
I to move the payload set to the bottom of the list of rules so that the global temperature variable
is set to meet such payload before message payload is changed.
All right.
So that you do it.
Can we deploy again?
And.
The flow still still working.
I'm going to trigger it, actually, just to be sure.
OK, and then going to verbals and check.
Temperature in this twenty two degrees and the humidity.
All right, so that works.
So I can see that we can use global variables set by this change room so that these variables can be
accessed across multiple flows.
Now, one thing that I also want to do is to have the ability to clear those variables.
So I'm going to show you how to do that in a moment, again, using the change rule.
But before we do that and show you how to reset or how to to clear the flow or global variables, I'm
going to show you how to set them and treat them using JavaScript inside a function node.
So let's pick a function going to place set up here and connect it to the to the twenty two node like
this and then in it.
I'm going to just do a bit of copy paste to save a bit of typing like this, so programmatically you
can set a flow variable by using Flotard set first parameter to give you a variable, a name, and then
the second parameter, whatever value you want to store in it.
In this case, I'm going to store the value stored in message humidity inside the humidity, flue variable
and the value stored in a mosquito payload inside the temperature through variable.
It's going to give this function a name and that's about it.
I don't really care what comes out of it because I am not going to connect anything to this particular
function node.
All right.
So this is how you can set a variable.
And now let's read those two local or flow variables using a function node and a little bit of JavaScript
inside it.
So here's my function node.
I'm going to go in and copy a little bit of code that does the reading.
So it looks like this very simple.
I'm using slow dot get and pass on the name of the flow Viterbo that I want to get the value off and
whatever comes back will store it in the humidity.
Local actually node variable and doing the same thing for the temperature variable.
All right.
Now I would like to get those two variables sent out to debug nodes and I want to have two different
debug nodes connected to this function.
So I'm going to create a an array.
They contained two small Jason documents inside, so each one has got its own payload, first one has
got the temperature of the second one, the humidity.
You've seen this notation before.
So this is going to have to outputs.
And before I forget, I will give this function or a name.
All right, so here is our function and we need to Depok notes.
Of course, I could simply connect those two outputs to this debug note, but just keep things tidy
and separate for now.
All right.
So the first one is going to be the temperature.
It's going to float temperature.
And the second one is going to be the slow.
Humidity.
And lost, I need some way to trigger this function, so again, I'll use and inject Nood.
All right, I just send that a number does really matter what it is, whatever it is, it will trigger
the funky mood chocolate trigger.
All right, to deploy, clean up and first trigger the DG to sensor variables is set and then let's
trigger the function to rip them out that you.
OK, now the last thing to do is to expand our function here so that it also sets global variable so
that we can read them from a separate flow, you can probably figure out how to do that is very easy.
So first, I'm just going to duplicate these two lines and instead of flow will say global.
And done, then I'm going to copy this set of notes and pasted in my separate smile like that, and
all I've got to do to make this work is to change flow.
To global.
And that you work, so let's give this a go and deploy and clean, the robots are already in memory,
so I'm going to click on Trigger.
These are the flu viruses that these are the global variables now coming through the debug window.
Awesome.
So now you know how to use both global and flu variables.
One thing I should mention is that these variables and the values are stored in memory.
When you deploy the flu or even if you go through full deployment or even a restarting of the Flo's,
those variables still remain in memory and they will only change if you do so via the function or change
node.
Now, if you want to reset or even delete these variables in order to start from a clean slate, that's
particularly important when you are debugging and you're testing a new flu, then you have to do that
manually again using change mode.
So let's do that now, can take a change node and then go in and say that this is going to be a node
that cleans or cleans or variables and just choose the delete rule.
So you want to delete.
Humidity.
At the flow level.
Delate.
Actually makes the global humidity and then another to delete.
Flo.
Temperature.
And again, the late.
Global temperature.
And I want to also set the notification, so I'm going to use a debug.
Note to indicate to me that fireboats had been deleted.
All right, then let's have our debug window to confirm deletion.
So I'm going to call it delete confirmation, and again, I need the trigger, so I'm going to copy
the trigger note and connected to the change node.
All right, so I'm going to deploy and first, let's see what is inside our flow variables.
So I'm going to trigger flood variables.
And you can see that they do have defined values.
Then I'm going to trigger the cleaner.
So trigger plenel variables and variables deleted.
Then I'm going to trigger again the arete flow variables.
So now let's have a look at the flow variables.
You can see that they are not defined.
I just noticed.
Need to make a little correction here.
So this should be get not set.
Right.
OK, now if I want to reset the global flow variables, I just need to trigger this sub flow.
So that's a value stored in those variables and then I can check them out again and you can see that
they're now set.
Same thing happens, of course, at the global context.
So right now, the global context has got values.
If I clean those variables again, because in the meantime, they would sit so clean them and then test
them, they could see that different.
OK, so with this, you now know everything you need to know to be able to work with flow and global
variables in the context of this project.
So in the remaining lectures in this section, I'm going to demonstrate the use of several other notes
from the left toolbar that will be using in this project.