Page 1 of 1
Average daily lowest temperature
Posted: Sunday 17 February 2019 10:45
by gertlind1
Hi
Need some help with a LUA script
Im running this query :
https://xxxx.com/json.htm?type=graph&se ... ange=month
Getting below output, this is great so far.
I want to get out the "tm" temp for the last 7 days, add them and divide by 7.
Where do i start ?
Code: Select all
{
"result" : [
...
...
...
{
"d" : "2019-02-11",
"hu" : "90",
"ta" : -0.19,
"te" : 1.3,
"tm" : -2.7999999999999998
},
{
"d" : "2019-02-12",
"hu" : "81",
"ta" : -2.6800000000000002,
"te" : -0.90000000000000002,
"tm" : -4.0
},
{
"d" : "2019-02-13",
"hu" : "89",
"ta" : 1.2,
"te" : 3.2000000000000002,
"tm" : -2.1000000000000001
},
{
"d" : "2019-02-14",
"hu" : "89",
"ta" : 1.4399999999999999,
"te" : 3.7999999999999998,
"tm" : -1.2
},
{
"d" : "2019-02-15",
"hu" : "92",
"ta" : 2.52,
"te" : 4.7000000000000002,
"tm" : 1.5
},
{
"d" : "2019-02-16",
"hu" : "82",
"ta" : 2.96,
"te" : 5.7999999999999998,
"tm" : 1.2
},
{
"d" : "2019-02-17",
"hu" : "85.8515625",
"ta" : -0.63671875,
"te" : 1.2,
"tm" : -1.7
}
],
"status" : "OK",
"title" : "Graph temp month"
}
Re: Average daily lowest temperature
Posted: Sunday 17 February 2019 18:49
by waaren
Try this dzVents script.
Code: Select all
-- getweekly average
local httpResponses = "getMonthlyTemperature"
return {
on = {
timer = { "at 06:07" },
httpResponses = { httpResponses .. "*" }
},
logging = {
level = domoticz.LOG_DEBUG,
marker = httpResponses
},
execute = function(dz, item)
-- ****************************** Your settings below this line ***************************************************
temperatureSensor = dz.devices(880) -- Replace with ID of your temperature Sensor
-- ****************************** No changes required below this line *********************************************
local Time = require('Time')
local function logWrite(str,level)
dz.log(tostring(str),level or dz.LOG_DEBUG)
end
local function triggerJSON(url, response, delay)
dz.openURL({ url = url,
method = "GET",
callback = response }).afterSec(delay or 1)
end
local function getAverage(rt)
local tmSummed = 0
local days = 0
local week = {}
for day=7,1,-1 do
week[day] = os.date("%Y-%m-%d",os.time()-day * 24 * 60 * 60)
end
for key in ipairs(rt) do
for day in ipairs(week) do
if week[day] == rt[key].d then
tmSummed = tmSummed + tonumber(rt[key].tm)
days = days + 1
end
end
end
return dz.utils.round(tmSummed / days,1)
end
if not item.isHTTPResponse then
triggerJSON(dz.settings['Domoticz url'] .. "/json.htm?type=graph&sensor=temp&range=month&idx=" .. temperatureSensor.id, httpResponses .. "_graph")
elseif item.ok then -- statusCode == 2xx
logWrite("Average tm: ".. getAverage(item.json.result))
else
logWrite("Could not get (good) data from domoticz. Error (" .. (item.statusCode or 999) .. ")" ,dz.LOG_ERROR)
logWrite(item.data)
end
end
}
Re: Average daily lowest temperature
Posted: Monday 18 February 2019 9:26
by gertlind1
Hi waaren
Thanks for your effort.
I do get an error.
I have only HTTPS enabled.
Tried to enable HTTP port 8080, no improvement.
Also tried to inject user and pw, same problem. WHen i run the query from chrome it works.
Code: Select all
2019-02-18 09:23:00.154 Status: dzVents: Info: getMonthlyTemperature: ------ Start external script: daily_temp_average.lua:, trigger: every minute
2019-02-18 09:23:00.184 Status: dzVents: Debug: getMonthlyTemperature: Processing device-adapter for Ute: Temperature+humidity device adapter
2019-02-18 09:23:00.185 Status: dzVents: Debug: getMonthlyTemperature: OpenURL: url = http://127.0.0.1:8080/json.htm?type=graph&sensor=temp&range=month&idx=19
2019-02-18 09:23:00.185 Status: dzVents: Debug: getMonthlyTemperature: OpenURL: method = GET
2019-02-18 09:23:00.185 Status: dzVents: Debug: getMonthlyTemperature: OpenURL: post data = nil
2019-02-18 09:23:00.185 Status: dzVents: Debug: getMonthlyTemperature: OpenURL: headers = nil
2019-02-18 09:23:00.185 Status: dzVents: Debug: getMonthlyTemperature: OpenURL: callback = getMonthlyTemperature_graph
2019-02-18 09:23:00.185 Status: dzVents: Info: getMonthlyTemperature: ------ Finished daily_temp_average.lua
2019-02-18 09:23:00.289 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2019-02-18 09:23:01.312 Error: Error opening url: http://127.0.0.1:8080/json.htm?type=graph&sensor=temp&range=month&idx=19
2019-02-18 09:23:01.493 Status: dzVents: Info: Handling httpResponse-events for: "getMonthlyTemperature_graph
2019-02-18 09:23:01.494 Status: dzVents: Info: getMonthlyTemperature: ------ Start external script: daily_temp_average.lua: HTTPResponse: "getMonthlyTemperature_graph"
2019-02-18 09:23:01.510 Status: dzVents: Debug: getMonthlyTemperature: Processing device-adapter for Ute: Temperature+humidity device adapter
2019-02-18 09:23:01.510 Status: dzVents: Error (2.4.6): getMonthlyTemperature: Could not get (good) data from domoticz. Error (401)
2019-02-18 09:23:01.510 Status: dzVents: Debug: getMonthlyTemperature:
2019-02-18 09:23:01.511 Status: dzVents: Info: getMonthlyTemperature: ------ Finished daily_temp_average.lua
Another thing that strikes me, maybe range=month is not good. What happens at the month break (feb to mar), will it still be 7 last entries?
Re: Average daily lowest temperature
Posted: Monday 18 February 2019 9:50
by waaren
gertlind1 wrote: ↑Monday 18 February 2019 9:26
I do get an error.
I have only HTTPS enabled.
Another thing that strikes me, maybe range=month is not good. What happens at the month break (feb to mar), will it still be 7 last entries?
If you are not yet familiar with dzvents then please look at the 10 lines
using dzVents with Domoticz for an intro to dzVents and howto get this script active in domoticz without errors on 127.0.0.1
the range=month will give the last 30 days or so.
Re: Average daily lowest temperature
Posted: Monday 18 February 2019 11:33
by gertlind1
Works as a charm now, thanks a lot.
Code: Select all
2019-02-18 11:32:00.181 Status: dzVents: Info: getMonthlyTemperature: ------ Start external script: daily_temp_average.lua:, trigger: every minute
2019-02-18 11:32:00.197 Status: dzVents: Debug: getMonthlyTemperature: Processing device-adapter for Ute: Temperature+humidity device adapter
2019-02-18 11:32:00.197 Status: dzVents: Debug: getMonthlyTemperature: OpenURL: url = http://127.0.0.1:8080/json.htm?type=graph&sensor=temp&range=month&idx=19
2019-02-18 11:32:00.197 Status: dzVents: Debug: getMonthlyTemperature: OpenURL: method = GET
2019-02-18 11:32:00.197 Status: dzVents: Debug: getMonthlyTemperature: OpenURL: post data = nil
2019-02-18 11:32:00.197 Status: dzVents: Debug: getMonthlyTemperature: OpenURL: headers = nil
2019-02-18 11:32:00.197 Status: dzVents: Debug: getMonthlyTemperature: OpenURL: callback = getMonthlyTemperature_graph
2019-02-18 11:32:00.197 Status: dzVents: Info: getMonthlyTemperature: ------ Finished daily_temp_average.lua
2019-02-18 11:32:00.263 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2019-02-18 11:32:01.494 Status: dzVents: Info: Handling httpResponse-events for: "getMonthlyTemperature_graph
2019-02-18 11:32:01.495 Status: dzVents: Info: getMonthlyTemperature: ------ Start external script: daily_temp_average.lua: HTTPResponse: "getMonthlyTemperature_graph"
2019-02-18 11:32:01.543 Status: dzVents: Debug: getMonthlyTemperature: Processing device-adapter for Ute: Temperature+humidity device adapter
2019-02-18 11:32:01.544 Status: dzVents: Debug: getMonthlyTemperature: Average tm: -1.3
2019-02-18 11:32:01.544 Status: dzVents: Info: getMonthlyTemperature: ------ Finished daily_temp_average.lua