Total count for past x days

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

Moderator: leecollings

Post Reply
ccaru
Posts: 49
Joined: Tuesday 05 March 2019 16:58
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Total count for past x days

Post by ccaru »

Hello everybody,

I am trying to build a DZVents script that will activate my watering system. I'd like to be able to calculate the total rain for the past x days and then determine the watering needs based on that.

Is it possible to return the total value of a counter (or rain meter) for the past x days ?

cheers,
Claude
RFXCom, Somfy RTS, 433Mhz Lighting, Evohome, Smart watering via Netatmo, solenoid valves, soil humidity sensors, Airthings Radon via bluetooth, Marley heat recovery fresh air units, Blue Iris, Alexa via DA Bridge.
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Total count for past x days

Post by waltervl »

In the following topic lots of examples how to extract data out of in this case energy counters (gas and electricity). Should be adaptable for rain counters. https://domoticz.com/forum/viewtopic.ph ... 4&start=20
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
ccaru
Posts: 49
Joined: Tuesday 05 March 2019 16:58
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Total count for past x days

Post by ccaru »

Hi, thank you very much for link. I have successfully modified the script to calculate the total rain for the past 7 days, which is then stored in a dz variable.

I use this value to control how much water I supply to my young trees. I am not sure how effective it is but the trees are looking healthy :D

Code: Select all

local scriptVar = 'Rain this week'

return
{
    on =
    {
        timer = 
        { 
            'every 15 minutes',
        },

        httpResponses =
        {
            scriptVar .. '*',
        },
    },

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

    data =
    {
        Rain =
        {
            initial = {},
        },
    },

    execute = function(dz, item)
    
        -- ***** Your settings below this line
        local Rain = dz.devices('Rain') -- change to name of your RainMeter between quotes or id without quotes
        local AverageRain = dz.variables('AverageRain') -- change to name of Rain variable
        
        -- ***** No changes required below this line

         local function getGraphData(period, id)
            if period == nil then period = 'year' end
            if id == nil then id = Rain.id end
            url = dz.settings['Domoticz url'] .. '/json.htm?type=graph&sensor=rain&range=' .. period .. '&idx=' .. id
            dz.openURL({ url = url, callback = scriptVar .. '_' .. period })
        end
        
        local function processJSON(t)
            local weekVolume = 0, 0, 0
            local day = 86400 -- (24 * 60 * 60)
            local startDay = os.date("%Y-%m-%d",os.time()-(7*day)) --Past 7 days

            for index, Rain in ipairs(t) do
                if Rain.d:match(dz.time.year) then
                    if Rain.d >= startDay then -- will sum everything which is on or after startofWeek
                        weekVolume = weekVolume + Rain.mm
                    end
                end
            end
            AverageRain.set(weekVolume)
        end

        -- main
        if item.isHTTPResponse and item.isJSON then
            processJSON(item.json.result)
        elseif item.isTimer or item.isDevice then
            getGraphData()
        else
            dz.log('Error while retrieving Rain data. Result is ' .. item.statusText ..' ; Response is: ' .. item.data,LOG_ERROR)
        end
    end
}
RFXCom, Somfy RTS, 433Mhz Lighting, Evohome, Smart watering via Netatmo, solenoid valves, soil humidity sensors, Airthings Radon via bluetooth, Marley heat recovery fresh air units, Blue Iris, Alexa via DA Bridge.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest