updating user var to slow  [Solved]

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

Moderator: leecollings

Post Reply
broker
Posts: 19
Joined: Friday 08 January 2016 14:57
Target OS: Linux
Domoticz version: 2020.2
Contact:

updating user var to slow

Post 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
}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: updating user var to slow

Post 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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
broker
Posts: 19
Joined: Friday 08 January 2016 14:57
Target OS: Linux
Domoticz version: 2020.2
Contact:

Re: updating user var to slow

Post 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!
broker
Posts: 19
Joined: Friday 08 January 2016 14:57
Target OS: Linux
Domoticz version: 2020.2
Contact:

Re: updating user var to slow

Post 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
}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: updating user var to slow

Post 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',
    },
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
broker
Posts: 19
Joined: Friday 08 January 2016 14:57
Target OS: Linux
Domoticz version: 2020.2
Contact:

Re: updating user var to slow  [Solved]

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest