persistent data reporting nil value over and over

Easy to use, 100% Lua-based event scripting framework.
Post Reply
snuiter
Posts: 67
Joined: Saturday 17 June 2017 12:30
Target OS: Raspberry Pi
Domoticz version: beta
Contact:

persistent data reporting nil value over and over

Post by snuiter » Tuesday 15 May 2018 10:57

I created a script to monitor the watermeter as I had a few instances where the toiletwater kept running. Although I fixed the toilet I recently had another small incident where water kept running. I now made a script using a variable that seems to be running fine but when I wanted it to use the persistent data I was not able to get it working as it keeps stating in the logs a nil value. I have tried and test many different things but no luck.

Basically any arithmetic is reported back that it is a nil value, going through the forums I can't get find any syntax error but I must be doing something silly here.

This line : domoticz.data.watercntr = 1 -- reset the counter
or this line: domoticz.data.watercntr = domoticz.data.watercntr + 1


Any idea?

Thx!
Spoiler: show

Code: Select all

return {
	on = {
		devices = {	'water(gpio)' },
		data = { watercntr = {initial=1} },
	},
	
	execute = function(domoticz, device)
	    local watertik = domoticz.variables('watert').value
	   	local waterled = domoticz.devices('water(gpio)')


        if waterled.lastUpdate.secondsAgo > 15 and watertik ~= 0 then
            domoticz.variables('watert').set(1)
            domoticz.data.watercntr = 1  -- reset the counter 
        end

		if waterled.lastUpdate.secondsAgo < 15 then
            watertik = watertik + 1
            domoticz.data.watercntr = domoticz.data.watercntr + 1
            
            -- print('water loopt #' .. watertik .. '')
		    if watertik > 200 then
		        domoticz.notify('','water loopt al een tijd - 200')
	        elseif watertik > 100 then
		        domoticz.notify('','water loopt al een tijd - 100')
            elseif watertik > 50 then
		        domoticz.notify('','water loopt al een tijd - 50')
	        end
    	    
    	    domoticz.variables('watert').set(watertik)
        end
    
	end
}

dannybloe
Posts: 1480
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi
Domoticz version:
Location: Ermelo
Contact:

Re: persistent data reporting nil value over and over

Post by dannybloe » Tuesday 15 May 2018 12:19

Not sure yet what might be wrong but in your case I would use an historical persistent variable because there each value that you put in there already has a time-stamp. So, when gpio turns 1 you put 1 in that history value and when it turns 0 you clear the history.

So, assuming that water(gpio) is active (state == 'On' or something like that) then this would be the script (untested):

Code: Select all

return {
	on = {
		devices = {'water(gpio)'}
	},
	data = { running = { history = true, maxItems = 1 } },
	execute = function(domoticz, waterled)
		const running = domoticz.data.running.getLatest()
		
		if (not waterled.active) then
			domoticz.data.running.reset()
			return
		end

		if (previous.running == true) then
			domoticz.log('Water has been running since: ' .. previous.time.minutesAgo)
		else
			-- water just starting to flow, set the history value to true
		domoticz.data.running.set(true)
		end
	end
}
However, if your device doesn't work like this then add a timer that checks every 15 minutes (timer = 'every 15 minutes') or something like that.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests