dzVents: MaxMin Temp and Humidity  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

AYAMY
Posts: 21
Joined: Sunday 27 May 2018 11:51
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

dzVents: MaxMin Temp and Humidity

Post by AYAMY »

Hi,
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 that I have changed as suggested with my idx sensor and time ...

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?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents: MaxMin Temp and Humidity

Post by waaren »

AYAMY wrote: Friday 18 January 2019 16:06 Code that I have changed as suggested with my idx sensor and time ...
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?
When I execute the script I receive a notification on pushover and see the following in the domoticz log:

Code: Select all

2019-01-18 21:31:01.580  Status: dzVents: !Info:
Extremes between Friday, 18 January 2019 (21:31) and Thursday, 17 January 2019 (21:31)
high Temperature: 4.3 Celsius at Friday, 18 January 2019 (15:00)
low temperature: -3.5 Celsius at Friday, 18 January 2019 (07:00)
high humidity: 100% at Friday, 18 January 2019 (07:00)
low humidity:  74% at Friday, 18 January 2019 (16:00)
2019-01-18 21:31:01.602  Status: Notification: Extremes between Friday, 18 January 2019 (21:31) and Thursday, 17 January 2019 (21:31)
2019-01-18 21:31:01.793  (Youless) YouLess Meter (Youless)
2019-01-18 21:31:03.206  Notification sent (pushover) => Success
When not yet familiar with dzVents please start with reading https://www.domoticz.com/wiki/DzVents:_ ... _Domoticz before implementing.
Special attention please for "In Domoticz go to Setup > Settings > Other and in the section EventSystem make sure the checkbox 'dzVents disabled' is not checked. Also make sure that in the Security section in the settings you allow 127.0.0.1 to not need a password. dzVents uses that port to send certain commands to Domoticz. Finally make sure you have set your current location in Setup > Settings > System > Location, otherwise there is no way to determine nighttime/daytime state."

For you convenience I set the log level to DEBUG and made the notification subsystem configurable.

Code: Select all

-- getMaxTempHum

local httpResponses = "getMaxTempHum"
return {
    on      =   {
                    timer           =   { "at 21:43"    },                       -- Your preferred time
                    httpResponses   =   { httpResponses }
                },

    logging =   {
                    level           =   domoticz.LOG_DEBUG,
                    marker          =   httpResponses
                },

    execute = function(dz, item)
        -- ****************************** Your settings below this line ***************************************************
        temphumDevice           = dz.devices(766)          -- Replace with ID of Device you want to be notified on
        -- notificationSubsystem   = dz.NSS_PUSHOVER          -- Replace with your subsystem of choice or nil to get a notication on all  
        notificationSubsystem   = nil          -- This will cause the script to send a notification to all subsystems  
        -- ****************************** 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, notificationSubsystem )
        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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
AYAMY
Posts: 21
Joined: Sunday 27 May 2018 11:51
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzVents: MaxMin Temp and Humidity

Post by AYAMY »

thanks for your help..i have used your new code....
and now the script executes but i have a different error....

Code: Select all

2019-01-19 16:33:00.437 dzVents: Info: getMaxTempHum: ------ Start internal script: GETMAXTEMP:, trigger: at 16:33
2019-01-19 16:33:00.453 dzVents: Debug: getMaxTempHum: Device-adapter found for Oregon TempHygro Starck Ext: Temperature+humidity device adapter
2019-01-19 16:33:00.458 dzVents: Debug: getMaxTempHum: Processing device-adapter for Oregon TempHygro Starck Ext: Temperature+humidity device adapter
2019-01-19 16:33:00.459 Error: dzVents: Error: getMaxTempHum: An error occured when calling event handler GETMAXTEMP
2019-01-19 16:33:00.459 Error: dzVents: Error: getMaxTempHum: ...omoticz/scripts/dzVents/generated_scripts/GETMAXTEMP.lua:101: attempt to index local 'item' (a nil value)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents: MaxMin Temp and Humidity

Post by waaren »

AYAMY wrote: Saturday 19 January 2019 16:39 thanks for your help..i have used your new code....
and now the script executes but i have a different error....

Code: Select all

2019-01-19 16:33:00.459 Error: dzVents: Error: getMaxTempHum: ...omoticz/scripts/dzVents/generated_scripts/GETMAXTEMP.lua:101: attempt to index local 'item' (a nil value)
Can you please check again if the copy/ past3 went OK and that you only changed the time and the device number ?
In the version I posted line 101 is triggerJSON()
The error message shows that there is something else now on line 101
If you changed other lines please send me your changed version via PM so I can have a look what caused this error
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
AYAMY
Posts: 21
Joined: Sunday 27 May 2018 11:51
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzVents: MaxMin Temp and Humidity

Post by AYAMY »

the code with my changes

Code: Select all

-- getMaxTempHum

local httpResponses = "getMaxTempHum"
return {
    on      =   {
                    timer           =   { "at 11:29"    },                       -- Your preferred time
                    httpResponses   =   { httpResponses }
                },

    logging =   {
                    level           =   domoticz.LOG_DEBUG,
                    marker          =   httpResponses
                },

    execute = function(dz, item)
        -- ****************************** Your settings below this line ***************************************************
        temphumDevice           = dz.devices(315)          -- Replace with ID of Device you want to be notified on
        -- notificationSubsystem   = dz.NSS_PUSHOVER          -- Replace with your subsystem of choice or nil to get a notication on all  
        notificationSubsystem   = nil          -- This will cause the script to send a notification to all subsystems  
        -- ****************************** 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, notificationSubsystem )
        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
}


the log result

Code: Select all

2019-01-20 11:29:00.157 dzVents: Info: getMaxTempHum: ------ Start internal script: DzIII:, trigger: at 11:29
2019-01-20 11:29:00.174 dzVents: Debug: getMaxTempHum: Device-adapter found for Oregon TempHygro Starck Ext: Temperature+humidity device adapter
2019-01-20 11:29:00.179 dzVents: Debug: getMaxTempHum: Processing device-adapter for Oregon TempHygro Starck Ext: Temperature+humidity device adapter
2019-01-20 11:29:00.179 Error: dzVents: Error: getMaxTempHum: An error occured when calling event handler DzIII
2019-01-20 11:29:00.179 Error: dzVents: Error: getMaxTempHum: ...ntu/domoticz/scripts/dzVents/generated_scripts/DzIII.lua:100: attempt to index local 'item' (a nil value)
2019-01-20 11:29:00.179 dzVents: Info: getMaxTempHum: ------ Finished DzIII
at line 100 in the event editor inside domoticz i can olny see this...
line100
line100
line100.PNG (260.1 KiB) Viewed 2325 times
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents: MaxMin Temp and Humidity

Post by waaren »

AYAMY wrote: Sunday 20 January 2019 11:35 the code with my changes
Strange. I use the exact same code and it works. What is your domoticz / dzVents version ?
And can you change line 100 to

Code: Select all

if item.isTimer or item.isDevice then

To see if that does change the behavior ? Thanks
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
AYAMY
Posts: 21
Joined: Sunday 27 May 2018 11:51
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzVents: MaxMin Temp and Humidity

Post by AYAMY »

I have changed the code to

Code: Select all

-- getMaxTempHum

local httpResponses = "getMaxTempHum"
return {
    on      =   {
                    timer           =   { "at 08:53"    },                       -- Your preferred time
                    httpResponses   =   { httpResponses }
                },

    logging =   {
                    level           =   domoticz.LOG_DEBUG,
                    marker          =   httpResponses
                },

    execute = function(dz, item)
        -- ****************************** Your settings below this line ***************************************************
        temphumDevice           = dz.devices(313)          -- Replace with ID of Device you want to be notified on
        -- notificationSubsystem   = dz.NSS_PUSHOVER          -- Replace with your subsystem of choice or nil to get a notication on all  
        notificationSubsystem   = nil          -- This will cause the script to send a notification to all subsystems  
        -- ****************************** 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, notificationSubsystem )
        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
        if item.isTimer or item.isDevice 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
}
and this is the result

Code: Select all

2019-01-21 08:53:00.357 dzVents: Info: getMaxTempHum: ------ Start internal script: DzIII:, trigger: at 08:53
2019-01-21 08:53:00.384 dzVents: Debug: getMaxTempHum: Device-adapter found for Imagintronix: Temperature+humidity device adapter
2019-01-21 08:53:00.389 dzVents: Debug: getMaxTempHum: Processing device-adapter for Imagintronix: Temperature+humidity device adapter
2019-01-21 08:53:00.389 Error: dzVents: Error: getMaxTempHum: An error occured when calling event handler DzIII
2019-01-21 08:53:00.389 Error: dzVents: Error: getMaxTempHum: ...ntu/domoticz/scripts/dzVents/generated_scripts/DzIII.lua:101: attempt to index local 'item' (a nil value)
2019-01-21 08:53:00.389 dzVents: Info: getMaxTempHum: ------ Finished DzIII
Thanks for your help @waaren
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents: MaxMin Temp and Humidity

Post by waaren »

AYAMY wrote: Monday 21 January 2019 8:55 I have changed the code to
And what versions do you use ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
AYAMY
Posts: 21
Joined: Sunday 27 May 2018 11:51
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzVents: MaxMin Temp and Humidity

Post by AYAMY »

About Domoticz
Version: 3.8153
Build Hash: 494fff7
Compile Date: 2017-07-30 12:19:41
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents: MaxMin Temp and Humidity

Post by waaren »

That explains. The script uses functions of dzVents that are not yet available in your version. If you want to use it you need to go to later stable (4.9700) or recent Beta (4.103xx)
And please update your profile to reflect your version. That will save time and frustration.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
AYAMY
Posts: 21
Joined: Sunday 27 May 2018 11:51
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzVents: MaxMin Temp and Humidity

Post by AYAMY »

now i have updated ;)

About Domoticz
Version: 4.9700
Build Hash: a3a45906
Compile Date: 2018-06-23 16:24:51
AYAMY
Posts: 21
Joined: Sunday 27 May 2018 11:51
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzVents: MaxMin Temp and Humidity

Post by AYAMY »

thank you thank you thank you thank you @waaren !!!
Now the script run but the ranges of the calculation are incorrect... The Entreme date is today (21 January ) and yesterday (20 January ) but the high\ low temperatures and high\low humidity are of the 16 January 2019 and 14 January 2019
I have to set up the code but thank you it works... now it's only about tuning the code.. ;)))

this is what i have received running the script ;)

Code: Select all

Extremes between Monday, 21 January 2019 (11:42) and Sunday, 20 January 2019 (11:42): 
high Temperature: 15 Celsius at Wednesday, 16 January 2019 (12:00)
low temperature: 3 Celsius at Wednesday, 16 January 2019 (01:00)
high humidity: 99% at Monday, 14 January 2019 (11:00)
low humidity:  96% at Monday, 14 January 2019 (23:00)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents: MaxMin Temp and Humidity

Post by waaren »

AYAMY wrote: Monday 21 January 2019 11:33 The ranges of the calculation are incorrect... The Entreme date is today (21 January ) and yesterday (20 January ) but the high\ low temperatures and high\low humidity are of the 16 January 2019 and 14 January 2019
Adjusted the script to make the range a configurable amount of hours.

Code: Select all

-- getMaxTempHum

local httpResponses = "getMaxTempHum"
return {
    on      =   {
                    timer           =   { "at 17:38"    },                       -- Your preferred time
                    httpResponses   =   { httpResponses }
                },

    logging =   {
                    level           =   domoticz.LOG_DEBUG,
                    marker          =   httpResponses
                },

    execute = function(dz, item)
        -- ****************************** Your settings below this line ***************************************************
        temphumDevice           = dz.devices(766)          -- Replace with ID of Device you want to be notified on
        timeSpan                = 24                       -- If set longer then the amount of short log sensors data in domoticz it will look at all data available
        -- notificationSubsystem   = dz.NSS_PUSHOVER       -- Replace with your subsystem of choice or nil to get a notication on all  
        notificationSubsystem   = nil          -- This will cause the script to send a notification to all subsystems  
        -- ****************************** 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,check)
            _,_, year, month, day, hour, minute = string.find(timeString, "(%d+)-(%d+)-(%d+) (%d+):(%d+)")
            
            if check then
                local seconds = os.time({year=year, month=month, day=day, hour=hour, minute=minute, seconds=0 })
                return seconds
            else    
                local fmtString = os.date("%A, %d %B %Y (%H:%M)",os.time({year=year, month=month, day=day, hour=hour, minute=minute })):gsub(" 0"," ")
                return fmtString                                 -- outgoing format: ddd d mmmm yyyy (hh:mm)
            end
            
        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 from, to
        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, notificationSubsystem )
        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
            local sinceTime             = os.time(os.date('*t')) - timeSpan * 3600
            
            
            for key in ipairs(rt) do
                if tonumber(rt[key].hu) > extremes.high.humidity then
                    if convertTime(rt[key].d,true) >  sinceTime 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
                end
                if tonumber(rt[key].hu) < extremes.low.humidity then
                    if convertTime(rt[key].d,true) >  sinceTime 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
                end
                if tonumber(rt[key].te) > extremes.high.temperature then
                    if convertTime(rt[key].d,true) >  sinceTime 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
                end
                if tonumber(rt[key].te) < extremes.low.temperature then
                    if convertTime(rt[key].d,true) >  sinceTime 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
            end
            return extremes
        end


        -- Main
        if not item.isHTTPResponse then
            triggerJSON()
        elseif item.ok then                                      -- statusCode == 2xx
            extremes = getExtremes(item.json.result)
            notify(getTimeWindow(timeSpan))
        else
            logWrite("Could not get (good) data from domoticz. Error (" .. (item.statusCode or 999) .. ")"  ,dz.LOG_ERROR)
            logWrite(item.data)
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
AYAMY
Posts: 21
Joined: Sunday 27 May 2018 11:51
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzVents: MaxMin Temp and Humidity

Post by AYAMY »

thank you...the script is perfect !!!!! really thank you for your help !!!
AYAMY
Posts: 21
Joined: Sunday 27 May 2018 11:51
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzVents: MaxMin Temp and Humidity

Post by AYAMY »

I have upgraded to Domoticz
Version: 4.10717
Build Hash: b38b49e5
Compile Date: 2019-05-09 13:04:08
dzVents Version: 2.4.19
Python Version: 3.7.3 (default, Apr 3 2019, 05:39:12) [GCC 8.2.0]

and Now the script doesn't run.... it has these errors
2019-11-18 16:37:00.527 Error: dzVents: local netWork not open for dzVents openURL call !
2019-11-18 16:37:00.527 Error: dzVents: check dzVents wiki (look for 'Using dzVents with Domoticz')
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents: MaxMin Temp and Humidity

Post by waaren »

AYAMY wrote: Monday 18 November 2019 16:41 I have upgraded to Domoticz

and Now the script doesn't run.... it has these errors
2019-11-18 16:37:00.527 Error: dzVents: local netWork not open for dzVents openURL call !
2019-11-18 16:37:00.527 Error: dzVents: check dzVents wiki (look for 'Using dzVents with Domoticz')
In case you don't have access to the dzVents wiki
Using dzVents.png
Using dzVents.png (54.03 KiB) Viewed 1944 times
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
AYAMY
Posts: 21
Joined: Sunday 27 May 2018 11:51
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzVents: MaxMin Temp and Humidity

Post by AYAMY »

my bad.... sorry....
I have a Fritzbox... and using 192.168.0.* doesn't work ... Fritz releases 192.168.178.* so i have added 192.168.178.* and now it works...
zavjah
Posts: 36
Joined: Tuesday 13 August 2019 10:20
Target OS: Raspberry Pi / ODroid
Domoticz version:

Re: dzVents: MaxMin Temp and Humidity

Post by zavjah »

Hi,
this is awesome as it is exactly what I need, but much simpler:
Can someone please help me how can I simplify the script to only get the Information of a highest temp within the last 24h?

Thx a lot,
zavjah
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents: MaxMin Temp and Humidity

Post by waaren »

zavjah wrote: Saturday 15 August 2020 14:57 Hi,
this is awesome as it is exactly what I need, but much simpler:
Can someone please help me how can I simplify the script to only get the Information of a highest temp within the last 24h?

Thx a lot,
zavjah
Remove lines 63-65

Code: Select all

            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)/code]
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
zavjah
Posts: 36
Joined: Tuesday 13 August 2019 10:20
Target OS: Raspberry Pi / ODroid
Domoticz version:

Re: dzVents: MaxMin Temp and Humidity

Post by zavjah »

Hi waaren,

I was able to "reduce" the script successfully to this:

Code: Select all

local MaxTemperature = 'getMaxTemp'

return {on = {timer           =   {'every Minute'},                       -- Your preferred time
              httpResponses   =   { MaxTemperature }
              },

    execute = function(dz, item)
        local tempDevice = dz.devices('Außen')
        local extremes = {}

        if item.isDevice or item.isTimer then
            dz.openURL({    url = 'http://192.168.178.23:8080/json.htm?type=graph&sensor=temp&range=day&idx=' .. tempDevice.idx  .. '&range=day',
                            method = "GET",
                            callback = MaxTemperature })
            return
        end

        local function getExtremes(rt)
            extremes.high               = {}
            extremes.high.temperature   = -256

            for key in ipairs(rt) do
                if tonumber(rt[key].te) > extremes.high.temperature then
                    extremes.high.temperature = tonumber(rt[key].te)
                end
            end
            return extremes
        end

        -- Main
        if item.ok and item.isJSON then
            extremes = getExtremes(item.json.result)
            dz.log('Höchsttemeratur gestern: ' .. extremes.high.temperature .. '°C')
        end
    end
}
Now, I'd like to combine this with a second script I have for rain level:

Code: Select all

local RainLevelTest = 'getRainLevel'

return {on= {
            timer = {'every Minute'},
            httpResponses = {RainLevelTest,},
            },
    
    logging = {level = domoticz.LOG_INFO,}, --LOG_DEBUG, LOG_ERROR, LOG_INFO

    execute = function(dz, item)
        local rainDevice = dz.devices('Regen') -- name enclosed in quotes or number without quotes
        local regenMaenge = 8       -- Wieviel mm Regen braucht es, damti ich NICHT gieße?
        local regenZeitraum = 2     -- Tage vom Zeitpunkt des Skript lauf aus, also heute!
        
        if item.isDevice or item.isTimer then
            dz.openURL(
                {url = 'http://192.168.178.23:8080/json.htm?type=graph&sensor=rain&idx=' .. rainDevice.idx .. '&range=day', --&range=day oder &range=week oder &range=month
                 callback = RainLevelTest,
                })
            return
        end
        
        local function getmm(rt)
            local startDate = (dz.time.addMinutes(-1 * regenZeitraum * 24 * 60)).rawDate
            local mm = 0
            for index, record in ipairs(rt) do
                mm = mm + record.mm
            end
            return mm
        end

-- Main
        if item.ok and item.isJSON then
            dz.log('Regenmenge in den letzten 24h: ' .. getmm(item.json.result) .. 'mm')    
        else
            dz.log('Keine valide Antwort von domoticz ',dz.LOG_ERROR)    
            dz.log(item,dz.LOG_ERROR)    
        end
    end
}
I lack the knowledge how to do that, I assume I do semthing wrong at "dz.OpebURL"

Can you help, please?

thx,
Zavjah
Post Reply

Who is online

Users browsing this forum: kniazio and 1 guest