NEWS
[Unsolved but satisfied] For loop inside for loop
-
Hi all,
So I'm synchronising the hmip adapter with the s7 adapter and all works flawlessly.
Below my working example:
function SyncProfile() { log("Heater profile list:"); var Heater = getObject("enum.functions.HeatingUp.profile").common.members; var PLC = getObject("enum.functions.PlcHeatingUp.profile").common.members; for(let i = 0; i < Heater.length; i++) { on({id: [].concat([PLC[i]]),change: "ne"},async function (obj) { let value = obj.state.val; let oldValue = obj.oldState.val; setStateDelayed(Heater[i], getState(PLC[i]).val, false, parseInt(((0) || "").toString(), 10), false); return; });on({id: [].concat([Heater[i]]),change: "ne"},async function (obj) { let value = obj.state.val; let oldValue = obj.oldState.val; setStateDelayed(PLC[i], getState(Heater[i]).val, false, parseInt(((0) || "").toString(), 10), false); await wait(1500); }); } } SyncProfile();
Now I am having 7 instances (more coming) running the same code so I want to simplify it by running a for loop inside a for loop like this:
function Sync2Way() { const FunctionHeater = [ "enum.functions.HeatingUp.setpoint", "enum.functions.HeatingUp.actual", "enum.functions.HeatingUp.hum", "enum.functions.HeatingUp.winStat", "enum.functions.HeatingUp.profile", "enum.functions.HeatingUp.boost", "enum.functions.HeatingUp.boostTime"]; const FunctionPLC = [ "enum.functions.PlcHeatingUp.setpoint", "enum.functions.PlcHeatingUp.actual", "enum.functions.PlcHeatingUp.hum", "enum.functions.PlcHeatingUp.winStat", "enum.functions.PlcHeatingUp.profile", "enum.functions.PlcHeatingUp.boost", "enum.functions.PlcHeatingUp.boostTime"]; for(let j = 0; j < Function.length; j++) { var Heater = getObject(FunctionHeater[j]).common.members; var PLC = getObject(FunctionPLC[j]).common.members; for(let i = 0; i < Heater.length; i++) { on({id: [].concat([PLC[i]]),change: "ne"},async function (obj) { let value = obj.state.val; let oldValue = obj.oldState.val; setStateDelayed(Heater[i], getState(PLC[i]).val, false, parseInt(((0) || "").toString(), 10), false); //return; }); on({id: [].concat([Heater[i]]),change: "ne"},async function (obj) { let value = obj.state.val; let oldValue = obj.oldState.val; setStateDelayed(PLC[i], getState(Heater[i]).val, false, parseInt(((0) || "").toString(), 10), false); //await wait(1500); }); } } } Sync2Way();
This is the log:
13:47:08.007 info javascript.0 (2003) Start javascript script.js.hmipS7BoostSync 13:47:08.035 silly admin.0 (1472) States system redis pmessage io.log.system.adapter.admin.0/io.log.system.adapter.admin.0:{"severity":"info","ts":1691149628007,"message":"javascript.0 (2003) Start javascript script.js.hmipS7BoostSync","from":"javascript.0","_id":76242350} 13:47:08.032 info javascript.0 (2003) script.js.hmipS7BoostSync: registered 14 subscriptions, 0 schedules, 0 messages, 0 logs and 0 file subscriptions 13:47:08.037 silly admin.0 (1472) States system redis pmessage io.log.system.adapter.admin.0/io.log.system.adapter.admin.0:{"severity":"info","ts":1691149628032,"message":"javascript.0 (2003) script.js.hmipS7BoostSync: registered 14 subscriptions, 0 schedules, 0 messages, 0 logs and 0 file subscriptions","from":"javascript.0","_id":76242351} 13:47:23.255 silly admin.0 (1472) Objects user redis pmessage */cfg.o.script.js.hmipS7BoostSync:{"common":{"name":"hmipS7BoostSync","expert":true,"engineType":"Javascript/js","engine":"system.adapter.javascript.0","source":"function Sync2Way() {\r\n const FunctionHeater = [\r\n \"enum.functions.HeatingUp.setpoint\",\r\n \"enum.functions.HeatingUp.actual\", \r\n \"enum.functions.HeatingUp.hum\", \r\n \"enum.functions.HeatingUp.winStat\", \r\n \"enum.functions.HeatingUp.profile\", \r\n \"enum.functions.HeatingUp.boost\", \r\n \"enum.functions.HeatingUp.boostTime\"];\r\n\r\n const FunctionPLC = [\r\n \"enum.functions.PlcHeatingUp.setpoint\",\r\n \"enum.functions.PlcHeatingUp.actual\", \r\n \"enum.functions.PlcHeatingUp.hum\", \r\n \"enum.functions.PlcHeatingUp.winStat\", \r\n \"enum.functions.PlcHeatingUp.profile\", \r\n \"enum.functions.PlcHeatingUp.boost\", \r\n \"enum.functions.PlcHeatingUp.boostTime\"];\r\n\r\n for(let j = 0; j < Function.length; j++) {\r\n var Heater = getObject(FunctionHeater[j]).common.members;\r\n var PLC = getObject(FunctionPLC[j]).common.members;\r\n for(let i = 0; i < Heater.length; i++) {\r\n on({id: [].concat([PLC[i]]),change: \"ne\"},async function (obj) {\r\n let value = obj.state.val;\r\n let oldValue = obj.oldState.val;\r\n setStateDelayed(Heater[i], getState(PLC[i]).val, false, parseInt(((0) || \"\").toString(), 10), false);\r\n //return;\r\n });\r\n on({id: [].concat([Heater[i]]),change: \"ne\"},async function (obj) {\r\n let value = obj.state.val;\r\n let oldValue = obj.oldState.val;\r\n setStateDelayed(PLC[i], getState(Heater[i]).val, false, parseInt(((0) || \"\").toString(), 10), false);\r\n //await wait(1500);\r\n });\r\n }\r\n }\r\n}\r\n\r\nSync2Way();","debug":false,"verbose":false,"enabled":false},"type":"script","from":"system.adapter.admin.0","user":"system.user.admin","ts":1691149643250,"_id":"script.js.hmipS7BoostSync","acl":{"object":1636,"owner":"system.user.admin","ownerGroup":"system.group.administrator"}} 13:47:23.260 info javascript.0 (2003) Stop script script.js.hmipS7BoostSync
As you can guess, because of my post here, it's not working...
Any idea's of what I'm doing wrong?
Thanks in advance!
P.s.: PLC guy here and my scripting knowledge started like yesterday or so, so please explanations for dummies
-
-
@paul53 Correct, I saw I only had a hmip array and not a PLC one. Forgot to change the "for code"...
Thanks, I'll give it another try -
@paul53 So fixed the length issue but now I'm having the errors as I had before when I only had one array.
function Sync2Way() { const FunctionHeater = [ "enum.functions.HeatingUp.setpoint", "enum.functions.HeatingUp.actual", "enum.functions.HeatingUp.hum", "enum.functions.HeatingUp.winStat", "enum.functions.HeatingUp.profile", "enum.functions.HeatingUp.boost", "enum.functions.HeatingUp.boostTime"]; const FunctionPLC = [ "enum.functions.PlcHeatingUp.setpoint", "enum.functions.PlcHeatingUp.actual", "enum.functions.PlcHeatingUp.hum", "enum.functions.PlcHeatingUp.winStat", "enum.functions.PlcHeatingUp.profile", "enum.functions.PlcHeatingUp.boost", "enum.functions.PlcHeatingUp.boostTime"]; for(let j = 0; j < FunctionHeater.length; j++) { var Heater = getObject(FunctionHeater[j]).common.members; var PLC = getObject(FunctionPLC[j]).common.members; for(let i = 0; i < Heater.length; i++) { on({id: [].concat([PLC[i]]),change: "ne"},async function (obj) { let value = obj.state.val; let oldValue = obj.oldState.val; setStateDelayed(Heater[i], getState(PLC[i]).val, false, parseInt(((0) || "").toString(), 10), false); //return; }); on({id: [].concat([Heater[i]]),change: "ne"},async function (obj) { let value = obj.state.val; let oldValue = obj.oldState.val; setStateDelayed(PLC[i], getState(Heater[i]).val, false, parseInt(((0) || "").toString(), 10), false); //await wait(1500); }); } } } Sync2Way();
14:42:22.207 info javascript.0 (2003) Start javascript script.js.hmipS7BoostSync 14:42:22.220 silly admin.0 (1472) States system redis pmessage io.log.system.adapter.admin.0/io.log.system.adapter.admin.0:{"severity":"info","ts":1691152942207,"message":"javascript.0 (2003) Start javascript script.js.hmipS7BoostSync","from":"javascript.0","_id":76242388} 14:42:22.218 info javascript.0 (2003) script.js.hmipS7BoostSync: registered 86 subscriptions, 0 schedules, 0 messages, 0 logs and 0 file subscriptions 14:42:22.222 silly admin.0 (1472) States system redis pmessage io.log.system.adapter.admin.0/io.log.system.adapter.admin.0:{"severity":"info","ts":1691152942218,"message":"javascript.0 (2003) script.js.hmipS7BoostSync: registered 86 subscriptions, 0 schedules, 0 messages, 0 logs and 0 file subscriptions","from":"javascript.0","_id":76242389} 14:42:25.025 warn javascript.0 (2003) at Object.<anonymous> (script.js.hmipS7BoostSync:33:41) 14:42:25.029 error javascript.0 (2003) script.js.hmipS7BoostSync: TypeError: Cannot read properties of undefined (reading 'match') 14:42:25.037 silly admin.0 (1472) States system redis pmessage io.log.system.adapter.admin.0/io.log.system.adapter.admin.0:{"severity":"warn","ts":1691152945025,"message":"javascript.0 (2003) at Object.<anonymous> (script.js.hmipS7BoostSync:33:41)","from":"javascript.0","_id":76242391} 14:42:25.040 silly admin.0 (1472) States system redis pmessage io.log.system.adapter.admin.0/io.log.system.adapter.admin.0:{"severity":"error","ts":1691152945029,"message":"javascript.0 (2003) script.js.hmipS7BoostSync: TypeError: Cannot read properties of undefined (reading 'match')","from":"javascript.0","_id":76242396} 14:42:25.030 error javascript.0 (2003) at Object.<anonymous> (script.js.hmipS7BoostSync:33:17) 14:42:25.049 silly admin.0 (1472) States system redis pmessage io.log.system.adapter.admin.0/io.log.system.adapter.admin.0:{"severity":"error","ts":1691152945030,"message":"javascript.0 (2003) at Object.<anonymous> (script.js.hmipS7BoostSync:33:17)","from":"javascript.0","_id":76242400} 14:42:28.554 silly admin.0 (1472) Objects user redis pmessage */cfg.o.script.js.hmipS7BoostSync:{"common":{"name":"hmipS7BoostSync","expert":true,"engineType":"Javascript/js","engine":"system.adapter.javascript.0","source":"function Sync2Way() {\r\n const FunctionHeater = [\r\n \"enum.functions.HeatingUp.setpoint\",\r\n \"enum.functions.HeatingUp.actual\", \r\n \"enum.functions.HeatingUp.hum\", \r\n \"enum.functions.HeatingUp.winStat\", \r\n \"enum.functions.HeatingUp.profile\", \r\n \"enum.functions.HeatingUp.boost\", \r\n \"enum.functions.HeatingUp.boostTime\"];\r\n\r\n const FunctionPLC = [\r\n \"enum.functions.PlcHeatingUp.setpoint\",\r\n \"enum.functions.PlcHeatingUp.actual\", \r\n \"enum.functions.PlcHeatingUp.hum\", \r\n \"enum.functions.PlcHeatingUp.winStat\", \r\n \"enum.functions.PlcHeatingUp.profile\", \r\n \"enum.functions.PlcHeatingUp.boost\", \r\n \"enum.functions.PlcHeatingUp.boostTime\"];\r\n\r\n for(let j = 0; j < FunctionHeater.length; j++) {\r\n var Heater = getObject(FunctionHeater[j]).common.members;\r\n var PLC = getObject(FunctionPLC[j]).common.members;\r\n for(let i = 0; i < Heater.length; i++) {\r\n on({id: [].concat([PLC[i]]),change: \"ne\"},async function (obj) {\r\n let value = obj.state.val;\r\n let oldValue = obj.oldState.val;\r\n setStateDelayed(Heater[i], getState(PLC[i]).val, false, parseInt(((0) || \"\").toString(), 10), false);\r\n //return;\r\n });\r\n on({id: [].concat([Heater[i]]),change: \"ne\"},async function (obj) {\r\n let value = obj.state.val;\r\n let oldValue = obj.oldState.val;\r\n setStateDelayed(PLC[i], getState(Heater[i]).val, false, parseInt(((0) || \"\").toString(), 10), false);\r\n //await wait(1500);\r\n });\r\n }\r\n }\r\n}\r\n\r\nSync2Way();","debug":false,"verbose":false,"enabled":false},"type":"script","from":"system.adapter.admin.0","user":"system.user.admin","ts":1691152948552,"_id":"script.js.hmipS7BoostSync","acl":{"object":1636,"owner":"system.user.admin","ownerGroup":"system.group.administrator"}} 14:42:28.556 info javascript.0 (2003) Stop script script.js.hmipS7BoostSync
Any idea?
-
@martiman
Try:on(PLC[i], function (dp) { if(dp.state.c != scriptName) setState(Heater[i], dp.state.val); }); on(Heater[i], function (dp) { if(dp.state.c != scriptName) setState(PLC[i], dp.state.val); });
-
@martiman sagte: Any idea?
No. All 14 arrays of members are complete?
-
@paul53 said in For loop inside for loop:
on(PLC[i], function (dp) { if(dp.state.c != scriptName) setState(Heater[i], dp.state.val); }); on(Heater[i], function (dp) { if(dp.state.c != scriptName) setState(PLC[i], dp.state.val); });
I'm using blockly and use the JS code there to puzzle this together...
So thanks, I would never have come to this adjustment.Well less warnings but still:
15:01:29.667 info javascript.0 (2003) Start javascript script.js.hmipS7BoostSync 15:01:29.687 silly admin.0 (1472) States system redis pmessage io.log.system.adapter.admin.0/io.log.system.adapter.admin.0:{"severity":"info","ts":1691154089667,"message":"javascript.0 (2003) Start javascript script.js.hmipS7BoostSync","from":"javascript.0","_id":76242545} 15:01:29.684 info javascript.0 (2003) script.js.hmipS7BoostSync: registered 86 subscriptions, 0 schedules, 0 messages, 0 logs and 0 file subscriptions 15:01:29.689 silly admin.0 (1472) States system redis pmessage io.log.system.adapter.admin.0/io.log.system.adapter.admin.0:{"severity":"info","ts":1691154089684,"message":"javascript.0 (2003) script.js.hmipS7BoostSync: registered 86 subscriptions, 0 schedules, 0 messages, 0 logs and 0 file subscriptions","from":"javascript.0","_id":76242546} 15:01:31.907 error javascript.0 (2003) at Object.<anonymous> (script.js.hmipS7BoostSync:28:46) 15:01:31.947 silly admin.0 (1472) States system redis pmessage io.log.system.adapter.admin.0/io.log.system.adapter.admin.0:{"severity":"error","ts":1691154091907,"message":"javascript.0 (2003) at Object.<anonymous> (script.js.hmipS7BoostSync:28:46)","from":"javascript.0","_id":76242550} 15:01:36.127 silly admin.0 (1472) Objects user redis pmessage */cfg.o.script.js.hmipS7BoostSync:{"common":{"name":"hmipS7BoostSync","expert":true,"engineType":"Javascript/js","engine":"system.adapter.javascript.0","source":"function Sync2Way() {\r\n const FunctionHeater = [\r\n \"enum.functions.HeatingUp.setpoint\",\r\n \"enum.functions.HeatingUp.actual\", \r\n \"enum.functions.HeatingUp.hum\", \r\n \"enum.functions.HeatingUp.winStat\", \r\n \"enum.functions.HeatingUp.profile\", \r\n \"enum.functions.HeatingUp.boost\", \r\n \"enum.functions.HeatingUp.boostTime\"];\r\n\r\n const FunctionPLC = [\r\n \"enum.functions.PlcHeatingUp.setpoint\",\r\n \"enum.functions.PlcHeatingUp.actual\", \r\n \"enum.functions.PlcHeatingUp.hum\", \r\n \"enum.functions.PlcHeatingUp.winStat\", \r\n \"enum.functions.PlcHeatingUp.profile\", \r\n \"enum.functions.PlcHeatingUp.boost\", \r\n \"enum.functions.PlcHeatingUp.boostTime\"];\r\n\r\n for(let j = 0; j < FunctionHeater.length; j++) {\r\n var Heater = getObject(FunctionHeater[j]).common.members;\r\n var PLC = getObject(FunctionPLC[j]).common.members;\r\n for(let i = 0; i < Heater.length; i++) {\r\n on(PLC[i], function (dp) {\r\n if(dp.state.c != scriptName) setState(Heater[i], dp.state.val);\r\n });\r\n on(Heater[i], function (dp) {\r\n if(dp.state.c != scriptName) setState(PLC[i], dp.state.val);\r\n });\r\n }\r\n }\r\n}\r\n\r\nSync2Way();","debug":false,"verbose":false,"enabled":false},"type":"script","from":"system.adapter.admin.0","user":"system.user.admin","ts":1691154096121,"_id":"script.js.hmipS7BoostSync","acl":{"object":1636,"owner":"system.user.admin","ownerGroup":"system.group.administrator"}} 15:01:36.129 info javascript.0 (2003) Stop script script.js.hmipS7BoostSync
To give you an idea, this is one of the arrays of the bool on the hmip side of what I try to sync to the PLC:
{ "type": "enum", "common": { "name": "Boost", "enabled": true, "color": false, "desc": "", "members": [ "hmip.0.groups.b29b9bef-cdda-491c-b664-2d8edcd540a8.boostMode", "hmip.0.groups.1d81098a-ef8e-4aaf-bdac-871e3de74127.boostMode", "hmip.0.groups.0b3141dc-92d8-4231-ba4e-d50128ea30cf.boostMode", "hmip.0.groups.47dbff3e-8ee5-436d-9bcf-11a40bb5801d.boostMode", "hmip.0.groups.44690c2e-d317-4969-aef0-d50c6041a3d5.boostMode", "hmip.0.groups.f28a0397-3999-4759-a7d7-63446e4131cf.boostMode", "hmip.0.groups.e181e361-bd13-4cb0-873d-5990d35bb1bc.boostMode" ] }, "native": {}, "_id": "enum.functions.HeatingUp.boost", "acl": { "object": 1636, "owner": "system.user.admin", "ownerGroup": "system.group.administrator" }, "from": "system.adapter.admin.0", "user": "system.user.admin", "ts": 1691077099049 }
This is the other side:
{ "type": "enum", "common": { "name": "Boost", "enabled": true, "color": false, "desc": "", "members": [ "s7.2.DBs.DB5.GR_Therm_Boost", "s7.2.DBs.DB5.GR_Therm_Bath_Boost", "s7.2.DBs.DB5.CRD_Therm_Boost", "s7.2.DBs.DB5.LNG_Therm_Boost", "s7.2.DBs.DB5.KR1_Therm_Boost", "s7.2.DBs.DB5.KR2_Therm_Boost", "s7.2.DBs.DB5.KRB_Therm_Boost" ] }, "native": {}, "_id": "enum.functions.PlcHeatingUp.boost", "acl": { "object": 1636, "owner": "system.user.admin", "ownerGroup": "system.group.administrator" }, "from": "system.adapter.admin.0", "user": "system.user.admin", "ts": 1691077151135 }
Sync in both directions gives me the error.
-
@martiman sagte: Sync in both directions gives me the error.
Check all IDs:
log('Heater: ' + Heater[i] + ', PLC: ' + PLC[i]); on(PLC[i], function (dp) { if(dp.state.c != scriptName) setState(Heater[i], dp.state.val); }); on(Heater[i], function (dp) { if(dp.state.c != scriptName) setState(PLC[i], dp.state.val); });
The data point type is identical on both sides?
-
@paul53 Hi, having a little mental breakdown at the moment...
Like I wrote in the beginning, all instances work in their own separate script.
Now I started checking them again and ioBroker is throwing me GUI errors and all I had this morning doesn't work anymore...
Even if I reroute all to dummy tags in Userdata_0 it doesn't work anymore.
Also after numerous reboots and restoring backups.
So uhm, I'm about to open a and call it a day... -
@paul53 Well I kicked out Edge and installed Chrome, less GUI errors now.
I also found that S7STRING in the S7 adapter needs to be declared as 32 length which was the main error.So I still have 2 issues:
-
Loop inside loop still not working
-
Edge detection is responding to being sync'd by the other one
First the loop in loop:
Code below works flawlessly (besides the updater being updated and so on)
function SyncSetpoint() { var Heater = getObject("enum.functions.HeatingUp.setpoint").common.members; var PLC = getObject("enum.functions.PlcHeatingUp.setpoint").common.members; for(let i = 0; i < Heater.length; i++) { on(PLC[i], function (dp) { if(dp.state.c != scriptName) setState(Heater[i], dp.state.val); return; }); on(Heater[i], function (dp) { if(dp.state.c != scriptName) setState(PLC[i], dp.state.val); }); } } function SyncProfile() { var Heater = getObject("enum.functions.HeatingUp.profile").common.members; var PLC = getObject("enum.functions.PlcHeatingUp.profile").common.members; for(let i = 0; i < Heater.length; i++) { on(PLC[i], function (dp) { if(dp.state.c != scriptName) setState(Heater[i], dp.state.val); return; }); on(Heater[i], function (dp) { if(dp.state.c != scriptName) setState(PLC[i], dp.state.val); }); } } function SyncBoost() { var Heater = getObject("enum.functions.HeatingUp.boost").common.members; var PLC = getObject("enum.functions.PlcHeatingUp.boost").common.members; for(let i = 0; i < Heater.length; i++) { on(PLC[i], function (dp) { if(dp.state.c != scriptName) setState(Heater[i], dp.state.val); return; }); on(Heater[i], function (dp) { if(dp.state.c != scriptName) setState(PLC[i], dp.state.val); }); } } function SyncBoostTime() { var Heater = getObject("enum.functions.HeatingUp.boostTime").common.members; var PLC = getObject("enum.functions.PlcHeatingUp.boostTime").common.members; for(let i = 0; i < Heater.length; i++) { on(PLC[i], function (dp) { if(dp.state.c != scriptName) setState(Heater[i], dp.state.val); return; }); on(Heater[i], function (dp) { if(dp.state.c != scriptName) setState(PLC[i], dp.state.val); }); } } SyncSetpoint(); SyncProfile(); SyncBoost(); SyncBoostTime();
The code below does not:
function Sync2Way() { const FunctionHeater = [ "enum.functions.HeatingUp.setpoint", "enum.functions.HeatingUp.profile", "enum.functions.HeatingUp.boost", "enum.functions.HeatingUp.boostTime" ]; const FunctionPLC = [ "enum.functions.PlcHeatingUp.setpoint", "enum.functions.PlcHeatingUp.profile", "enum.functions.PlcHeatingUp.boost", "enum.functions.PlcHeatingUp.boostTime" ]; for(let j = 0; j < FunctionHeater.length; j++) { var Heater = getObject(FunctionHeater[j]).common.members; var PLC = getObject(FunctionPLC[j]).common.members; for(let i = 0; i < Heater.length; i++) { log('Heater: ' + Heater[i] + ', PLC: ' + PLC[i]); on(PLC[i], function (dp) { if(dp.state.c != scriptName) setState(Heater[i], dp.state.val); return; }); on(Heater[i], function (dp) { if(dp.state.c != scriptName) setState(PLC[i], dp.state.val); }); } } } Sync2Way();
Strange thing is that the setpoint (REAL) and the boost time (INT) don't trigger the "on" function, no response in the log at all.
The profile (STRING) and boost (BOOL) do and give following error.17:07:08.747 warn javascript.0 (1494) at Object.<anonymous> (script.js.Test:23:46) 17:07:08.755 silly admin.0 (1466) States system redis pmessage io.log.system.adapter.admin.0/io.log.system.adapter.admin.0:{"severity":"warn","ts":1691248028747,"message":"javascript.0 (1494) at Object.<anonymous> (script.js.Test:23:46)","from":"javascript.0","_id":14262598} 17:07:22.639 warn javascript.0 (1494) at Object.<anonymous> (script.js.Test:23:46) 17:07:22.644 silly admin.0 (1466) States system redis pmessage io.log.system.adapter.admin.0/io.log.system.adapter.admin.0:{"severity":"warn","ts":1691248042639,"message":"javascript.0 (1494) at Object.<anonymous> (script.js.Test:23:46)","from":"javascript.0","_id":14262606}
Edge detection is responding to being triggered
Your alteration works very smooth but originally I put in a wait command because the edge detection gets in a loop.
For the Heater edge sync I added "return;" which helps a bit to avoid a loop but it still fluctuates a couple of times when values are changed ON and OFF.
The reason for the loop is the hmip device communication, when you activate boost (also in their iOS app, not just ioBroker), it will go on-off-on.Any idea how to block the "on(Heater[i]..." when it's being updated by the "on(PLC[i]..." and vice versa?
Found some delay and wait instructions but that will pause the whole script no?
I'd like to only cancel the feedback read for like say 5 seconds after the value was sync'd.In S7 I would simply put an ODT before the edge, triggered by the other edge
-
-
@martiman sagte: Any idea how to block the "on(Heater[i]..." when it's being updated by the "on(PLC[i]..." and vice versa?
if(dp.state.c != scriptName)
is to prevent the trigger infinite loop.
-
@paul53 said in For loop inside for loop:
is to prevent the trigger infinite loop.
Well it's apparently not enough
I think the arrays are not being synchronised in the for loop as I got this log:
javascript.0 2023-08-05 17:41:51.811 warn You are assigning a boolean to the state "hmip.0.groups.47dbff3e-8ee5-436d-9bcf-11a40bb5801d.boostDuration" which expects a number. Please fix your code to use a number or change the state type to boolean. This warning might become an error in future versions.
-
@martiman sagte: The code below does not:
Try to slow down the loop with async ( line 1) and await wait(100).
async function Sync2Way() { const FunctionHeater = [ "enum.functions.HeatingUp.setpoint", "enum.functions.HeatingUp.actual", "enum.functions.HeatingUp.hum", "enum.functions.HeatingUp.winStat", "enum.functions.HeatingUp.profile", "enum.functions.HeatingUp.boost", "enum.functions.HeatingUp.boostTime"]; const FunctionPLC = [ "enum.functions.PlcHeatingUp.setpoint", "enum.functions.PlcHeatingUp.actual", "enum.functions.PlcHeatingUp.hum", "enum.functions.PlcHeatingUp.winStat", "enum.functions.PlcHeatingUp.profile", "enum.functions.PlcHeatingUp.boost", "enum.functions.PlcHeatingUp.boostTime"]; for(let j = 0; j < FunctionHeater.length; j++) { var Heater = getObject(FunctionHeater[j]).common.members; var PLC = getObject(FunctionPLC[j]).common.members; for(let i = 0; i < Heater.length; i++) { log('Heater: ' + Heater[i] + ', PLC: ' + PLC[i]); on(PLC[i], function (dp) { if(dp.state.c != scriptName) setState(Heater[i], dp.state.val); }); on(Heater[i], function (dp) { if(dp.state.c != scriptName) setState(PLC[i], dp.state.val); }); await wait(100); } } } Sync2Way();
-
@paul53 said in For loop inside for loop:
async function Sync2Way() { const FunctionHeater = [ "enum.functions.HeatingUp.setpoint", "enum.functions.HeatingUp.actual", "enum.functions.HeatingUp.hum", "enum.functions.HeatingUp.winStat", "enum.functions.HeatingUp.profile", "enum.functions.HeatingUp.boost", "enum.functions.HeatingUp.boostTime"]; const FunctionPLC = [ "enum.functions.PlcHeatingUp.setpoint", "enum.functions.PlcHeatingUp.actual", "enum.functions.PlcHeatingUp.hum", "enum.functions.PlcHeatingUp.winStat", "enum.functions.PlcHeatingUp.profile", "enum.functions.PlcHeatingUp.boost", "enum.functions.PlcHeatingUp.boostTime"]; for(let j = 0; j < FunctionHeater.length; j++) { var Heater = getObject(FunctionHeater[j]).common.members; var PLC = getObject(FunctionPLC[j]).common.members; for(let i = 0; i < Heater.length; i++) { log('Heater: ' + Heater[i] + ', PLC: ' + PLC[i]); on(PLC[i], function (dp) { if(dp.state.c != scriptName) setState(Heater[i], dp.state.val); }); on(Heater[i], function (dp) { if(dp.state.c != scriptName) setState(PLC[i], dp.state.val); }); await wait(100); } } } Sync2Way();
Thanks, but still same issue.
If I use an array of only 2 and swap the order, it works
-
@paul53 BTW thanks for the async declaration and await!
I read await was not possible in a sync function so skipped trying it.If I put await for 1000ms it blocks the back and forth updating due to the hmip communication inconsistency.
Again, I'm really newby when it comes to scripting -
So I decided to drop the loop inside loop as the arrays were not synchronising.
The time to troubleshoot would be more than the loop would save me time from programming the full code bit by bit.
Just for future reference if someone wants to do the same:
The "First_Run" bit is to fill the PLC DB so it doesn't overwrite the actual settings with an empty DB.async function SyncSetpoint() { var Heater = getObject("enum.functions.HeatingUp.setpoint").common.members; var PLC = getObject("enum.functions.PlcHeatingUp.setpoint").common.members; for(let i = 0; i < Heater.length; i++) { on(PLC[i], function (dp) { if(dp.state.c != scriptName) setState(Heater[i], dp.state.val); return; }); on(Heater[i], function (dp) { if(dp.state.c != scriptName) setState(PLC[i], dp.state.val); }); await wait(1000); } } async function SyncProfile() { var Heater = getObject("enum.functions.HeatingUp.profile").common.members; var PLC = getObject("enum.functions.PlcHeatingUp.profile").common.members; for(let i = 0; i < Heater.length; i++) { on(PLC[i], function (dp) { if(dp.state.c != scriptName) setState(Heater[i], dp.state.val); return; }); on(Heater[i], function (dp) { if(dp.state.c != scriptName) setState(PLC[i], dp.state.val); }); await wait(1000); } } async function SyncBoost() { var Heater = getObject("enum.functions.HeatingUp.boost").common.members; var PLC = getObject("enum.functions.PlcHeatingUp.boost").common.members; for(let i = 0; i < Heater.length; i++) { on(PLC[i], function (dp) { if(dp.state.c != scriptName) setState(Heater[i], dp.state.val); return; }); on(Heater[i], function (dp) { if(dp.state.c != scriptName) setState(PLC[i], dp.state.val); }); await wait(1000); } } async function SyncBoostTime() { var Heater = getObject("enum.functions.HeatingUp.boostTime").common.members; var PLC = getObject("enum.functions.PlcHeatingUp.boostTime").common.members; for(let i = 0; i < Heater.length; i++) { on(PLC[i], function (dp) { if(dp.state.c != scriptName) setState(Heater[i], dp.state.val); return; }); on(Heater[i], function (dp) { if(dp.state.c != scriptName) setState(PLC[i], dp.state.val); }); await wait(1000); } } function SyncActualTemp() { var Heater = getObject("enum.functions.HeatingUp.actual").common.members; var PLC = getObject("enum.functions.PlcHeatingUp.actual").common.members; for(let i = 0; i < Heater.length; i++) { on(Heater[i], function (dp) { if(dp.state.c != scriptName) setState(PLC[i], dp.state.val); }); } } function SyncActualHum() { var Heater = getObject("enum.functions.HeatingUp.hum").common.members; var PLC = getObject("enum.functions.PlcHeatingUp.hum").common.members; for(let i = 0; i < Heater.length; i++) { on(Heater[i], function (dp) { if(dp.state.c != scriptName) setState(PLC[i], dp.state.val); }); } } function SyncWinStat() { var connStatHeater = getObject("enum.functions.HeatingUp.connStatWin").common.members; var connStatPLC = getObject("enum.functions.PlcHeatingUp.connStatWin").common.members; var winStatHeater = getObject("enum.functions.HeatingUp.winStat").common.members; var winStatPLC = getObject("enum.functions.PlcHeatingUp.winStat").common.members; var batStatHeater = getObject("enum.functions.HeatingUp.batStatWin").common.members; var batStatPLC = getObject("enum.functions.PlcHeatingUp.batStatWin").common.members; for(let i = 0; i < connStatPLC.length; i++) { on(connStatHeater[i], function (dp) { if(dp.state.c != scriptName) setState(connStatPLC[i], dp.state.val); }); if (getState(connStatHeater[i]).val != true) { on(winStatHeater[i], function (dp) { if(dp.state.c != scriptName) setState(winStatPLC[i], dp.state.val); }); on(batStatHeater[i], function (dp) { if(dp.state.c != scriptName) setState(batStatPLC[i], dp.state.val); }); } } } function SyncThermStat() { var connStatHeater = getObject("enum.functions.HeatingUp.connStatTherm").common.members; var connStatPLC = getObject("enum.functions.PlcHeatingUp.connStatTherm").common.members; var batStatHeater = getObject("enum.functions.HeatingUp.batStatTherm").common.members; var batStatPLC = getObject("enum.functions.PlcHeatingUp.batStatTherm").common.members; for(let i = 0; i < connStatPLC.length; i++) { on(connStatHeater[i], function (dp) { if(dp.state.c != scriptName) setState(connStatPLC[i], dp.state.val); }); if (getState(connStatHeater[i]).val != true) { on(batStatHeater[i], function (dp) { if(dp.state.c != scriptName) setState(batStatPLC[i], dp.state.val); }); } } } function SyncRadStat() { var connStatHeater = getObject("enum.functions.HeatingUp.connStatRad").common.members; var connStatPLC = getObject("enum.functions.PlcHeatingUp.connStatRad").common.members; var batStatHeater = getObject("enum.functions.HeatingUp.batStatRad").common.members; var batStatPLC = getObject("enum.functions.PlcHeatingUp.batStatRad").common.members; for(let i = 0; i < connStatPLC.length; i++) { on(connStatHeater[i], function (dp) { if(dp.state.c != scriptName) setState(connStatPLC[i], dp.state.val); }); if (getState(connStatHeater[i]).val != true) { on(batStatHeater[i], function (dp) { if(dp.state.c != scriptName) setState(batStatPLC[i], dp.state.val); }); } } } //function SyncOnStart() { on({id: "s7.2.DBs.DB5.First_Run"/*First_Run*/, change: "ne"}, async function (obj) { var SPHeater = getObject("enum.functions.HeatingUp.setpoint").common.members; var SPPLC = getObject("enum.functions.PlcHeatingUp.setpoint").common.members; var TempHeater = getObject("enum.functions.HeatingUp.actual").common.members; var TempPLC = getObject("enum.functions.PlcHeatingUp.actual").common.members; var HumHeater = getObject("enum.functions.HeatingUp.hum").common.members; var HumPLC = getObject("enum.functions.PlcHeatingUp.hum").common.members; var ProfileHeater = getObject("enum.functions.HeatingUp.profile").common.members; var ProfilePLC = getObject("enum.functions.PlcHeatingUp.profile").common.members; var BoostHeater = getObject("enum.functions.HeatingUp.boost").common.members; var BoostPLC = getObject("enum.functions.PlcHeatingUp.boost").common.members; var BoostTimeHeater = getObject("enum.functions.HeatingUp.boostTime").common.members; var BoostTimePLC = getObject("enum.functions.PlcHeatingUp.boostTime").common.members; var WinStatHeater = getObject("enum.functions.HeatingUp.winStat").common.members; var WinStatPLC = getObject("enum.functions.PlcHeatingUp.winStat").common.members; var batStatRadHeater = getObject("enum.functions.HeatingUp.batStatRad").common.members; var batStatRadPLC = getObject("enum.functions.PlcHeatingUp.batStatRad").common.members; var connStatRadHeater = getObject("enum.functions.HeatingUp.connStatRad").common.members; var connStatRadPLC = getObject("enum.functions.PlcHeatingUp.connStatRad").common.members; var batStatThermHeater = getObject("enum.functions.HeatingUp.batStatTherm").common.members; var batStatThermPLC = getObject("enum.functions.PlcHeatingUp.batStatTherm").common.members; var connStatThermHeater = getObject("enum.functions.HeatingUp.connStatTherm").common.members; var connStatThermPLC = getObject("enum.functions.PlcHeatingUp.connStatTherm").common.members; let value = obj.state.val; let oldValue = obj.oldState.val; if (getState("s7.2.DBs.DB5.First_Run").val == true) { for(let i = 0; i < SPHeater.length; i++) { setStateDelayed(SPPLC[i], getState(SPHeater[i]).val, false, parseInt(((0) || "").toString(), 10), false); setStateDelayed(TempPLC[i], getState(TempHeater[i]).val, false, parseInt(((0) || "").toString(), 10), false); setStateDelayed(HumPLC[i], getState(HumHeater[i]).val, false, parseInt(((0) || "").toString(), 10), false); setStateDelayed(ProfilePLC[i], getState(ProfileHeater[i]).val, false, parseInt(((0) || "").toString(), 10), false); setStateDelayed(BoostPLC[i], getState(BoostHeater[i]).val, false, parseInt(((0) || "").toString(), 10), false); setStateDelayed(BoostTimePLC[i], getState(BoostTimeHeater[i]).val, false, parseInt(((0) || "").toString(), 10), false); }; for(let i = 0; i < WinStatHeater.length; i++) { setStateDelayed(WinStatPLC[i], getState(WinStatHeater[i]).val, false, parseInt(((0) || "").toString(), 10), false); }; for(let i = 0; i < batStatRadHeater.length; i++) { setStateDelayed(batStatRadPLC[i], getState(batStatRadHeater[i]).val, false, parseInt(((0) || "").toString(), 10), false); setStateDelayed(connStatRadPLC[i], getState(connStatRadHeater[i]).val, false, parseInt(((0) || "").toString(), 10), false); }; for(let i = 0; i < batStatThermHeater.length; i++) { setStateDelayed(batStatThermPLC[i], getState(batStatThermHeater[i]).val, false, parseInt(((0) || "").toString(), 10), false); setStateDelayed(connStatThermPLC[i], getState(connStatThermHeater[i]).val, false, parseInt(((0) || "").toString(), 10), false); }; await wait(3000); setStateDelayed("s7.2.DBs.DB5.First_Run", false, false, parseInt(((0) || "").toString(), 10), false); } }); SyncSetpoint(); SyncProfile(); SyncBoost(); SyncBoostTime(); SyncActualHum(); SyncWinStat(); SyncThermStat(); SyncRadStat();
So thanks @paul53 but I gave up and took the less beautiful way out