switch node: how to access value by key?

498 views
Skip to first unread message

urs.epp...@switch.ch

unread,
May 8, 2017, 2:46:47 PM5/8/17
to Node-RED
Hello
I plan to use the switch node in the following way:
There is a global object global.MsgNumbers which contains {"sonoffth1": 324, "sonoffth2": 2}  (it contains more key value pairs in the same way)
Then there is a variable which contains the sensor name msg.nodegroup "sonoffth1"
And there is a payload variable which contains a number: msg.payload.MsgNr 325

The switch node needs to check, if msg.payload.MsgNr is bigger than the corresponding value in the global variable.
If it is bigger, then send the msg to output 1, else to output 2.
Using JavaScript I would first fetch the global variable and then do something like this fetchedNumbers[msg.nodegroup]

I tried this in the switch node as in the picture below. It does not like it.
As you can see, according to the error message, it does accept the open bracket [ , but it barfs at the "m".

Am I too creative here? Can somebody point me in the right direction?

Many thanks and kind regards,

Urs.


Nick O'Leary

unread,
May 8, 2017, 2:55:04 PM5/8/17
to Node-RED

That isn't possible with the switch node; you will have to use a Function node and write the logic in JavaScript.

The next release adds support for jsonata expressions to access context variables, so would be possible then.

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.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/4132eccd-088b-42b6-b36b-25a2be9a47d6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

urs.epp...@switch.ch

unread,
May 8, 2017, 3:21:00 PM5/8/17
to Node-RED
Hello Nick
Thanks for the very quick response.
I just wanted to use standard nodes wherever possible, but since I got the official absolution from you, I will write the one liner into a function node.
Kind regards,
Urs.

Dave C-J

unread,
May 8, 2017, 3:33:35 PM5/8/17
to node...@googlegroups.com
there's nothing (much) wrong with function nodes :-).... we don't want to discourage people from opening up a function to fix things... it encourages people into coding, which is half the plan !

Nick O'Leary

unread,
May 8, 2017, 4:32:30 PM5/8/17
to Node-RED Mailing List
For completeness, in the next release, where you've tried to use `global....` in the Switch rule, you'd set it to be an 'expression' type with a value of: 

    $lookup($globalContext("MsgNumbers"), nodegroup)



Nick

On 8 May 2017 at 20:33, Dave C-J <dce...@gmail.com> wrote:
there's nothing (much) wrong with function nodes :-).... we don't want to discourage people from opening up a function to fix things... it encourages people into coding, which is half the plan !

--
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 email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Glenn

unread,
May 8, 2017, 7:23:11 PM5/8/17
to Node-RED
JSONata is great for a bit of fuzy logic too. I love getting a big data set and using jsonata to extract lots of elements from different areas
Reply all
Reply to author
Forward
0 new messages