Page 1 of 1

Using calculated amount of rain parameters from Domoticz

Posted: Thursday 27 May 2021 22:17
by Afterburner
Domoticz has a buildin report function for the amount of Rain that has fallen per day, week, month etc.

I want to use that data in a script to control the amount of water I drip into my garden.

Question 1. Someone who build that function already??

Question 2. Are those, reported, calculated amounts available for scripting?

Thanks for helping already..

Re: Using calculated amount of rain parameters from Domoticz

Posted: Thursday 27 May 2021 22:36
by waaren
Afterburner wrote: Thursday 27 May 2021 22:17 Are those, reported, calculated amounts available for scripting?
Not directly but you can use the API calls

/json.htm?type=graph&sensor=rain&idx=23&range=day
/json.htm?type=graph&sensor=rain&idx=23&range=week
/json.htm?type=graph&sensor=rain&idx=23&range=month
/json.htm?type=graph&sensor=rain&idx=23&range=year

and process the results in your script.

You can use below dzVents script that I once created as an example / starting point.

Code: Select all

local scriptVar = 'water the plants'

return
{
    on =
    {
        timer =
        {
            'at 09:09', -- change to the time you want to check the amount of rain in the set number of days
        },

        httpResponses =
        {
           scriptVar,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when script works as expected
        marker = scriptVar,
    },

    execute = function(dz, item)

        local minAmount = 2 -- in mm rain
        local daysToEvaluate = 5 -- in days

        local rainDevice = dz.devices('myRain') -- name enclosed in quotes or number without quotes
        local waterPump = dz.devices('Beregening')

        if item.isDevice or item.isTimer then
            dz.openURL(
            {
                url = dz.settings['Domoticz url'] .. '/json.htm?type=graph&sensor=rain&idx=' .. rainDevice.idx .. '&range=month',
                callback = scriptVar,
            })
            return
        end

        local function getmm(rt)
            local startDate = (dz.time.addMinutes(-1 * daysToEvaluate * 24 * 60)).rawDate
            local mm = 0
            dz.log('startDate: ' .. startDate, dz.LOG_DEBUG)
            for index, record in ipairs(rt) do
                if record.d > startDate then
                    mm = mm + record.mm
                end
            end
            return mm
        end

        if item.ok and item.isJSON then
            local mmRain = getmm(item.json.result, days )
            if mmRain < minAmount then

                waterPump.cancelQueuedCommands()
                waterPump.switchOn()
                waterPump.switchOff().afterSec(3600)
                dz.log('Not enough rain in the last ' .. daysToEvaluate .. ' days (' .. mmRain .. ' mm). Switching waterpump on for 1 hour', dz.LOG_DEBUG)
            else
                dz.log('Enough rain in the last ' .. daysToEvaluate .. ' days (' .. mmRain .. 'mm). No need to turn on waterpump.', dz.LOG_DEBUG)
            end
        else
            dz.log('No valid response from domoticz ',dz.LOG_ERROR)
            dz.log(item,dz.LOG_ERROR)
        end
    end
}

Re: Using calculated amount of rain parameters from Domoticz

Posted: Thursday 27 May 2021 22:55
by Afterburner
Good startingpoint. Thanks.