node-red-contrib-solar-multi-switch 0.9.0
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/package.json +20 -0
- package/solar-multi-switch.html +89 -0
- package/solar-multi-switch.js +101 -0
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "node-red-contrib-solar-multi-switch",
|
|
3
|
+
"version": "0.9.0",
|
|
4
|
+
"description": "Schaltet mehrere Verbraucher basierend auf Solar-Überschussleistung mit Priorisierung.",
|
|
5
|
+
"main": "solar-multi-switch.js",
|
|
6
|
+
"node-red": {
|
|
7
|
+
"nodes": {
|
|
8
|
+
"solar-multi-switch": "solar-multi-switch.js"
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"node-red",
|
|
13
|
+
"solar",
|
|
14
|
+
"photovoltaic",
|
|
15
|
+
"surplus",
|
|
16
|
+
"energy-management"
|
|
17
|
+
],
|
|
18
|
+
"author": "Serene152",
|
|
19
|
+
"license": "MIT"
|
|
20
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
<script type="text/javascript">
|
|
2
|
+
RED.nodes.registerType('solar-multi-switch', {
|
|
3
|
+
category: 'function',
|
|
4
|
+
color: '#90ee90',
|
|
5
|
+
defaults: {
|
|
6
|
+
name: { value: "" },
|
|
7
|
+
surplusProperty: { value: "payload" },
|
|
8
|
+
invertSurplus: { value: false },
|
|
9
|
+
minSurplus: { value: 0 },
|
|
10
|
+
payloadOn: { value: "true" },
|
|
11
|
+
payloadOff: { value: "false" },
|
|
12
|
+
consumers: { value: [] },
|
|
13
|
+
outputs: { value: 1 }
|
|
14
|
+
},
|
|
15
|
+
inputs: 1,
|
|
16
|
+
outputs: 1,
|
|
17
|
+
icon: "font-awesome/fa-sun-o",
|
|
18
|
+
label: function() {
|
|
19
|
+
return this.name || "Solar Multi Switch";
|
|
20
|
+
},
|
|
21
|
+
oneditprepare: function() {
|
|
22
|
+
var node = this;
|
|
23
|
+
$("#node-input-consumers-container").editableList({
|
|
24
|
+
addItem: function(container, i, data) {
|
|
25
|
+
var consumer = data || { id: "c"+Date.now()+i, name: "Gerät "+(i+1), priority: i+1, maxConsumption: 1000, minRuntime: 60 };
|
|
26
|
+
var row = $('<div/>', {style: "display:flex; width:100%; gap:5px"}).appendTo(container);
|
|
27
|
+
$('<input/>',{class:"node-input-consumer-name", type:"text", placeholder:"Name", style:"flex:3"}).val(consumer.name).appendTo(row);
|
|
28
|
+
$('<input/>',{class:"node-input-consumer-prio", type:"number", placeholder:"Prio", style:"flex:1", title:"Prio (1=Hoch)"}).val(consumer.priority).appendTo(row);
|
|
29
|
+
$('<input/>',{class:"node-input-consumer-power", type:"number", placeholder:"Watt", style:"flex:1.5", title:"Verbrauch (W)"}).val(consumer.maxConsumption).appendTo(row);
|
|
30
|
+
$('<input/>',{class:"node-input-consumer-time", type:"number", placeholder:"Sek", style:"flex:1.5", title:"Mindestlaufzeit (s)"}).val(consumer.minRuntime).appendTo(row);
|
|
31
|
+
},
|
|
32
|
+
removable: true, sortable: true
|
|
33
|
+
});
|
|
34
|
+
if (node.consumers) node.consumers.forEach(c => $("#node-input-consumers-container").editableList('addItem', c));
|
|
35
|
+
},
|
|
36
|
+
oneditsave: function() {
|
|
37
|
+
var node = this;
|
|
38
|
+
node.consumers = [];
|
|
39
|
+
$("#node-input-consumers-container").editableList('items').each(function(i) {
|
|
40
|
+
var c = $(this);
|
|
41
|
+
node.consumers.push({
|
|
42
|
+
id: "c" + i + Date.now(),
|
|
43
|
+
name: c.find(".node-input-consumer-name").val(),
|
|
44
|
+
priority: parseInt(c.find(".node-input-consumer-prio").val()),
|
|
45
|
+
maxConsumption: parseInt(c.find(".node-input-consumer-power").val()),
|
|
46
|
+
minRuntime: parseInt(c.find(".node-input-consumer-time").val())
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
this.outputs = node.consumers.length;
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
</script>
|
|
53
|
+
|
|
54
|
+
<script type="text/html" data-template-name="solar-multi-switch">
|
|
55
|
+
<div class="form-row">
|
|
56
|
+
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
|
|
57
|
+
<input type="text" id="node-input-name" placeholder="Name">
|
|
58
|
+
</div>
|
|
59
|
+
<hr>
|
|
60
|
+
<div style="background: #f1f5f9; padding: 12px; border-radius: 6px; margin-bottom: 12px; border: 1px solid #e2e8f0;">
|
|
61
|
+
<h4 style="margin-top:0">Allgemeine Konfiguration</h4>
|
|
62
|
+
<div class="form-row">
|
|
63
|
+
<label for="node-input-surplusProperty">msg-Pfad</label>
|
|
64
|
+
msg.<input type="text" id="node-input-surplusProperty" style="width: 60%" placeholder="payload">
|
|
65
|
+
</div>
|
|
66
|
+
<div class="form-row">
|
|
67
|
+
<label for="node-input-minSurplus">Min. Einspeisung</label>
|
|
68
|
+
<input type="number" id="node-input-minSurplus" style="width: 100px"> W
|
|
69
|
+
</div>
|
|
70
|
+
<div class="form-row">
|
|
71
|
+
<label for="node-input-invertSurplus">Invertieren</label>
|
|
72
|
+
<input type="checkbox" id="node-input-invertSurplus" style="width:auto">
|
|
73
|
+
</div>
|
|
74
|
+
<hr>
|
|
75
|
+
<div class="form-row">
|
|
76
|
+
<label for="node-input-payloadOn">Payload (AN)</label>
|
|
77
|
+
<input type="text" id="node-input-payloadOn" style="width: 60%">
|
|
78
|
+
</div>
|
|
79
|
+
<div class="form-row">
|
|
80
|
+
<label for="node-input-payloadOff">Payload (AUS)</label>
|
|
81
|
+
<input type="text" id="node-input-payloadOff" style="width: 60%">
|
|
82
|
+
</div>
|
|
83
|
+
</div>
|
|
84
|
+
<hr>
|
|
85
|
+
<h4>Verbraucher</h4>
|
|
86
|
+
<div class="form-row">
|
|
87
|
+
<ol id="node-input-consumers-container"></ol>
|
|
88
|
+
</div>
|
|
89
|
+
</script>
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
module.exports = function(RED) {
|
|
2
|
+
function SolarMultiSwitch(config) {
|
|
3
|
+
RED.nodes.createNode(this, config);
|
|
4
|
+
const node = this;
|
|
5
|
+
|
|
6
|
+
node.surplusProperty = config.surplusProperty || "payload";
|
|
7
|
+
node.invertSurplus = config.invertSurplus;
|
|
8
|
+
node.minSurplus = parseInt(config.minSurplus) || 0;
|
|
9
|
+
|
|
10
|
+
const parsePayload = (val) => {
|
|
11
|
+
if (val === "true") return true;
|
|
12
|
+
if (val === "false") return false;
|
|
13
|
+
if (!isNaN(val) && val.trim() !== "") return Number(val);
|
|
14
|
+
return val;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
node.payloadOn = parsePayload(config.payloadOn);
|
|
18
|
+
node.payloadOff = parsePayload(config.payloadOff);
|
|
19
|
+
node.consumers = config.consumers || [];
|
|
20
|
+
|
|
21
|
+
let states = node.context().get('states') || {};
|
|
22
|
+
|
|
23
|
+
node.on('input', function(msg) {
|
|
24
|
+
try {
|
|
25
|
+
let surplus = RED.util.getMessageProperty(msg, node.surplusProperty);
|
|
26
|
+
if (typeof surplus !== 'number') {
|
|
27
|
+
node.status({fill:"yellow", shape:"ring", text:"Ungültiger Messwert"});
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (node.invertSurplus) surplus *= -1;
|
|
32
|
+
|
|
33
|
+
const now = Date.now();
|
|
34
|
+
let hasChanged = false;
|
|
35
|
+
|
|
36
|
+
node.consumers.forEach(c => {
|
|
37
|
+
if (!states[c.id]) states[c.id] = { isOn: false, startTime: null };
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const sortedByPriority = [...node.consumers].sort((a,b) => a.priority - b.priority);
|
|
41
|
+
|
|
42
|
+
// LOGIK 1: Einschalten (Höchste Priorität zuerst)
|
|
43
|
+
if (surplus > node.minSurplus) {
|
|
44
|
+
for (const c of sortedByPriority) {
|
|
45
|
+
if (!states[c.id].isOn && (surplus - c.maxConsumption >= node.minSurplus)) {
|
|
46
|
+
states[c.id].isOn = true;
|
|
47
|
+
states[c.id].startTime = now;
|
|
48
|
+
hasChanged = true;
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// LOGIK 2: Ausschalten (Niedrigste Priorität zuerst)
|
|
55
|
+
if (surplus < node.minSurplus) {
|
|
56
|
+
const reversedByPriority = [...sortedByPriority].reverse();
|
|
57
|
+
for (const c of reversedByPriority) {
|
|
58
|
+
if (states[c.id].isOn) {
|
|
59
|
+
const elapsed = (now - (states[c.id].startTime || now)) / 1000;
|
|
60
|
+
if (elapsed >= (c.minRuntime || 0)) {
|
|
61
|
+
states[c.id].isOn = false;
|
|
62
|
+
states[c.id].startTime = null;
|
|
63
|
+
hasChanged = true;
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (hasChanged) {
|
|
71
|
+
node.context().set('states', states);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Generiere Status-String basierend auf dem Ausgangs-Index (1, 2, 3...)
|
|
75
|
+
const statusString = node.consumers.map((c, index) => {
|
|
76
|
+
const s = states[c.id].isOn ? "an" : "aus";
|
|
77
|
+
return (index + 1) + ": " + s;
|
|
78
|
+
}).join(" ");
|
|
79
|
+
|
|
80
|
+
const activeCount = Object.values(states).filter(s => s.isOn).length;
|
|
81
|
+
node.status({
|
|
82
|
+
fill: activeCount > 0 ? "green" : "grey",
|
|
83
|
+
shape: "dot",
|
|
84
|
+
text: statusString
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
const outputMsgs = node.consumers.map(c => {
|
|
88
|
+
return states[c.id].isOn
|
|
89
|
+
? { ...msg, payload: node.payloadOn, topic: c.name }
|
|
90
|
+
: { ...msg, payload: node.payloadOff, topic: c.name };
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
node.send(outputMsgs);
|
|
94
|
+
|
|
95
|
+
} catch (err) {
|
|
96
|
+
node.error("Fehler: " + err.message);
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
RED.nodes.registerType("solar-multi-switch", SolarMultiSwitch);
|
|
101
|
+
};
|