-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInverter.js
187 lines (174 loc) · 4.42 KB
/
Inverter.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const mqttPrefix = Shelly.getComponentConfig( "mqtt" ).topic_prefix;
let counter = 0;
let cntCycle = 0;
let inverterSoll;
let inverterleistung;
let reportedState;
function showState(state)
{
if( state !== reportedState )
{
reportedState = state;
print( "showState:", state );
MQTT.publish( mqttPrefix+"/status/enum:200", "{\"value\":\""+state+"\"}", 1, false );
}
}
function switchInverter(value)
{
print( "switchInverter", value );
Shelly.call( "Switch.Set", { id:1, on:value }, null, null );
}
function switchAusgang(value)
{
print( "switchAusgang", value );
Shelly.call( "Switch.Set", { id:0, on:value }, null, null );
}
function controlInverter()
{
//print( "controlInverter", inverterSoll );
if( inverterSoll === undefined )
{
return;
}
const ausgang = Shelly.getComponentStatus( "switch", 0 ).output;
const inverter = Shelly.getComponentStatus( "switch", 1 ).output;
const inverteronline = Shelly.getComponentStatus( "input", 0 ).state;
counter++;
if( inverter !== ausgang )
{
if( inverterSoll === "on" )
{
//print( "inverteronline", inverteronline );
if( inverteronline )
{
switchAusgang( true );
}
else
{
if( ( counter % 30 ) === 0 )
{
print( "!!! PANIK: no voltage generated !!!" );
switchInverter( false );
showState( "Panik2" );
}
else
{
print( "waiting for InverterOnline", counter );
}
}
}
else if ( inverterSoll === "off" )
{
if( inverterleistung === 0 || counter >= 30 )
{
switchInverter( false )
}
else
{
print( "waiting for inverterLeistung==0", counter );
}
}
else if ( inverterSoll === "only" )
{
counter = 0;
showState( inverterSoll );
}
}
else if( !inverter && inverterSoll==="on" )
{
switchInverter( true );
showState( "switching on" );
}
else if( ausgang && inverterSoll==="off" )
{
switchAusgang( false );
showState( "switching off" );
}
else if( inverterSoll === "only" )
{
switchAusgang( false );
switchInverter( true );
showState( "switching only" );
}
else
{
counter = 0;
if( inverterSoll==="on" && !inverteronline )
{
print( "!!! Panik: inverterSoll==='on' && !inverteronline !!!" );
showState( "Panik1" );
}
else
{
showState( inverterSoll );
}
}
}
function setInverter(inverter)
{
if( counter === 0 || inverterSoll === inverter)
{
print( "setInverter", inverter );
}
else
{
print( "!!! setInverter", inverter, "(change of mind) !!!" );
}
inverterSoll = inverter;
//controlInverter();
}
function rpcCallback(topic,message,ud)
{
//print(topic,message)
try
{
message = JSON.parse( message );
if( message.method === "Enum.Set" && message.params.id == 200 )
{
let answer = {
id: message.id,
src: mqttPrefix,
dst: message.src,
result: {}
};
//print("set",message.params.value);
setInverter( message.params.value );
MQTT.publish( message.src+"/rpc", JSON.stringify( answer ), 1, false );
}
}
catch (error)
{
print( error );
}
}
function cyclic(ud)
{
if( ++cntCycle > 120 )
{
cntCycle = 0;
reportedState = undefined;
}
controlInverter();
}
function inverterCallback(topic,message,id)
{
try
{
message = JSON.parse( message );
if( typeof message == "number" )
{
print( "Inverterleistung", message );
inverterleistung = message;
}
}
catch (error)
{
print( error );
}
}
// main
showState( "init" );
MQTT.subscribe( mqttPrefix+"/rpc", rpcCallback, null );
MQTT.subscribe( "shellies/PV_Speicher_Inverterleistung/relay/0/power", inverterCallback, null );
Timer.set( 1000, true, cyclic, null );
print( "started" );