Repository navigation
Expand file tree
/
Copy pathPrecioPVPC.txt
More file actions
126 lines (105 loc) · 4.82 KB
/
Copy pathPrecioPVPC.txt
File metadata and controls
126 lines (105 loc) · 4.82 KB
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
-- Este script baja la lista de precios de tarifa PVPC 2.0 TD con discrminación horaria y guarda en una
-- variable (lowCostEnergyHour) la hora a la que la energía tiene el mejor precio al cabo del día
-- Es necesario tener creada la variable lowCostEnergyHour como tipo string
-- Para cargar el precio de cada hora tenemos también que crear 24 variables
-- PVPC_0, PVPC_1, PVPC_2 .... así hasta PVPC_23
-- Crearemos otro dummie, de tipo texto, que se llame "Precio Luz" que cada hora se actualizará con el precio en ese momento de la Luz
-- Crearemos otro dummie, de tipo custom sensor que se llame "PrecioPVPC"
-- Tanto los PVPC_XX como el "lowCostEnergyHour" serán un variables tipo texto
-- Autor @ea4gkq
-- Visita www.domotuto.com
return {
on = {
timer = {
'at 00:00',
'every hour'
},
httpResponses = {
'pvpcCallback' -- must match with the callback passed to the openURL command
}
},
execute = function(domoticz, item)
if (item.isTimer) then
domoticz.log('Script PVCP raised by timer', domoticz.LOG_ERROR)
local Time = require('Time')
local now = Time()
local precioLuz = domoticz.devices('Precio Luz').text
if(now.hour == 0 or precioLuz == '999' ) then
for i = 1,24,1
do
hora = i-1
local someValue = 999
print('Reset valor PVPC '..tostring(i-1)..'h '..someValue)
local variableName = 'PVPC_'..hora
domoticz.variables(variableName).set(someValue)
end
domoticz.log('Llamada API PVPC', domoticz.LOG_ERROR)
domoticz.openURL({
url = 'https://api.esios.ree.es/archives/70/download_json?locale=es',
method = 'GET',
callback = 'pvpcCallback', -- see httpResponses above.
})
end
local variableName = 'PVPC_'..now.hour
--domoticz.log('Variable: '..variableName, domoticz.LOG_ERROR)
local precioActual = domoticz.variables(variableName).value
--domoticz.log('precioActual: '..precioActual, domoticz.LOG_ERROR)
--domoticz.log('Precio actual: '..precioActual, domoticz.LOG_ERROR)
local precioFloat = 0
precioFloat = tonumber(precioActual)+0.0
if(precioFloat<900)then
domoticz.devices('PrecioPVPC').updateCustomSensor(precioFloat)
else
domoticz.devices('PrecioPVPC').updateCustomSensor(0)
end
precioActual = tostring(precioActual)
--domoticz.log('Precio actual: '..precioActual, domoticz.LOG_ERROR)
domoticz.devices('Precio Luz').updateText(precioActual)
end
if (item.isHTTPResponse) then
print('Respuesta API PVPC '..item.statusCode)
if (item.statusCode == 200) then
--print("Es Json: "..tostring(item.isJSON))
if (item.isJSON) then
--print("entra 1")
local precioEco = 999999
local horaEco = 0
local hora = 0
for i = 1,24,1
do
hora = i-1
local someValue = item.json.PVPC[i]["PCB"] -- just an example
someValue = someValue:gsub(',','.')
someValue = tonumber(someValue)/10
print('Valor PVPC '..tostring(i-1)..'h '..someValue)
if someValue<precioEco then
precioEco = someValue
horaEco = hora
end
--if hora <= 12 then
local variableName = 'PVPC_'..hora
domoticz.variables(variableName).set(someValue)
--end
if hora == 0 then
domoticz.log('Precio actual: '..precioEco, domoticz.LOG_ERROR)
domoticz.devices('Precio Luz').updateText(precioEco)
end
end
local precioEcoTXT = tostring(precioEco)
--precioEcoTXT = precioEcoTXT:gsub('.', ',')
local texto = 'Precio más bajo PVPC 2.0 DHA a las *'..tonumber(horaEco)..' hrs* a *'..precioEcoTXT..' centimos/kWh*'
print(texto)
domoticz.openURL({
url = "https://api.telegram.org/bot111111111:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/sendMessage?chat_id=-111111111&parse_mode=Markdown&text="..texto,
method = 'GET'
})
--Actualizamos la variable con la hora de tarifa más económica para hoy
domoticz.variables('lowCostEnergyHour').set(horaEco)
end
else
domoticz.log('There was a problem handling the request', domoticz.LOG_ERROR)
domoticz.log(item, domoticz.LOG_ERROR)
end
end
end
}