About comparing two values in Function Node in Node Red

6,282 views
Skip to first unread message

Kyaw Zin Latt

unread,
Sep 4, 2014, 9:41:23 PM9/4/14
to node...@googlegroups.com
I want to compare two different values inside Function Node. E.g. One is coming from Xively and one from my own sensor. I just want to compare these two values. Can I do that inside Function Node or Do I need to create new custom node?

Thanks

Dave C-J

unread,
Sep 5, 2014, 8:05:16 AM9/5/14
to node...@googlegroups.com
Hi

you can use other properties of the message to compare the two inputs and do what you need to do. You will need to store the value of one (or both) of them as, being asynchronous, there will only ever be one input arrive at one time. Thus you have to hold the other one in the context object for when the second arrives...

Here is a simple demo flow that shows the principle...

[{"id":"be996e1.f41669","type":"inject","name":"inject timestamp","topic":"b","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":166,"y":506,"z":"a5d52cc4.5a2ad","wires":[["c47edc04.3b812"]]},{"id":"c47edc04.3b812","type":"function","name":"","func":"context.flow = context.flow || false;\n\n// if the messages has property a - (from first button)\nif (msg.topic === \"a\") {\n\tcontext.flow = !context.flow;\n}\nelse {  // otherwise it's from the other input\n\tif (context.flow) { return msg;}\n}\n\nreturn null;","outputs":1,"x":347,"y":504,"z":"a5d52cc4.5a2ad","wires":[["36aea60d.c9515a"]]},{"id":"49255bc5.b6daa4","type":"inject","name":"Turn flow on/off","topic":"a","payload":"1234","payloadType":"date","repeat":"","crontab":"","once":false,"x":174,"y":566,"z":"a5d52cc4.5a2ad","wires":[["c47edc04.3b812"]]},{"id":"36aea60d.c9515a","type":"debug","name":"","active":true,"console":"false","complete":"false","x":511,"y":533,"z":"a5d52cc4.5a2ad","wires":[]}]

This has two inputs... - one just sends a timestamp through the function to the debug... the second input toggles a flag to allow the output to occur or not. Obviously you can combine them however you want inside a function. The key is that one input has a msg.topic property set to "a" and the other doesn't - so you can tell them apart. - If you are getting things from Xively as an MQTT feed then it should have a topic set already...



Kyaw Zin Latt

unread,
Sep 6, 2014, 10:26:22 PM9/6/14
to node...@googlegroups.com
Let me explain you what I want to do. I want to compare two different temperatures. One is coming from forecast.io and another one from DHT11 sensor. I will filter the message to get the temperature value from both forecast.io and DHT11.Then, I want to compare these two values using if-else condition and return the different messages depend on the current condition. E.g. The message "Warning" will be returned when value from forecast.io>value from DHT11, else the message "Ready" will be returned.

John Robinson

unread,
Sep 6, 2014, 11:18:39 PM9/6/14
to node...@googlegroups.com
K,
So get the weather every 5-15 mins and store it like: 'context.global.myweathertemp= valFromweather'

Then every min pull the other temp and compare to context.global.myweathertemp

The var context.global is node-reds global var available to every function



You can use it to require libraries at the bottom of settings.js


Sent from my iPhone

On Sep 6, 2014, at 9:26 PM, Kyaw Zin Latt <mr.kyaw...@gmail.com> wrote:

Let me explain you what I want to do. I want to compare two different temperatures. One is coming from forecast.io and another one from DHT11 sensor. I will filter the message to get the temperature value from both forecast.io and DHT11.Then, I want to compare these two values using if-else condition and return the different messages depend on the current condition. E.g. The message "Warning" will be returned when value from forecast.io>value from DHT11, else the message "Ready" will be returned.

--
http://nodered.org
---
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.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages