.rain return 0

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

Moderator: leecollings

Post Reply
hestia
Posts: 361
Joined: Monday 25 December 2017 23:06
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: Paris
Contact:

.rain return 0

Post by hestia »

In DzVents docs
Rain meter
rain: Number (please note that this does return the rain total for today)
rainRate: Number
If the value of the counter in less than 1, the .rain returns 0, the value on the device (sValue) is OK
If the value is greater then 1, event though we put value w 1 decimal, it could returns a value w a lot of decimals
The value shows in the GUI is less than the value in the device (sValue)

A script the test

Code: Select all

--[[


return 
{
    on = 
    {
        devices = 
        {
            "tippingbucketrain",
        },
    },   

    logging = 
    {
        level = domoticz.LOG_DEBUG,            -- INFO, ERROR or DEBUG
        marker = "ESP test",
    }, 

    execute = function(dz, item)
               
        local rainDevice = dz.devices("Regenmeter")               -- Your (virtual) rain device
        local rainSwitch = dz.devices("tippingbucketrain")        -- Your (triggered by bucket full) switch 
        local rainmm = 2                                          -- find out what 1 bucket full means in terms of mm 
        local rainTotal = 0
        local timeSlice = math.min( (rainDevice.lastUpdate.secondsAgo / 3600), 10) -- at least 1 bucket  in 10 hours     
        
        local rainAmountHour = dz.utils.round((rainmm / timeSlice),1)
        
        if item.active then
            rainTotal = dz.utils.round((rainmm + rainDevice.rain),1)  
            rainDevice.updateRain(rainAmountHour, rainTotal)           
            dz.log("One bucket full ==>> updating raindevice. rainrate: " .. rainAmountHour .. " mm/hr, " .. rainTotal .." mm in total today ", dz.LOG_DEBUG )
            rainSwitch.switchOff().silent()
        end
    end
 }

]]--

local TEST = 205  -- a dummy switch for testing w/o waiting minutes / remove comment to use / comment to ignore

local dev_rain1 = 2593 -- dummy to receive rain in mm / Type Rain
local dev_rain2 = 2594 -- dummy to receive rain in mm / Type Rain
--local uv_rain = 38

local TIME_INTERVAL = 'every 15 minutes except at 00:00-00:55' -- the except to avoid the reset of the events (dzVents) and late go to 0 off the PWS

-- log options
local LOG_DEBUG = 2     -- 0 =>ERROR / 1 => FORCE / 2 => DEBUG
local LOG_LEVEL
local LOGGING
if LOG_DEBUG == 2 then
    LOGGING = domoticz.LOG_DEBUG
    LOG_LEVEL = domoticz.LOG_DEBUG
elseif LOG_DEBUG == 1 then
    LOGGING = domoticz.LOG_FORCE
    LOG_LEVEL = domoticz.LOG_FORCE
else
    LOGGING = domoticz.LOG_ERROR
    LOG_LEVEL = domoticz.LOG_INFO
end


return {
    logging =   {
                level =   LOGGING
                },
	on = {
		devices = {TEST},
        timer   = {TIME_INTERVAL}

  	            },


	execute = function(dz, triggerObject)
	_G.logMarker =  dz.moduleLabel -- set logmarker to scriptname
	local _u =  dz.utils

	    -- /// Functions start \\\

        local function logWrite(str,level)  -- Support function for shorthand debug log statements
            if level == nil then
                level = LOG_LEVEL
            end
            dz.log(tostring(str),level)
        end
        
        local function jsonRainCounter(p_IDX, p_RAINRATE, p_RAINCOUNTER)
            logWrite('IDX= ' .. p_IDX .. ' rate= ' .. p_RAINRATE .. ' rain= ' .. p_RAINCOUNTER)
            local cmd = '/json.htm?type=command&param=udevice&idx=' .. p_IDX .. '&nvalue=0&svalue=' .. p_RAINRATE .. ';' .. p_RAINCOUNTER
            local url = 'http://127.0.0.1:8080' .. cmd
            logWrite('url= ' .. url)
            dz.openURL(url)
            return
        end

    	-- \\\ Functions end ///

        local rainTot, rainRate

        --if dz.variables(uv_rain).lastUpdate.isToday then
            --rainTot = dz.variables(uv_rain).value
            --logWrite('Same day: ' .. rainTot)
        --else
            --logWrite('New day', dz.LOG_FORCE)
            --rainTot = 0
        --end
        
        rainTot = dz.devices(dev_rain1).rain
        logWrite('rainTot initial: ' .. rainTot, dz.LOG_FORCE)
                
        local rainVal = dz.devices(dev_rain1).sValue
        logWrite('rainVal : ' .. rainVal, dz.LOG_FORCE)
        
        local w_rainValTable = _u.stringSplit(rainVal,';')
        rainTot = w_rainValTable[2]
        logWrite('rainValTot : ' .. rainTot, dz.LOG_FORCE)        
        
        
        if dz.time.matchesRule('between 07:59 and 23:59') then
            rainTot = _u.round(rainTot + 0.1, 1)
            rainRate = 0.1 * 12
            logWrite('rainTot increases: ' .. rainTot, dz.LOG_FORCE)
        else
            rainRate = 0
            logWrite('rainTot unchanged: ' .. rainTot, dz.LOG_FORCE)
        end
        
        dz.devices(dev_rain1).updateRain(rainRate * 100, rainTot)
        
        jsonRainCounter(dev_rain2, rainRate * 100, rainTot)
        
        --dz.variables(uv_rain).set(rainTot)

    
        logWrite ('***** FIN *****')

    end
}
Extracts of the log
part "rainTot initial:" + "rainVal:"
27/04/2022 09:45:00 Notice hestia local6 domoticz "dzVents: !Info: A_Rain: rainVal : 120.0 0.5"
27/04/2022 09:45:00 Notice hestia local6 domoticz "dzVents: !Info: A_Rain: rainTot initial: 0.40000000596046 "
27/04/2022 09:40:00 Notice hestia local6 domoticz "dzVents: !Info: A_Rain: rainVal : 120.0 0.4"
27/04/2022 09:40:00 Notice hestia local6 domoticz "dzVents: !Info: A_Rain: rainTot initial: 0.30000001192093 "
27/04/2022 09:35:00 Notice hestia local6 domoticz "dzVents: !Info: A_Rain: rainVal : 120.0 0.3"
27/04/2022 09:35:00 Notice hestia local6 domoticz "dzVents: !Info: A_Rain: rainTot initial: 0.20000000298023 "
27/04/2022 09:34:39 Notice hestia local6 domoticz "dzVents: !Info: A_Rain: rainVal : 120.0 0.2"
27/04/2022 09:34:39 Notice hestia local6 domoticz "dzVents: !Info: A_Rain: rainTot initial: 0.10000000149012 "
27/04/2022 09:34:02 Notice hestia local6 domoticz "dzVents: !Info: A_Rain: rainVal : 120.0 0.1"
27/04/2022 09:34:02 Notice hestia local6 domoticz "dzVents: !Info: A_Rain: rainTot initial: 0 "
27/04/2022 09:33:14 Notice hestia local6 domoticz "dzVents: !Info: A_Rain: rainVal : 120.0 0.1"
27/04/2022 09:33:14 Notice hestia local6 domoticz "dzVents: !Info: A_Rain: rainTot initial: 0 "
27/04/2022 09:30:00 Notice hestia local6 domoticz "dzVents: !Info: A_Rain: rainVal : 120.0 0.1"
27/04/2022 09:30:00 Notice hestia local6 domoticz "dzVents: !Info: A_Rain: rainTot initial: 0 "
[/spoiler]

Last run
Spoiler: show
2022-04-27 12:30:01.479 Status: dzVents: !Info: A_Rain: rainTot initial: 1.7000000476837
2022-04-27 12:30:01.479 Status: dzVents: !Info: A_Rain: rainVal : 120.0;1.8
2022-04-27 12:30:01.479 Status: dzVents: !Info: A_Rain: rainValTot : 1.8
2022-04-27 12:30:01.480 Status: dzVents: !Info: A_Rain: rainTot increases: 1.9
Screenshot 2022-04-27 124237.png
Screenshot 2022-04-27 124237.png (17.09 KiB) Viewed 258 times
at 12:44
should have been 1.9 instead of 1.8
Screenshot 2022-04-27 124719.png
Screenshot 2022-04-27 124719.png (38.76 KiB) Viewed 258 times
Screenshot 2022-04-27 124940.png
Screenshot 2022-04-27 124940.png (9.19 KiB) Viewed 258 times
For the rain: "Number (please note that this does return the rain total for today" I need to wait for tomorrow the see if it is today or not ; idem for sValue
hestia
Posts: 361
Joined: Monday 25 December 2017 23:06
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: Paris
Contact:

Re: .rain return 0

Post by hestia »

More info...
First in the DzVents doc:
Rain meter
rain: Number (please note that this does return the rain total for today)
rainRate: Number
updateRain(rate, counter): Function. (rate in mm * 100 per hour, counter is total in mm)
rain is the rain of the day
counter is the total rain of the counter from the begining of the counter
So not easy!
This function gives the total rain of the counter
local function RainTot (p_id)
local f_rainValTable = _u.stringSplit(dz.devices(p_id).sValue,';')
local f_rainTot
if f_rainValTable[2] ~= nil then
f_rainTot = math.ceil(f_rainValTable[2] * 10) / 10
else
f_rainTot = 0
end
logWrite('Rain Tot = ' .. f_rainTot, dz.LOG_DEBUG)
return f_rainTot
end
After we have to had to this value the delta.
But usually rain meters give the value of the day...
So I keep the raintot at the beginning of the day with something like this (part of a script as an example)

Code: Select all

                                local newDay

                                if dz.devices(dev_PWS_rain).lastUpdate.isToday then
                                    logWrite('Same day')
                                    newDay = false
                                else
                                    logWrite('New day', dz.LOG_FORCE)
                                    newDay = true
                                end 

                                if dz.data.PWS_rainTotalOh == nil then
                                    logWrite(WU_Station .. ' dz.data.PWS_rainTotalOh NIL', dz.LOG_ERROR)
                                end
                                
                                if newDay or dz.data.PWS_rainTotalOh == nil then
                                    dz.data.PWS_rainTotalOh = RainTot(dev_PWS_rain) -- save rain tot at the beg of the day
                                    logWrite(WU_Station .. ' rain total at beg of the day: ' .. dz.data.PWS_rainTotalOh, dz.LOG_FORCE)
                                end
After just add the rain of the day to the rain tot at the beg of the day and update the device like

Code: Select all

                                local w_rainTot = dz.data.PWS_rainTotalOh + WUPrecipTotal
                                logWrite(WU_Station .. ' rainTot: ' .. w_rainTot, dz.LOG_FORCE)
        			dz.devices(dev_PWS_rain).updateRain(WUPrecipRate*100,w_rainTot)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest