-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmodules.lua
1035 lines (898 loc) · 32.1 KB
/
modules.lua
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
bibliothèque de fonctions pour domoticz
utiles à la réalisation de scripts d'automation en langage lua
/!\ certaines fonctions ne fonctionneront pas sous windows.
copier ce qui se trouve entre les 2 lignes ci dessous, en début de tout vos script
pour charger ce fichier et pouvoir en utiliser les fonctions
--------------------------------------------------------------------------------------------------------
-- chargement des modules (http://easydomoticz.com/forum/viewtopic.php?f=17&t=3940)
dofile('/home/pi/domoticz/scripts/lua/modules.lua')
local debug = true -- true pour voir les logs dans la console log Dz ou false pour ne pas les voir
--------------------------------------------------------------------------------------------------------
]]
--------------------------------
------ USER SETTINGS ------
--------------------------------
-- domoticz
domoticzIP = '192.168.22.100' --'127.0.0.1'
domoticzPORT = '8080'
domoticzUSER = '' -- nom d'utilisateur
domoticzPSWD = '' -- mot de pass
domoticzPASSCODE = '' -- pour interrupteur protégés
domoticzURL = 'http://'..domoticzIP..':'..domoticzPORT
--------------------------------
------ END ------
--------------------------------
-- chemin vers le dossier lua et curl
if (package.config:sub(1,1) == '/') then
-- system linux
luaDir = debug.getinfo(1).source:match("@?(.*/)")
curl = '/usr/bin/curl -m 15 ' -- ne pas oublier l'espace à la fin
else
-- system windows
luaDir = string.gsub(debug.getinfo(1).source:match("@?(.*\\)"),'\\','\\\\')
-- download curl : https://bintray.com/vszakats/generic/download_file?file_path=curl-7.54.0-win32-mingw.7z
curl = 'c:\\Programs\\Curl\\curl.exe ' -- ne pas oublier l'espace à la fin
end
-- chargement du fichier JSON.lua
json = assert(loadfile(luaDir..'JSON.lua'))()
--time.hour ou time.min ou time.sec
--ex : if (time.hour == 17 and time.min == 05) then
time = os.date("*t")
-- retourne l'heure actuelle ex: "12:45"
heure = string.sub(os.date("%X"), 1, 5)
-- retourne la date ex: "01:01"
date = os.date("%d:%m")
-- retourne l'heure du lever de soleil ex: "06:41"
leverSoleil = string.sub(os.date("!%X",60*timeofday['SunriseInMinutes']), 1, 5)
-- retourne l'heure du coucher de soleil ex: "22:15"
coucherSoleil = string.sub(os.date("!%X",60*timeofday['SunsetInMinutes']), 1, 5)
-- retourne le jour actuel en français ex: "mardi"
days = {"dimanche","lundi","mardi","mercredi","jeudi","vendredi","samedi"}
jour = days[(os.date("%w")+1)]
-- retourne VRAI si la semaine est paire
-- usage :
-- if semainePaire() then ..
function semainePaire()
local tm = os.time()
local function getYearBeginDayOfWeek()
yearBegin = os.time{year=os.date("*t",tm).year,month=1,day=1}
yearBeginDayOfWeek = tonumber(os.date("%w",yearBegin))
-- sunday correct from 0 -> 7
if(yearBeginDayOfWeek == 0) then yearBeginDayOfWeek = 7 end
return yearBeginDayOfWeek
end
local function getDayAdd()
yearBeginDayOfWeek = getYearBeginDayOfWeek(tm)
if(yearBeginDayOfWeek < 5 ) then
-- first day is week 1
dayAdd = (yearBeginDayOfWeek - 2)
else
-- first day is week 52 or 53
dayAdd = (yearBeginDayOfWeek - 9)
end
return dayAdd
end
dayOfYear = os.date("%j",tm)
dayAdd = getDayAdd(tm)
dayOfYearCorrected = dayOfYear + dayAdd
if(dayOfYearCorrected < 0) then
-- week of last year - decide if 52 or 53
lastYearBegin = os.time{year=os.date("*t",tm).year-1,month=1,day=1}
lastYearEnd = os.time{year=os.date("*t",tm).year-1,month=12,day=31}
dayAdd = getDayAdd(lastYearBegin)
dayOfYear = dayOfYear + os.date("%j",lastYearEnd)
dayOfYearCorrected = dayOfYear + dayAdd
end
weekNum = math.floor((dayOfYearCorrected) / 7) + 1
if( (dayOfYearCorrected > 0) and weekNum == 53) then
-- check if it is not considered as part of week 1 of next year
nextYearBegin = os.time{year=os.date("*t",tm).year+1,month=1,day=1}
yearBeginDayOfWeek = getYearBeginDayOfWeek(nextYearBegin)
if(yearBeginDayOfWeek < 5 ) then
weekNum = 1
end
end
return weekNum%2 == 0
end
-- il fait jour
dayTime = timeofday['Daytime']
-- il fait nuit
nightTime = timeofday['Nighttime']
-- température
function getTemp(device)
return round(tonumber(otherdevices_temperature[device]),1)
end
-- humidité
function getHum(device)
return round(tonumber(otherdevices_humidity[device]),1)
end
-- humidité moyenne
function humMoy(device)
local monthLog = assert(io.popen(curl..'-u '..domoticzUSER..':'..domoticzPSWD..' "'..domoticzURL..'/json.htm?range=month&sensor=temp&type=graph&idx='..otherdevices_idx[device]..'"'))
local list = monthLog:read('*all')
monthLog:close()
local data = ReverseTable(json:decode(list).result)
return(round(data[1].hu))
end
-- humidité absolue
function humAbs(t,hr)
-- https://carnotcycle.wordpress.com/2012/08/04/how-to-convert-relative-humidity-to-absolute-humidity/
-- Formule pour calculer l'humidité absolue
-- Dans la formule ci-dessous, la température (T) est exprimée en degrés Celsius, l'humidité relative (hr) est exprimée en%, et e est la base des logarithmes naturels 2.71828 [élevée à la puissance du contenu des crochets]:
-- Humidité absolue (grammes / m3 ) = (6,122 * e^[(17,67 * T) / (T + 243,5)] * rh * 2,1674))/(273,15 + T)
-- Cette formule est précise à 0,1% près, dans la gamme de température de -30 ° C à + 35 ° C
return round((6.112 * math.exp((17.67 * t)/(t+243.5)) * hr * 2.1674)/ (273.15 + t),1)
end
-- set setpoint (faster way)
function setPoint(device,value)
os.execute(curl..'-u '..domoticzUSER..':'..domoticzPSWD..' "'..domoticzURL..'/json.htm?type=command¶m=udevice&idx='..otherdevices_idx[device]..'&nvalue=0&svalue='..value..'" &')
end
function dimUp15(device)
-- 15 step
switchOn(device, constrain(otherdevices_svalues[device]+1,1,15))
end
function dimDown15(device)
-- 15 step
switchOn(device, constrain(otherdevices_svalues[device]-1,1,15))
end
function dimUp(device)
-- 100 step
switchOn(device, constrain(otherdevices_svalues[device]+10,10,100))
end
function dimDown(device)
-- 100 step
switchOn(device, constrain(otherdevices_svalues[device]-10,10,100))
end
-- vérifie s'il y a eu changement d'état
function stateChange(device)
if (uservariables['lastState_'..device] == nil) then
creaVar('lastState_'..device,otherdevices[device])
log('stateChange : création variable manquante lastState_'..device,debug)
return false
elseif (devicechanged[device] == nil) then
return false
elseif (devicechanged[device] == uservariables['lastState_'..device]) then
return false
else
duree = lastSeen(uservariables_lastupdate['lastState_'..device])
updateVar('lastState_'..device,otherdevices[device])
return otherdevices[device]
end
end
-- convertion degrés en direction cardinale
function wind_cardinals(deg)
local cardinalDirections = {
['N'] = {348.75, 360},
['N'] = {0, 11.25},
['NNE'] = {11.25, 33.75},
['NE'] = {33.75, 56.25},
['ENE'] = {56.25, 78.75},
['E'] = {78.75, 101.25},
['ESE'] = {101.25, 123.75},
['SE'] = {123.75, 146.25},
['SSE'] = {146.25, 168.75},
['S'] = {168.75, 191.25},
['SSW'] = {191.25, 213.75},
['SW'] = {213.75, 236.25},
['WSW'] = {236.25, 258.75},
['W'] = {258.75, 281.25},
['WNW'] = {281.25, 303.75},
['NW'] = {303.75, 326.25},
['NNW'] = {326.25, 348.75}
}
local cardinal
for dir, angle in pairs(cardinalDirections) do
if (deg >= angle[1] and deg < angle[2]) then
cardinal = dir
break
end
end
return cardinal
end
-- dump all variables supplied to the script
-- usage
-- LogVariables(_G,0,'')
function LogVariables(x,depth,name)
for k,v in pairs(x) do
if (depth>0) or ((string.find(k,'device')~=nil) or (string.find(k,'variable')~=nil) or
(string.sub(k,1,4)=='time') or (string.sub(k,1,8)=='security')) then
if type(v)=="string" then print(name.."['"..k.."'] = '"..v.."'") end
if type(v)=="number" then print(name.."['"..k.."'] = "..v) end
if type(v)=="boolean" then print(name.."['"..k.."'] = "..tostring(v)) end
if type(v)=="table" then LogVariables(v,depth+1,k); end
end
end
end
-- os.execute() output or web page content return
-- usage
-- local resultat = os.capture(cmd , true)
-- print('resultat: ' .. resultat)
function os.capture(cmd, raw)
local f = assert(io.popen(cmd, 'r'))
local s = assert(f:read('*a'))
f:close()
if raw then return s end
s = string.gsub(s, '^%s+', '')
s = string.gsub(s, '%s+$', '')
s = string.gsub(s, '[\n\r]+', ' ')
return s
end
-- retourne le type de la variable
-- 'string' , 'number' , 'table'
function typeof(var)
local _type = type(var);
if(_type ~= "table" and _type ~= "userdata") then
return _type;
end
local _meta = getmetatable(var);
if(_meta ~= nil and _meta._NAME ~= nil) then
return _meta._NAME;
else
return _type;
end
end
-- affiche les logs en bleu sauf si debug est spécifié à false
function log(txt,debug)
if (debug ~= false) then
--print("<font color='#0206a9'>"..txt.."</font>")
print(txt)
end
end
-- affiche les logs en rouge sauf si debug est spécifié à false
function warn(txt,debug)
if (debug ~= false) then
--print("<font color='red'>"..txt.."</font>")
print(txt)
end
end
-- écriture dans un fichier texte dans le dossier lua
function logToFile(fileName,data)
f = assert(io.open(luaDir..fileName..'.txt',"a"))
f:write(os.date("%c")..' '..data..'\n')
f:close()
end
-- teste l'existance d'un fichier
function file_exists(file)
local f = io.open(file, "rb")
if f then f:close() end
return f ~= nil
end
-- retourne le nom du switch selon son IDX
function getDeviceName(deviceIDX)
for i, v in pairs(otherdevices_idx) do
if v == deviceIDX then
return i
end
end
return 0
end
-- get all lines from a file, returns an empty
-- list/table if the file does not exist
function lines_from(file)
if not file_exists(luaDir..file..'.txt') then return {} end
lines = {}
for line in io.lines(luaDir..file..'.txt') do
lines[#lines + 1] = line
end
return lines
end
-- encode du texte pour le passer dans une url
function url_encode(str)
if (str) then
str = string.gsub (str, "\n", "\r\n")
str = string.gsub (str, "([^%w %-%_%.%~])",
function (c) return string.format ("%%%02X", string.byte(c)) end)
str = string.gsub (str, " ", "+")
end
return str
end
-- supprime les accents de la chaîne
function sans_accent(str)
if (str) then
str = string.gsub (str,"Ç", "C")
str = string.gsub (str,"ç", "c")
str = string.gsub (str,"[-èéêë']+", "e")
str = string.gsub (str,"[-ÈÉÊË']+", "E")
str = string.gsub (str,"[-àáâãäå']+", "a")
str = string.gsub (str,"[-@ÀÁÂÃÄÅ']+", "A")
str = string.gsub (str,"[-ìíîï']+", "i")
str = string.gsub (str,"[-ÌÍÎÏ']+", "I")
str = string.gsub (str,"[-ðòóôõö']+", "o")
str = string.gsub (str,"[-ÒÓÔÕÖ']+", "O")
str = string.gsub (str,"[-ùúûü']+", "u")
str = string.gsub (str,"[-ÙÚÛÜ']+", "U")
str = string.gsub (str,"[-ýÿ']+", "y")
str = string.gsub (str,"Ý", "Y")
end
return str
end
-- retourne le temps en seconde depuis la dernière maj du péréphérique
function lastSeen(device)
timestamp = otherdevices_lastupdate[device] or device
y, m, d, H, M, S = timestamp:match("(%d+)-(%d+)-(%d+) (%d+):(%d+):(%d+)")
difference = os.difftime(os.time(), os.time{year=y, month=m, day=d, hour=H, min=M, sec=S})
return difference
end
-- contraindre
function constrain(x, a, b)
if (x < a) then
return a
elseif (x > b) then
return b
else
return x
end
end
-- arrondire
function round(num, dec)
if num == 0 then
return 0
else
local mult = 10^(dec or 0)
return math.floor(num * mult + 0.5) / mult
end
end
-- met le script en pause (fortement déconseillé)
-- usage
-- sleep(10) -- pour mettre en pause 10 secondes
function sleep(n)
os.execute('sleep '..n)
end
-- création de variable utilisateur
-- usage
-- creaVar('toto','10') -- pour créer une variable nommée toto comprenant la valeur 10
function creaVar(name,value)
local api = '/json.htm?type=command¶m=adduservariable'
local name = '&vname='..url_encode(name)
local vtype = '&vtype=2'
local value = '&vvalue='..url_encode(value)
api = api..name..vtype..value
os.execute(curl..'-u '..domoticzUSER..':'..domoticzPSWD..' "'..domoticzURL..api..'" &')
end
-- update an existing variable
function updateVar(name,value)
local api = '/json.htm?type=command¶m=updateuservariable'
local name = '&vname='..url_encode(name)
local vtype = '&vtype=2'
local value = '&vvalue='..url_encode(value)
api = api..name..vtype..value
os.execute(curl..'-u '..domoticzUSER..':'..domoticzPSWD..' "'..domoticzURL..api..'" &')
end
-- envoie dans un capteur text une chaîne de caractères
-- le text sera intercepté et lu par la custom page grâce à sa fonction MQTT
-- usage
-- speak('tts','bonjour nous sommes dimanche')
function speak(TTSDeviceName,txt)
commandArray[#commandArray+1] = {['OpenURL'] = domoticzIP..":"..domoticzPORT..'/json.htm?type=command¶m=udevice&idx='..otherdevices_idx[TTSDeviceName]..'&nvalue=0&svalue='..url_encode(txt)}
end
-- récupère les infos json du périphérique
-- usage
-- local lampe = jsonInfos('ma lampe')
-- print(lampe.Name)
-- print(lampe.Status)
-- etc..
function jsonInfos(device)
local rid = assert(io.popen(curl..'-u '..domoticzUSER..':'..domoticzPSWD..' "'..domoticzURL..'/json.htm?type=devices&rid='..otherdevices_idx[device]..'"'))
local list = rid:read('*all')
rid:close()
return json:decode(list).result[1]
end
-- parcours la table dans l'ordre
function spairs(t)
local keys = {}
for k in pairs(t) do keys[#keys+1] = k end
table.sort(keys)
local i = 0
return function()
i = i + 1
if keys[i] then
return keys[i], t[keys[i]]
end
end
end
-- Renverse une table
function ReverseTable(t)
local reversedTable = {}
local itemCount = #t
for k, v in ipairs(t) do
reversedTable[itemCount + 1 - k] = v
end
return reversedTable
end
-- affiche le contenu d'une table
--[[
Author: Julio Manuel Fernandez-Diaz
Date: January 12, 2007
(For Lua 5.1)
Modified slightly by RiciLake to avoid the unnecessary table traversal in tablecount()
Formats tables with cycles recursively to any depth.
The output is returned as a string.
References to other tables are shown as values.
Self references are indicated.
The string returned is "Lua code", which can be procesed
(in the case in which indent is composed by spaces or "--").
Userdata and function keys and values are shown as strings,
which logically are exactly not equivalent to the original code.
This routine can serve for pretty formating tables with
proper indentations, apart from printing them:
print(table_show(t, "t")) -- a typical use
Heavily based on "Saving tables with cycles", PIL2, p. 113.
Arguments:
t is the table.
name is the name of the table (optional)
indent is a first indentation (optional).
]]
function table_show(t, name, indent)
local cart -- a container
local autoref -- for self references
--[[ counts the number of elements in a table
local function tablecount(t)
local n = 0
for _, _ in pairs(t) do n = n+1 end
return n
end
]]
-- (RiciLake) returns true if the table is empty
local function isemptytable(t) return next(t) == nil end
local function basicSerialize (o)
local so = tostring(o)
if type(o) == "function" then
local info = debug.getinfo(o, "S")
-- info.name is nil because o is not a calling level
if info.what == "C" then
return string.format("%q", so .. ", C function")
else
-- the information is defined through lines
return string.format("%q", so .. ", defined in (" ..
info.linedefined .. "-" .. info.lastlinedefined ..
")" .. info.source)
end
elseif type(o) == "number" or type(o) == "boolean" then
return so
else
return string.format("%q", so)
end
end
local function addtocart (value, name, indent, saved, field)
indent = indent or ""
saved = saved or {}
field = field or name
cart = cart .. indent .. field
if type(value) ~= "table" then
cart = cart .. " = " .. basicSerialize(value) .. ";\n"
else
if saved[value] then
cart = cart .. " = {}; -- " .. saved[value]
.. " (self reference)\n"
autoref = autoref .. name .. " = " .. saved[value] .. ";\n"
else
saved[value] = name
--if tablecount(value) == 0 then
if isemptytable(value) then
cart = cart .. " = {};\n"
else
cart = cart .. " = {\n"
for k, v in pairs(value) do
k = basicSerialize(k)
local fname = string.format("%s[%s]", name, k)
field = string.format("[%s]", k)
-- three spaces between levels
addtocart(v, fname, indent .. " ", saved, field)
end
cart = cart .. indent .. "};\n"
end
end
end
end
name = name or "table"
if type(t) ~= "table" then
return name .. " = " .. basicSerialize(t)
end
cart, autoref = "", ""
addtocart(t, name, indent)
return cart .. autoref
end
-- retourne la table des derniers log (première ligne = dernier log)
function lastLogEntry()
local rid = assert(io.popen(curl..'-u '..domoticzUSER..':'..domoticzPSWD..' "'..domoticzURL..'/json.htm?type=command¶m=getlog"'))
local list = rid:read('*all')
rid:close()
local tableau = json:decode(list).result
return ReverseTable(tableau)
end
-- notification pushbullet
-- usage:
-- pushbullet('test','ceci est un message test')
function pushbullet(title,body)
local settings = assert(io.popen(curl..'-u '..domoticzUSER..':'..domoticzPSWD..' "'..domoticzURL..'/json.htm?type=settings"'))
local list = settings:read('*all')
settings:close()
local pushbullet_key = json:decode(list).PushbulletAPI
os.execute(curl..'-H \'Access-Token:'..pushbullet_key..'\' -H \'Content-Type:application/json\' --data-binary \'{"title":"'..title..'","body":"'..body..'","type":"note"}\' -X POST "https://api.pushbullet.com/v2/pushes"')
end
-- switch On a device and set level if dimmmable
function switchOn(device,level)
local api = '/json.htm?type=command¶m=switchlight'
local idx = '&idx='..otherdevices_idx[device]
local cmd
if level ~= nil then
cmd = '&switchcmd=Set%20Level&level='..level
else
cmd = '&switchcmd=On'
end
local passcode = '&passcode='..domoticzPASSCODE
api = api..idx..cmd..passcode
os.execute(curl..'-u '..domoticzUSER..':'..domoticzPSWD..' "'..domoticzURL..api..'" &')
end
-- switch On a devive for x secondes
function switchOnFor(device, secs)
switchOn(device)
commandArray[#commandArray+1] = {[device] = "Off AFTER "..secs}
end
-- switch Off a device
function switchOff(device)
local api = '/json.htm?type=command¶m=switchlight'
local idx = '&idx='..otherdevices_idx[device]
local cmd = '&switchcmd=Off'
local passcode = '&passcode='..domoticzPASSCODE
api = api..idx..cmd..passcode
os.execute(curl..'-u '..domoticzUSER..':'..domoticzPSWD..' "'..domoticzURL..api..'" &')
end
-- Toggle a device
function switch(device)
local api = '/json.htm?type=command¶m=switchlight'
local idx = '&idx='..otherdevices_idx[device]
local cmd = '&switchcmd=Toggle'
local passcode = '&passcode='..domoticzPASSCODE
api = api..idx..cmd..passcode
os.execute(curl..'-u '..domoticzUSER..':'..domoticzPSWD..' "'..domoticzURL..api..'" &')
end
-- switch On a group or scene
function groupOn(device)
local api = '/json.htm?type=command¶m=switchscene'
local idx = '&idx='..otherdevices_scenesgroups_idx[device]
local cmd = '&switchcmd=On'
local passcode = '&passcode='..domoticzPASSCODE
api = api..idx..cmd..passcode
os.execute(curl..'-u '..domoticzUSER..':'..domoticzPSWD..' "'..domoticzURL..api..'" &')
end
-- switch Off a group
function groupOff(device)
local api = '/json.htm?type=command¶m=switchscene'
local idx = '&idx='..otherdevices_scenesgroups_idx[device]
local cmd = '&switchcmd=Off'
local passcode = '&passcode='..domoticzPASSCODE
api = api..idx..cmd..passcode
os.execute(curl..'-u '..domoticzUSER..':'..domoticzPSWD..' "'..domoticzURL..api..'" &')
end
-- Set switch to Stop
function switchStop(device)
local api = '/json.htm?type=command¶m=switchlight'
local idx = '&idx='..otherdevices_idx[device]
local cmd = '&switchcmd=Stop'
local passcode = '&passcode='..domoticzPASSCODE
api = api..idx..cmd..passcode
os.execute(curl..'-u '..domoticzUSER..':'..domoticzPSWD..' "'..domoticzURL..api..'" &')
end
-- Setup a color & brightness of an RGB(W) light
-- API : https://www.domoticz.com/wiki/Domoticz_API/JSON_URL%27s#Set_an_RGB.28W.29_light_to_a_certain_color_and_brightness
function setColorAndBrightness(device, color, brightness)
local api = '/json.htm?type=command¶m=setcolbrightnessvalue'
local idx = '&idx='..otherdevices_idx[device]
--local color = '&hue='..color
local color = '&hex='..color
local brightness = '&brightness='..brightness
local iswhite = '&iswhite=false'
local passcode = '&passcode='..domoticzPASSCODE
api = api..idx..color..brightness..iswhite..passcode
os.execute(curl..'-u '..domoticzUSER..':'..domoticzPSWD..' "'..domoticzURL..api..'" &')
end
function KelvinToRGB(temp)
-- http://www.tannerhelland.com/4435/convert-temperature-rgb-algorithm-code/
temp = temp/100
local red, green, blue
--Calculate Red:
if temp <= 66 then
red = 255
else
red = constrain(round(329.698727446 * ((temp - 60) ^ -0.1332047592)),0,255)
end
--Calculate Green:
if temp <= 66 then
green = constrain(round(99.4708025861 * math.log(temp) - 161.1195681661),0,255)
else
green = constrain(round(288.1221695283 * ((temp - 60) ^ -0.0755148492)),0,255)
end
--Calculate Blue:
if temp >= 66 then
blue = 255
else
if temp <= 19 then
blue = 0
else
blue = constrain(round(138.5177312231 * math.log(temp - 10) - 305.0447927307),0,255)
end
end
return {red,green,blue}
end
function RGBToHex(rgb)
-- https://gist.github.com/marceloCodget/3862929
local hexadecimal = ''
for key, value in pairs(rgb) do
local hex = ''
while(value > 0)do
local index = math.fmod(value, 16) + 1
value = math.floor(value / 16)
hex = string.sub('0123456789ABCDEF', index, index) .. hex
end
if(string.len(hex) == 0)then
hex = '00'
elseif(string.len(hex) == 1)then
hex = '0' .. hex
end
hexadecimal = hexadecimal .. hex
end
return hexadecimal
end
function suntimeToKelvin()
-- http://easydomoticz.com/forum/viewtopic.php?f=10&t=6160
local mini = 1900
local maxi = 6600
local delta = maxi - mini
local wakeup = 60*timeofday['SunriseInMinutes']
local goodnight = 60*timeofday['SunsetInMinutes']
local periode = goodnight - wakeup
local offset = wakeup-periode/2
local color = mini
local time = os.date("*t")
local now = 60*(time.hour*60 + time.min)
if now >= wakeup and now < goodnight then
color = math.floor((maxi-delta/2)+(delta/2)*math.cos((now-offset)*2*math.pi/periode)+0.5)
end
return color
end
-- régulation chauffage (PID)
--[[
usage:
local pid={}
pid['debug'] = true -- true pour voir les logs dans la console log Dz ou false pour ne pas les voir
pid['zone'] = 'salon' -- nom de la zone pour affichage dans les logs et ditinction de variables
pid['sonde'] = 'salon' -- Nom de la sonde de température
pid['OnOff'] = 'chauffage' -- Nom de l'interrupteur virtuel de mise en route (hivers/été)
pid['thermostat'] = 'th_salon' -- consigne ou 'nom' de l'interrupteur virtuel de thermostat
-- actionneur
pid['radiateur'] = 'radiateur salon' -- Nom de l'interrupteur de chauffage
pid['invert'] = false -- si On et Off doivent être inversé ou non
-- PID --
pid['Kp'] = 70 -- Coefficient proportionnel
pid['Ki'] = 8 -- Coefficient intégrateur
pid['Kd'] = 3 -- Coefficient dérivateur
pid['cycle'] = 15 -- temps en minute d'un cycle PID
pid['secu'] = 60 -- temps mini en seconde entre 2 ordres opposés
commandArray = {}
compute(pid)
return commandArray
]]
function compute(pid)
local init = 0
-- récupération température
local temp = getTemp(pid['sonde'])
-- récupération température ext
local temp_ext = nil
if pid['sonde_ext'] ~= '' and pid['sonde_ext'] ~= nil then
temp_ext = getTemp(pid['sonde_ext'])
end
-- création variable : 4 dernières températures
if (uservariables['PID_temps_'..pid['zone']] == nil ) then
creaVar('PID_temps_'..pid['zone'],string.rep(temp..';',3)..temp)
init = 1
end
-- création variable : intégrale
if (uservariables['PID_integrale_'..pid['zone']] == nil ) then
creaVar('PID_integrale_'..pid['zone'],'0')
init = 1
end
if init == 1 then
log('PID '..pid['zone']..' initialisation..',pid['debug'])
do return end
end
-- définition des variables locales
local moy_erreur = 0
local n = 1
local somme_erreurs = 0
local heatTime
local marche
local arret
local tmp = {}
-- définition des commandes marche/arrêt
if pid['invert'] then
marche = 'Off' ; arret = 'On'
else
marche = 'On' ; arret = 'Off'
end
-- à chaque cycle
if ( time.min%pid['cycle'] == 0 ) then
-- maj des 4 dernières temps
local temps = string.match(uservariables['PID_temps_'..pid['zone']],";([^%s]+)")..";"..temp
commandArray[#commandArray+1] = {['Variable:PID_temps_'..pid['zone']] = temps}
-- si l'on veut chauffer
if ( otherdevices[pid['OnOff']] == 'On' ) then
-- récupération de la consigne
local consigne = tonumber(otherdevices_svalues[pid['thermostat']]) or pid['thermostat']
-- calcul de l'erreur
local erreur = consigne-temp
-- calcul intégrale auto consumée et moyenne erreur glissante
temps:gsub("([+-]?%d+%.*%d*)",function(t)
tmp[n] = tonumber(t)
err = tonumber(consigne-t)
somme_erreurs = somme_erreurs+err
moy_erreur = moy_erreur+err*n^3
n = n+1
end)
somme_erreurs = round(constrain(somme_erreurs,0,255),1)
moy_erreur = round(moy_erreur/100,2)
-- calcul de la dérivée (régression linéaire - méthode des moindres carrés)
local delta_erreurs = round((4*(4*tmp[1]+3*tmp[2]+2*tmp[3]+tmp[4])-10*(tmp[1]+tmp[2]+tmp[3]+tmp[4]))/20,2)
-- aux abords de la consigne, passage au second systême integrale
if math.abs(somme_erreurs) < 2 then
somme_erreurs = tonumber(uservariables['PID_integrale_'..pid['zone']])
-- re calcule intégrale si hors hysteresis
-- à moins d'un dixièmes de degré d'écart avec la consigne
-- le ratrapage est considéré OK, l'intégrale n'est pas recalculée
if math.abs(erreur) > 0.11 then
-- calcule intégrale
somme_erreurs = round(constrain(somme_erreurs+erreur/2,0,5),2)
-- maj
commandArray[#commandArray+1] = {['Variable:PID_integrale_'..pid['zone']] = tostring(somme_erreurs)}
end
end
-- boucle ouverte,
-- modification dynamique des paramètres de régulation suivant température extérieure
local Kb = 0
if temp_ext ~= nil then
Kb = pid['Kb'] * (consigne - temp_ext - pid['ref']) / 100
end
pid['Kp'] = round(pid['Kp'] + pid['Kp'] * Kb)
pid['Ki'] = round(pid['Ki'] + pid['Ki'] * Kb)
pid['Kd'] = round(pid['Kd'] + pid['Kd'] * Kb)
-- calcul pid
local P = round(pid['Kp']*moy_erreur,2)
local I = round(pid['Ki']*somme_erreurs,2)
local D = round(pid['Kd']*delta_erreurs,2)
-- calcul de la commande en %
local commande = round(constrain(P+I+D,0,100))
-- calcul du temps de fonctionnement
if commande == 100 then
-- débordement de 20s pour ne pas couper avant recalcule
heatTime = (pid['cycle']*60)+20
elseif commande > 0 then
-- secu mini maxi
heatTime = round(constrain(commande*pid['cycle']*0.6,pid['secu'],(pid['cycle']*60)-pid['secu']))
elseif commande == 0 then
-- coupure retardée
heatTime = constrain(pid['secu']-lastSeen(pid['radiateur']),0,pid['secu'])
end
-- AFTER n'aime pas 1 ou 2..
if heatTime == 1 or heatTime == 2 then
heatTime = 0
end
-- action sur l'élément chauffant
if heatTime > 0 then
commandArray[#commandArray+1] = {[pid['radiateur']] = marche}
commandArray[#commandArray+1] = {[pid['radiateur']] = arret..' AFTER '..heatTime}
else
commandArray[#commandArray+1] = {[pid['radiateur']]=arret}
end
-- journalisation
if pid['debug'] then
log('PID zone: '..string.upper(pid['zone']))
if temp_ext ~= nil then
log('temperature ext: '..temp_ext..'°C')
end
log('température int: '..temp..'°C pour '..consigne..'°C souhaité')
log('Kp: '..pid['Kp'])
log('Ki: '..pid['Ki'])
log('Kd: '..pid['Kd'])
log('erreur: '..moy_erreur)
log('somme erreurs: '..somme_erreurs)
log('delta erreurs: '..delta_erreurs)
log('P: '..P)
log('I: '..I)
log('D: '..D)
log('cycle: '..pid['cycle']..'min (sécu: '..pid['secu']..'s)')
-- avertissement si secu dépasse 1/4 du cycle
if ((100*pid['secu'])/(60*pid['cycle'])>25) then
warn('sécu trop importante, ralonger durée de cycle..')
end
log('commande: '..commande..'% ('..string.sub(os.date("!%X",heatTime),4,8):gsub("%:", "\'")..'\")')
log('')
end
-- maj sonde virtuelle
--commandArray[#commandArray+1] = {['UpdateDevice'] = otherdevices_idx[pid['sonde']..'_pid']..'|0|'..temp..';'..commande..';0'}
end
end
-- toutes les 15 minutes, si on ne veut pas chauffer
if ( time.min%15 == 0 and otherdevices[pid['OnOff']] == 'Off' ) then
-- arrêt chauffage (renvoi commande systematique par sécurité)
commandArray[#commandArray+1] = {[pid['radiateur']] = arret..' AFTER '..constrain(pid['secu']-lastSeen(pid['radiateur']),3,pid['secu'])}
-- maj sonde virtuelle
--commandArray[#commandArray+1] = {['UpdateDevice'] = otherdevices_idx[pid['sonde']..'_pid']..'|0|'..temp..';0;0'}
end
end
-- détermination automatique des paramètres de régulation PID
function autotune(pid)
-- http://brettbeauregard.com/blog/2012/01/arduino-pid-autotune-library/
if devicechanged[pid['sonde']] then
-- définition des commandes marche/arrêt
if pid['invert'] then
marche = 'Off' ; arret = 'On'
else
marche = 'On' ; arret = 'Off'
end
-- récupération température
local temp = getTemp(pid['sonde'])
-- récupération consigne
local consigne = tonumber(otherdevices_svalues[pid['thermostat']]) or pid['thermostat']
-- hysteresis
if temp > consigne then
commandArray[#commandArray+1] = {[pid['radiateur']] = arret}
elseif temp < consigne then
commandArray[#commandArray+1] = {[pid['radiateur']] = marche}
end
-- timestamp
local now = os.time()
-- save all temps
logToFile(pid['zone']..'_temps',now..';'..temp)
local max1 = consigne
local max2 = consigne
local mini = consigne
local max1_ts = now
local max2_ts = now
local mini_ts = now
local init = 0
-- recherche des valeurs mini/maxi
for _,v in spairs(ReverseTable(lines_from(pid['zone']..'_temps'))) do
t = tonumber(string.match(v,";([^%s]+)"))
ts = tonumber(string.match(v,"([^%s]+);"))
if t < mini and init == 0 then
init = 1
elseif t > max2 and (init == 1 or init == 2) then
init = 2
max2 = t
max2_ts = ts
elseif t < mini and (init == 2 or init == 3) then
init = 3
mini = t