# Timer the Switch with mysql database

**URL:** <https://discourse.nodered.org/t/timer-the-switch-with-mysql-database/9650>\
**Category:** General\
**Created:** [1 April 2019 09:53 UTC](https://discourse.nodered.org/t/timer-the-switch-with-mysql-database/9650 "2019-04-01T09:53:13Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![hebergeweb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hebergeweb/32/6394_2.png) [@hebergeweb](https://discourse.nodered.org/u/hebergeweb)\
**Post date:** [1 April 2019 09:53 UTC](https://discourse.nodered.org/t/timer-the-switch-with-mysql-database/9650/1 "2019-04-01T09:53:13Z")

</div>

Hi,  
in my flow if I open the switch I go through MySQL database to get the last value of field level after it sends value to the function FN to recover this value as a time remains the switch running.  
I take an example with this scenario: click switch "on" after I take the last value in our database is equal to 76 after the function fn return the msg to the trigger node after 76 to close the switch "off".  
The problem of my code at the function fn?

 ![Untitled](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/0/0f9391c9a38bbdfc11b6863a840217e4d212d8d2.png)

---

<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:** [1 April 2019 10:14 UTC](https://discourse.nodered.org/t/timer-the-switch-with-mysql-database/9650/2 "2019-04-01T10:14:18Z")

</div>

Check your school boy error inside the brackets of your `if` on line 1.

Use `==` not `=`

---

<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:** [1 April 2019 10:15 UTC](https://discourse.nodered.org/t/timer-the-switch-with-mysql-database/9650/3 "2019-04-01T10:15:07Z")

</div>

Ps, the exclamation mark on line 1 tells you the problem.

---

<div class="post-metadata">

**Author:** ![hebergeweb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hebergeweb/32/6394_2.png) [@hebergeweb](https://discourse.nodered.org/u/hebergeweb)\
**Post date:** [1 April 2019 11:54 UTC](https://discourse.nodered.org/t/timer-the-switch-with-mysql-database/9650/4 "2019-04-01T11:54:33Z")

</div>

but when I click on the switch it always remains in active state and the debug node does not show anything

---

<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:** [1 April 2019 12:00 UTC](https://discourse.nodered.org/t/timer-the-switch-with-mysql-database/9650/5 "2019-04-01T12:00:11Z")

</div>

So your function node will only return anything if the `if` statement is true.  
Add logging events to your function node to see what you are actually passing and to check your logic.

[https://nodered.org/docs/writing-functions#logging-events](https://nodered.org/docs/writing-functions#logging-events)

---

<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:** [1 April 2019 12:18 UTC](https://discourse.nodered.org/t/timer-the-switch-with-mysql-database/9650/6 "2019-04-01T12:18:16Z")

</div>

That's because inside the if, you set msg.payload to true.

Do what I said and change the assignment `=` to comparison `==`

---

<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:** [1 April 2019 12:20 UTC](https://discourse.nodered.org/t/timer-the-switch-with-mysql-database/9650/7 "2019-04-01T12:20:29Z")

</div>

Look at line 1.

You actually set the payload to a string `'true'`.

Then in line 2, `t` get set to a string `'true'` therefore your timeout doesn't work!!!!

---

<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:** [1 April 2019 12:37 UTC](https://discourse.nodered.org/t/timer-the-switch-with-mysql-database/9650/8 "2019-04-01T12:37:42Z")

</div>

Actually, msg.payload cannot be 'true' and a value. It's either one other the other.

Perhaps you should do ...

```auto
var t = msg.payload;
if(t){
   //Set timer etc etc
}

```

---

<div class="post-metadata">

**Author:** ![hebergeweb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hebergeweb/32/6394_2.png) [@hebergeweb](https://discourse.nodered.org/u/hebergeweb)\
**Post date:** [1 April 2019 13:21 UTC](https://discourse.nodered.org/t/timer-the-switch-with-mysql-database/9650/9 "2019-04-01T13:21:11Z")

</div>

```auto
if(msg['payload'] == 'true'){
var t = msg.payload;
if(t){
setTimeout(function(){
    node.status({fill:"red", shape:"ring", text:" "});
    msg['payload'] = 'true';
    node.send(msg);
}, t);
}
node.status({fill:"blue", shape:"ring", text:' '});
}

```

debug node does not show anything,  
msg.payload it's the result of mysql value "level"

---

<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:** [1 April 2019 13:41 UTC](https://discourse.nodered.org/t/timer-the-switch-with-mysql-database/9650/10 "2019-04-01T13:41:42Z")

</div>

Put a debug node before this function node. See what is in msg.payload.

You do realise that `msg.payload` is the same as `msg['payload']` right?

```auto
//If msg payload is actually == true
if(msg['payload'] == 'true'){
//Then it cannot magically be a number. 
var t = msg.payload; 

```

So if msg.payload is actually a number, it will NEVER == 'true'

Try this (not tested - might be buggy but you should get the jist)

```auto
//Test msg.payload is not empty
if(msg.payload){
//Get value of msg.payload
var t = msg.payload;
setTimeout(function(){
    node.status({fill:"red", shape:"ring", text:" "});
    msg.payload = true;
    node.send(msg);
}, t);
node.status({fill:"blue", shape:"ring", text:' '});
}

```

---

<div class="post-metadata">

**Author:** ![hebergeweb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hebergeweb/32/6394_2.png) [@hebergeweb](https://discourse.nodered.org/u/hebergeweb)\
**Post date:** [1 April 2019 14:40 UTC](https://discourse.nodered.org/t/timer-the-switch-with-mysql-database/9650/11 "2019-04-01T14:40:11Z")

</div>

with this function the node red is blocked  
for your question " You do realise that `msg.payload` is the same as `msg['payload']` right?"  
I mean msg.payload it's the value of mysql.  
msg['payload'] is the state of Switch node "true" or "false"

---

<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:** [1 April 2019 14:54 UTC](https://discourse.nodered.org/t/timer-the-switch-with-mysql-database/9650/12 "2019-04-01T14:54:41Z")

</div>

I'm not certain you understand.

> [@hebergeweb](#):
>
> I mean msg.payload it's the value of mysql.  
> msg['payload'] is the state of Switch node "true" or "false"

msg.payload IS EXACTLY THE SAME OBJECT AS msg['payload']

They cannot contain different things in the same function node.

Look at the example below.

```auto
var msg = {};
msg['payload'] = 'true';
msg.payload = 96;

console.log(msg['payload']);
// expected output: true

console.log(msg.payload);
// expected output: 96

```

Actual output

```auto
96
96

```

Proof that they are the same so your if() will NEVER work.

Honestly check your code. Use an online editor or something. It'll never work!

---

<div class="post-metadata">

**Author:** ![hebergeweb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hebergeweb/32/6394_2.png) [@hebergeweb](https://discourse.nodered.org/u/hebergeweb)\
**Post date:** [1 April 2019 15:40 UTC](https://discourse.nodered.org/t/timer-the-switch-with-mysql-database/9650/13 "2019-04-01T15:40:00Z")

</div>

I relied on this msg.payload have the same one result, in all the function I use a single variable _ **msg.payload** _ that gives the result of database value. but debug node does not show anything

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [1 April 2019 17:51 UTC](https://discourse.nodered.org/t/timer-the-switch-with-mysql-database/9650/14 "2019-04-01T17:51:32Z")

</div>

Please put a debug node attached to mysql node and show us what is actually in msg.payload.  
If the value in msg.payload is a boolean `true` and not a string `'true'` nothing will come out of the function.

---

<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:** [1 April 2019 18:23 UTC](https://discourse.nodered.org/t/timer-the-switch-with-mysql-database/9650/15 "2019-04-01T18:23:26Z")

</div>

is this what you are trying to achieve?

 ![nr-help](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/9/9367a07ddbf826e2260547bdaac1dbd91c9d24a2.png)

---

<div class="post-metadata">

**Author:** ![hebergeweb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hebergeweb/32/6394_2.png) [@hebergeweb](https://discourse.nodered.org/u/hebergeweb)\
**Post date:** [2 April 2019 08:07 UTC](https://discourse.nodered.org/t/timer-the-switch-with-mysql-database/9650/16 "2019-04-02T08:07:33Z")

</div>

the function "fn" is correct.  
but the solution works well when I delete the node switch and linked the trigger directly with the node fn and switch.

---

<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:** [2 April 2019 08:15 UTC](https://discourse.nodered.org/t/timer-the-switch-with-mysql-database/9650/17 "2019-04-02T08:15:35Z")

</div>

This might suggest one way you could do it... [https://cookbook.nodered.org/basic/route-on-context](https://cookbook.nodered.org/basic/route-on-context)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [2 April 2019 09:08 UTC](https://discourse.nodered.org/t/timer-the-switch-with-mysql-database/9650/18 "2019-04-02T09:08:41Z")

</div>

Perhaps you should clearly explain what you are trying to achieve. I have read your question 4 times, still confused.

The value "76" - are you checking a string or an integer ?  
The value "true" are you checking a string or a boolean ?

---

<div class="post-metadata">

**Author:** ![hebergeweb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hebergeweb/32/6394_2.png) [@hebergeweb](https://discourse.nodered.org/u/hebergeweb)\
**Post date:** [2 April 2019 10:46 UTC](https://discourse.nodered.org/t/timer-the-switch-with-mysql-database/9650/19 "2019-04-02T10:46:28Z")

</div>

The value 76 considers as an integer : `var level = msg.payload[0].level;`  
I send `msg.payload = false;` a boolean type to turn off the switch

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [2 April 2019 13:33 UTC](https://discourse.nodered.org/t/timer-the-switch-with-mysql-database/9650/20 "2019-04-02T13:33:13Z")

</div>

`msg.payload[0].level`

Look at your screenshot.

string: `level: "76.0"`  
number: `level: 76.0`

You are comparing against a string.
