Page 1 of 1

How much will it rain the next 24 hours

Posted: Thursday 31 January 2019 21:51
by limar
Using the below scripts to determine the amount of rain that is expected for the next 24 hours and the amount of rain that fell in the last 24 hours. These values get placed in variables and can be used (in my case) to decide whether or not to turn on the garden sprinklers.

Note that you need to get the location of JSON.lua right for this one (I'm running on a Pi) as well as create or rename the referenced variables and obviously change the url to include your creds and location. Also note that one is wunderground and the other openweathermap because of availability of the respective data.

Next 24 hours

Code: Select all

return {
    active = true,
    on = {
        timer = {'at 00:05'}
    },
    execute = function(domoticz)

        --json = assert(loadfile "./json.lua")()
        --json = assert(loadfile "/usr/local/domoticz/var/scripts/lua/JSON.lua")() --for synology 
        json = assert(loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
        --json = assert(loadfile "JSON.lua")() -- one-time load of the routines
        
        local config=assert(io.popen('curl "http://api.openweathermap.org/data/2.5/forecast?q=YOURLOCATION,YOURCOUNTRYCODE&mode=json&APPID=XXXXXXXXXX&units=metric"'))
        
        local weather = config:read('*all')
        config:close()
        local jsonWeather = json:decode(weather)
              
        local totalrain = 0
        
        local k = 1
        while k <= 8 do
           if type(jsonWeather.list[k].rain) ~= "nil" then
              if type(jsonWeather.list[k].rain["3h"]) ~= "nil" then
                 totalrain = totalrain + jsonWeather.list[k].rain["3h"]
              end
           end
           k = k + 1
        end
        
        domoticz.devices('Predicted Rainfall').update(0,totalrain)
    end
}
Last 24 hours

Code: Select all

return {
    active = true,
    on = {
        timer = {'at 00:05'}
    },
    execute = function(domoticz)
   
        --json = (loadfile "./json.lua")()
        --json = assert(loadfile "/usr/local/domoticz/var/scripts/lua/JSON.lua")() --for synology 
        json = assert(loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
        --json = assert(loadfile "JSON.lua")() -- one-time load of the routines
        
        local now = os.time()
        local yesterday = now-86400
        
        local urlYear = os.date("%Y",yesterday)
        local urlMonth = os.date("%m",yesterday)
        local urlDay = os.date("%d",yesterday)
        
        undUrl = "http://api.wunderground.com/api/XXXXXXXXXX/history_" .. urlYear .. urlMonth .. urlDay .. "/q/pws:YOURLOCATION.json"
        local config=assert(io.popen('curl ' .. undUrl))
        
        local weather = config:read('*all')
        config:close()
        local jsonWeather = json:decode(weather)
        
        local totalyesterdayrain = 0
        
        if type(jsonWeather.history.dailysummary[1]) ~= "nil" then
            if type(jsonWeather.history.dailysummary[1].precipm) ~= "nil" then
                totalyesterdayrain = jsonWeather.history.dailysummary[1].precipm
            end
        end
        
        domoticz.devices('Yesterday Rainfall').update(0,totalyesterdayrain)
    end
}

Re: How much will it rain the next 24 hours

Posted: Sunday 10 November 2019 10:30
by Scaevola
Hi,
I'm trying to use your script to gather information about predicted rain. I run it on Rpi3.
I entered my station coords and OWM api.
Also I created dummy hardware device "RainPredict" with new rain device with name "Opady".
To check if it works, i changed timer to "every other minute". The script starts and downloads proper data, but then log says:
2019-11-10 10:24:00.209 Status: dzVents: Info: ------ Start internal script: Opady2:, trigger: every other minute
2019-11-10 10:24:00.904 Status: dzVents: Info: ------ Finished Opady2
2019-11-10 10:24:00.904 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2019-11-10 10:24:00.937 Status: Warning: Expecting svalue with at least 2 elements separated by semicolon, 1 elements received ("7.5"), notification not sent (Hardware: 15 - RainPredict, ID: 82023, Unit: 1, Type: 55 - Rain, SubType: 3 - TFA)
The "Opady" device is not updated.
What I am doing wrong?

Re: How much will it rain the next 24 hours

Posted: Thursday 14 November 2019 10:17
by Scaevola
Ok, I found a solution for my problem. Since the dummy rain sensor awaits two variables and gets only one, I just changed it to temperature sensor - it awaits only one variable. Now script updates the variable.
The variable type not matters for me, since I want to use it only to check if it is gonna rain and condition start of sprinklers in Blockly.