If ( var1 = true and var2 = true ) then / else

so how are you setting timerOn and timerActive?

You are testing them to be a value but they are an object

yes I can see this but not understand correctly!

Object >> payload "true" or "false" must be expected

but may be is this function wrong,
for Timer:

var newMsg = { payload: msg.payload};
flow.set('timeOn',newMsg);
return newMsg;

and for timerActive:

var newMsg = { payload: msg.payload};
flow.set('timerActive',newMsg);
return newMsg;

So you set timeOn to be an object { payload: msg.payload}

which means that timeOn can never be timeOn === true

Try changing where you set your flow value to:

flow.set('timeOn',msg.payload);

flow.set('timerActive',msg.payload);

HAAARIIIIKAAA. thank you, it works :grin:

[{ā€žid":"61e29655.5ff498","type":"timerswitch","z":"a61d422a.aa2ce8","name":"Irrigation-Plan","ontopic":"","offtopic":"","onpayload":"true","offpayload":"false","disabled":false,"schedules":[{"on_h":"13","on_m":"56","on_s":"00","off_h":"21","off_m":"00","off_s":"00","valid":true}],"x":135,"y":185,"wires":[["34757b35.ef72d4"]]},{"id":"4862840d.9c7bdc","type":"ui_switch","z":"a61d422a.aa2ce8","name":"Automatik","label":"Zeitschaltung","group":"48fa5d97.39c614","order":1,"width":0,"height":0,"passthru":false,"decouple":"false","topic":"Auto","style":"","onvalue":"true","onvalueType":"str","onicon":"","oncolor":"","offvalue":"false","offvalueType":"str","officon":"","offcolor":"","x":125,"y":79,"wires":[["f3310232.74d2b"]]},{"id":"ff80c323.425db","type":"debug","z":"a61d422a.aa2ce8","name":"Automatik","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":792.5,"y":79,"wires":[]},{"id":"bd8caa84.46ea3","type":"function","z":"a61d422a.aa2ce8","name":"Auto","func":"var timerActive = flow.get('timerActive');\nvar timeOn = flow.get('timeOn');\nnode.warn(\"timerActive is \" + timerActive)\nnode.warn(\"timerOn is \" + timeOn)\n\n\n  if (timerActive === \"true\"  && timeOn === \"true\") {\n    msg.payload = 1;\n  }    \n   else {\n        msg.payload = 0;\n}\n\treturn msg;","outputs":1,"noerr":0,"x":607.5,"y":79,"wires":[["ff80c323.425db"]]},{"id":"cc1e768f.7c3a7","type":"debug","z":"a61d422a.aa2ce8","name":"TimerON","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":618,"y":183,"wires":[]},{"id":"b142b2e1.1e07a","type":"debug","z":"a61d422a.aa2ce8","name":"Timer","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":607.5,"y":232,"wires":[]},{"id":"34757b35.ef72d4","type":"function","z":"a61d422a.aa2ce8","name":"Flow Set TimerOn","func":"var newMsg = { payload: msg.payload};\nflow.set('timeOn',msg.payload);\nreturn newMsg;","outputs":1,"noerr":0,"x":360,"y":185,"wires":[["b142b2e1.1e07a","bd8caa84.46ea3"]]},{"id":"f3310232.74d2b","type":"function","z":"a61d422a.aa2ce8","name":"Flow Set TimerActive","func":"var newMsg = { payload: msg.payload};\nflow.set('timerActive',msg.payload);\nreturn newMsg;","outputs":1,"noerr":0,"x":335,"y":79,"wires":[["bd8caa84.46ea3","cc1e768f.7c3a7"]]},{"id":"48fa5d97.39c614","type":"ui_group","z":"","name":"Automation","tab":"4db4f194.142aa","order":1,"disp":true,"width":"6","collapse":false},{"id":"4db4f194.142aa","type":"ui_tab","z":"","name":"Home","icon":"dashboard","orderā€œ:1}]

thank you all once again!!!
best regards
Max

Hi, I have a similar problem but in my case, I have two switch and I have a function node which contain var x = false;
var y = true;
if (x === true && y !== true) {
msg.payload = 1;
}
else {
msg.payload = 0;
}
return msg;

The problem is that when I turn on one switch which my other switch turns off, I got payload "1" instead of "0".
problem

[{"id":"53110180.d01ef","type":"function","z":"104663c2.b4074c","name":"","func":"var x = false;\nvar y = true;\nif (x === true && y !== true) {\nmsg.payload = 1;\n}\nelse {\nmsg.payload = 0;\n}\nreturn msg;","outputs":1,"noerr":0,"x":460,"y":760,"wires":[["390e6939.f52ed6"]]}]

Did you realise you only exported the function node? I don't see any way that the function will ever send a payload of 1, can you post it with the switches too, or inject nodes for example, showing the problem?

Please check it.

[{"id":"9e0441f3.1daf4","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"1a249525.ebf54b","type":"function","z":"9e0441f3.1daf4","name":"","func":"var x = false;\nvar y = true;\nif (x === true && y !== true) {\nmsg.payload = 1;\n}\nelse {\nmsg.payload = 0;\n}\nreturn msg;","outputs":1,"noerr":0,"x":440,"y":300,"wires":[["2c8e10e.ce26af"]]},{"id":"2c8e10e.ce26af","type":"debug","z":"9e0441f3.1daf4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":680,"y":320,"wires":[]},{"id":"d8d6d755.5eb888","type":"ui_switch","z":"9e0441f3.1daf4","name":"","label":"switch1","tooltip":"","group":"a131b547.834c88","order":4,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":270,"y":240,"wires":[["1a249525.ebf54b"]]},{"id":"c0a3c0d2.2072d","type":"ui_switch","z":"9e0441f3.1daf4","name":"","label":"switch2","tooltip":"","group":"a131b547.834c88","order":5,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","x":270,"y":380,"wires":[["1a249525.ebf54b"]]},{"id":"a131b547.834c88","type":"ui_group","z":"","name":"Status","tab":"ded835ff.3dd788","order":1,"disp":true,"width":"6","collapse":true},{"id":"ded835ff.3dd788","type":"ui_tab","z":"","name":"Lights","icon":"fa-lightbulb-o","disabled":false,"hidden":false}]

Seems you want to set y/x with the button outputs? But this will never happen, you never set x/y with the msg.payload. Besides, I think you need to use context to remember the values for the next switchings to happen

Assuming you want to use the current switch positions in the function then you need to read this example from the cookbook for how to make them both accessible to the function at the same time.
However your function appears to make no attempt to even look at the value of the switches since it sets x and y such that the test will always fail, so the message sent will allways have msg.payload 0

Hi, I think I got stuck with a simple IF-statement using two variables. Craig, I tried your version:

craig_version

...and I trieded what I found on stack overflow:

stack_version

What am I doing wrong?

I just want to make a simple on/off-switch detecting if my headpump ist above or below 150W. When I want to count the amount of on/off-cycles. I believe it can be done a lot easier. Here ist my flow:

flow

[{"id":"9dc2f226.a46d5","type":"inject","z":"ced13ee.fb373c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"power","payload":"150","payloadType":"num","x":290,"y":160,"wires":[["8aed43d1.29418"]]},{"id":"424b9319.ec8c0c","type":"inject","z":"ced13ee.fb373c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"power","payload":"50","payloadType":"num","x":289,"y":230,"wires":[["8aed43d1.29418"]]},{"id":"8aed43d1.29418","type":"function","z":"ced13ee.fb373c","name":"Anzahl Einschaltungen","func":"var pwr = msg.payload;\nvar switch_on = flow.get('schalter_taegliche_schaltungen')||0;\nvar amount = flow.get('taegliche_schaltungen')||0;\n\nif (pwr > 100 && switch_on = 0) {\n    amount += 1;\n    flow.set('taegliche_schaltungen', amount);\n    switch_on = 1;\n    flow.set('schalter_taegliche_schaltungen', switch_on);\n}   \n\nif (pwr < 100 && switch_on = 1) {\n    switch_on = 0;\n    flow.set('schalter_taegliche_schaltungen', switch_on);\n}\n\nnode.status({fill:\"blue\",shape:\"ring\",text:\"switch_on: \" + switch_on + \" amount: \" + amount});\n \nmsg.payload = amount;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":560,"y":200,"wires":[["b6adc386.5243c"]]},{"id":"b6adc386.5243c","type":"debug","z":"ced13ee.fb373c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":770,"y":200,"wires":[]}]

Secondly I would love to calculate on- and off-time per day, but I realize, I'm still fare away from my goal.

Thanks for your help,
Timo

If must have brackets around the whole part.

Bad...

if (var1 ==1 ) && (var2==1) {
  ...
}

Good...

if (var1 == 1 && var2 == 1) {
  ...
}

You are assigning (single equals) inside the if clause instead of comparing (double/triple equals).

Bad...

if (switch_on = 0) {
  ...
}

Good...

if (switch_on == 0) {
  ...
}

I think the clue is in the error message.
I would have written it as...

if ( (pwr > 100 ) && (switch_on == 0) ) {

Better...

if (switch_on === 0) {
  ...
}

3 = is better because it checks the left and right-hand data types more closely.

Not always better. But it will be useful and wise if you know what will be the difference using one or other.

1 Like

Personally I would use, in a case like this
if ( pwr > 100 && !switch_on)
as I think the meaning is clearer. Assuming that switch_on does relate to a switch, and 0 is off. If it doesn't then it is a bad choice of variable name :slight_smile:

Thanks to everybody! This is working perfectly!

[{"id":"9dc2f226.a46d5","type":"inject","z":"ced13ee.fb373c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"power","payload":"150","payloadType":"num","x":290,"y":160,"wires":[["8aed43d1.29418"]]},{"id":"424b9319.ec8c0c","type":"inject","z":"ced13ee.fb373c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"power","payload":"50","payloadType":"num","x":289,"y":230,"wires":[["8aed43d1.29418"]]},{"id":"8aed43d1.29418","type":"function","z":"ced13ee.fb373c","name":"Anzahl Einschaltungen","func":"var pwr = msg.payload;\nvar switch_on = flow.get('schalter_taegliche_schaltungen')||0;\nvar amount = flow.get('taegliche_schaltungen')||0;\n\nif ((pwr > 100) && (switch_on == 0)) {\n    amount += 1;\n    flow.set('taegliche_schaltungen', amount);\n    switch_on = 1;\n    flow.set('schalter_taegliche_schaltungen', switch_on);\n}   \n\nif ((pwr < 100) && (switch_on == 1)) {\n    switch_on = 0;\n    flow.set('schalter_taegliche_schaltungen', switch_on);\n}\n\nnode.status({fill:\"blue\",shape:\"ring\",text:\"switch_on: \" + switch_on + \" amount: \" + amount});\n \nmsg.payload = amount;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":560,"y":200,"wires":[["b6adc386.5243c"]]},{"id":"b6adc386.5243c","type":"debug","z":"ced13ee.fb373c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":770,"y":200,"wires":[]}]

Does anybody have an idea how to calculate on- and off-time per day?

Yep here you go - some code that a very nice gentleman gave me recently and works perfectly

should be easy for you to adapt

[{"id":"4ffc8b6b.f77ba4","type":"comment","z":"be8b1f7c.56858","name":"Dashboard Display and Control","info":"","x":1150,"y":340,"wires":[]},{"id":"73a5fd3d.dea5f4","type":"mqtt in","z":"be8b1f7c.56858","name":"MQTT from Pool Pump","topic":"tele/Tasmota_Pool_Pump_Controller/STATE","qos":"2","datatype":"json","broker":"e2ed99fb.aa45d8","x":160,"y":400,"wires":[["910ba253.d8efd"]]},{"id":"f6c7316b.6f05","type":"comment","z":"be8b1f7c.56858","name":"Capture State of Pool Pump and increment Runtime","info":"","x":250,"y":360,"wires":[]},{"id":"57b79da6.526f54","type":"comment","z":"be8b1f7c.56858","name":"Reset Daily and Weekly Counters","info":"","x":190,"y":600,"wires":[]},{"id":"93d62265.b9e5e","type":"ui_text","z":"be8b1f7c.56858","group":"81c56d43.95adb","order":4,"width":0,"height":0,"name":"Pool Pump Status","label":"Pump On/Off Status","format":"{{msg.payload.state}}","layout":"col-center","x":1170,"y":540,"wires":[]},{"id":"2b6921de.e68eee","type":"debug","z":"be8b1f7c.56858","name":"stats","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":450,"y":520,"wires":[]},{"id":"910ba253.d8efd","type":"function","z":"be8b1f7c.56858","name":"Track Daily, Weekly and Monthly runtimes","func":"const flowVarName = \"POOLPUMP\";\n\nconst stats = flow.get(flowVarName) || { state: \"UNKNOWN\", onMS: 0, offMS: 0, onMSdaily: 0, offMSdaily: 0, onMSweekly: 0, offMSweekly: 0, onMSmonthly: 0, offMSmonthly: 0, lastOn: null, lastOff: null, lastUpdate: Date.now()};\nif (msg.topic == \"RESET\") {\n    stats.onMSdaily = 0;\n    stats.offMSdaily = 0;\n    stats.onMSweekly = 0;\n    stats.offMSWeekly = 0;\n    stats.onMSmonthly = 0;\n    stats.offMSmonthly = 0;\n} else if (msg.topic == \"RESET:DAILY\") {\n    stats.onMSdaily = 0;\n    stats.offMSdaily = 0;\n} else if (msg.topic == \"RESET:WEEKLY\") {\n    stats.onMSweekly = 0;\n    stats.offMSWeekly = 0;\n} else if (msg.topic == \"RESET:MONTHLY\") {\n    stats.onMSmonthly = 0;\n    stats.offMSmonthly = 0;\n} else if(msg.topic == \"TICK\") {\n    var dif = (Date.now() - stats.lastUpdate);\n    if(stats.state == \"ON\") {\n        stats.onMS += dif;\n        stats.onMSdaily += dif;\n        stats.onMSweekly += dif;\n        stats.onMSmonthly += dif;\n    } else {\n        stats.offMS += dif;\n        stats.offMSdaily += dif;\n        stats.offMSweekly += dif;\n        stats.offMSmonthly += dif;\n    }\n    stats.lastUpdate = Date.now();\n} else {\n    var state = msg.payload.POWER;\n    if (state == \"ON\" && stats.state != \"ON\") {\n        stats.lastOn = Date.now();\n        node.status({fill:\"green\",shape:\"ring\",text:\"on\"});\n    }\n    if (state == \"OFF\" && stats.state != \"OFF\") {\n        stats.lastOff = Date.now();\n        node.status({ fill: \"red\", shape: \"ring\", text: \"off\"});\n    }\n    stats.state = state;\n}\n\nflow.set(flowVarName, stats); //update stats\nmsg.payload = stats;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":520,"y":440,"wires":[["93d62265.b9e5e","2b6921de.e68eee","3d0c7049.965ee","144a6e03.1b67d2","41efea51.620244"]]},{"id":"21ccd71.fa0b028","type":"inject","z":"be8b1f7c.56858","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"10","crontab":"","once":false,"onceDelay":0.1,"topic":"TICK","payload":"","payloadType":"date","x":170,"y":460,"wires":[["910ba253.d8efd"]]},{"id":"2a9bbb73.ee9ff4","type":"ui_text","z":"be8b1f7c.56858","group":"81c56d43.95adb","order":6,"width":0,"height":0,"name":"Weekly Running Time","label":"Weekly Runtime","format":"{{msg.payload}}","layout":"row-spread","x":1220,"y":400,"wires":[]},{"id":"fd0dc9ab.1849f8","type":"ui_text","z":"be8b1f7c.56858","group":"81c56d43.95adb","order":5,"width":0,"height":0,"name":"Todays Running Time","label":"Todays Runtime","format":"{{msg.payload}}","layout":"row-spread","x":1220,"y":440,"wires":[]},{"id":"4e481d34.6dc014","type":"inject","z":"be8b1f7c.56858","name":"Reset Weekly Run Counter for Pool Pump","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"01 00 * * 1","once":false,"onceDelay":0.1,"topic":"RESET:WEEKLY","payload":"","payloadType":"date","x":250,"y":680,"wires":[["8efaeb25.2a16d8"]]},{"id":"34352b0f.ad1714","type":"inject","z":"be8b1f7c.56858","name":"Reset Daily Run Counter for Pool Pump","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"01 00 * * *","once":false,"onceDelay":0.1,"topic":"RESET:DAILY","payload":"","payloadType":"date","x":240,"y":640,"wires":[["8efaeb25.2a16d8"]]},{"id":"8b394fe2.fd508","type":"inject","z":"be8b1f7c.56858","name":"Reset all counters","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"RESET","payload":"","payloadType":"date","x":170,"y":720,"wires":[["8efaeb25.2a16d8"]]},{"id":"979f2251.8e931","type":"link in","z":"be8b1f7c.56858","name":">> command","links":["8efaeb25.2a16d8"],"x":190,"y":500,"wires":[["910ba253.d8efd"]],"l":true},{"id":"8efaeb25.2a16d8","type":"link out","z":"be8b1f7c.56858","name":"command >>","links":["979f2251.8e931"],"x":570,"y":680,"wires":[],"l":true},{"id":"3d0c7049.965ee","type":"function","z":"be8b1f7c.56858","name":"","func":"var day, hours, minutes, seconds;\n    elapsedms = flow.get(\"POOLPUMP.onMSdaily\");\n    seconds = Math.floor(elapsedms / 1000);\n    minutes = Math.floor(seconds / 60);\n    seconds = seconds % 60;\n    hours = Math.floor(minutes / 60);\n    minutes = minutes % 60;\n    //day = Math.floor(hour / 24);\n    //hour = hour % 24;\n    hours = (hours < 10) ? \"0\" + hours : hours;\n    minutes = (minutes < 10) ? \"0\" + minutes : minutes;\n    seconds = (seconds < 10) ? \"0\" + seconds : seconds;\n\n    msg.payload = hours + \":\" + minutes;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":840,"y":280,"wires":[["54b257cf.347848"]]},{"id":"54b257cf.347848","type":"debug","z":"be8b1f7c.56858","name":"stats","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1010,"y":280,"wires":[]},{"id":"41efea51.620244","type":"function","z":"be8b1f7c.56858","name":"Convert WeeklyMs to Hours:Minutes","func":"var day, hours, minutes, seconds;\n    elapsedms = flow.get(\"POOLPUMP.onMSweekly\");\n    seconds = Math.floor(elapsedms / 1000);\n    minutes = Math.floor(seconds / 60);\n    seconds = seconds % 60;\n    hours = Math.floor(minutes / 60);\n    minutes = minutes % 60;\n    //day = Math.floor(hour / 24);\n    //hour = hour % 24;\n    hours = (hours < 10) ? \"0\" + hours : hours;\n    minutes = (minutes < 10) ? \"0\" + minutes : minutes;\n    seconds = (seconds < 10) ? \"0\" + seconds : seconds;\n\n    msg.payload = hours + \":\" + minutes;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":930,"y":400,"wires":[["2a9bbb73.ee9ff4"]]},{"id":"144a6e03.1b67d2","type":"function","z":"be8b1f7c.56858","name":"Convert DailyMs to Hours:Minutes","func":"var day, hours, minutes, seconds;\n    elapsedms = flow.get(\"POOLPUMP.onMSdaily\");\n    seconds = Math.floor(elapsedms / 1000);\n    minutes = Math.floor(seconds / 60);\n    seconds = seconds % 60;\n    hours = Math.floor(minutes / 60);\n    minutes = minutes % 60;\n    //day = Math.floor(hour / 24);\n    //hour = hour % 24;\n    hours = (hours < 10) ? \"0\" + hours : hours;\n    minutes = (minutes < 10) ? \"0\" + minutes : minutes;\n    seconds = (seconds < 10) ? \"0\" + seconds : seconds;\n\n    msg.payload = hours + \":\" + minutes;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":920,"y":440,"wires":[["fd0dc9ab.1849f8"]]},{"id":"e2ed99fb.aa45d8","type":"mqtt-broker","z":"","name":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"81c56d43.95adb","type":"ui_group","z":"","name":"Pump Status","tab":"a30b3814.18c8b8","order":1,"disp":true,"width":"6","collapse":false},{"id":"a30b3814.18c8b8","type":"ui_tab","z":"","name":"Pool Control","icon":"dashboard","disabled":false,"hidden":false}]

Craig

1 Like

Hi Craig,

thank you so much. I believe the TICK-timestamp is just to initiate updates. The only msg.payload is comming from the MQTT-node. I see msg.payload.POWER to trigger the state of the pool pump. I changed msg.payload.POWER to msg.payload and submit the payload "ON" and "OFF" in text format and it is working. :+1:

Thank you,
Timo

Yes thats right - the tick is just used to give you a running counter - check out the context data viewer on the right hand side of the editor window and you will see the Variables being updated in realtime.

Craig

1 Like