I'm trying to create a script which calculates the yield of my solar boiler system in kWh. I'm measuring and registering boiler temperatures on 2 seperate Domoticz devices (Temp_Upper_IDX & Temp_Bottom_IDX). I've created a virtual device to store the yield. I've used pieces of code of a script for measuring degree days, specifically the JSON decode part.
To calculate the yield in kWh I'm measuring the delta temperatures between the day minus and maximum temperature for the two temp sensors. These two values are plussed and after that divided by 2. I have a 300 liter boiler in which heating up all 300 liters takes 0.34 kWh, so the value is multiplied by 0.34.
The values tm and te represent the minimum and maximum temperatures for the sensor. If I browse directly to the JSON url I get the following values:
Code: Select all
{
"d" : "2018-09-20",
"hu" : "51",
"ta" : 64.879999999999995,
"te" : 73.299999999999997,
"tm" : 58.700000000000003
},
{
"d" : "2018-09-21",
"hu" : "51",
"ta" : 56.189999999999998,
"te" : 59.899999999999999,
"tm" : 49.600000000000001
},
{
"d" : "2018-09-22",
"hu" : "51.0",
"ta" : 51.1013333333334,
"te" : 56.899999999999999,
"tm" : 40.299999999999997
}
],
"status" : "OK",
"title" : "Graph temp month"
Code: Select all
local t, jresponse, status, decoded_response
server_url = "http://"..Domo_IP..":"..Domo_Port
t = server_url.."/json.htm?type=graph&sensor=counter&range=week&idx="..tostring(Gasmeter_IDX)
jresponse, status = http.request(t)
decoded_response = JSON:decode(jresponse)
result = decoded_response["result"]
gas_usage_today = tonumber(result[8]["v"])
Code: Select all
--script_device_calculate_solaryield.lua
-- Variables that can be changed
Debug = 1 --Enable/Disable logging
Domo_IP = ('x.x.x.x') -- IP Address of Domoticz instance
Domo_Port = ('8080') -- Port used by Domoticz instance
local SolarBoiler_IDX = 193 --Device ID to write calculated Solar Yield (virtual Electric (actual & counter)
Temp_Upper_IDX = 174 --Name of boiler upper temp sensor.
Temp_Bottom_IDX = 176 --Name of boiler bottom temp sensor.
-- End Vars
commandArray = {}
time = os.date("*t")
if((time.min % 1)==0)then --Run every 5 minutes
--###############################
-- Load necessary Lua libraries
--###############################
http = require "socket.http";
socket = require "socket";
-- https = require "ssl.https";
-- JSON = require "json";
JSON = (loadfile "/home/marco/domoticz/scripts/lua/JSON.lua")()
local u, jresponseu, statusu, decoded_responseu
server_url = "http://"..Domo_IP..":"..Domo_Port
local b, jresponseb, statusb, decoded_responseb
server_url = "http://"..Domo_IP..":"..Domo_Port
u = server_url.."/json.htm?type=graph&sensor=temp&range=month&idx="..tostring(Temp_Upper_IDX)
b = server_url.."/json.htm?type=graph&sensor=temp&range=month&idx="..tostring(Temp_Bottom_IDX)
jresponseu, statusu = http.request(u)
jresponseb, statusb = http.request(b)
decoded_responseu = JSON:decode(jresponseu)
decoded_responseb = JSON:decode(jresponseb)
resultu = decoded_responseu["resultu"]
resultb = decoded_responseb["resultb"]
temp_upper_day_min = tonumber(resultu[8]["tm"])
temp_upper_day_max = tonumber(resultu[8]["te"])
temp_bottom_day_min = tonumber(resultb[8]["tm"])
temp_bottom_day_max = tonumber(resultb[8]["te"])
temp_upper_delta = temp_upper_day_max - temp_upper_day_min
temp_bottom_delta = temp_bottom_day_max - temp_bottom_day_min
temp_delta = temp_upper_delta + temp_bottom_delta / 2
solar_yield = temp_delta * 0.34
if solar_yield == nil or solar_yield == '' then
solar_yield = 0
end
-- Debug Data
if Debug == 1 then
print("Upper Temp Max: " ..temp_upper_day_max)
print("Upper Temp Min: " ..temp_upper_day_min)
print("Upper Temp Delta: " ..temp_upper_delta)
print("Bottom Temp Max: " ..temp_bottom_day_max)
print("Bottom Temp Min: " ..temp_bottom_day_min)
print("Bottom Temp Delta: " ..temp_bottom_delta)
print("Solar Yield: " ..solar_yield)
end
-- End Debug Data
-- Update Devices
commandArray[SolarBoiler_IDX] = { ['UpdateDevice'] = SolarBoiler_IDX..'|0|'..solar_yield }
-- End Update Devices
end
return commandArray
2018-09-22 13:38:30.019 Error: EventSystem: in /home/marco/domoticz/scripts/lua/script_device_calculate_solaryield.lua: ...oticz/scripts/lua/script_device_calculate_solaryield.lua:42: attempt to index global 'resultu' (a nil value)