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

**URL:** <https://discourse.nodered.org/t/if-var1-true-and-var2-true-then-else/3359>\
**Category:** General\
**Created:** [22 September 2018 13:40 UTC](https://discourse.nodered.org/t/if-var1-true-and-var2-true-then-else/3359 "2018-09-22T13:40:02Z")\
**Posts on this page:** 19\
**Page:** 2

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [23 September 2018 10:08 UTC](https://discourse.nodered.org/t/if-var1-true-and-var2-true-then-else/3359/21 "2018-09-23T10:08:34Z")

</div>

so how are you setting timerOn and timerActive?

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

---

<div class="post-metadata">

**Author:** ![maxmueller50](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maxmueller50/32/2621_2.png) [@maxmueller50](https://discourse.nodered.org/u/maxmueller50)\
**Post date:** [23 September 2018 10:14 UTC](https://discourse.nodered.org/t/if-var1-true-and-var2-true-then-else/3359/22 "2018-09-23T10:14:52Z")

</div>

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;

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [23 September 2018 10:26 UTC](https://discourse.nodered.org/t/if-var1-true-and-var2-true-then-else/3359/23 "2018-09-23T10:26:50Z")

</div>

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);`

---

<div class="post-metadata">

**Author:** ![maxmueller50](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maxmueller50/32/2621_2.png) [@maxmueller50](https://discourse.nodered.org/u/maxmueller50)\
**Post date:** [23 September 2018 11:00 UTC](https://discourse.nodered.org/t/if-var1-true-and-var2-true-then-else/3359/24 "2018-09-23T11:00:18Z")

</div>

> [@maxmueller50](#):
>
> var newMsg = { payload: msg.payload};  
> flow.set('timeOn',newMsg);  
> return newMsg;

HAAARIIIIKAAA. thank you, it works 😁

```auto
[{„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

---

<div class="post-metadata">

**Author:** ![usmanEmder](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@usmanEmder](https://discourse.nodered.org/u/usmanEmder)\
**Post date:** [15 April 2020 20:35 UTC](https://discourse.nodered.org/t/if-var1-true-and-var2-true-then-else/3359/25 "2020-04-15T20:35:37Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/6/a6da382f3b80d1a0c07cf4198b821602d3a39fc4.jpeg)

```auto
[{"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"]]}]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [16 April 2020 08:00 UTC](https://discourse.nodered.org/t/if-var1-true-and-var2-true-then-else/3359/26 "2020-04-16T08:00:06Z")

</div>

> [@usmanEmder](#):
>
> [{"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?

---

<div class="post-metadata">

**Author:** ![usmanEmder](https://avatars.discourse-cdn.com/v4/letter/u/977dab/32.png) [@usmanEmder](https://discourse.nodered.org/u/usmanEmder)\
**Post date:** [16 April 2020 08:10 UTC](https://discourse.nodered.org/t/if-var1-true-and-var2-true-then-else/3359/27 "2020-04-16T08:10:36Z")

</div>

Please check it.

```auto
[{"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}]

```

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [16 April 2020 09:16 UTC](https://discourse.nodered.org/t/if-var1-true-and-var2-true-then-else/3359/28 "2020-04-16T09:16:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [16 April 2020 10:04 UTC](https://discourse.nodered.org/t/if-var1-true-and-var2-true-then-else/3359/29 "2020-04-16T10:04:50Z")

</div>

Assuming you want to use the current switch positions in the function then you need to read [this example from the cookbook](https://cookbook.nodered.org/basic/join-streams) 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

---

<div class="post-metadata">

**Author:** ![Ion3](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ion3/32/36892_2.png) [@Ion3](https://discourse.nodered.org/u/Ion3)\
**Post date:** [15 February 2021 19:03 UTC](https://discourse.nodered.org/t/if-var1-true-and-var2-true-then-else/3359/31 "2021-02-15T19:03:52Z")

</div>

> [@craigcurtin](#):
>
> basic Javascript
> 
> ```auto
> if (var1 ==1 ) && (var2==1) {
> var 3 = 1;
> }
> else {
> (var3 =0);
> }
> 
> ```
> 
> regards
> 
> Craig

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

![craig_version](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/1/514b0d677f42cad9227386beb9185d0f9171e775.png)

...and I trieded what I found on [stack overflow](https://stackoverflow.com/a/8710458):

![stack_version](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/7/9730bd7b4771b5309dad0fe9fe4e482ef6004631.png)

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/9/79c002b833bb0c0eac505af7bd5b7f32f7b25085.png)

```auto
[{"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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [15 February 2021 19:23 UTC](https://discourse.nodered.org/t/if-var1-true-and-var2-true-then-else/3359/32 "2021-02-15T19:23:58Z")

</div>

> [@Ion3](#):
>
> ![craig_version](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/1/514b0d677f42cad9227386beb9185d0f9171e775.png)

`If` must have brackets around the whole part.

Bad...

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

```

Good...

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

```

* * *

> [@Ion3](#):
>
> ![stack_version](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/7/9730bd7b4771b5309dad0fe9fe4e482ef6004631.png)
> 
> What am I doing wrong?

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

Bad...

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

```

Good...

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

```

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [15 February 2021 19:26 UTC](https://discourse.nodered.org/t/if-var1-true-and-var2-true-then-else/3359/33 "2021-02-15T19:26:27Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [15 February 2021 19:52 UTC](https://discourse.nodered.org/t/if-var1-true-and-var2-true-then-else/3359/34 "2021-02-15T19:52:06Z")

</div>

> [@Steve-Mcl](#):
>
> Good...
> 
> ```auto
> if (switch_on == 0) {
> ...
> }
> 
> ```

Better...

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

```

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

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [15 February 2021 20:03 UTC](https://discourse.nodered.org/t/if-var1-true-and-var2-true-then-else/3359/35 "2021-02-15T20:03:05Z")

</div>

> [@TotallyInformation](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [15 February 2021 20:50 UTC](https://discourse.nodered.org/t/if-var1-true-and-var2-true-then-else/3359/36 "2021-02-15T20:50:46Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![Ion3](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ion3/32/36892_2.png) [@Ion3](https://discourse.nodered.org/u/Ion3)\
**Post date:** [15 February 2021 21:34 UTC](https://discourse.nodered.org/t/if-var1-true-and-var2-true-then-else/3359/37 "2021-02-15T21:34:20Z")

</div>

> [@dynamicdave](#):
>
> I think the clue is in the error message.  
> I would have written it as...
> 
> `if ( (pwr > 100 ) && (switch_on == 0) ) {`

Thanks to everybody! This is working perfectly!

```auto
[{"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?

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [16 February 2021 06:29 UTC](https://discourse.nodered.org/t/if-var1-true-and-var2-true-then-else/3359/38 "2021-02-16T06:29:23Z")

</div>

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

should be easy for you to adapt

```auto
[{"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

---

<div class="post-metadata">

**Author:** ![Ion3](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ion3/32/36892_2.png) [@Ion3](https://discourse.nodered.org/u/Ion3)\
**Post date:** [16 February 2021 08:41 UTC](https://discourse.nodered.org/t/if-var1-true-and-var2-true-then-else/3359/39 "2021-02-16T08:41:42Z")

</div>

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. 👍

Thank you,  
Timo

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [16 February 2021 09:10 UTC](https://discourse.nodered.org/t/if-var1-true-and-var2-true-then-else/3359/40 "2021-02-16T09:10:55Z")

</div>

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

[Previous page](https://discourse.nodered.org/t/if-var1-true-and-var2-true-then-else/3359.md?page=1)
