Some time ago I have posted in the Blocky Board looking for a solution to my problem... Find and send a notification of the lowest and highest temperatures and %humidity in the last 24 hours.
@warren tells me to look towards dzVents to find a how to figure out ... and he suggested me the following solution
Code: Select all
-- getMaxTempHum
local httpResponses = "getMaxTempHum"
return {
on = {
timer = { "at 19:35" }, -- Your preferred time
httpResponses = { httpResponses }
},
logging = {
level = domoticz.LOG_ERROR,
marker = httpResponse
},
execute = function(dz, item)
-- ****************************** Your settings below this line ***************************************************
temphumDevice = dz.devices(766) -- Replace with ID of Device you want to be notified on
-- ****************************** No changes required below this line *********************************************
local extremes = {}
local function logWrite(str,level)
dz.log(tostring(str),level or dz.LOG_DEBUG)
end
local function triggerJSON()
local URLString = dz.settings['Domoticz url'] .. "/json.htm?type=graph&sensor=temp&range=day&idx=" .. temphumDevice.id
dz.openURL({ url = URLString,
method = "GET",
callback = httpResponses })
end
local function convertTime(timeString) -- based on incoming format yyyy-mm-dd hh:mm
local year, month, day, hour, minute
_,_, year, month, day, hour, minute = string.find(timeString, "(%d+)-(%d+)-(%d+) (%d+):(%d+)")
fmtString = os.date("%A, %d %B %Y (%H:%M)",os.time({year=year, month=month, day=day, hour=hour, minute=minute })):gsub(" 0"," ")
logWrite(fmtString)
return fmtString -- outgoing format: ddd d mmmm yyyy (hh:mm)
end
local function showResult(str)
textDevice.updateText(str)
logWrite(str,dz.LOG_FORCE)
end
local function getTimeWindow(hours)
local to = os.time(os.date("*t"))
local from = os.date("%A, %d %B %Y (%H:%M)",to - hours * 3600)
local to = os.date("%A, %d %B %Y (%H:%M)",to)
return to, from
end
local function notify(from,now)
local notificationString = "\nhigh Temperature: " .. extremes.high.temperature .. " Celsius at " .. convertTime(extremes.high.tTime) .. "\n"
notificationString = notificationString .. "low temperature: " .. extremes.low.temperature .. " Celsius at " .. convertTime(extremes.low.tTime) .. "\n"
notificationString = notificationString .. "high humidity: " .. extremes.high.humidity .. "% at " .. convertTime(extremes.high.hTime) .. "\n"
notificationString = notificationString .. "low humidity: " .. extremes.low.humidity .. "% at " .. convertTime(extremes.low.hTime)
logWrite("\nExtremes between " .. from .. " and " .. now .. "\n" .. notificationString,dz.LOG_FORCE)
dz.notify("Extremes between " .. from .. " and " .. now, notificationString, dz.PRIORITY_NORMAL, nil,nil, dz.NSS_PUSHOVER )
end
local function getExtremes(rt)
extremes.low = {}
extremes.high = {}
extremes.low.temperature = 10000
extremes.low.humidity = 100
extremes.high.humidity = 0
extremes.high.temperature = -256
for key in ipairs(rt) do
if tonumber(rt[key].hu) > extremes.high.humidity then
logWrite("New max humidity found on " .. convertTime(rt[key].d) .. " ==>> " .. rt[key].hu )
extremes.high.humidity = tonumber(rt[key].hu)
extremes.high.hTime = rt[key].d
end
if tonumber(rt[key].hu) < extremes.low.humidity then
logWrite("New min humidity found on " .. convertTime(rt[key].d) .. " ==>> " .. rt[key].hu )
extremes.low.humidity = tonumber(rt[key].hu)
extremes.low.hTime = rt[key].d
end
if tonumber(rt[key].te) > extremes.high.temperature then
logWrite("New max temperature found on " .. convertTime(rt[key].d) .. " ==>> " .. rt[key].te )
extremes.high.temperature = tonumber(rt[key].te)
extremes.high.tTime = rt[key].d
end
if tonumber(rt[key].te) < extremes.low.temperature then
logWrite("New min temperature found on " .. convertTime(rt[key].d) .. " ==>> " .. rt[key].te )
extremes.low.temperature = tonumber(rt[key].te)
extremes.low.tTime = rt[key].d
end
end
return extremes
end
-- Main
if not item.isHTTPResponse then
triggerJSON()
elseif item.ok then -- statusCode == 2xx
extremes = getExtremes(item.json.result)
notify(getTimeWindow(24))
else
logWrite("Could not get (good) data from domoticz. Error (" .. (item.statusCode or 999) .. ")" ,dz.LOG_ERROR)
logWrite(item.data)
end
end
}
Code: Select all
-- getMaxTempHum
local httpResponses = "getMaxTempHum"
return {
on = {
timer = { "at 11:20" }, -- Your preferred time
httpResponses = { httpResponses }
},
logging = {
level = domoticz.LOG_ERROR,
marker = httpResponse
},
execute = function(dz, item)
-- ****************************** Your settings below this line ***************************************************
temphumDevice = dz.devices(315) -- Replace with ID of Device you want to be notified on
-- ****************************** No changes required below this line *********************************************
local extremes = {}
local function logWrite(str,level)
dz.log(tostring(str),level or dz.LOG_DEBUG)
end
local function triggerJSON()
local URLString = dz.settings['Domoticz url'] .. "/json.htm?type=graph&sensor=temp&range=day&idx=" .. temphumDevice.id
dz.openURL({ url = URLString,
method = "GET",
callback = httpResponses })
end
local function convertTime(timeString) -- based on incoming format yyyy-mm-dd hh:mm
local year, month, day, hour, minute
_,_, year, month, day, hour, minute = string.find(timeString, "(%d+)-(%d+)-(%d+) (%d+):(%d+)")
fmtString = os.date("%A, %d %B %Y (%H:%M)",os.time({year=year, month=month, day=day, hour=hour, minute=minute })):gsub(" 0"," ")
logWrite(fmtString)
return fmtString -- outgoing format: ddd d mmmm yyyy (hh:mm)
end
local function showResult(str)
textDevice.updateText(str)
logWrite(str,dz.LOG_FORCE)
end
local function getTimeWindow(hours)
local to = os.time(os.date("*t"))
local from = os.date("%A, %d %B %Y (%H:%M)",to - hours * 3600)
local to = os.date("%A, %d %B %Y (%H:%M)",to)
return to, from
end
local function notify(from,now)
local notificationString = "\nhigh Temperature: " .. extremes.high.temperature .. " Celsius at " .. convertTime(extremes.high.tTime) .. "\n"
notificationString = notificationString .. "low temperature: " .. extremes.low.temperature .. " Celsius at " .. convertTime(extremes.low.tTime) .. "\n"
notificationString = notificationString .. "high humidity: " .. extremes.high.humidity .. "% at " .. convertTime(extremes.high.hTime) .. "\n"
notificationString = notificationString .. "low humidity: " .. extremes.low.humidity .. "% at " .. convertTime(extremes.low.hTime)
logWrite("\nExtremes between " .. from .. " and " .. now .. "\n" .. notificationString,dz.LOG_FORCE)
dz.notify("Extremes between " .. from .. " and " .. now, notificationString, dz.PRIORITY_NORMAL, nil,nil, dz.NSS_PUSHOVER )
end
local function getExtremes(rt)
extremes.low = {}
extremes.high = {}
extremes.low.temperature = 10000
extremes.low.humidity = 100
extremes.high.humidity = 0
extremes.high.temperature = -256
for key in ipairs(rt) do
if tonumber(rt[key].hu) > extremes.high.humidity then
logWrite("New max humidity found on " .. convertTime(rt[key].d) .. " ==>> " .. rt[key].hu )
extremes.high.humidity = tonumber(rt[key].hu)
extremes.high.hTime = rt[key].d
end
if tonumber(rt[key].hu) < extremes.low.humidity then
logWrite("New min humidity found on " .. convertTime(rt[key].d) .. " ==>> " .. rt[key].hu )
extremes.low.humidity = tonumber(rt[key].hu)
extremes.low.hTime = rt[key].d
end
if tonumber(rt[key].te) > extremes.high.temperature then
logWrite("New max temperature found on " .. convertTime(rt[key].d) .. " ==>> " .. rt[key].te )
extremes.high.temperature = tonumber(rt[key].te)
extremes.high.tTime = rt[key].d
end
if tonumber(rt[key].te) < extremes.low.temperature then
logWrite("New min temperature found on " .. convertTime(rt[key].d) .. " ==>> " .. rt[key].te )
extremes.low.temperature = tonumber(rt[key].te)
extremes.low.tTime = rt[key].d
end
end
return extremes
end
-- Main
if not item.isHTTPResponse then
triggerJSON()
elseif item.ok then -- statusCode == 2xx
extremes = getExtremes(item.json.result)
notify(getTimeWindow(24))
else
logWrite("Could not get (good) data from domoticz. Error (" .. (item.statusCode or 999) .. ")" ,dz.LOG_ERROR)
logWrite(item.data)
end
end
}
I have saved and Enabled the script but at 11:20 I haven't seen any info about temp or humidity... the log only shows
EventSystem: reset all events...
EventSystem: Write file: /home/ubuntu/domoticz/scripts/dzVents/generated_scripts/GETMAXTEMP.lua
Could you help me?