-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSumPhases.js
112 lines (106 loc) · 2.86 KB
/
SumPhases.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
const mqttPrefix = Shelly.getComponentConfig( "mqtt" ).topic_prefix;
const number200 = Virtual.getHandle( "number:200" );
const number201 = Virtual.getHandle( "number:201" );
let last = {total_act:0,total_act_ret:0};
let purchase = number201.getValue();
let feed = number200.getValue();
function calculate(emdata)
{
//print("calc");
const delta = ( emdata.total_act - last.total_act ) - ( emdata.total_act_ret - last.total_act_ret );
print("delta",delta);
if( delta > 0 )
{
purchase += delta;
print("purchase",purchase)
number201.setValue( purchase );
Shelly.call( "KVS.Set", { key:"purchase", value:purchase } );
}
else if( delta < 0 )
{
feed -= delta;
print("feed",feed)
number200.setValue( feed );
Shelly.call( "KVS.Set", { key:"feed", value:feed } );
}
last.total_act = emdata.total_act;
last.total_act_ret = emdata.total_act_ret;
Shelly.call( "KVS.Set", { key:"last", value:JSON.stringify(last) } );
}
// get NVRAM data
Shelly.call( "KVS.Get", { key:"last" },
function(result)
{
if( result !== undefined )
{
last = JSON.parse(result.value);
print("last",JSON.stringify(last));
}
},
null );
Shelly.call( "KVS.Get", { key:"purchase" },
function(result)
{
if( result !== undefined )
{
purchase = JSON.parse(result.value);
print("purchase",purchase);
number201.setValue( purchase );
}
},
null );
Shelly.call( "KVS.Get", { key:"feed" },
function(result)
{
if( result !== undefined )
{
feed = JSON.parse(result.value);
print("feed",feed);
number200.setValue( feed );
}
},
null );
// MQTT
/*
function mqttSet(topic,message,ud)
{
if( message != "" )
{
message = JSON.parse( message );
print(message);
if( message.purchase !== undefined )
{
purchase = message.purchase;
Shelly.call( "KVS.Set", { key:"purchase", value:purchase } );
}
if( message.feed !== undefined )
{
feed = message.feed;
Shelly.call( "KVS.Set", { key:"feed", value:feed } );
}
}
}
MQTT.subscribe( mqttPrefix+"/emeter:0/set", mqttSet, 0 );
*/
// Handler
Shelly.addStatusHandler(
function(status,ud)
{
if( status.name === 'emdata')
{
//print( "status",JSON.stringify( status.delta ) );
calculate( status.delta );
}
},
null
);
// Timer
Timer.set( 600*1000, true,
function(ud)
{
MQTT.publish( mqttPrefix+"/status/number:200", JSON.stringify( {value:feed} ), 1, false );
MQTT.publish( mqttPrefix+"/status/number:201", JSON.stringify( {value:purchase} ), 1, false );
},
null
);
print( "Started" );