-
Notifications
You must be signed in to change notification settings - Fork 9
/
shelly-porssisahko-ht-sensor-temp.js
153 lines (124 loc) · 22.2 KB
/
shelly-porssisahko-ht-sensor-temp.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
/**
* @license
*
* shelly-porssisahko
*
* (c) Jussi isotalo - http://jisotalo.fi
* https://github.com/jisotalo/shelly-porssisahko
*
* License: GNU Affero General Public License v3.0
*/
let C_HIST=24,C_ERRC=3,C_ERRD=120,C_DEF={mode:0,m0:{cmd:0},m1:{lim:0},m2:{per:24,cnt:0,lim:-999,sq:0,m:999,ps:0,pe:23,ps2:0,pe2:23,cnt2:0},vat:24,day:0,night:0,bk:0,err:0,outs:[0],fh:0,fhCmd:0,inv:0,min:60,oc:0},_={s:{v:"2.13.0",dn:"",st:0,str:"",cmd:-1,chkTs:0,errCnt:0,errTs:0,upTs:0,timeOK:0,configOK:0,fCmdTs:0,fCmd:0,tz:"+02:00",tzh:0,p:[{ts:0,now:0,low:0,high:0,avg:0},{ts:0,now:0,low:0,high:0,avg:0}]},p:[[],[]],h:[],c:C_DEF},m=!1,r=!1;function c(e,t){t-=e;return 0<=t&&t<3600}function i(e,t,s){return Math.min(s,Math.max(e,t))}function l(e){return Math.floor((e?e.getTime():Date.now())/1e3)}function o(e,t,s){let o=e.toString();for(;o.length<t;)o=s?s+o:" "+o;return o}function n(e){return e.getDate()}function a(e){let t=e.toString(),s=0;"+0000"==(t=t.substring(3+t.indexOf("GMT")))?(t="Z",s=0):(s=+t.substring(0,3),t=t.substring(0,3)+":"+t.substring(3)),t!=_.s.tz&&(_.s.p[0].ts=0),_.s.tz=t,_.s.tzh=s}function u(e){console.log((new Date).toString().substring(16,24)+":",e)}function h(){for(;0<C_HIST&&_.h.length>=C_HIST;)_.h.splice(0,1);_.h.push([l(),r?1:0,_.s.st])}function p(){var e=new Date;_.s.timeOK=2e3<e.getFullYear()?1:0,_.s.dn=Shelly.getComponentConfig("sys").device.name,!_.s.upTs&&_.s.timeOK&&(_.s.upTs=l(e))}function d(e){Shelly.call("KVS.Get",{key:"porssi-config"},function(t,e,s,o){_.c=t?t.value:{},"function"==typeof USER_CONFIG&&(_.c=USER_CONFIG(_.c,_,!0));{t=function(e){_.s.configOK=e?1:0,_.s.chkTs=0,o&&(m=!1,Timer.set(1e3,!1,A))};let e=0;if(C_DEF){for(var n in null==_.c.fhCmd&&null!=_.c.fh&&(_.c.fhCmd=_.c.fh),C_DEF)if(void 0===_.c[n])_.c[n]=C_DEF[n],e++;else if("object"==typeof C_DEF[n])for(var r in C_DEF[n])void 0===_.c[n][r]&&(_.c[n][r]=C_DEF[n][r],e++);void 0!==_.c.out&&(_.c.outs=[_.c.out],_.c.out=void 0),C_DEF=null,0<e?Shelly.call("KVS.Set",{key:"porssi-config",value:_.c},function(e,t,s,o){0!==t&&u("chkConfig() - virhe:"+t+" - "+s),o&&o(0===t)},t):t&&t(!0)}else t&&t(!0)}},e)}function A(){try{m||(m=!0,p(),_.s.configOK?e(0)?t(0):!function(){if(0==_.s.chkTs)return 1;var e=new Date,t=new Date(1e3*_.s.chkTs);return t.getHours()!=e.getHours()||t.getFullYear()!=e.getFullYear()||0<_.s.fCmdTs&&_.s.fCmdTs-l(e)<0||0==_.s.fCmdTs&&_.c.min<60&&e.getMinutes()>=_.c.min&&_.s.cmd+_.c.inv==1}()?e(1)?t(1):m=!1:f():d(!0))}catch(e){u("loop() - virhe:"+e),m=!1}}function e(e){var t=new Date;let s=!1;return s=1==e?_.s.timeOK&&0===_.s.p[1].ts&&15<=t.getHours():((e=n(new Date(1e3*_.s.p[0].ts))!==n(t))&&(_.s.p[1].ts=0,_.p[1]=[]),_.s.timeOK&&(0==_.s.p[0].ts||e)),_.s.errCnt>=C_ERRC&&l(t)-_.s.errTs<C_ERRD?s=!1:_.s.errCnt>=C_ERRC&&(_.s.errCnt=0),s}function t(i){try{let r=new Date;a(r);var t=1==i?new Date(864e5+new Date(r.getFullYear(),r.getMonth(),r.getDate()).getTime()):r;let e=t.getFullYear()+"-"+o(1+t.getMonth(),2,"0")+"-"+o(n(t),2,"0")+"T00:00:00"+_.s.tz.replace("+","%2b");var s=e.replace("T00:00:00","T23:59:59");let c={url:"https://dashboard.elering.ee/api/nps/price/csv?fields=fi&start="+e+"&end="+s,timeout:5,ssl_ca:"*"};r=null,e=null,Shelly.call("HTTP.GET",c,function(t,e,s){c=null;try{if(0!==e||null==t||200!==t.code||!t.body_b64)throw Error("virhe: "+e+"("+s+") - "+JSON.stringify(t));{t.headers=null,s=t.message=null,_.p[i]=[],_.s.p[i].avg=0,_.s.p[i].high=-999,_.s.p[i].low=999,t.body_b64=atob(t.body_b64),t.body_b64=t.body_b64.substring(1+t.body_b64.indexOf("\n"));let e=0;for(;0<=e;){t.body_b64=t.body_b64.substring(e);var o=[e=0,0];if(0===(e=1+t.body_b64.indexOf('"',e)))break;o[0]=+t.body_b64.substring(e,t.body_b64.indexOf('"',e)),e=2+t.body_b64.indexOf('"',e),e=2+t.body_b64.indexOf(';"',e),o[1]=+(""+t.body_b64.substring(e,t.body_b64.indexOf('"',e)).replace(",",".")),o[1]=o[1]/10*(100+(0<o[1]?_.c.vat:0))/100;var n=new Date(1e3*o[0]).getHours();o[1]+=7<=n&&n<22?_.c.day:_.c.night,_.p[i].push(o),_.s.p[i].avg+=o[1],o[1]>_.s.p[i].high&&(_.s.p[i].high=o[1]),o[1]<_.s.p[i].low&&(_.s.p[i].low=o[1]),e=t.body_b64.indexOf("\n",e)}if(t=null,_.s.p[i].avg=0<_.p[i].length?_.s.p[i].avg/_.p[i].length:0,_.s.p[i].ts=l(r),1==i&&_.p[i].length<23)throw Error("huomisen hintoja ei saatu")}}catch(e){u("getPrices() - virhe:"+e),_.s.errCnt+=1,_.s.errTs=l(),_.s.p[i].ts=0,_.p[i]=[]}1==i?m=!1:Timer.set(1e3,!1,f)})}catch(e){u("getPrices() - virhe:"+e),_.s.p[i].ts=0,_.p[i]=[],1==i?m=!1:Timer.set(1e3,!1,f)}}function f(){try{"function"==typeof USER_CONFIG&&(_.c=USER_CONFIG(_.c,_,!1)),r=!1;var e,t,s=new Date;function o(e){if(null==e)m=!1;else if(r!=e&&(_.s.st=12),r=e,_.c.inv&&(r=!r),1==_.c.oc&&_.s.cmd==r)u("logic(): lähtö on jo oikeassa tilassa"),h(),_.s.cmd=r?1:0,_.s.chkTs=l(),m=!1;else{let t=0,s=0;for(let e=0;e<_.c.outs.length;e++)!function(n,e){var t="{id:"+n+",on:"+(r?"true":"false")+"}";Shelly.call("Switch.Set",t,function(e,t,s,o){0!=t&&u("setRelay() - ohjaus #"+n+" epäonnistui: "+t+" - "+s),o(0==t)},e)}(_.c.outs[e],function(e){t++,e&&s++,t==_.c.outs.length&&(s==t&&(h(),_.s.cmd=r?1:0,_.s.chkTs=l()),m=!1)})}}a(s),!function(){if(_.s.timeOK&&0!=_.s.p[0].ts){var t=l();for(let e=0;e<_.p[0].length;e++)if(c(_.p[0][e][0],t))return _.s.p[0].now=_.p[0][e][1];return _.p[0].length<24&&(_.s.p[0].ts=0),_.s.p[0].now=0}_.s.p[0].now=0}(),0===_.c.mode?(r=1===_.c.m0.cmd,_.s.st=1):_.s.timeOK&&0<_.s.p[0].ts&&n(new Date(1e3*_.s.p[0].ts))===n(s)?1===_.c.mode?(r=_.s.p[0].now<=("avg"==_.c.m1.lim?_.s.p[0].avg:_.c.m1.lim),_.s.st=r?2:3):2===_.c.mode&&(r=function(){_.c.m2.ps=i(0,_.c.m2.ps,23),_.c.m2.pe=i(_.c.m2.ps,_.c.m2.pe,24),_.c.m2.ps2=i(0,_.c.m2.ps2,23),_.c.m2.pe2=i(_.c.m2.ps2,_.c.m2.pe2,24),_.c.m2.cnt=i(0,_.c.m2.cnt,0<_.c.m2.per?_.c.m2.per:_.c.m2.pe-_.c.m2.ps),_.c.m2.cnt2=i(0,_.c.m2.cnt2,_.c.m2.pe2-_.c.m2.ps2);var o=[];for(y=_.c.m2.per<0?1:_.c.m2.per,v=0;v<_.p[0].length;v+=y)if(!((C=-2==_.c.m2.per&&1<=v?_.c.m2.cnt2:_.c.m2.cnt)<=0)){var n=[];for(T=v,w=v+_.c.m2.per,_.c.m2.per<0&&0==v?(T=_.c.m2.ps,w=_.c.m2.pe):-2==_.c.m2.per&&1==v&&(T=_.c.m2.ps2,w=_.c.m2.pe2),b=T;b<w&&!(b>_.p[0].length-1);b++)n.push(b);if(_.c.m2.sq){let t=999,s=0;for(b=0;b<=n.length-C;b++){let e=0;for(g=b;g<b+C;g++)e+=_.p[0][n[g]][1];e/C<t&&(t=e/C,s=b)}for(b=s;b<s+C;b++)o.push(n[b])}else{for(b=0,g=1;g<n.length;g++){var e=n[g];for(b=g-1;0<=b&&_.p[0][e][1]<_.p[0][n[b]][1];b--)n[b+1]=n[b];n[b+1]=e}for(b=0;b<C;b++)o.push(n[b])}if(-1==_.c.m2.per||-2==_.c.m2.per&&1<=v)break}let t=l(),s=!1;for(let e=0;e<o.length;e++)if(c(_.p[0][o[e]][0],t)){s=!0;break}return s}(),_.s.st=r?5:4,!r&&_.s.p[0].now<=("avg"==_.c.m2.lim?_.s.p[0].avg:_.c.m2.lim)&&(r=!0,_.s.st=6),r)&&_.s.p[0].now>("avg"==_.c.m2.m?_.s.p[0].avg:_.c.m2.m)&&(r=!1,_.s.st=11):_.s.timeOK?(_.s.st=7,e=1<<s.getHours(),(_.c.bk&e)==e&&(r=!0)):(r=1===_.c.err,_.s.st=8),_.s.timeOK&&0<_.c.fh&&(t=1<<s.getHours(),(_.c.fh&t)==t)&&(r=(_.c.fhCmd&t)==t,_.s.st=10),r&&_.s.timeOK&&s.getMinutes()>=_.c.min&&(_.s.st=13,r=!1),_.s.timeOK&&0<_.s.fCmdTs&&(0<_.s.fCmdTs-l(s)?(r=1==_.s.fCmd,_.s.st=9):_.s.fCmdTs=0),"function"==typeof USER_OVERRIDE?USER_OVERRIDE(r,_,o):o(r)}catch(e){u("logic() - virhe:"+JSON.stringify(e)),m=!1}}let v=0,b=0,g=0,y=0,C=0,T=0,w=0;u("shelly-porssisahko v."+_.s.v),u("URL: http://"+(Shelly.getComponentStatus("wifi").sta_ip??"192.168.33.1")+"/script/"+Shelly.getCurrentScriptId()),HTTPServer.registerEndpoint("",function(s,o){try{if(m)return s=null,o.code=503,void o.send();var n=function(e){var t={},s=e.split("&");for(let e=0;e<s.length;e++){var o=s[e].split("=");t[o[0]]=o[1]}return t}(s.query);s=null;let e="application/json",t=(o.code=200,!0);var r="text/html",c="text/javascript";"s"===n.r?(p(),o.body=JSON.stringify(_),t=!1):"r"===n.r?(d(_.s.configOK=!1),_.s.p[0].ts=0,_.s.p[1].ts=0,o.code=204,t=!1):"f"===n.r&&n.ts?(_.s.fCmdTs=+(""+n.ts),_.s.fCmd=+(""+n.c),_.s.chkTs=0,o.code=204,t=!1):n.r?"s.js"===n.r?(o.body=atob("H4sIAAAAAAAACo1W4W7bNhB+FYXrAhJmCKft/thTjbbJ1nVJM9RegaEoFkaiI9o06ZAnt4art8mb5MV2lGRHCdx2f+KY/O7uu7vvjjYKkr/fn6WEcPwYx8+bkEL6IndZuVAWxE2p/HqsjMrAeQqMn7z6PaUsfbGp+HjycnL673jyPv1I/ry7XVurAyi4u727tYTHo6BdMZNlwG9vtAWZSGNU4uVM2t3R2uhHJ0onhTQrvVhqmaA7Y+5uk4iYyXlwxsgd8n/C6qhSW5ksI7kaeLSN+UH6RVkGKC20bBMaGaClm0mOdnN0r1XuQpAs+ry7xWigjdzCawjadFF/yfncHW0RP4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THaIryOxnJ97YOFUvqjtLustbALDcOmhyxQo7cpJaQbzhWkaCO5Djd4NHavojuWDateU4McVdfDPvUmC1JRFDqazPpWUrMq2L+BMXiQl30CZoPHGwkx2g8uFlBPNGc1/LSz9RekxbDNhRjKBV5L46IFqAJZde9lfxk5IUplz2csNrxP6XBjtZ5vlNcl8H8cd/wFnTmKS0gQAAA=="),e=r),o.headers=[["Content-Type",e]],t&&o.headers.push(["Content-Encoding","gzip"])}catch(e){u("http - virhe:"+e),o.code=500}o.send()}),Timer.set(1e4,!0,A),A();
/**
* Tämä esimerkki hyödyntää Shelly H&T:n lähettämää lämpötilaa pörssisähköohjausten asetuksissa
* Mitä kylmempi lämpötila, sitä useampi halvempi tunti ohjataan ja samalla myös ohjausminuuttien määrää kasvatetaan.
*
* Käyttöönotto:
* -----
* Lisää Shelly H&T-asetuksiin "actions -> sensor reports" -osoitteisiin osoite
* http://ip-osoite/script/1/update-temp
* missä ip-osoite on tämän shellyn osoite.
* Muista myös ottaa "sensor reports" -ominaisuus käyttöön
*/
//Kuinka vanha lämpötilatieto sallitaan ohjauksessa (tunteina)
let TEMPERATURE_MAX_AGE_HOURS = 12;
//Viimeisin tiedossa oleva lämpötiladata
let data = null;
//Alkuperäiset muokkaamattomat asetukset
let originalConfig = {
hours: 0,
minutes: 60
};
function USER_CONFIG(config, state, initialized) {
//Tallenentaan alkuperäiset asetukset muistiin
if (initialized) {
originalConfig.hours = config.m2.cnt;
originalConfig.minutes = config.min;
console.log("Alkuperäiset asetukset:", originalConfig);
}
//Käytetää lähtökohtaisesti alkuperäisiin asetuksiin tallennettua tuntimäärää ja ohjausminuutteja
//Näin ollen jos tallentaa asetukset käyttöliittymältä, tulee ne myös tähän käyttöön
let hours = originalConfig.hours;
let minutes = originalConfig.minutes;
try {
if (data == null) {
console.log("Lämpötilatietoa ei ole saatavilla");
state.s.str = "Lämpötila ei tiedossa -> halvat tunnit: " + hours + " h, ohjaus: " + minutes + " min";
} else {
let age = (Date.now() - data.ts) / 1000.0 / 60.0 / 60.0;
console.log("Lämpötila on tiedossa (päivittynyt " + age.toFixed(2) + " h sitten):", data);
if (age <= TEMPERATURE_MAX_AGE_HOURS * 60) {
//------------------------------
// Toimintalogiikka
// muokkaa haluamaksesi
//------------------------------
//Muutetaan lämpötilan perusteella lämmitystuntien määrää ja minuutteja
if (data.temp <= -15) {
hours = 8;
minutes = 60;
} else if (data.temp <= -10) {
hours = 7;
minutes = 45;
} else if (data.temp <= -5) {
hours = 6;
minutes = 45;
} else {
//Ei tehdä mitään --> käytetään käyttöliittymän asetuksia
}
//------------------------------
// Toimintalogiikka päättyy
//------------------------------
state.s.str = "Lämpötila " + data.temp.toFixed(1) + "°C (" + age.toFixed(1) + "h sitten) -> halvat tunnit: " + hours + " h, ohjaus: " + minutes + " min";
console.log("Lämpötila:", data.temp.toFixed(1), "°C -> asetettu halvimpien tuntien määräksi ", hours, "h ja ohjausminuuteiksi", minutes, "min");
} else {
console.log("Lämpötilatieto on liian vanha -> ei käytetä");
state.s.str = "Lämpötilatieto liian vanha (" + age.toFixed(1) + " h) -> halvat tunnit: " + hours + " h, ohjaus: " + minutes + " min";
}
}
} catch (err) {
state.s.str = "Virhe lämpötilaohjauksessa:" + err;
console.log("Virhe tapahtui USER_CONFIG-funktiossa. Virhe:", err);
}
//Asetetaan arvot asetuksiin
config.m2.cnt = hours;
config.min = minutes;
return config;
}
/**
* Apufunktio, joka kerää parametrit osoitteesta
*/
function parseParams(params) {
let res = {};
let splitted = params.split("&");
for (let i = 0; i < splitted.length; i++) {
let pair = splitted[i].split("=");
res[pair[0]] = pair[1];
}
return res;
}
/**
* Takaisinkutsu, joka suoritetaan kun saadaan HTTP-pyyntö
*/
function onHttpRequest(request, response) {
try {
let params = parseParams(request.query);
request = null;
if (params.temp != undefined) {
data = {
temp: Number(params.temp),
ts: Math.floor(Date.now())
};
console.log("Lämpötilatiedot päivitetty, pyydetään pörssisähkölogiikan ajoa. Data:", data);
_.s.chkTs = 0;
response.code = 200;
} else {
console.log("Lämpötilatiedojen päivitys epäonnistui, 'temp' puuttuu parametreista:", params);
response.code = 400;
}
response.send();
} catch (err) {
console.log("Virhe:", err);
}
}
//Rekisteröidään /script/x/update-temp -osoite
HTTPServer.registerEndpoint('update-temp', onHttpRequest);