Help with Global Variable - Setting and using with Switch Node

4,210 views
Skip to first unread message

Richard Sears

unread,
May 28, 2016, 6:27:49 PM5/28/16
to Node-RED
I am very new to node-red and I am trying to put together my first project. I am making progress, but I am stuck on a particular function and need some insight.

I am working on a presence notification system that will great my family members by name when they get home. I am working with just my phone now, but 
will expand it to cover the entire family once I have it working. In Python, this would be easy, but I really want to learn node-red!

Here is how it works (so far):

1) I have a flow that pings my cell phone (static IP) every 10 seconds. It updates a mysql db based on if the ping is successful or not. If successful, writes a 1, if not, writes a 0.

2) I then have a function that grabs that status.

3) If the status returned is 1 (meaning I am home) then it goes to another switch that checks a global variable using the switch property of global.dad_welcome. If dad_welcome == "not_played"
then it plays a welcome message over the loud speaker and then sets the global.dad_welcome to "played".  The idea is that if the message was already played, it won't play again.

4) If the status returned is 0 (meaning I have left) then the switch sends it to a function that sets the global.dad_welcome to "not_played" so the next loop through when the status returns a 1 it will play the message.


Here are my functions I am using to set the global variable:

global.set("dad_welcome","played");
context
.global.dad_welcome = msg.payload;
return msg;

 

and 

global.set("dad_welcome","not_played");
context
.global.dad_welcome = msg.payload;
return msg;



but I get nothing. Even in debug mode where I tell it to return msg.payload I do not get the global variable returned to me, so I am sure I am not setting it properly! 

Here is my switch setup:


Any help would be appreciated.


Thanks




Nicholas O'Leary

unread,
May 28, 2016, 6:32:41 PM5/28/16
to Node-RED Mailing List
At a quick glance the issue appears to be that you're setting the global value twice to different values:

First you set it to "not_played" (or "played") using the proper syntax:
global.set("dad_welcome","not_played");

Then you set it again to the current value of msg.payload, using the old syntax:
context
.global.dad_welcome = msg.payload

This means global.dad_welcome is always left with the value of the last msg.payload and not the text you expect.

Nick


--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
For more options, visit https://groups.google.com/d/optout.

Richard Sears

unread,
May 28, 2016, 6:42:34 PM5/28/16
to node...@googlegroups.com

This is the intent. I need the variable to change based on if i have played the welcome or not already.

You received this message because you are subscribed to a topic in the Google Groups "Node-RED" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-red/Wa65rMHI840/unsubscribe.
To unsubscribe from this group and all its topics, send an email to node-red+u...@googlegroups.com.

Richard Sears

unread,
May 28, 2016, 6:44:08 PM5/28/16
to node...@googlegroups.com

Oh wait, i think I see what you saying...i should remove the global.dad_welcome = msg.payload otherwise i am resetting it instead of setting it.....

On May 28, 2016 3:32 PM, "Nicholas O'Leary" <nick....@gmail.com> wrote:
You received this message because you are subscribed to a topic in the Google Groups "Node-RED" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-red/Wa65rMHI840/unsubscribe.
To unsubscribe from this group and all its topics, send an email to node-red+u...@googlegroups.com.

Richard Sears

unread,
May 28, 2016, 7:01:58 PM5/28/16
to Node-RED, ric...@sears.net
Thank you Nick - one simple change and it works!


global.set("dad_welcome","played");

msg
.payload = context.global.dad_welcome;
return msg;


To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
For more options, visit https://groups.google.com/d/optout.

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to a topic in the Google Groups "Node-RED" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-red/Wa65rMHI840/unsubscribe.
To unsubscribe from this group and all its topics, send an email to node-red+unsubscribe@googlegroups.com.

jo2

unread,
May 29, 2016, 8:12:19 AM5/29/16
to Node-RED, ric...@sears.net
Thanks for this, I was not aware of one can use flow or global in the Switch node. Have always put things in the msg object before the Switch. Quite convenient to have these choices!

-jo2 

Julian Knight

unread,
Jun 1, 2016, 3:05:08 PM6/1/16
to Node-RED, ric...@sears.net
You probably want to use global variables sparingly as they can be quite tricky to debug in complex flows. It is generally better I think to pass information in the msg if you can.

Also note my comment in a previous thread that pinging cell phones to see if they are present is not always very accurate, especially for iPhones as they turn off the WiFi in standby.

jo2

unread,
Jun 2, 2016, 5:40:55 AM6/2/16
to Node-RED, ric...@sears.net
Good point, thanks. I try to limit global context usage, and have moved some, when possible, to flow context.

Cheers
-jo2
Reply all
Reply to author
Forward
0 new messages