-
Notifications
You must be signed in to change notification settings - Fork 1
/
smartpi-treshold-trigger.js
28 lines (28 loc) · 1.08 KB
/
smartpi-treshold-trigger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
module.exports = function(RED) {
function SmartPiTresholdTrigger(config) {
RED.nodes.createNode(this, config);
flowContext = this.context().flow
this.levelup = config.levelup || 1;
this.leveldown = config.leveldown || 0;
var node = this;
this.on('input', function(msg) {
if (msg.hasOwnProperty('payload')) {
var value = Number(msg.payload);
if (!isNaN(value)) {
var outvalue;
if (value >= config.rising_threshold) {
outvalue = this.levelup;
} else if (value < config.falling_threshold) {
outvalue = this.leveldown;
} else {
outvalue = flowContext.get("treshold") || 0;
}
flowContext.set("treshold", outvalue);
msg.payload = outvalue;
node.send(msg);
}
}
});
}
RED.nodes.registerType('smartpi-treshold-trigger', SmartPiTresholdTrigger);
}