Switch Node Expression Help

1,122 views
Skip to first unread message

Joe Morris

unread,
Jun 26, 2017, 2:36:28 PM6/26/17
to Node-RED
I am attempting to set up a switch node with a rule that combines text and a global variable.

I have tried for 2 hours trying to find documentation on the switch node that has the appropriate detail and have failed.

Here is an example of what I am looking to do:

switch rule ==> contains: "myTxt" + global.get("myVariable")

Thanks

Joe

steve rickus

unread,
Jun 26, 2017, 3:08:35 PM6/26/17
to Node-RED
Fairly recently, the ability to use flow/global values was added to the Jsonata expression evaluator in the switch and change nodes. I'm not sure if your version will support it, but you could try something like this:

"myTxt" & $globalContext("myVariable")

Just make sure you click the little down-arrow and select "J: expression" as the field type (not the"a/z" text type)
--
Steve
Message has been deleted
Message has been deleted
Message has been deleted

Joe Morris

unread,
Jun 26, 2017, 4:02:44 PM6/26/17
to Node-RED
OT: Is there a way to edit posts? I posted 2 messages that I needed to change upon my further research. So I deleted them and reposted.

Jsonata is wonderful. I will find many uses for it.

However, msg.payload contains a string and not JSON. I can possible convert it to JSON. But is there another way?

Colin Law

unread,
Jun 26, 2017, 4:07:03 PM6/26/17
to node...@googlegroups.com
No you can't edit posts, this is a mailing list, you cannot edit or
delete the version sent to me (for example). At least I hope you can't
hack into my PC and edit my emails :)

Coin
> --
> 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.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/8d4fe271-8fce-41e7-ae0f-54f0574ad34c%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.

Colin Law

unread,
Jun 26, 2017, 4:13:50 PM6/26/17
to node...@googlegroups.com
According to [1] & is the string concatenation operator, so I would
have expected it to work with &. I have not used this feature much
though. Hopefully someone more knowledgable will come along shortly.

Colin

[1] https://github.com/jsonata-js/jsonata/blob/master/tutorial.md


On 26 June 2017 at 20:43, Joe Morris <jmorr...@gmail.com> wrote:
> With your sample I would get the error ""Invalid expression: Attempted to
> invoke a non-function""
>
> I replaced the "+" in my example wit the "&" and initially I thought it was
> working. I need the text and variable to only be caught when they are next
> together. IT is catching them wherever they are in the payload.
>
> Any ideas?
>
> Joe
>
> On Monday, June 26, 2017 at 2:08:35 PM UTC-5, steve rickus wrote:
>>
> --
> 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.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/d5b09023-5b99-45ad-b868-db266712a1af%40googlegroups.com.

Nick O'Leary

unread,
Jun 26, 2017, 4:16:27 PM6/26/17
to Node-RED Mailing List
Hi Joe,

I'm not entirely clear where you are with this given your various posts.

What I can say is the $globalContext function is not generally available - it is in 0.17 which is to be released later this week. That is why you get the 'Attempted to invoke a non-function' message.

Nick


> To post to this group, send email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/d5b09023-5b99-45ad-b868-db266712a1af%40googlegroups.com.
>
> 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 the Google Groups "Node-RED" group.
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 an email to node...@googlegroups.com.

Joe Morris

unread,
Jun 26, 2017, 4:22:21 PM6/26/17
to Node-RED
My humble apologies to everyone. I was not thinking of this being a mail-list. I have been so active lately that I had "blog" on the brain. :)

Nick - It actually worked when I changed it from globalContext to global.Context. (adding the dot)

In Steve's response he took me down a JSON track. Which I immediately glommed onto. I found JSONata so intriguing. My msg.payload is not JSON however. It is a string.

So my original question still stands. How can I trap a string plus a variable in switch rule.

(a very humble) Joe

> To post to this group, send email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/d5b09023-5b99-45ad-b868-db266712a1af%40googlegroups.com.
>
> 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 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.
Reply all
Reply to author
Forward
0 new messages