STrange Behavior with this script

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

Moderator: leecollings

Post Reply
acaonweb
Posts: 87
Joined: Thursday 23 March 2017 14:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

STrange Behavior with this script

Post by acaonweb »

HI guys,
I need an help on this script, that is running since 2018 like a charme

is a script form my irrigation system and works like that:

Code: Select all

-- Check the wiki at
-- http://www.domoticz.com/wiki/%27dzVents%27:_next_generation_LUA_scripting

local notificationdevice = {'iPhone6S','iPadAir','PC','Laptop'}

return {

	-- 'active' controls if this entire script is considered or not
	active = true, -- set to false to disable this script

	-- trigger
	-- can be a combination:
	on = {
		devices = {'Irrigatore'},
		timer = {'30 minutes after sunset'}
		--timer = {'at 21:00'}
	},
	logging = {
	    level = domoticz.LOG_DEBUG,
	    marker = "Irrigazione: "
	},

	-- actual event code
	-- in case of a timer event or security event, device == nil
	execute = function(dz, device, tginfo)
	    dz.log("EVENTO "..tginfo.type)
	    local v_01 = dz.devices("Valvola_01")
	    local v_02 = dz.devices("Valvola_02")
	    local irrig = dz.devices("Irrigatore")
	    --local webcam= dz.devices("Webcam Sala")
	    local rain = dz.devices("Rain").rain
	    local baro= string.lower(dz.devices("THB").forecastString)
	    local waterConsumption=dz.devices("water")
	    local tExt=math.floor(dz.devices("Temperatura Esterna").temperature)
	    local notifyText =""
	    local freqTime = ''
	    local frequenza = dz.devices("Frequenza Irrigatore").level
	    local freqState = dz.devices("Frequenza Irrigatore").state
	    local hum1 = 40
	    local hum2 = 40
	    local tRif=dz.variables('maxTemp').value
	    if (frequenza == 0) then
	        freqTime = ''
        elseif (frequenza == 10) then
            freqTime = 'on mon, tue, wed,thu, fri, sat, sun'
        elseif (frequenza == 20) then
            freqTime = 'on mon, wed, fri, sun'
        elseif (frequenza == 30) then 
            freqTime = 'on sun'
        end
        
	            
	    dz.log("Rain Ratio: "..rain)
	    dz.log("Meteo Status: "..baro)
	    --dz.log(">>>>>"..dz.BARO_CLOUDY)
	    dz.log("Temp tExt: "..tExt)
	    dz.log("Frequ: "..freqTime)
	    dz.log("HUM01: "..hum1)
	    dz.log("HUM02: "..hum2)
	    hum1 = dz.devices("Soil Moisture 1").moisture
	    hum2 = dz.devices("Soil Moisture 1").moisture
	    
	    dz.log("NEW HUM01: "..hum1)
	    dz.log("NEW HUM02: "..hum2)
	    
	    --newtempoIrrigazione01=(90-hum1)/(90-5)*60
	    --newtempoIrrigazione02=(90-hum2)/(90-5)*60
	    
	    --select algoritm
	    if (dz.devices('Rain Evaluation').state == "Yes") then
	        waterIsOK = (rain < 0.5 or baro ~= "rain") 
        elseif (dz.devices('Rain Evaluation').state == "No") then
            waterIsOK = true
        end
        
	    --dz.log("alg ")
	    dz.log("Test water ok: "..tostring(waterIsOK))
	    
	    
	    
	    if (waterIsOK) then
	    --if (rain < 0.5 or baro ~= "rain") then
	        if(tginfo.type==dz.EVENT_TYPE_DEVICE) then

                
                newtempoIrrigazione01=dz.helpers.irrigationTime(hum1,tRif,baro)
                newtempoIrrigazione02=dz.helpers.irrigationTime(hum2,tRif,baro)
                
                totalTime=math.max(newtempoIrrigazione01,newtempoIrrigazione02)
                dz.log("Total Time: "..totalTime)
                
                
                notifyText=notifyText.." Irrigazione attivata per \n"
                notifyText=notifyText.."Valvola 01 :"..tostring(newtempoIrrigazione01).." minuti \n"
                notifyText=notifyText.."Valvola 02 :"..tostring(newtempoIrrigazione02).." minuti \n"
                notifyText=notifyText.."Tempo Totale :"..tostring(totalTime).." minuti \n"
                notifyText=notifyText.."Temperatura esterna "..tostring(tRif).."°C \n"
                notifyText=notifyText.."Previsione "..baro
                
	            if (irrig.state=="On") then
	                --waterConsumption.updateCustomSensor(2)
	                dz.log("inzio irrigazione")
	                dz.helpers.notification(dz,'Irrigazione',notifyText,'water_drops',notificationdevice)
	                dz.devices("irrigazioneText").updateText(notifyText)

	                
	                v_01.switchOn().forMin(newtempoIrrigazione01)
	                v_02.switchOn().forMin(newtempoIrrigazione02)

	                dz.log("EVENTO..ON")
	                irrig.switchOff().afterMin(totalTime)
	                dz.log("EVENTO..OFF afterMin")
	                
	                dz.helpers.alexa(dz,"Inizio Irrigazione, durata "..totalTime.." minuti",10)
	                message= 'mosquitto_pub -t irrigtime -m "{\\"tempoirrigazione\\":'..totalTime..'}"'
	                dz.log(message)
	                dz.executeShellCommand(message)

                end
                if irrig.state=="Off" then
                    
                    irrig.cancelQueuedCommands()
                    v_01.cancelQueuedCommands()
	                v_02.cancelQueuedCommands()
	                
	                v_01.switchOff().repeatAfterMin(1,5)
	                v_02.switchOff().repeatAfterMin(1,5)
	                dz.devices("irrigazioneText").updateText("Stop")
	                
	                --calcolo consumo acqua
	                local q1 = 0.7 --litri al minuto
	                local n_irrigatori=2 --numero n_irrigatori
	                local dt = math.floor(irrig.lastUpdate.secondsAgo) --tempo in secondi di emissione acqua
	                dz.log("Time "..dt)
	                
	                waterConsumption.updateCustomSensor(math.floor(irrig.lastUpdate.secondsAgo/60*0.7*2))
	                waterConsumption.updateCustomSensor(0).afterMin(5)

	                --contatore incrementale
	                dz.devices('Water Counter').updateCounter(math.floor(irrig.lastUpdate.secondsAgo/60*0.7*2))

	                --scrivo il riassunto updateCustomSensor
	                local riassunto = os.date("%d/%m/%y %H:%M").."\nTempo effettivo: "..tostring(math.floor(irrig.lastUpdate.secondsAgo/60)).." minuti\n"
	                riassunto = riassunto.."Litri effettivi: "..tostring(math.floor(irrig.lastUpdate.secondsAgo/60*0.7*2)).."\n"
	                dz.devices("irrigazioneText").updateText(riassunto)
	                --notifiche
	                dz.log("2")

	                dz.helpers.notification(dz,'Irrigazione', "Stop\n\n"..riassunto,'water_drops',notificationdevice)
	                dz.helpers.alexa(dz,"Fine Irrigazione, durata totale"..tostring(math.floor(irrig.lastUpdate.secondsAgo/60)).." minuti",10)
	                stop=0
	                message= 'mosquitto_pub -t irrigtime -m "{\\"tempoirrigazione\\":'..stop..'}"'
	                dz.executeShellCommand(message)
                end
            end
            if(tginfo.type==dz.EVENT_TYPE_TIMER) then
                if(freqTime ~= "") then
                    if (dz.time.matchesRule(freqTime)) then
                        irrig.switchOn()
                        dz.log("EVENTO..ON Timer")
                    else
                        dz.log("Ogni tot giorni")
                        dz.helpers.notification(dz,'Irrigazione', "Disattivo. Impostato a: "..freqState,'water_drops',notificationdevice)
                    end
                else
                    dz.log("Disattivo")
	                dz.helpers.notification(dz,'Irrigazione', "Impianto Disattivato",'water_drops',notificationdevice)
                end
                
            end
        else
            dz.log("Pioggia")
            dz.helpers.notification(dz,'Irrigazione','Piove '..tostring(math.floor(rain))..' mm','water_drops', notificationdevice)
            irrig.cancelQueuedCommands()
            irrig.switchOff().silent()
            dz.devices("irrigazioneText").updateText("Piove")
        
        end
	end
}
the script is triggered from one device and one timer
when the dummy device irrig is switch on, two dummy (v_01 and v_02 are switched on for some minutes with .forMin(), and irrig is switched off with .afterMin() for the same amount of minutes.
When the device irrig is switched off, two dummy (v_01 and v_02 are switched off, all the queue are cancelled, some calculation about the water consumption using irrig.lastUpdate.secondsAgo() are done and update some counters.
I've inserted a log immediately after execute

Code: Select all

	execute = function(dz, device, tginfo)
	    dz.log("EVENTO "..tginfo.type)
Now I've noticed that the script run twice as is triggered for example ON, like log shows:

Code: Select all

2022-06-05 11:14:02.073 Status: dzVents: Info: Irrigazione: : EVENTO device
2022-06-05 11:14:02.317 Status: dzVents: Info: Irrigazione: : EVENTO device


I'm blocked with the debug because I really don't understand what is happening, because I never had this problem.
I'm pretty sure about that, because the calculation of the water flow return the first time the right number of seconds, and immediately zero, because on the run of the script irrig.lastUpdate.secondsAgo() return zero (and this is correct)
I've ever had water calculation, but now is 0 at all, so I'm sure that the script before was running good.

What's happenig?
My setup

Code: Select all

Version: 2022.1 (build 14352)
Build Hash: 5765540b9
Compile Date: 2022-06-03 07:39:12
dzVents Version: 3.1.8
Python Version: 3.7.3 (default, Jan 22 2021, 20:04:44) [GCC 8.3.0]
Uptime: 17 Hours, 30 Minutes, 25 Seconds 

Code: Select all

PRETTY_NAME="Raspbian GNU/Linux 10 (buster)"
NAME="Raspbian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs
Thanx in advance

Fabrizio
Post Reply

Who is online

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