Persistent Data problem

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

Moderator: leecollings

Post Reply
randytsuch
Posts: 90
Joined: Sunday 20 March 2016 18:56
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: LA, Ca USA
Contact:

Persistent Data problem

Post by randytsuch »

I'm sure I'm doing something stupid, but not sure what.

I'm getting this error message from Domoticz logs:
2017-06-14 07:02:00.114 LUA: ...scuit/dev-domoticz/scripts/lua/scripts/SpaPumpSwitch.lua:30: attempt to perform arithmetic on field 'UpdateTimeTotal' (a nil value)

I'm trying to make a persistent variable UpdateTimeTotal which is the time of a switch push, in seconds, so really just an integer.
The error message above is for this line of code (I know the code's logic is wrong, just trying to fix the error message):
DeltaTime = domoticz.data.UpdateTimeTotal + CurrentTimeTotal

Here is the complete code, which is very much a work in progress so please don't laugh :)
BTW, I'm a HW type, know just enough about SW to be dangerous.

Code: Select all

local Time = require('Time')
local UpdateTime = Time()
--local UpdateTimeTotal  -- Update time with hours and mins converted to seconds
local CurrentTimeTotal --Current time with hours and mins converted to seconds 
local DeltaTime -- difference between current and update times

     return {
    	active = true,
    	on = {
    		['timer'] = 'every minute',
             'SpaPumpOnButton', 
             'SpaPumpCommand', 
             'SpaHeatOnButton'
    	},
    	    data = {
	    UpdateTimeTotal = {initial=1}
	},
    	execute = function(domoticz)
      --  UpdateTime=(domoticz.time)
     --   print (UpdateTime.hour, UpdateTime.min, UpdateTime.sec)
        CurrentTimeTotal = (domoticz.time.hour*3600 + domoticz.time.min*60 + domoticz.time.sec)
        print (CurrentTimeTotal)
        print (domoticz.data.UpdateTimeTotal)
        DeltaTime = domoticz.data.UpdateTimeTotal + CurrentTimeTotal
        print (DeltaTime)
    	if (domoticz.devices['SpaPumpOnButton'].state == 'On') then     -- if button pushed
    		if (domoticz.devices['SpaPumpCommand'].state == 'On') then
                        domoticz.devices['SpaPumpCommand'].switchOff()
    			print ('Hey!', 'I am off now!')
    		else
                        domoticz.devices['SpaPumpCommand'].switchOn()
                        print ('Hey!', 'I am on now!')
    		end
    	end
    end
    }

Any help would be most appreciated.
Eventually, this will be used to do something different on a double push of a button versus a single push. Also for debouncing the push button.

Randy
randytsuch
Posts: 90
Joined: Sunday 20 March 2016 18:56
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: LA, Ca USA
Contact:

Re: Persistent Data problem

Post by randytsuch »

I gave up :(
I even tried to use the "sample" code, the counter example, and still got errors with it.

So I created a Domoticz user variable, and am using that now, and it works fine :)
Between adding a cap to the switch, and my new code that ignores button pushes less then 2 seconds apart, things work fine.

Code: Select all

--[[
handle switch pushes for spa
SpaPumpCommand is a virtual switch that turns the pump on

 ]]

--domoticz.variables['PushTimeTotal'].value  global variable to save time of last button push, in seconds
local CurrentTimeTotal --Current time with hours and mins converted to seconds 
local DeltaTime -- difference between current and last button push time

return {
    active = true,
    on = {
    --	['timer'] = 'every minute',
        'SpaPumpOnButton', 
        --'SpaPumpCommand', 
        'SpaHeatOnButton'
    	},

    execute = function(domoticz)
    	CurrentTimeTotal = (domoticz.time.hour*3600 + domoticz.time.min*60 + domoticz.time.sec)
    	DeltaTime = CurrentTimeTotal - domoticz.variables['PushTimeTotal'].value
    	print ('DeltaTime')
    	print (DeltaTime)
    	if (DeltaTime > 2) then    --debounce logic, ignore if pushes are too close together
    	    domoticz.variables['PushTimeTotal'].set(CurrentTimeTotal)  --Set Push Time to current time
    	    print ('new time')    
        	if (domoticz.devices['SpaPumpOnButton'].state == 'On') then     -- if pump on button was pushed
    	 	   if (domoticz.devices['SpaPumpCommand'].state == 'Off') then   -- check if pump is off or on
            	   domoticz.devices['SpaPumpCommand'].switchOn()
    			  -- print ('Hey!', 'I am on now!')
    		   else
            	  -- need to add code to check if heater is on. 
            	   domoticz.devices['SpaPumpCommand'].switchOff()
            	  -- print ('Hey!', 'I am off now!')
    		   end
    	   end 
    	end    
    end
}  
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Persistent Data problem

Post by dannybloe »

Mm.. interesting.. I know there's a bug with persistent data in version 1.1.x that should be fixed in 2.0. Perhaps you can try again as soon as dzVents 2.0 is merged into Domoticz beta. Worth testing coz I hate bugs ;-)

Also, if you use a persistent variable with history=true and maxItems = 1, then you automatically get a timestamp on each value. That makes it almost trivial to see how long it's been since the previous value. (when it works ;-))
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Persistent Data problem

Post by dannybloe »

Oh, and you can use domoticz.devices['SpaPumpCommand'].toggleSwitch(). Saves you a couple of lines of code:

Code: Select all

domoticz.devices['SpaPumpCommand'].toggleSwitch()
domoticz.log(domoticz.devices['SpaPumpCommand'].bState and "I am off now" or "I am on now")
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
randytsuch
Posts: 90
Joined: Sunday 20 March 2016 18:56
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: LA, Ca USA
Contact:

Re: Persistent Data problem

Post by randytsuch »

Thanks for the info Danny.
Looking forward to trying version 2 as soon as it's merged into the Domoticz beta.
The history idea is interesting, but I already have working code so will likely stick with what I have. I'm not a SW person so making changes is not so easy for me.

I can't use toggleSwitch, there will eventually be different actions for off. When turning the pump off, I need to check if the heater is on. I need to turn the heater off first, run the pump for 10 mins more (cool off the heater), then turn the pump off. It will be nice to automate this, I need to do it manually right now.

And for anyone else reading, I had a logic error when there is a day between button pushes you can get a negative delta time, because I didn't account for days.

So I had to change code to:

Code: Select all

 if (DeltaTime > 2 or DeltaTime < 0) then
Randy
randytsuch
Posts: 90
Joined: Sunday 20 March 2016 18:56
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: LA, Ca USA
Contact:

Re: Persistent Data problem

Post by randytsuch »

dannybloe wrote:Mm.. interesting.. I know there's a bug with persistent data in version 1.1.x that should be fixed in 2.0. Perhaps you can try again as soon as dzVents 2.0 is merged into Domoticz beta. Worth testing coz I hate bugs ;-)
It would be great to add a known bugs section to the read me or the quick reference guide.

Might have saved me a few grey hairs lol.

Randy
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest