Page 1 of 1

updating user var to slow

Posted: Monday 21 September 2020 15:52
by broker
Hello,

it seems that I have run into a brick wall and I am in need of an extra set of eyes (more experianced!)

I am building a script for my wellpump and 3 zones that are switched via a z-wave powersocket
I want the pump to automatically start when I enable one of the zone switches for 60minutes -1 and the zone for 60minutes.
if I switch on 2 zones and switch off one zone, then the pump must continue as there is still a zone who is "asking" for water.

I have used user variables in domoticz voor_aan, achter_aan and druppelslang_aan.
what I see happening is that the pump will start how it is requested, but it will not disable.
I see that the user var seems to be changed after the script calls for the enable/disable pump function and thus sees still one zone as 'true'

is there a way how I make sure the order is done correctly and (for me) as expected? or am I missing something?

Code: Select all

return {
	on = {
		devices = {
			'Waterpomp',
			'Sproeier Voor',
			'Sproeier Achter',
			'Druppelslang'
		}
	},
	execute = function(domoticz, device)
	    --declaratie van devices
	    local pomp          = domoticz.devices('Waterpomp')
	    local sachter       = domoticz.devices('Sproeier Achter')
	    local svoor         = domoticz.devices('Sproeier Voor')
	    local druppel       = domoticz.devices('Druppelslang')
	    --Hoelang moet er gesproeid worden
	    local sproeitijd    = 60

	    --help functie
	    local function schakelPomp(pomp,sproeitijd)
	        print("voor: "..domoticz.variables('voor_aan').value,"Achter: "..domoticz.variables('achter_aan').value,"Druppel: "..domoticz.variables('druppel_aan').value)
	        if pomp.active == false
	           then 
	               pomp.switchOn().forMin(sproeitijd-1)
	               print ("Pomp gaat aan via functie")
	       elseif pomp.active == true and domoticz.variables('voor_aan').value == 'false' and domoticz.variables('achter_aan').value == 'false' and domoticz.variables('druppel_aan').value == 'false'
	           then 
	                pomp.switchOff()
	                print("pomp gaat uit via functie")
            end
            return self --om te testen dat het is gelukt
        end
        
	    --pomp alleen aan, let op de druk van de pomp!
	    if 
	        device.name == 'Waterpomp' and device.active == false and domoticz.variables('voor_aan').value == 'false' and domoticz.variables('achter_aan').value == 'false' and domoticz.variables('druppel_aan').value == 'false'
    	    then  
    	        device.switchOff().afterMin(sproeitijd)
    	       print("pomp aan via schakelaar")
        --Sproeier in voortuin aan
        elseif 
            device.name == 'Sproeier Voor'
            then
                if device.active
                    then
                        svoor.switchOff().afterMin(sproeitijd)
                        domoticz.variables('voor_aan').set('true')
                        print('Voor aan')
                        schakelPomp(pomp,sproeitijd)
                        
                    else
                        domoticz.variables('voor_aan').set('false')
                        schakelPomp(pomp,sproeitijd)
                        print("Voor uit")
                end
        --Sproeier in achtertuin aan        
        elseif 
            device.name == 'Sproeier Achter'
	        then 
	            if device.active
	                then
                        sachter.switchOff().afterMin(sproeitijd)
                        domoticz.variables('achter_aan').set('true')
                        schakelPomp(pomp,sproeitijd)
                        print("Achter aan")
                    else
                        domoticz.variables('achter_aan').set('false')
                        schakelPomp(pomp,sproeitijd)
                        print("achter uit")
                end
        --Druppelslang voor en achter
        elseif 
            device.name == 'Druppelslang'
            then 
                if device.active
                    then
                        druppel.switchOff().afterMin(sproeitijd)
                        domoticz.variables('druppel_aan').set('true')
                        schakelPomp(pomp,sproeitijd)
                        print("Druppel aan")
                    else
                        domoticz.variables('druppel_aan').set('false')
                        schakelPomp(pomp,sproeitijd)
                        print("Druppel uit")
                    end
                end

	end
}

Re: updating user var to slow

Posted: Monday 21 September 2020 16:36
by waaren
broker wrote: Monday 21 September 2020 15:52 it seems that I have run into a brick wall and I am in need of an extra set of eyes (more experianced!)
is there a way how I make sure the order is done correctly and (for me) as expected? or am I missing something?
What I see is that you set the uservariables to 'false' and in the same execution of the script check if the value is 'false'
This does not work in eventscripts because the actual change of the var (and also the switch of a device) will only happen after the script finished. The commands are send back to domoticz and the domoticz main process will perform the actions.
If you want variables to be interpreted during the same execution of the script and also in other runs of the same script you need to change them to dzVents persistent variables.

Re: updating user var to slow

Posted: Monday 21 September 2020 16:57
by broker
Thanks, I was not aware of that fact, I just read up on the persistent vars and I will change it and check if it works!

Re: updating user var to slow

Posted: Monday 21 September 2020 22:47
by broker
hmm, I guess I am in need of some more advice, I guess I am using the persistent variable wrong, can some one please have a look. if I execute one of the zones, i get this message: Error: dzVents: Error: (3.0.4) .../domoticz/scripts/dzVents/generated_scripts/pomp_lua.lua:57: attempt to index a nil value (field 'data') I am also not sure if I can use actual booleans here, so I reverted to string checking.

Code: Select all

return {
	on = {
		devices = {
			'Waterpomp',
			'Sproeier Voor',
			'Sproeier Achter',
			'Druppelslang'
		},
		data = {
		    voor_aan    = {initial='false'},
		    achter_aan  = {initial='false'},
		    druppel_aan = {initial='false'}
		}
	},
	execute = function(domoticz, device)
	    --declaratie van devices
	    local pomp          = domoticz.devices('Waterpomp')
	    local sachter       = domoticz.devices('Sproeier Achter')
	    local svoor         = domoticz.devices('Sproeier Voor')
	    local druppel       = domoticz.devices('Druppelslang')
	    --Hoelang moet er gesproeid worden
	    local sproeitijd    = 60

	    --help functie
	    local function schakelPomp(pomp,sproeitijd)
	        --print("voor: "..domoticz.variables('voor_aan').value,"Achter: "..domoticz.variables('achter_aan').value,"Druppel: "..domoticz.variables('druppel_aan').value)
	        if pomp.active == false
	           then 
	               pomp.switchOn().forMin(sproeitijd-1)
	               print ("Pomp gaat aan via functie")
	       elseif pomp.active == true and domoticz.data.voor_aan == 'false' and domoticz.data.achter_aan == 'false' and domoticz.data.druppel_aan == 'false'
	           then 
	                pomp.switchOff()
	                print("pomp gaat uit via functie")
            end
            return self --om te testen dat het is gelukt
        end
        
	    --pomp alleen aan, let op de druk van de pomp!
	    if 
	        device.name == 'Waterpomp' and domoticz.data.voor_aan == 'false' and domoticz.data.achter_aan == 'false' and domoticz.data.druppel_aan == 'false'
    	    then  
    	        device.switchOff().afterMin(sproeitijd)
    	       print("pomp aan via schakelaar")
        --Sproeier in voortuin aan
        elseif 
            device.name == 'Sproeier Voor'
            then
                if device.active
                    then
                        svoor.switchOff().afterMin(sproeitijd)
                        domoticz.data.voor_aan = 'true'
                        print('Voor aan')
                        schakelPomp(pomp,sproeitijd)
                        
                    else
                        domoticz.data.voor_aan = 'false'
                        schakelPomp(pomp,sproeitijd)
                        print("Voor uit")
                end
        --Sproeier in achtertuin aan        
        elseif 
            device.name == 'Sproeier Achter'
	        then 
	            if device.active
	                then
                        sachter.switchOff().afterMin(sproeitijd)
                        domoticz.data.achter_aan = 'true'
                        schakelPomp(pomp,sproeitijd)
                        print("Achter aan")
                    else
                        domoticz.data.achter_aan = 'false'
                        schakelPomp(pomp,sproeitijd)
                        print("achter uit")
                end
        --Druppelslang voor en achter
        elseif 
            device.name == 'Druppelslang'
            then 
                if device.active
                    then
                        druppel.switchOff().afterMin(sproeitijd)
                        domoticz.data.druppel_aan = 'true'
                        schakelPomp(pomp,sproeitijd)
                        print("Druppel aan")
                    else
                        domoticz.data.druppel_aan = 'false'
                        schakelPomp(pomp,sproeitijd)
                        print("Druppel uit")
                    end
                end

	end
}

Re: updating user var to slow

Posted: Monday 21 September 2020 23:22
by waaren
broker wrote: Monday 21 September 2020 22:47 hmm, I guess I am in need of some more advice, I guess I am using the persistent variable wrong, can some one please have a look. if I execute one of the zones, i get this message: Error: dzVents: Error: (3.0.4) .../domoticz/scripts/dzVents/generated_scripts/pomp_lua.lua:57: attempt to index a nil value (field 'data') I am also not sure if I can use actual booleans here, so I reverted to string checking.
You misplaced one }
dzvents now sees data as part of the on = section
the data can be boolean.

Should look like

Code: Select all

return 
{
    on = 
    {
        devices = 
        {
            'Waterpomp',
            'Sproeier Voor',
            'Sproeier Achter',
            'Druppelslang'
        },
    },
    
    data = 
    {
            voor_aan    = { initial = 'false' },
            achter_aan  = { initial = 'false' },
            druppel_aan = { initial = 'false' },
    },        
    
    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'Waterpomp',
    },

Re: updating user var to slow  [Solved]

Posted: Tuesday 22 September 2020 9:03
by broker
lol that makes sense, thanks!
its good to make mistakes, now I learned what to look for in my other scripts, as I am converting my blockly to dzvents.