setRainVar to rainfall in mm of last defined hours

Post Reply
User avatar
waaren
Posts: 1216
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

setRainVar to rainfall in mm of last defined hours

Post by waaren » Monday 09 July 2018 7:23

Made it a separate topic: originated from a feature request in "updateWeatherSensors (yet another buienradar script)"

script will set a uservar with the the amount of rainfall in mm during a defined period in hours.

Code: Select all

-- setRainVar.lua for [ dzVents >= 2.4 ] 
-- Before first execution create var as type float and enter the deviceName or IDX of the raindevice

return {
    on      =   {   timer           =   { "every hour" },                       -- Triggers the getJsonPart
                    httpResponses   =   { "setRainVar" }                        -- Trigger the handle Json part
                },

    logging =   {   level     =   domoticz.LOG_ERROR,
                    marker    =   "setRainVar"    },

    execute = function(dz,trigger)

        local rainDevice    = dz.devices(nnn)                                 -- name enclosed in quotes or number without quotes
        local rainVar       = dz.variables("amountOfRainDuringLastHours")     -- name enclosed in quotes or number without quotes 
        local relevantHours = 12                                              -- below 24 day, above 23 year  
        local range         = "day"
        if relevantHours > 23 then range = "year" end 
        
               function isDevice(device)
            if device ~= nil then
                if device.deviceType == "General" then
                    return device.deviceSubType
                else
                    return device.deviceType
                end
            else
                return "device is not defined in domoticz"
            end
        end
        
        local function triggerJSON()
            local  URLString   = dz.settings['Domoticz url'] .. "/json.htm?type=graph&sensor=rain&idx=" .. rainDevice.idx .. "&range=" .. range 
            dz.openURL({    url = URLString,
                            method = "GET",
                            callback = "setRainVar" })                      
        end

        local function setVar()
            local Time = require('Time')
            local now  = Time()
            local relevantAmount = 0 
            local delta,t

            if trigger.json.result ~= nil then
                rt = trigger.json.result
            else
                dz.log("No values found in history. Put 0 in uservar",dz.LOG_DEBUG )
                rainVar.set(0)      -- Store 0 in uservariable
                return
            end            
            
            if range == "year" then timeCompletion = " 00:00:00" else timeCompletion = ":00" end
                
            for i=1,#rt do                                 -- Loop through the result
                t = Time(rt[i].d .. timeCompletion)        -- get time and prepare for compare
                delta = (t.compare(now).hours)             -- diff in hours between json record and now

                if delta <= relevantHours then
                    relevantAmount = relevantAmount + tonumber(rt[i].mm) 
                    dz.log(     "Date/time: " .. rt[i].d .. " ===>> Value in mm " .. 
                                rt[i].mm .. " ===>> Delta hours: "  .. delta .. 
                                " ===>> Total until now: " .. relevantAmount
                                ,dz.LOG_DEBUG)
                end    
            end
            if (rainVar.value ~= relevantAmount) or (rainVar.lastUpdate.hoursAgo > 12) then
                rainVar.set(relevantAmount)   -- Store value in uservariable
                dz.log("Set ".. rainVar.name .. " to: " .. relevantAmount,dz.LOG_DEBUG )
            end    
        end

        if isDevice(rainDevice) ~= "Rain" then 
           dz.log("Not a (valid) raindevice: " .. isDevice(rainDevice),dz.LOG_ERROR)    
           return 
        end
        
        if rainVar == nil or not(rainVar.isVariable) or rainVar.type ~= dz.FLOAT then
           dz.log("Not a (valid) rainVar",dz.LOG_ERROR)    
           return 
        end
        
        if trigger.isTimer or trigger.isDevice then
            triggerJSON()
        elseif trigger.ok and trigger.json.status ~= "ERR" then                                      -- statusCode == 2xx
            setVar()
        else
            dz.log("No valid response from domoticz; assuming rain = 0",dz.LOG_ERROR)    
            rainVar.set(0)      -- Store value in uservariable
        end
    end
}
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest