@mschaeffler/node-red-logic 0.6.2 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -0
- package/examples/statistics.json +64 -0
- package/examples/statistics.png +0 -0
- package/math_reduce.html +0 -13
- package/math_statistics.html +103 -0
- package/math_statistics.js +83 -0
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -202,6 +202,45 @@ The message property to be used as payload can be defined with the `Property` pr
|
|
|
202
202
|
|min |gets the minimal value of the `topic` channels|
|
|
203
203
|
|max |gets the maximal value of the `topic` channels|
|
|
204
204
|
|
|
205
|
+
### statistics
|
|
206
|
+
|
|
207
|
+
Calculate statistical values of one or more data streams.
|
|
208
|
+
|
|
209
|
+
Each stream can be debounced by arithmatic mean.
|
|
210
|
+
|
|
211
|
+
If the msg property `invalid` is present in the message, all nodes ignore the message.
|
|
212
|
+
|
|
213
|
+
#### Input
|
|
214
|
+
|
|
215
|
+
The different inputs are differentiated by topics.
|
|
216
|
+
|
|
217
|
+
The message property to be used as payload can be defined with the `Property` property.
|
|
218
|
+
|
|
219
|
+
|msg. | type | description |
|
|
220
|
+
|:-------|:-------|:----------------------------------|
|
|
221
|
+
|topic | string | name of the input channel. |
|
|
222
|
+
|payload | number | input value for `topic`. |
|
|
223
|
+
|
|
224
|
+
#### Output
|
|
225
|
+
|
|
226
|
+
|msg. | type | description |
|
|
227
|
+
|:-------|:-------|:----------------------------------|
|
|
228
|
+
|payload | | same as in input message.|
|
|
229
|
+
|stats.count | number | number of data elements.|
|
|
230
|
+
|stats.min number | number | minimal value.|
|
|
231
|
+
|stats.max number | number | maximal value.|
|
|
232
|
+
|stats.average number | number | average value.|
|
|
233
|
+
|stats.deviation number | number | standard deviation.|
|
|
234
|
+
|stats.variation number | number | coefficient of variation.|
|
|
235
|
+
|
|
236
|
+
#### Parameters
|
|
237
|
+
|
|
238
|
+
|config| type | description |
|
|
239
|
+
|:-----|:-------|:----------------------------------|
|
|
240
|
+
|Property| string | defines the message property to be used as payload.|
|
|
241
|
+
|delta time|number|time frame to collect the data for the analysis.|
|
|
242
|
+
|min. Data|number|min. amount of data points to generate an output.|
|
|
243
|
+
|
|
205
244
|
## Edge Nodes
|
|
206
245
|
|
|
207
246
|

|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"id": "8b8b9dd67621a97e",
|
|
4
|
+
"type": "statistics",
|
|
5
|
+
"z": "3c59326457645408",
|
|
6
|
+
"name": "",
|
|
7
|
+
"property": "payload",
|
|
8
|
+
"deltaTime": "10",
|
|
9
|
+
"minData": "2",
|
|
10
|
+
"x": 360,
|
|
11
|
+
"y": 2100,
|
|
12
|
+
"wires": [
|
|
13
|
+
[
|
|
14
|
+
"1a6086132fd93e1a"
|
|
15
|
+
]
|
|
16
|
+
]
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
"id": "1a6086132fd93e1a",
|
|
20
|
+
"type": "debug",
|
|
21
|
+
"z": "3c59326457645408",
|
|
22
|
+
"name": "debug 15",
|
|
23
|
+
"active": true,
|
|
24
|
+
"tosidebar": true,
|
|
25
|
+
"console": false,
|
|
26
|
+
"tostatus": false,
|
|
27
|
+
"complete": "true",
|
|
28
|
+
"targetType": "full",
|
|
29
|
+
"statusVal": "",
|
|
30
|
+
"statusType": "auto",
|
|
31
|
+
"x": 540,
|
|
32
|
+
"y": 2100,
|
|
33
|
+
"wires": []
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
"id": "40735ba7d52d0d38",
|
|
37
|
+
"type": "inject",
|
|
38
|
+
"z": "3c59326457645408",
|
|
39
|
+
"name": "",
|
|
40
|
+
"props": [
|
|
41
|
+
{
|
|
42
|
+
"p": "payload"
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
"p": "topic",
|
|
46
|
+
"vt": "str"
|
|
47
|
+
}
|
|
48
|
+
],
|
|
49
|
+
"repeat": "1",
|
|
50
|
+
"crontab": "",
|
|
51
|
+
"once": true,
|
|
52
|
+
"onceDelay": 0.1,
|
|
53
|
+
"topic": "random",
|
|
54
|
+
"payload": "$random()*100",
|
|
55
|
+
"payloadType": "jsonata",
|
|
56
|
+
"x": 190,
|
|
57
|
+
"y": 2100,
|
|
58
|
+
"wires": [
|
|
59
|
+
[
|
|
60
|
+
"8b8b9dd67621a97e"
|
|
61
|
+
]
|
|
62
|
+
]
|
|
63
|
+
}
|
|
64
|
+
]
|
|
Binary file
|
package/math_reduce.html
CHANGED
|
@@ -45,19 +45,6 @@
|
|
|
45
45
|
types:["msg"],
|
|
46
46
|
typeField: "#node-input-property-type"
|
|
47
47
|
});
|
|
48
|
-
/*
|
|
49
|
-
$("#node-input-algo").typedInput({
|
|
50
|
-
types:[{
|
|
51
|
-
value:"algo",
|
|
52
|
-
options: [
|
|
53
|
-
{ value: "add", label: "Addition"},
|
|
54
|
-
{ value: "mean", label: "Mean Value"},
|
|
55
|
-
{ value: "prod", label: "Product"},
|
|
56
|
-
{ value: "min", label: "Minimum" },
|
|
57
|
-
{ value: "max", label: "Maximum" }
|
|
58
|
-
]
|
|
59
|
-
}]
|
|
60
|
-
})*/
|
|
61
48
|
}
|
|
62
49
|
});
|
|
63
50
|
</script>
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
<script type="text/javascript">
|
|
2
|
+
RED.nodes.registerType('statistics',{
|
|
3
|
+
category: 'function',
|
|
4
|
+
color: "#C0DEED",
|
|
5
|
+
defaults: {
|
|
6
|
+
name:{value:""},
|
|
7
|
+
property:{value:"payload", required:true},
|
|
8
|
+
deltaTime:{value:"60", required:true, validate:function(v){
|
|
9
|
+
return v!=='' && !isNaN(v) && Number(v)>0;}
|
|
10
|
+
},
|
|
11
|
+
minData:{value:"1", required:true, validate:function(v){
|
|
12
|
+
return v!=='' && !isNaN(v) && Number(v)>0;}
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
inputs:1,
|
|
16
|
+
outputs:1,
|
|
17
|
+
icon:"font-awesome/fa-arrows-alt",
|
|
18
|
+
label: function() {
|
|
19
|
+
return this.name||"statistics";
|
|
20
|
+
},
|
|
21
|
+
labelStyle: function() {
|
|
22
|
+
return this.name?"node_label_italic":"";
|
|
23
|
+
},
|
|
24
|
+
inputLabels: 'input',
|
|
25
|
+
outputLabels: 'output',
|
|
26
|
+
oneditprepare: function() {
|
|
27
|
+
$("#node-input-property").typedInput({
|
|
28
|
+
type:"msg",
|
|
29
|
+
types:["msg"],
|
|
30
|
+
typeField: "#node-input-property-type"
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
</script>
|
|
35
|
+
|
|
36
|
+
<script type="text/html" data-template-name="statistics">
|
|
37
|
+
<div class="form-row">
|
|
38
|
+
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
|
39
|
+
<input type="text" id="node-input-name" placeholder="Name">
|
|
40
|
+
</div>
|
|
41
|
+
<div class="form-row">
|
|
42
|
+
<label for="node-input-input"><i class="fa fa-ellipsis-h"></i> Eigenschaft</label>
|
|
43
|
+
<input type="text" id="node-input-property">
|
|
44
|
+
<input type="hidden" id="node-input-property-type">
|
|
45
|
+
</div>
|
|
46
|
+
<div class="form-row">
|
|
47
|
+
<label for="node-input-minMean"><i class="fa fa-hourglass-o"></i> Zeitspanne</label>
|
|
48
|
+
<input type="text" id="node-input-deltaTime">
|
|
49
|
+
</div>
|
|
50
|
+
<div class="form-row">
|
|
51
|
+
<label for="node-input-minData"><i class="fa fa-check-square-o "></i> min. Data</label>
|
|
52
|
+
<input type="text" id="node-input-minData">
|
|
53
|
+
</div>
|
|
54
|
+
</script>
|
|
55
|
+
|
|
56
|
+
<script type="text/html" data-help-name="statistics">
|
|
57
|
+
|
|
58
|
+
<p>Calculate statistical values of one or more data streams.</p>
|
|
59
|
+
<p>Each stream can be debounced by arithmatic mean.</p>
|
|
60
|
+
<p>If the msg property <code>invalid</code> is present in the message, all nodes ignore the message.</p>
|
|
61
|
+
|
|
62
|
+
<h3>Input</h3>
|
|
63
|
+
|
|
64
|
+
<p>The different inputs are differentiated by topics.</p>
|
|
65
|
+
<p>The message property to be used as payload can be defined with the <code>Property</code> property.</p>
|
|
66
|
+
|
|
67
|
+
<dl class="message-properties">
|
|
68
|
+
<dt>topic <span class="property-type">string</span></dt>
|
|
69
|
+
<dd> name of the input channel.</dd>
|
|
70
|
+
<dt>payload <span class="property-type">number</span></dt>
|
|
71
|
+
<dd> input value for <code>topic</code>.</dd>
|
|
72
|
+
</dl>
|
|
73
|
+
|
|
74
|
+
<h3>Output</h3>
|
|
75
|
+
|
|
76
|
+
<dl class="message-properties">
|
|
77
|
+
<dt>payload <span class="property-type">number</span></dt>
|
|
78
|
+
<dd> same as in input message.</dd>
|
|
79
|
+
<dt>stats.count <span class="property-type">number</span></dt>
|
|
80
|
+
<dd> number of data elements.</dd>
|
|
81
|
+
<dt>stats.min <span class="property-type">number</span></dt>
|
|
82
|
+
<dd> minimal value.</dd>
|
|
83
|
+
<dt>stats.max <span class="property-type">number</span></dt>
|
|
84
|
+
<dd> maximal value.</dd>
|
|
85
|
+
<dt>stats.average <span class="property-type">number</span></dt>
|
|
86
|
+
<dd> average value.</dd>
|
|
87
|
+
<dt>stats.deviation <span class="property-type">number</span></dt>
|
|
88
|
+
<dd> standard deviation.</dd>
|
|
89
|
+
<dt>stats.variation <span class="property-type">number</span></dt>
|
|
90
|
+
<dd> coefficient of variation.</dd>
|
|
91
|
+
</dl>
|
|
92
|
+
|
|
93
|
+
<h3>Parameter</h3>
|
|
94
|
+
|
|
95
|
+
<dl class="message-properties">
|
|
96
|
+
<dt>Property <span class="property-type">string</span></dt>
|
|
97
|
+
<dd> defines the message property to be used as payload.</dd>
|
|
98
|
+
<dt>delta time <span class="property-type">number</span></dt>
|
|
99
|
+
<dd> time frame to collect the data for the analysis.</dd>
|
|
100
|
+
<dt>min. Data <span class="property-type">number</span></dt>
|
|
101
|
+
<dd> min. amount of data points to generate an output.</dd>
|
|
102
|
+
</dl>
|
|
103
|
+
</script>
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
module.exports = function(RED) {
|
|
2
|
+
|
|
3
|
+
function StatsNode(config) {
|
|
4
|
+
RED.nodes.createNode(this,config);
|
|
5
|
+
//this.config = config;
|
|
6
|
+
var node = this;
|
|
7
|
+
var context = this.context();
|
|
8
|
+
this.property = config.property || "payload";
|
|
9
|
+
this.deltaTime= Number( config.deltaTime )*1000;
|
|
10
|
+
this.minData = Number( config.minData );
|
|
11
|
+
|
|
12
|
+
node.on('input', function(msg,send,done) {
|
|
13
|
+
if( msg.invalid )
|
|
14
|
+
{
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
if( msg.reset || msg.topic==="init" )
|
|
18
|
+
{
|
|
19
|
+
context.set( "data", {} );
|
|
20
|
+
node.status( "" );
|
|
21
|
+
}
|
|
22
|
+
else
|
|
23
|
+
{
|
|
24
|
+
const now = Date.now();
|
|
25
|
+
const payload = Number( RED.util.getMessageProperty( msg, node.property ) );
|
|
26
|
+
if( ! isNaN( payload ) )
|
|
27
|
+
{
|
|
28
|
+
let data = context.get( "data" ) ?? {};
|
|
29
|
+
let item = data[msg.topic] ?? [];
|
|
30
|
+
item.push( { time:now, value:payload } );
|
|
31
|
+
while( item[0].time < now - this.deltaTime )
|
|
32
|
+
{
|
|
33
|
+
item.shift();
|
|
34
|
+
}
|
|
35
|
+
data[msg.topic] = item;
|
|
36
|
+
context.set( "data", data );
|
|
37
|
+
|
|
38
|
+
if( item.length >= this.minData )
|
|
39
|
+
{
|
|
40
|
+
msg.stat = {
|
|
41
|
+
count: item.length,
|
|
42
|
+
min: Number.MAX_SAFE_INTEGER,
|
|
43
|
+
max: Number.MIN_SAFE_INTEGER };
|
|
44
|
+
let sum = 0;
|
|
45
|
+
for( const value of item )
|
|
46
|
+
{
|
|
47
|
+
sum += value.value;
|
|
48
|
+
if( value.value < msg.stat.min )
|
|
49
|
+
{
|
|
50
|
+
msg.stat.min = value.value;
|
|
51
|
+
}
|
|
52
|
+
if( value.value > msg.stat.max )
|
|
53
|
+
{
|
|
54
|
+
msg.stat.max = value.value;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
msg.stat.average = sum/msg.stat.count;
|
|
58
|
+
let varianz = 0;
|
|
59
|
+
for( const value of item )
|
|
60
|
+
{
|
|
61
|
+
varianz += ( value.value - msg.stat.average ) ** 2;
|
|
62
|
+
}
|
|
63
|
+
msg.stat.deviation = Math.sqrt( varianz / msg.stat.count );
|
|
64
|
+
msg.stat.variation = msg.stat.deviation / msg.stat.average;
|
|
65
|
+
node.status({fill:"green",shape:"dot",text:msg.stat.count});
|
|
66
|
+
send( msg );
|
|
67
|
+
}
|
|
68
|
+
else
|
|
69
|
+
{
|
|
70
|
+
node.status({fill:"gray",shape:"dot",text:"to less data"});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
else
|
|
74
|
+
{
|
|
75
|
+
node.status({fill:"red",shape:"dot",text:"payload is NaN"});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
done();
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
RED.nodes.registerType("statistics",StatsNode);
|
|
83
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mschaeffler/node-red-logic",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "nodes for simple operations.",
|
|
5
5
|
"license": "LGPL-2.1",
|
|
6
6
|
"homepage": "https://github.com/m-schaeffler/node-red-my-nodes/tree/main/node-red-logic",
|
|
@@ -25,7 +25,8 @@
|
|
|
25
25
|
"reduce":"math_reduce.js",
|
|
26
26
|
"fallingEdge":"math_falling.js",
|
|
27
27
|
"raisingEdge":"math_raising.js",
|
|
28
|
-
"hysteresisEdge":"math_hysteresis.js"
|
|
28
|
+
"hysteresisEdge":"math_hysteresis.js",
|
|
29
|
+
"statistics":"math_statistics.js"
|
|
29
30
|
}
|
|
30
31
|
},
|
|
31
32
|
"author": {
|