Page 1 of 1

DsVents global var lastupdate

Posted: Friday 09 March 2018 7:14
by Deluka
Is it possible to get the lastupdate time of a global var
Form what i can tell reading the wiki it could be possible but i can’t get it working

Re: DsVents global var lastupdate

Posted: Friday 09 March 2018 7:55
by waaren
Yes, it is possible if you mean a global var within dzVents

Look at the A special kind of persistent variables: history = true (Historical variables API) section in

https://www.domoticz.com/wiki/DzVents:_ ... _variables


relevant scriptlines:

Code: Select all

data = {
		myGlobalVar = { history = true, maxItems = 1 }
	},

local myPersistentVar = domoticz.data.myGlobalVar

myPersistentVar.add("test data")
print ( 'Oldest time stamp in history: ' ..  myPersistentVar.getOldest().time.getISO())
print ( 'Last added value in history: ' ..  myPersistentVar.getLatest().data)

Re: DsVents global var lastupdate

Posted: Friday 09 March 2018 8:27
by Deluka
Thx and what About script level vars

Re: DsVents global var lastupdate

Posted: Friday 09 March 2018 8:41
by waaren
Please elaborate on what your exact requirement is. A sample script might help me understand what you try to achieve

Re: DsVents global var lastupdate

Posted: Friday 09 March 2018 19:11
by Deluka
Its for a lux meter.
If the lux level is below or above a set level for a set time turn on or off the lights
Made this in plain lua and want to port this to dzvents
There the var is only updated once and only if the current lux is above or under the set level.
If the var is not changed for a set time the lights a turned off or on
Hope this makes sence ;)

Re: DsVents global var lastupdate

Posted: Friday 09 March 2018 22:16
by waaren
Deluka wrote: Friday 09 March 2018 19:11 Its for a lux meter.
If the lux level is below or above a set level for a set time turn on or off the lights
Made this in plain lua and want to port this to dzvents
There the var is only updated once and only if the current lux is above or under the set level.
If the var is not changed for a set time the lights a turned off or on
Hope this makes sence ;)
Could you try this and come back with your result ?

Code: Select all

--[[ 
luxStateTest
 ]]--

return {
	on 	= { timer = { "every minute" } 	},
	data 	= { luxLevel = { history = true, maxItems = 1, initial = "Empty" } },
	
	execute = function(domoticz, _)
		local currentLux = domoticz.devices("Name of your lux device").lux or 'fail'
		local luxSwitchLevel = 100   		-- change to lux value where you want to switch on 
		local timeFence = 30   			-- change to how many minutes you want to wait with switching the device on / off
		local luxDependendLight = domoticz.devices("Name of your light")
		
		local luxState = domoticz.data.luxLevel
		local luxRecord = luxState.getLatest() 
		local luxData = luxRecord.data
		local luxTime = luxRecord.time
		
		if currentLux ~= 'fail' then 
			if currentLux >= luxSwitchLevel then
				if luxData ~= "High" then
					luxState.add("High")
			    	elseif luxTime.minutesAgo > timeFence then
					luxDependendLight.switchOff().checkFirst()	
				end
			elseif currentLux < luxSwitchLevel then
				if luxData ~= "Low" then
					luxState.add("Low")
				elseif luxTime.minutesAgo > timeFence then
					luxDependendLight.switchOn().checkFirst()	
				end
			end
		else
			print ( 'Skipped adding lux to my (Wrong lux reading) ' )
		end
	end
}

Re: DsVents global var lastupdate

Posted: Saturday 10 March 2018 7:45
by Deluka
Thx for your help but getting a error
attempt to index local 'luxRecord' (a nil value)

Re: DsVents global var lastupdate

Posted: Saturday 10 March 2018 12:42
by waaren
Deluka wrote: Saturday 10 March 2018 7:45 Thx for your help but getting a error
attempt to index local 'luxRecord' (a nil value)
Probably because it is not defined yet at the first time the script is executed.

You could try to change the luxReord line into

Code: Select all

local luxRecord.data = luxState.getLatest().data or 'fail' 
If it still gives you an error please come back with the relevant lines in your logging.

Re: DsVents global var lastupdate

Posted: Saturday 10 March 2018 19:30
by Deluka
still getting a error

Code: Select all

2018-03-10 18:41:28.354 dzVents: Error (2.4.1): error loading module 'lux' from file '/home/pi/domoticz/scripts/dzVents/scripts/lux.lua':
/home/pi/domoticz/scripts/dzVents/scripts/lux.lua:16: unexpected symbol near '.'
When I alter this line in

Code: Select all

local luxRecord.date = luxState.getLatest() or 'fail'
into

Code: Select all

local luxRecord = luxState.getLatest() or 'fail'
The scripts works perfect
Thanks for your help with my first baby steps in DZvents :D

Re: DsVents global var lastupdate

Posted: Saturday 10 March 2018 19:41
by waaren
:!: Happy to see that you managed to get it working :!: