00:01
once you've installed and configured
00:02
your groov I/O nodes for the first time
00:04
you're ready to start doing something
00:05
more substantial with your data
00:07
if you don't already have these nodes
00:08
installed or a localhost device
00:10
configured
00:10
go ahead and watch our previous videos
00:13
in this video I'm going to show you how
00:14
to bring in an analog
00:16
channel value and then use that to
00:18
actually write a digital state change
00:20
and we'll be doing a basic decision
00:22
making in the middle and there's a
00:23
couple of different ways that we can do
00:24
that with Node-RED
00:25
so we'll take a look at the options what
00:27
the pros and cons of each are and so you
00:29
can find the best approach for you
00:37
when it comes to making decisions in
00:38
Node-RED one of the simplest ways to do
00:40
it is with a switch node
00:42
this switch node essentially re-routes
00:44
the flow of traffic
00:45
based on some value so let's double
00:47
click it and see how this works
00:49
here you check some property to see if
00:51
it matches
00:52
some condition in this case we are going
00:54
to be checking message.payload and
00:56
that's going to be a temperature
00:58
that we're going to be reading in then
01:00
we're going to see if it meets some
01:01
threshold let's say if it's
01:03
greater than a number right number being
01:06
85 degrees so if our incoming
01:08
temperature is greater than 85 degrees
01:10
it's quite warm and we're going to
01:12
toggle an output on that could be
01:14
something like a fan
01:15
or a refrigeration unit but we also need
01:18
to turn it off when it is less than or
01:20
equal to 85 degrees
01:22
so just come down to the bottom here
01:23
you'll see there's the option to add a
01:25
new condition
01:26
and here I could set it to be less than
01:28
or equal to
01:29
again 85 but there's another option at
01:32
the bottom here called
01:33
otherwise and that way I don't have to
01:35
put in any conditions it's just sort of
01:37
a catch-all
01:38
anything that's not greater than 85
01:40
degrees is going to be coming out of
01:42
this
01:43
second output and you'll see that we
01:45
have one output on the switch by default
01:47
but as soon as I save this change we'll
01:49
see that we now have two outputs
01:51
one will switch the flow out this way
01:54
when it's a greater than 85 degrees
01:56
and every other message will come out
01:57
this bottom output
01:59
so now we just need to figure out
02:00
exactly how to configure our input and
02:02
output groove nodes
02:04
and we'll be able to do some basic
02:05
control here so I'll come up to groov
02:07
manage
02:08
and select myI/O channels here you can
02:11
see that my
02:12
fuel level is on two but this analog
02:14
temperature on three is the one I want
02:16
to read in
02:17
and I don't have a fan hooked up to my
02:19
groove rear right now so I'm just going
02:20
to simulate it with this
02:22
blue ring led so that's my analog input
02:25
on three and my digital output on four
02:28
and sinceI/Only have the one module for
02:30
groov RIO we all know it's going to be
02:31
all on module 0.
02:33
so I'll go back to Node-RED and use a
02:35
little bit of video editing magic to
02:37
give you an overlay in the bottom right
02:39
here where we can see
02:40
our values live and we'll go ahead and
02:42
configure these
02:44
so our analog input is going to be from
02:46
localhost
02:47
it is an analog channel value and it's
02:49
on module 0
02:51
and we can see right there it is in fact
02:53
on channel 3.
02:55
so we'll just put that in and call it
02:56
something like check temperature
03:00
but we don't want to get every single
03:02
degree of change
03:03
that could be a good thing in some cases
03:05
but I actually want to get more messages
03:07
than that
03:08
so I'm going to decrease the deadband to
03:10
0.5 degrees
03:12
which is going to increase the volume of
03:14
messages I get
03:15
so I'm going to get twice as many
03:16
messages if I had set it to an entire
03:18
degree
03:19
now that in some situations may be too
03:22
small of a change
03:23
and you may want to look at say a 10
03:25
degree change and only get updated
03:27
when it fluctuates by that much in this
03:29
case I'll go with 0.5 degrees and hit
03:32
done
03:33
now I need to configure my groovI/O
03:35
write node
03:36
so when we check the temperature we're
03:38
going to be pushing an
03:40
output out this way when it's greater
03:41
than 85 degrees
03:43
and so that means we need to write on or
03:45
turn the fan
03:46
on so we'll come in here to our device
03:49
select our localhost device
03:50
we are toggling a digital output and it
03:53
is on module 0
03:54
and you can see right there we're going
03:55
to be simulating our fan with the blue
03:57
ring led
03:58
so that would be on index 4.
04:01
but this value this message.payload at
04:03
this point in the flow
04:05
is just that temperature being
04:06
redirected through the switch
04:08
up to this group via write node so the
04:10
message.payload in this case is still
04:12
going to be the temperature we can't
04:14
write the temperature to a digital
04:16
output state
04:16
we need to write a specific value like
04:18
true or false
04:20
in this case we want to turn on the
04:22
output
04:23
and so we'll be sending the value true
04:25
when it is greater than 85 degrees
04:28
so I'll hit done but now I also need to
04:30
make sure I turn it off
04:31
when it's not greater than 85 degrees to
04:34
do that
04:35
I'll just make a copy of thisI/O node
04:37
with control c
04:38
paste that with ctrl v and wire that in
04:42
now I just need to modify this copied
04:44
node to send the value false
04:46
to that same module 0 index 4 and we'll
04:49
rename this to be
04:50
turn off so with that done we can see we
04:53
have a really simple flow here
04:55
it will check the temperature every half
04:56
degree of change and then the switch
04:58
node will reroute the traffic upwards to
05:00
turn this
05:01
output on whenever we are greater than
05:04
85 degrees I can confirm that by
05:06
hovering over the output port here and
05:08
otherwise we will go
05:10
ahead and turn it off when it is less
05:11
than or equal to 85 degrees
05:13
so let's see this work and we'll go
05:15
ahead and deploy
05:17
oh and just so that we can see
05:18
everything's working correctly let's
05:20
also drag in some debug nodes we'll drag
05:22
in one for this top
05:23
and one for this bottom part of the flow
05:25
so every time it gets turned on we'll
05:27
see a message and every time it gets
05:28
turned off
05:29
we'll see a message so now when I deploy
05:32
that initial value will come through
05:34
and we'll see yes it's 80 degrees so
05:36
it's actually quite cool so
05:37
if I were to grab the temperature sensor
05:39
from the RIO
05:40
and start heating it up with my body
05:42
temperature we'll see that this is going
05:43
to start climbing
05:45
and every half a degree of change that
05:47
it goes upwards we are seeing that
05:49
dashed outline here
05:50
around this turn off debug node here so
05:54
you can see we're constantly turning it
05:55
off
05:56
whenever it's 85 degrees or less now you
05:58
can see we've crossed the threshold
06:00
we've turned it on and you can see the
06:02
blue led ring is on there our simulated
06:04
fan is running
06:05
so we know that this works so we're just
06:07
continually turning it on
06:09
whenever we're above 85 degrees and then
06:11
continually turning it back
06:12
off whenever we're less than 85 degrees
06:15
so this all seems to be working
06:17
let's turn these debug nodes off so that
06:19
we don't keep getting messages here
06:21
and clean up our debug pane
06:25
so while this works and is really simple
06:27
there may be more efficient ways to do
06:28
this
06:29
because every half degree of change we
06:31
are writing this output
06:33
and it's simply not necessary we're
06:35
saying oh you're at
06:36
86 degrees we better turn you on you're
06:38
at 87 degrees we better turn you on
06:40
but at that point it's already been
06:42
turned on these are redundant
06:44
messages so let's see if we can find a
06:46
way around this by using another method
06:48
to do that I'll go ahead and disconnect
06:50
this input node and drag this down so
06:52
that I don't lose my previous work but
06:54
this flow will no longer be running
06:55
since technically there's nothing being
06:57
inputted into it
06:58
so I'll deploy and we'll take a look at
07:00
our next node which is going to be
07:02
the function node this is a great node
07:04
if you have a little bit of experience
07:06
with programming or if you're not afraid
07:07
of writing a little bit of code
07:09
if I bring up in this function node you
07:11
can see that I just have this huge text
07:12
block where I can write whatever
07:14
javascript code I want
07:16
in this case it's going to be relatively
07:17
straightforward I'm going to say
07:19
if the incoming message.payload object
07:22
which
07:22
holds the temperature reading is greater
07:24
than 85 degrees
07:26
then I want to set message.payload
07:29
to be equal to true so even though I'm
07:32
reading in a number on message.payload
07:34
i can change message.payload into
07:36
anything I want in this case I want it
07:38
to be true there
07:39
and otherwise or else I want
07:41
message.payload
07:42
to be equal to false now with that
07:45
simple change I can now return the
07:47
message
07:47
with this modified payload and hit done
07:50
oh
07:50
we'll just go back and give that node a
07:52
name like write true false that's a good
07:54
one
07:55
and hit done so now when we go down
07:58
and drag in that grooveI/O right node
08:01
and go ahead to configure it
08:03
we can see that we can leave this value
08:05
to be message.payload since it now no
08:07
longer holds a temperature
08:08
number it's going to hold a boolean
08:10
either true or false
08:12
so I'll configure this to be my
08:14
localhost device
08:15
go ahead and put the module index of 0
08:18
the channel index of again down there we
08:20
can see that my fan is on
08:21
output 4 and we'll go ahead and write
08:23
this to be
08:24
right true or false so here we can see
08:28
the value
08:28
i leave as message.payload so now when i
08:31
click done
08:32
I'll add another debug node this time
08:34
I'll hold left control and left click on
08:36
the screen to
08:38
quickly add one in really easily with
08:39
that shortcut and we can wire this all
08:41
together
08:43
so to review every half degree of change
08:45
in temperature
08:46
we'll go here and we'll convert that
08:48
temperature from
08:49
the number into a boolean you can even
08:52
rename this to be more appropriate
08:54
convert to boolean and we'll wire that
08:57
into the right
08:58
true or false since it's already been
08:59
converted and we'll go ahead over here
09:01
and we'll watch this message.payload to
09:03
see the messages coming in
09:06
so now when I deploy we can see instead
09:08
of seeing a number over here we're
09:10
seeing
09:10
false the fan is off since it's quite
09:12
cool again if I were to grab the
09:14
temperature sensor we'll see it start
09:16
heating up
09:17
and we're continually riding false
09:19
because you'll see down there in the
09:20
bottom right we're still down at 70
09:22
degrees we're increasing quite quickly
09:24
and every half degree of change
09:26
again we're confirming off off off
09:29
off and we're continually riding false
09:31
and so we'll see that continually until
09:33
we pass the 85 degree mark
09:36
should just take a second longer and
09:38
then we'll start seeing true show up and
09:40
we'll see this output get toggled
09:42
we let it cool down a little too much
09:43
it's taking a moment here but there you
09:44
go
09:45
it's been toggled on and if we have a
09:47
look down here
09:48
let's just hide that overlay there we go
09:51
we can see that we are in fact now
09:52
writing true
09:54
but again we have this constant stream
09:56
of true
09:57
true true true whenever it gets written
10:00
and we want to be more efficient than
10:01
that
10:02
so we can drop an extra node in here
10:04
between the conversion and the writing
10:07
and the node we'll use is rbe and that
10:09
stands for report
10:10
by exception if we double click this
10:12
node we can see exactly what it does
10:14
right here
10:15
this will block value unless it changes
10:18
and what that means is it'll hear the
10:20
false message and then pass that along
10:22
then it hears another false and it goes
10:23
wait this isn't new and it won't hand it
10:25
along
10:26
and it won't hand along a false message
10:28
until something new comes along and
10:30
changes it
10:31
so if we get true it'll say hey this is
10:33
an exception to the last value
10:35
and I'll go ahead and hand that along
10:37
and this will essentially reduce
10:38
the repeated values to be only sent out
10:41
once
10:42
so I'll leave this to the default mode
10:44
of block and less value changes
10:46
hit done and drag this into my wire you
10:48
can see it goes dashed
10:50
and I'm able to drop that right in there
10:52
so now when I clear up the debug and hit
10:54
deploy
10:56
we can see that as we're initially
10:58
deploying the flow we'll see it's true
11:00
because we are in fact above 85 degrees
11:02
and if we start cooling down the
11:04
temperature sensor and it drops below 85
11:07
degrees
11:08
we'll only get one message false and
11:11
here it comes as we drop below 85
11:13
degrees the output is going to turn off
11:15
since we're going to get that one false
11:17
value but we're not going to continually
11:19
get those false values every half degree
11:21
of change
11:22
and that's thanks to this report by
11:23
exception or rbe node
11:26
now the benefit here is not only having
11:27
a much quieter debug pane if you leave
11:29
this debug enabled
11:30
it also means you're hitting this node
11:32
less often which frees up
11:34
this application programming interface
11:36
this api to be able to do
11:38
other things this can be a really good
11:41
thing if you have a lot of flows running
11:43
and you don't want to be constantly
11:44
hammering your apis
11:46
but we don't necessarily have to use a
11:48
function node in order to use the rbe
11:50
node
11:50
let's have a look at what that looks
11:52
like we can do that with a change node
11:55
so we'll just again delete this wire and
11:58
drag this input down
12:00
keep our progress on the previous flow
12:02
and go ahead and deploy so that we can
12:04
start working on this change node
12:06
so here instead of changing
12:09
message.payload and pushing it down a
12:10
different route depending on the value
12:12
or converting the value using some
12:15
javascript code
12:16
we're going to be using this change node
12:17
to change the value a little bit
12:19
differently
12:20
this is going to be a nice compact
12:22
expression
12:23
here we want to set the message.payload
12:25
to be
12:26
that expression now you may be tempted
12:29
to come up here and select
12:30
change but you're going to have a little
12:32
bit of trouble with what you search for
12:34
since we need to search for all the
12:35
values above
12:36
85 degrees and then search for all the
12:38
values that aren't above 85 degrees
12:41
it's much more straightforward to do a
12:42
similar thing in the
12:44
javascript that we did and just set the
12:46
message.payload and redefine it entirely
12:49
rather than trying to change it
12:50
individually
12:51
so we'll use this javascript or jsonada
12:55
expression to be able to do this now if
12:58
you're not familiar with jsonada that's
12:59
okay
13:00
if you click these three dots here we
13:02
have a really nice testing tab
13:03
where we can see where everything works
13:06
now based on a simple google search i
13:08
know that I can make decisions with just
13:09
a question mark
13:11
my first expression which is going to be
13:12
true a colon and my session
13:15
second expression which is going to be
13:16
false this is just simple context that
13:19
we can put some kind of decision
13:21
here question that decision and then
13:23
give two potential outputs
13:25
in this case if it's true I want it to
13:28
set the value to be true
13:29
and if it's false I want it to set the
13:31
value to be false
13:33
of course my expression here is is the
13:34
message.payload
13:36
greater than 85 degrees here we can also
13:39
test it with specific payload values
13:41
so here I'm not expecting a value like
13:43
hello world I'm expecting a number like
13:46
82 degrees and we can see that 82
13:48
degrees is
13:49
not greater than 85 so we'll get returns
13:52
the value false and we can see in fact
13:54
our result is false over here
13:56
if I get a value like 85 we can see
13:58
that's not greater than 85
14:00
it's equal to it so we'll again get
14:02
false but
14:04
at 86 there we go it is greater than 85
14:07
and we're getting the expression
14:08
resulting in the result
14:10
true so we know all that works I can
14:12
click done
14:14
and there we go we're converting our
14:15
message.payload to be true
14:17
or false depending on this simple
14:18
expression now I can just click done
14:22
drag in another rbe node and we'll also
14:24
make a copy of this right
14:26
true false and again since we're
14:28
converting or changing message.payload
14:31
the value itself
14:32
this message.payload is no longer a
14:34
temperature number
14:35
it is true or false it's that boolean so
14:38
we can go ahead and keep that as the
14:39
value here
14:40
we're good to go so I'll again drag in a
14:43
debug
14:45
and then every half degree of change
14:47
will convert or change the
14:48
message.payload to be either true or
14:50
false
14:51
check to see if it's changed from true
14:53
to false or back again
14:55
so that we can get rid of those repeated
14:56
values write that value
14:59
and then confirm it in the debug pane so
15:02
now I'll go ahead and click deploy
15:04
we can see that there we go we get our
15:06
one value of false
15:07
and as we start heating up here just
15:09
grab that temperature sensor again
15:11
we'll see as the temperature is climbing
15:12
down here in the bottom right
15:14
we'll see it going up but every half
15:16
degree we're not getting a message
15:18
similar to the javascript approach we're
15:20
only going to get a message when it
15:22
crosses that threshold
15:23
we actually toggle the output this is
15:25
going to be nice and light and low
15:27
weight on our api
15:28
and we'll be able to have an easy way to
15:30
toggle this output
15:31
without having to mess around with the
15:33
code just using this nice simple change
15:35
node
15:36
so there we go we're about to crush the
15:38
threshold and 84
15:40
85 degrees and there we go our one
15:43
message has been let through that one
15:45
new true message
15:46
but no more further messages have come
15:49
through since we've already toggled it
15:50
on
15:50
we don't need to let it know it needs to
15:52
be turned on again and there we go
15:54
we're good to go so here you can see we
15:57
have three different methods of control
15:59
we have the switch node that can
16:01
redirect traffic and write a
16:03
constant value of true or
16:06
false to our digital output we also have
16:08
the option to use raw javascript code
16:11
which you can also use more advanced
16:12
functions
16:13
if that's something you're interested in
16:15
we'll have some links in the
16:16
descriptions of some great places to get
16:17
more familiar with javascript
16:19
or you can just use a simple uh jsonata
16:22
expression
16:23
to keep it nice and contained in this
16:24
change node whatever approach you take
16:27
you might want to consider using these
16:28
report by exceptions
16:30
so that you can block the value unless
16:32
it changes so that there's a little bit
16:33
less weight on your writing nodes
16:35
and that'll free up your api to do other
16:37
things
16:38
as always once it's actually all working
16:40
you can go ahead and disable these debug
16:42
nodes
16:43
since you know it's good to go and you
16:44
can just let it run
16:46
now whichever one you decide to wire up
16:48
to is going to depend on whatever you're
16:49
most comfortable with and whatever you
16:50
must
16:51
have the most experience with the
16:53
important thing is is that you're able
16:54
to do it in many different ways using
16:56
Node-RED
16:56
they're all supported and they all get
16:58
the job done