20 Jun 20:38:14 - [error] [function:context.state = "Closed"] ReferenceError: clearTimeout is not defined (line 27, col 9)
20 Jun 20:38:14 - [info] [debug:ERROR:Timers]
{ topic: 'SENSORS/S/MagSw_MS01/LWRF/RFX',
payload: 'Off',
qos: 0,
retain: false,
_topic: 'SENSORS/S/MagSw_MS01/LWRF/RFX',
_msgid: 'b6ca4dcb.4935b',
error:
{ message: 'ReferenceError: clearTimeout is not defined (line 27, col 9)',
source: { id: 'cbcc38af.fde778', type: 'function', count: 1 } } }
if ( msg.payload == "On" ) {
node.log( "open");
context.state = "Open";
context.timer = setTimeout(function(){
if ( context.state = "Open" ) {
node.log("State is OPEN - triggered from timeout");
node.send( msg );
} else {
node.log("State is CLOSED - triggered from timeout");
}
}, 5000);
} else {
context.state = "Closed";
clearTimeout(context.timer);
node.log("State is CLOSED - triggered direct");
}
--
http://nodered.org
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
When the door is shut is the light on? :-)
// Calculate the elapsed time
function elapsed() {
var elapsed = {};
// Milliseconds
if (context.openTime) {
elapsed.ms = new Date() - context.openTime;
} else {
elapsed.ms = 0;
}
// seconds
elapsed.sec = Math.round( elapsed.ms / 1000 ); // or .toFixed(numdp);
// minutes
elapsed.min = Math.round( elapsed.ms / 60000 ); // or .toFixed(numdp);
// return object
return elapsed;
}
// What to do after the timout interval
function action() {
// When countdown expires, check if door is still open
if ( context.state == "Open" ) {
// Check whether door has really been open all the time or whether
// it has been closed then reopened. In the later case, the elapsed
// time will be less than the timeout. Wouldn't need this if we could use clearTimeout()!
if ( elapsed()['ms'] >= timeout ) {
//node.warn( "Freezer door is still open! - triggered from timeout - Elapsed time: " + elapsed()['min'] );
// Send the msg downstream
node.send( {'payload': 'Freezer door has been open for ' + elapsed()['min'] + ' minutes!'} );
// Set up another reminder in timeout's time (keep going till someone shuts the door!)
setTimeout(action, timeout);
} else {
//node.warn("Freezer door was closed then opened again - triggered from timeout - Elapsed time: " + elapsed()['min'] );
}
} else {
//node.warn("Freezer door already closed - triggered from timeout - Elapsed time: " + elapsed()['min'] );
}
}
// How many minutes do we want to wait until we trigger the alarm?
// 5 minute countdown = 5*60*1000= 300000
var timeout = 300000;
if ( msg.payload == "On" ) {
node.warn( "Freezer door opened");
// Keep track of how long the door is open
if (!context.openTime) context.openTime = new Date();
// Track state
context.state = "Open";
// Create a new timer
context.timeout1 = setTimeout(action, timeout);
} else {
node.warn( "Freezer door has been closed - Elapsed time: " + elapsed()['min'] );
// track state
context.state = "Closed";
// If the door was open a long time, send a msg that it is now closed
if ( elapsed()['ms'] >= timeout ) {
node.send( {'payload': 'Freezer door is now closed but was open for ' + elapsed()['min'] + ' minutes!'} );
}
// Reset the open time
if (context.openTime) context.openTime = false;
// Cancel the timeout
//clearTimeout(context.timeout1);
}
Adding via settings file very much documented on the page dedicated to writing functions....
http://nodered.org/docs/writing-functions.html
Nick