Timer the Switch with mysql database

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?

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

Use == not =

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

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

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

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

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

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!!!!

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

Perhaps you should do ...

var t = msg.payload;
if(t){
   //Set timer etc etc
}
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"

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?

//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)

//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:' '});
}

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"

I'm not certain you understand.

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.

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

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

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

Actual output

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!

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

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.

is this what you are trying to achieve?

1 Like

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.

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

1 Like

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 ?

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

msg.payload[0].level

Look at your screenshot.

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

You are comparing against a string.