Node-Red JavaScript 布尔切换
Node-Red JavaScript Boolean toggle
我有一个声音传感器,它向 node-red 发送一条 mqtt 消息,node-red 向模块发送一条新的 mqtt 消息以获得电源或关闭灯。它与用于电源或关闭灯的 IR 代码相同。
我的 objective 是知道什么时候开机,什么时候关机。所以我尝试使用一个简单的布尔代码,但它不起作用,因为它不保存以前的状态。
我找到了不同的解决方案,但这些人将新状态保存在 "cookie" 上,问题是,在 Node-Red 中我们找不到这种东西。
我无法安装节点,因为我使用的是云托管的 Node-Red(https://fred.sensetecnic.com/)
我测试了很多不同的代码,我选择这个是因为它很容易理解我想做什么:
var value = true;
if (msg.payload === "sensor detection"){
if (value === true){
!value;
msg.payload = "off";
}else{
!value;
msg.payload = "on";
}
}
return msg;
我 post 在这里是因为我真的不知道该怎么做,我尝试了很多不同的代码,但现在我在 google 上找不到更多信息。我不是很好,不知道 Javascript 中的所有不同提示,所以我希望有经验的人可以给我有趣的信息以找到解决方案。
谢谢!
您需要使用上下文对象来存储函数节点执行之间的状态:
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;
这是我的最终代码:
//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;
还有一个函数只用于存储第一个值:
context.global.state = false;
这是最终的 node-red 代码:
[{"id":"f49168ea.0b6e98","type":"debug","name":"","active":true,"console":"false","complete":"false","x":1175,"y":1664,"z":"a08c8e03.5f737","wires":[]},{"id":"33e86259.cc179e","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":[["33e86259.cc179e"]]},{"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":[["33e86259.cc179e"]]},{"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"]]}]
这些页面也对我有帮助:
http://noderedguide.com/index.php/2015/11/06/node-red-lecture-6-intermediate-flows-2/
还有这个(对我的个人代码非常有用):
https://www.ibm.com/developerworks/community/blogs/hickmat/entry/nodered_powered_christmas_lights?lang=en
这个我找了好久!
谢谢你
这是我的贡献,使用多个切换功能:
/*
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;
也许更好的方法是根据每个切换函数的 ID 执行此操作。这使得它易于使用,每次切换都不需要设置。每个切换都有自己的状态,由唯一的 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代码:[{"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"]]}]
我有一个声音传感器,它向 node-red 发送一条 mqtt 消息,node-red 向模块发送一条新的 mqtt 消息以获得电源或关闭灯。它与用于电源或关闭灯的 IR 代码相同。 我的 objective 是知道什么时候开机,什么时候关机。所以我尝试使用一个简单的布尔代码,但它不起作用,因为它不保存以前的状态。
我找到了不同的解决方案,但这些人将新状态保存在 "cookie" 上,问题是,在 Node-Red 中我们找不到这种东西。 我无法安装节点,因为我使用的是云托管的 Node-Red(https://fred.sensetecnic.com/)
我测试了很多不同的代码,我选择这个是因为它很容易理解我想做什么:
var value = true;
if (msg.payload === "sensor detection"){
if (value === true){
!value;
msg.payload = "off";
}else{
!value;
msg.payload = "on";
}
}
return msg;
我 post 在这里是因为我真的不知道该怎么做,我尝试了很多不同的代码,但现在我在 google 上找不到更多信息。我不是很好,不知道 Javascript 中的所有不同提示,所以我希望有经验的人可以给我有趣的信息以找到解决方案。
谢谢!
您需要使用上下文对象来存储函数节点执行之间的状态:
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;
这是我的最终代码:
//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;
还有一个函数只用于存储第一个值:
context.global.state = false;
这是最终的 node-red 代码:
[{"id":"f49168ea.0b6e98","type":"debug","name":"","active":true,"console":"false","complete":"false","x":1175,"y":1664,"z":"a08c8e03.5f737","wires":[]},{"id":"33e86259.cc179e","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":[["33e86259.cc179e"]]},{"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":[["33e86259.cc179e"]]},{"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"]]}]
这些页面也对我有帮助: http://noderedguide.com/index.php/2015/11/06/node-red-lecture-6-intermediate-flows-2/ 还有这个(对我的个人代码非常有用): https://www.ibm.com/developerworks/community/blogs/hickmat/entry/nodered_powered_christmas_lights?lang=en
这个我找了好久! 谢谢你
这是我的贡献,使用多个切换功能:
/*
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;
也许更好的方法是根据每个切换函数的 ID 执行此操作。这使得它易于使用,每次切换都不需要设置。每个切换都有自己的状态,由唯一的 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代码:[{"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"]]}]