As is always the case in these situations, put debug nodes in and check that the messages you expect are getting through.
I added the debug node with the output of the change node but there is still no message displayed. Is the msg.topic and msg.payload will affect the debug message?
if (msg.payload.clench = 0 ) {
msg.payload = "clench signal";
}
else if (msg.payload.frown = 0) {
msg.payload = "frown signal";
}
There is flaw what in programming called "assignment in conditional expression".
To understand it better take a little lesson:
https://www.w3schools.com/js/js_statements.asp
https://www.w3schools.com/js/js_comparisons.asp
Proper way:
if (msg.payload.clench == 0 ) {
msg.payload = "clench signal";
}
else if (msg.payload.frown == 0) {
msg.payload = "frown signal";
}
Thank you for your correction. I've changed the code but there is still no output message displayed. BTW, node-red suggest me to use "===" to compare with 0. Which is the right way? "==" or "==="?
Seems you haven't read the provided links as it is explained there
Ohh.. I'll read it then. Sorry because I'm trying to find out the error.
You will find it if you understand it. Not before.
I've changed my flow. I used the switch node and change node and connecting with the function node.
[{"id":"73f9235a.d2b04c","type":"tab","label":"Flow 7","disabled":false,"info":""},{"id":"d8fcbd7d.fef46","type":"EMOTIV","z":"73f9235a.d2b04c","name":"EMOTIV","x":60,"y":420,"wires":[["f40d8a53.8ecc68"]]},{"id":"f40d8a53.8ecc68","type":"Profile-Name","z":"73f9235a.d2b04c","name":"Profile Name","profileName":"latest","x":250,"y":420,"wires":[["4cbfd87b.f24c78","30401b6c.f4c924"]]},{"id":"71de9eba.eda1","type":"debug","z":"73f9235a.d2b04c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":1330,"y":380,"wires":[]},{"id":"74ef51e3.0320a","type":"switch","z":"73f9235a.d2b04c","name":"sensitivity","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"40","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":740,"y":360,"wires":[["5d5268b1.69f868"]]},{"id":"5d5268b1.69f868","type":"change","z":"73f9235a.d2b04c","name":"over 80","rules":[{"t":"set","p":"topic","pt":"msg","to":"clench","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":940,"y":340,"wires":[["b6627176.c1dc6","7f76f2c8.10eccc"]]},{"id":"30401b6c.f4c924","type":"Facial-Expression","z":"73f9235a.d2b04c","name":"Facial Expressions","action":"clench","sens":"800","x":520,"y":360,"wires":[["74ef51e3.0320a","31ee440f.db792c"]]},{"id":"4cbfd87b.f24c78","type":"Facial-Expression","z":"73f9235a.d2b04c","name":"Facial Expressions","action":"frown","sens":"800","x":510,"y":480,"wires":[["784f5a09.e8ff84","79bb3053.9b05b"]]},{"id":"784f5a09.e8ff84","type":"switch","z":"73f9235a.d2b04c","name":"","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"40","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":730,"y":480,"wires":[["b230d70d.ce6d58"]]},{"id":"b230d70d.ce6d58","type":"change","z":"73f9235a.d2b04c","name":"over 80","rules":[{"t":"set","p":"topic","pt":"msg","to":"frown","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":940,"y":480,"wires":[["b6627176.c1dc6","7f76f2c8.10eccc"]]},{"id":"7f76f2c8.10eccc","type":"function","z":"73f9235a.d2b04c","name":"","func":"if (msg.payload.frown > 40 && msg.payload.clench < 40) {\n msg.payload = \"frown signal\";\n}\n\nelse if (msg.payload.clench > 40 && msg.payload.frown < 40) {\n msg.payload = \"clench signal\";\n}\nelse {\n msg.payload = \"no signal\";\n}\nreturn msg;","outputs":1,"noerr":0,"x":1170,"y":380,"wires":[["71de9eba.eda1"]]},{"id":"b6627176.c1dc6","type":"debug","z":"73f9235a.d2b04c","name":"1","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":1190,"y":220,"wires":[]},{"id":"31ee440f.db792c","type":"debug","z":"73f9235a.d2b04c","name":"2","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":820,"y":200,"wires":[]},{"id":"79bb3053.9b05b","type":"debug","z":"73f9235a.d2b04c","name":"3","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":700,"y":600,"wires":[]}]
The debug messages are keep on display the "no signal" message only. Is there any wrong about the first two display message codings?
I've used the debug node to test the output integer (0-100) of the change node and it works fine. The integer will varies between 0 to 100.
What is your debug node '1' showing are the values of msg.payload.from and msg.payload.clench?
Yes, they are the values that larger than 40.
Enable that debug node and show us what you get in the debug window.
That is showing the payload is a number, there is no attribute msg.payload.frown, or msg.payload.clench. Your code is expecting the payload to be an object containing attributes frown and clench. It contains neither.
Then the code can be used to compare the number? How can I define that the payload is a number?
It already is a number. If you want to test the number you can just use, for example, if (msg.payload > 40) {
I see you have the topic set to "frown" or "clench" so you can test that too using something like
if (msg.topic === "frown" && msg.payload > 40) {
but notice that you only have access to the most recent message, which is either topic frown or clench. You said you only needed access to the most recent message and did not need access to messages from both paths at the same time. I am not sure that is correct, but only you know what you want.
Thank you Colin! The message can now be displayed.
But I wonder the flow only run the second path as I mentioned before. When I enable debug node '4' and '5'. There is no any message for the debug node '4' even when I disable the debug node '5'. The same situation applies after the function node as well.
Your image does not show where '4' is connected.
If there are no messages at debug node 4 then either there is nothing coming out of the clench expression node or the switch is not letting it through. I am sure you don't need me to tell you how to determine which it is.
The number that shows at the output of the clench is 0. I'll use the headset tomorrow to try the facial expressions in reality. Thank you for your helping Colin!