DsVents global var lastupdate

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

Moderator: leecollings

Post Reply
Deluka
Posts: 19
Joined: Thursday 03 March 2016 18:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

DsVents global var lastupdate

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

Re: DsVents global var lastupdate

Post 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)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Deluka
Posts: 19
Joined: Thursday 03 March 2016 18:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: DsVents global var lastupdate

Post by Deluka »

Thx and what About script level vars
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: DsVents global var lastupdate

Post by waaren »

Please elaborate on what your exact requirement is. A sample script might help me understand what you try to achieve
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Deluka
Posts: 19
Joined: Thursday 03 March 2016 18:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: DsVents global var lastupdate

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

Re: DsVents global var lastupdate

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Deluka
Posts: 19
Joined: Thursday 03 March 2016 18:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: DsVents global var lastupdate

Post by Deluka »

Thx for your help but getting a error
attempt to index local 'luxRecord' (a nil value)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: DsVents global var lastupdate

Post 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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Deluka
Posts: 19
Joined: Thursday 03 March 2016 18:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: DsVents global var lastupdate

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

Re: DsVents global var lastupdate

Post by waaren »

:!: Happy to see that you managed to get it working :!:
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest