admin管理员组文章数量:1384341
I have a sound sensor which sends a mqtt message to node-red and node-red sends an new mqtt message to the module for power or trun off the light. It's the same IR code for power or turn off the light. My objective is to know when it's power on and when it's turn off. So i tried to use a simple boolean code but it doesn't work because it don't save the previous state.
I found different solution but the guys save the new state on a "cookie", the problem is, in Node-Red we don't find this kind of thing. I can't install nodes because i use a cloud hosted Node-Red(/)
I tested many different codes, i choosed this one because it's simple to understand what i'm trying to do:
var value = true;
if (msg.payload === "sensor detection"){
if (value === true){
!value;
msg.payload = "off";
}else{
!value;
msg.payload = "on";
}
}
return msg;
I have a sound sensor which sends a mqtt message to node-red and node-red sends an new mqtt message to the module for power or trun off the light. It's the same IR code for power or turn off the light. My objective is to know when it's power on and when it's turn off. So i tried to use a simple boolean code but it doesn't work because it don't save the previous state.
I found different solution but the guys save the new state on a "cookie", the problem is, in Node-Red we don't find this kind of thing. I can't install nodes because i use a cloud hosted Node-Red(https://fred.sensetecnic./)
I tested many different codes, i choosed this one because it's simple to understand what i'm trying to do:
var value = true;
if (msg.payload === "sensor detection"){
if (value === true){
!value;
msg.payload = "off";
}else{
!value;
msg.payload = "on";
}
}
return msg;
I post here because i really don't know how to do it, i tried many different codes and now i can't find more on google. I'm not really good and don't know all the differents tips in Javascript, so i hope someone with experience can give me interesting informations for find the solution.
Thx!
Share Improve this question asked Jan 13, 2016 at 22:19 RaoulosRaoulos 31 gold badge1 silver badge3 bronze badges4 Answers
Reset to default 3You need to use the context object to store state between execution of the function node:
context.value = context.value || true;
if (msg.payload === "sensor detection"){
if (context.value === true){
!context.value;
msg.payload = "off";
}else{
!context.value;
msg.payload = "on";
}
}
return msg;
Here is my final code:
//i changed "sensor detection" for "input"
if(msg.payload === "input"){
if(context.global.state === false){
msg.payload = true;
}else{
msg.payload = false;
}
context.global.state = msg.payload;
}
return msg;
There is an another function just for store the first value:
context.global.state = false;
Here is the final node-red code:
[{"id":"f49168ea.0b6e98","type":"debug","name":"","active":true,"console":"false","plete":"false","x":1175,"y":1664,"z":"a08c8e03.5f737","wires":[]},{"id":"33e86259179e","type":"function","name":"Main function","func":"if(msg.payload === \"input\"){\n\nif(context.global.state === false){\n msg.payload = true; \n}else{\n msg.payload = false;\n}\ncontext.global.state = msg.payload;\n}\nreturn msg;","outputs":1,"noerr":0,"x":883,"y":1712,"z":"a08c8e03.5f737","wires":[["f49168ea.0b6e98"]]},{"id":"9a2c2fc6.65d3d","type":"function","name":"First value","func":"//push \"Start\" only once after deployed.\ncontext.global.state = false;\nreturn msg;","outputs":1,"noerr":0,"x":381,"y":1714,"z":"a08c8e03.5f737","wires":[["33e86259179e"]]},{"id":"b81c9765.47e368","type":"inject","name":"Input(mqtt etc)","topic":"","payload":"input","payloadType":"string","repeat":"","crontab":"","once":false,"x":361,"y":1648,"z":"a08c8e03.5f737","wires":[["33e86259179e"]]},{"id":"c77a0446.3885f8","type":"inject","name":"","topic":"","payload":"Start","payloadType":"string","repeat":"","crontab":"","once":false,"x":146,"y":1714,"z":"a08c8e03.5f737","wires":[["9a2c2fc6.65d3d"]]}]
Those pages also helped me: http://noderedguide./index.php/2015/11/06/node-red-lecture-6-intermediate-flows-2/ And this one (it was very useful for my personal code): https://www.ibm./developerworks/munity/blogs/hickmat/entry/nodered_powered_christmas_lights?lang=en
I searched for this a very long time! Thank you for this
Here is my contribution, to use more than one toggle function:
/*
To use more toggle functions we give them
a unique name here
*/
var fname = "toggle1";
if(msg.payload === "input"){
if(context.get(fname) === false){
msg.payload = true;
}else{
msg.payload = false;
}
context.set(fname, msg.payload);
}
return msg;
Perhaps an better way to do this based on the ID of each toggle function. This makes it easy to use, no setup required per toggle. Each toggle has its own state stored by unique id.
// Toggle Function (unique to nodes id)
if(context.get(node.id) === true){
msg.payload = false;
}else{
msg.payload = true;
}
context.set(node.id, msg.payload);
return msg;
node-red code: [{"id":"2f72e700.98a7aa","type":"function","z":"56c38643.490368","name":"Toogle","func":"// Toggle Function (unique to nodes id)\nif(context.get(node.id) === true){\n msg.payload = false; \n}else{\n msg.payload = true;\n}\ncontext.set(node.id, msg.payload);\nreturn msg;","outputs":"1","noerr":0,"initialize":"","finalize":"","x":210,"y":340,"wires":[["f05ff5d3.904228"]]}]
本文标签: NodeRed JavaScript Boolean toggleStack Overflow
版权声明:本文标题:Node-Red JavaScript Boolean toggle - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744536571a2611351.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论