Hello,
I try to control my lights via xiaomi aqara cubes.
At the moment iam stuck with the usage of a functionscript which is calling an actionnodes multiple times for webrequests. (I need the state of my hue-lamps)
The call is working fine, but iam not able to use my results from the webrequest in my nodes.
Is there a way to wait until my webrequest is finished?
**Script from function-node**
// ##### Script Variablen #####
bri = 254;
sat = 254;
var tmp_var = msg.payload;
switch (msg.uebergabe.function) {
case "lampenAnyOn":
lampenGruppe = msg.uebergabe.lampenGruppe;
lampenAnyOn(lampenGruppe);
break;
default:
break;
}
// ##### Zuordnung der Variablen #####
return msg;
function lampenAus(lampenGruppe) {
for (var i = 0; i < lampen[lampenGruppe].length; i++) {
flow.set('lightnumber',lampen[lampenGruppe][i]);
flow.set('bodytext','{"on":false}');
flow.set('method',"PUT")
flow.set('art','lampenAus')
webrequest();
}
}
function lampenAn(lampenGruppe) {
for (var i = 0; i < lampen[lampenGruppe].length; i++) {
flow.set('lightnumber',lampen[lampenGruppe][i]);
flow.set('bodytext','{"on":true}');
flow.set('method',"PUT")
flow.set('art','lampenAN')
webrequest();
}
}
function lampenAnyOn(lampenGruppe) {
tmp_ret = 0;
flow.set('return_value',-1);
flow.set('return_nr',lampen[lampenGruppe].length);
for (var i = 0; i < lampen[lampenGruppe].length; i++) {
flow.set('lightnumber',lampen[lampenGruppe][i]);
flow.set('bodytext','{"on":true}');
flow.set('method',"GET")
flow.set('art','lampenAN')
flow.get("return_value", function(err, myCount) { webrequest_get2(); });
}
lampeAnyOnTimer();
}
function lampeAnyOnTimer(){
setTimeout(function (){
tmp_ret = flow.get('return_nr');
msg.return_nr = tmp_ret;
if (tmp_ret > 0){
lampeAnyOnTimer();
} else
{
tmp_var = flow.get('return_value');
msg.return_value = tmp_var;
if(tmp_var == 1){
szeneStop(lampenGruppe);
lampenAus(lampenGruppe);
} else {
lampenAn(lampenGruppe);
}
}
}, 200);
}
function webrequest_get2(value) {
var af = global.get("actionflows");
var p = af.invoke("rrot_webrequest_get",msg);
p.then(function(msg){
node.send(msg);
});
}
Script from actionflow
tmp_var = msg.payload.state.on;
var x = flow.get('return_nr');
var x = x - 1;
flow.set('return_nr',x);
if (tmp_var === true){
flow.set('return_value',1);
msg.test = "Erfolg";
}
return msg;
Flow from node
[{"id":"3ee30764.182478","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"49a1afd8.864e2","type":"function","z":"3ee30764.182478","name":"one_time_actions","func":"// ##### Script Variablen #####\n\nbri = 254;\nsat = 254;\nvar tmp_var = msg.payload;\n\n\nswitch (msg.uebergabe.function) {\n case \"lampenAnyOn\":\n lampenGruppe = msg.uebergabe.lampenGruppe;\n lampenAnyOn(lampenGruppe);\n break;\n default:\n break;\n }\n \n \n\n// ##### Zuordnung der Variablen #####\n\nreturn msg;\n\n\n\n// ##### Script Umgebaut #####\nfunction lampenAus(lampenGruppe) {\n for (var i = 0; i < lampen[lampenGruppe].length; i++) {\n \n flow.set('lightnumber',lampen[lampenGruppe][i]);\n flow.set('bodytext','{\"on\":false}');\n flow.set('method',\"PUT\")\n flow.set('art','lampenAus')\n \n webrequest();\n }\n}\n\nfunction lampenAn(lampenGruppe) {\n for (var i = 0; i < lampen[lampenGruppe].length; i++) {\n \n flow.set('lightnumber',lampen[lampenGruppe][i]);\n flow.set('bodytext','{\"on\":true}');\n flow.set('method',\"PUT\")\n flow.set('art','lampenAN')\n \n webrequest();\n }\n}\n\nfunction lampenAnyOn(lampenGruppe) {\n tmp_ret = 0;\n flow.set('return_value',-1);\n flow.set('return_nr',lampen[lampenGruppe].length);\n for (var i = 0; i < lampen[lampenGruppe].length; i++) {\n \n flow.set('lightnumber',lampen[lampenGruppe][i]);\n flow.set('bodytext','{\"on\":true}');\n flow.set('method',\"GET\")\n flow.set('art','lampenAN')\n \n webrequest_get2();\n }\n lampeAnyOnTimer();\n}\n\nfunction lampeAnyOnTimer(){\n \n setTimeout(function (){\n tmp_ret = flow.get('return_nr');\n msg.return_nr = tmp_ret;\n \n if (tmp_ret > 0){\n lampeAnyOnTimer(); \n } else\n {\n tmp_var = flow.get('return_value'); \n msg.return_value = tmp_var;\n if(tmp_var == 1){ \n szeneStop(lampenGruppe); \n lampenAus(lampenGruppe);\n } else {\n lampenAn(lampenGruppe); \n }\n }\n \n }, 200);\n}\n\n\n\nfunction webrequest_get2(value) {\nvar af = global.get(\"actionflows\");\nvar p = af.invoke(\"rrot_webrequest_get\",msg);\n\n p.then(function(msg){\n node.send(msg);\n });\n}\n\n\n\n\n\n","outputs":1,"noerr":0,"x":596,"y":160,"wires":[["3b5928f0.5bdb48"]]},{"id":"fda3ef15.9699e","type":"actionflows_in","z":"3ee30764.182478","name":"hue_lampen_einmal in","priority":"50","links":[],"scope":"global","x":311.87489128112793,"y":160.1210813522339,"wires":[["49a1afd8.864e2"]]},{"id":"3b5928f0.5bdb48","type":"actionflows_out","z":"3ee30764.182478","name":"hue_lampen_einmal out","links":[],"x":887.874870300293,"y":159.09766006469727,"wires":[]},{"id":"3384d6c7.266bea","type":"http request","z":"3ee30764.182478","name":"Webrequest","method":"GET","ret":"obj","url":"","tls":"","x":790,"y":297,"wires":[["c83f7cc0.506d6"]]},{"id":"28d9bbb7.9805b4","type":"function","z":"3ee30764.182478","name":"set path","func":"msg.topic = {};\nmsg.headers = {};\nmsg.url = \"http://xx.xxx.xxx.xx/api/api-user-id/lights/\"+flow.get('lightnumber');\n\nmsg.payload = flow.get('bodytext')\nreturn msg;","outputs":1,"noerr":0,"x":561.9998931884766,"y":296.9993371963501,"wires":[["3384d6c7.266bea"]]},{"id":"af4d015a.64cf3","type":"actionflows_in","z":"3ee30764.182478","name":"rrot_webrequest_get in","priority":"50","links":[],"scope":"protected","x":315.8670654296875,"y":297.108642578125,"wires":[["28d9bbb7.9805b4"]]},{"id":"5de4a98f.ba79f8","type":"actionflows_out","z":"3ee30764.182478","name":"rrot_webrequest_get out","links":[],"x":1465.980842590332,"y":298.66248321533203,"wires":[]},{"id":"c83f7cc0.506d6","type":"function","z":"3ee30764.182478","name":"Returnvalue bei Method GET","func":"tmp_var = msg.payload.state.on;\nvar x = flow.get('return_nr');\nvar x = x - 1;\nflow.set('return_nr',x);\nif (tmp_var === true){\n flow.set('return_value',1);\n msg.test = \"Erfolg\";\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":1123.859260559082,"y":297.34765625,"wires":[["5de4a98f.ba79f8"]]}]