How do I start a variable timer from the dashboard

873 views
Skip to first unread message

Gaz

unread,
Mar 20, 2017, 4:17:48 AM3/20/17
to Node-RED
Hi,

I have a use for a timer that will send out a message to an Arduino that enables a Relay that turns on music for when i go to sleep.
But i need to be able to have it turned of after a time I set on a drop down list ( using the dashboard ) , for example 20 minutes, 60 minutes etc.

I have looked at BigTimer but I don't think that will work ?

I can make it work using NodeRed Trigger node, but that can't be set from the dashboard.

Any ideas on how I would do this?


regards


Gaz


Mark Setrem

unread,
Mar 20, 2017, 4:37:04 AM3/20/17
to Node-RED
See the answer given to this question which is less than 20 posts below

https://groups.google.com/forum/m/#!topic/node-red/1-5MBPcu2vA

Gaz

unread,
Mar 20, 2017, 7:17:19 AM3/20/17
to Node-RED
Thanks Mark

I now have the delay function working.

But I am doing something wrong in setting a value in the UI and then getting the function to receive it. At the bottom is the value I set in the UI.


And this is my function:

 var stat = global.get("MP3_timer");

if (stat == 5 )
{
    node.status({fill:"red",shape:"ring",text:"disconnected"});
    msg.payload = "MP3_delay_time"
    return msg;
}






regards


Gaz


Auto Generated Inline Image 1

Dave C-J

unread,
Mar 20, 2017, 9:37:22 AM3/20/17
to node...@googlegroups.com
Aha... yes - the options are wrong...
They are Value and Label pairs... not name value pairs...
so you are trying to set them all to a value of "MP3_Timer" which is not what you want... (I think)
just use  (numbers)
   5   5
  10  10        etc
   

Dave C-J

unread,
Mar 20, 2017, 9:38:55 AM3/20/17
to node...@googlegroups.com
and that will just come out of the select node as msg.payload... so then you will need a change node to move it from msg.payload to global  MP3_Timer

Gaz

unread,
Mar 20, 2017, 12:16:11 PM3/20/17
to Node-RED
Hi,

I am getting there, I can now detect a number and branch to correct code.
But the timer does not run correctly, it does if I leave the 5000 part in on my code, but id I try to use number it completes with no delay ?
I know this is easy to sort but looked on internet and cannot find the answer !


if (msg.payload == 30)
{
   
    var number = msg.payload;



// X second delay

    node.status({fill:"green",shape:"dot",text:"Enabled"});
    msg.payload = "RLY = ON"
    node.send(msg)



setTimeout(function()

{
    node.status({fill:"red",shape:"ring",text:"Disabled"});
    msg.payload = "RLY = OFF"
    node.send(msg)
    //}, 5000); // sets timeout
   
}   , number); // sets timeout


return null;

}



regards


Gaz


steve rickus

unread,
Mar 20, 2017, 12:56:00 PM3/20/17
to Node-RED
You need to multiply the number of seconds by 1000, since setTimeout(func, millis) takes a number of milliseconds as the second arg.

Indeed, the timeout if working, but is pausing for only 0.030 seconds ;*)

Gaz

unread,
Mar 20, 2017, 1:21:18 PM3/20/17
to Node-RED
Thanks Steve,

That was the problem,
The answer is easy when someone points out your mistake :-)

regards


Gaz


Reply all
Reply to author
Forward
0 new messages