Issue with persistent data

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

Moderator: leecollings

Post Reply
Ric68
Posts: 15
Joined: Wednesday 28 January 2015 17:47
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Issue with persistent data

Post by Ric68 »

Hi,

I have been struggling to get persistent data to work without success.

I am running RPI3, Raspian Stretch Lite 2017-11-29 updated and upgraded recently with domoticz V3.8799

What I am trying to accomplish is to increase a lamp to 100% if there is a motion detected and once the motion is off again, revert to the level the lamp had before, in this case 33%

The script is really simple, but I get 'nil' when I am trying to run it.
Any ideas?

Code: Select all

return {
	active = true,
	on = {
		devices = { 'Trapphus Innerdörr' },
		timer = {},
		variables = {}
	},
	data = { OldLevel = {initial=0} },
	logger = {},
	
	execute = function(domoticz, device, info)
		-- code
	    if (device.state == 'Open') then
	        OldLevel = domoticz.devices('Kök Fönsterlampa').level
	        domoticz.log('Level at ON = ' .. tostring(OldLevel), domoticz.LOG_INFO)
	        domoticz.devices('Kök Fönsterlampa').dimTo(100)
        else
	        domoticz.devices('Kök Fönsterlampa').dimTo(OldLevel)
	        domoticz.log('Level at OFF = ' .. tostring(OldLevel), domoticz.LOG_INFO)
        end
		
	end
}

Code: Select all

2018-01-09 01:44:20.487 dzVents: Info: Handling events for: "Trapphus Innerdörr", value: "Open"
2018-01-09 01:44:20.487 dzVents: Info: ------ Start internal script: Test: Device: "Trapphus Innerdörr (Zwave)", Index: 318
2018-01-09 01:44:20.488 dzVents: Info: Level at ON = 33
2018-01-09 01:44:20.489 dzVents: Info: ------ Finished Test
2018-01-09 01:44:20.490 EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2018-01-09 01:44:25.132 dzVents: Info: Handling events for: "Trapphus Innerdörr", value: "Closed"
2018-01-09 01:44:25.133 dzVents: Info: ------ Start internal script: Test: Device: "Trapphus Innerdörr (Zwave)", Index: 318
2018-01-09 01:44:25.134 dzVents: Info: Level at OFF = nil
2018-01-09 01:44:25.134 dzVents: Info: ------ Finished Test
2018-01-09 01:44:25.135 EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Issue with persistent data

Post by waaren »

Not sure if this is the best solution but you can try changing "OldLevel" to "domoticz.data.OldLevel" within the if/else construct
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: Issue with persistent data

Post by emme »

waaren wrote: Tuesday 09 January 2018 9:46 Not sure if this is the best solution but you can try changing "OldLevel" to "domoticz.data.OldLevel" within the if/else construct
you HAVE to ;)
The most dangerous phrase in any language is:
"We always done this way"
Ric68
Posts: 15
Joined: Wednesday 28 January 2015 17:47
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Issue with persistent data

Post by Ric68 »

Thank you!

I obviously had missed that detail.

But I still do not get it to work. The persistent data is still nil when I enter the else case.

Code: Select all

return {
	active = true,
	on = {
		devices = { 'Trapphus Innerdörr' },
		timer = {},
		variables = { OldLevel = {initial=0} }
	},
	data = {},
	logger = {},
	
	execute = function(domoticz, device, info)
		-- code
	    if (device.state == 'Open') then
            local Level = domoticz.devices('Kök Fönsterlampa').level
	        domoticz.data.OldLevel = Level
	        domoticz.devices('Kök Fönsterlampa').dimTo(100)
	        domoticz.log('Level at ON = ' .. tostring(Level), domoticz.LOG_INFO)
	        domoticz.log('OldLevel persistent at ON = ' .. tostring(domoticz.data.OldLevel), domoticz.LOG_INFO)
        else
            local Level = domoticz.data.OldLevel
	        domoticz.devices('Kök Fönsterlampa').dimTo(Level)
	        domoticz.log('Level at OFF = ' .. tostring(Level), domoticz.LOG_INFO)
	        domoticz.log('OldLevel persistent at OFF = ' .. tostring(domoticz.data.OldLevel), domoticz.LOG_INFO)
        end
	end
}
The respons is

Code: Select all

2018-01-09 12:04:47.018 dzVents: Info: Handling events for: "Trapphus Innerdörr", value: "Open"
2018-01-09 12:04:47.018 dzVents: Info: ------ Start internal script: Test: Device: "Trapphus Innerdörr (Zwave)", Index: 318
2018-01-09 12:04:47.019 dzVents: Info: Level at ON = 20
2018-01-09 12:04:47.019 dzVents: Info: OldLevel persistent at ON = 20
2018-01-09 12:04:47.020 dzVents: Info: ------ Finished Test
2018-01-09 12:04:47.020 EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2018-01-09 12:05:12.372 dzVents: Info: Handling events for: "Trapphus Innerdörr", value: "Closed"
2018-01-09 12:05:12.372 dzVents: Info: ------ Start internal script: Test: Device: "Trapphus Innerdörr (Zwave)", Index: 318
2018-01-09 12:05:12.374 dzVents: Info: Level at OFF = nil
2018-01-09 12:05:12.374 dzVents: Info: OldLevel persistent at OFF = nil
2018-01-09 12:05:12.374 dzVents: Info: ------ Finished Test
2018-01-09 12:05:12.375 EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Issue with persistent data

Post by dannybloe »

But now you have emptied the data section in your code.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Issue with persistent data

Post by waaren »

Try this one...

Code: Select all


return {
	active = true,
	on = {
		devices = { 'Trapphus Innerdörr' },
	     },

	data = { OldLevel = {initial=0} },

	 
	execute = function(domoticz, TriggerDevice, info)
		
	
		DimmableDevice = domoticz.devices('Kök Fönsterlampa')
		
	  if (TriggerDevice.state == 'Open') then
	        domoticz.data.OldLevel = DimmableDevice.level
	        domoticz.log('Level at ON = ' .. tostring(domoticz.data.OldLevel), domoticz.LOG_INFO)
	        DimmableDevice.dimTo(100)
        else
	        DimmableDevice.dimTo(domoticz.data.OldLevel)
	        domoticz.log('Level at OFF = ' .. tostring(domoticz.data.OldLevel), domoticz.LOG_INFO)
        end
		
		
	end
}

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Ric68
Posts: 15
Joined: Wednesday 28 January 2015 17:47
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Issue with persistent data

Post by Ric68 »

Thank you again, sorry for the confusion with the missing data-segment, but I tried basically everything without getting it to work, so ignore the last post.

So now it work, but I found another problem. Don't know if it is due to Domoticz or dzVents, but there seems to be some kind of rounding somewhere as the level decreases by 6-7 % until it gets fixed at an even decade.
The lamp is a IKEA Trådfri E27 WS opal 980lm with SW 1.2.217 running via Philips Hue Bridge

This is ok if I am at 86% and land at 80%, but I had a case where I started at 13% and the next step was then 0%.

Starting at 86%

Code: Select all

2018-01-09 15:06:14.100 EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2018-01-09 15:06:33.470 dzVents: Info: Handling events for: "Sovrum Rörelse", value: "On"
2018-01-09 15:06:33.470 dzVents: Info: ------ Start internal script: Altan: Device: "Sovrum Rörelse (RfxTrx)", Index: 290
2018-01-09 15:06:33.474 dzVents: Info: Level On = 100
2018-01-09 15:06:33.475 dzVents: Info: ------ Finished Altan
2018-01-09 15:06:33.477 EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2018-01-09 15:06:41.389 dzVents: Info: Handling events for: "Sovrum Rörelse", value: "Off"
2018-01-09 15:06:41.389 dzVents: Info: ------ Start internal script: Altan: Device: "Sovrum Rörelse (RfxTrx)", Index: 290
2018-01-09 15:06:41.391 dzVents: Info: Level Off = 100
2018-01-09 15:06:41.391 dzVents: Info: ------ Finished Altan
2018-01-09 15:06:41.392 EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2018-01-09 15:06:41.761 dzVents: Info: Handling events for: "Trapphus Innerdörr", value: "Open"
2018-01-09 15:06:41.761 dzVents: Info: ------ Start internal script: Test: Device: "Trapphus Innerdörr (Zwave)", Index: 318
2018-01-09 15:06:41.762 dzVents: Info: Level at ON = 86
2018-01-09 15:06:41.763 dzVents: Info: ------ Finished Test
2018-01-09 15:06:41.764 EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2018-01-09 15:06:44.587 dzVents: Info: Handling events for: "Trapphus Innerdörr", value: "Closed"
2018-01-09 15:06:44.587 dzVents: Info: ------ Start internal script: Test: Device: "Trapphus Innerdörr (Zwave)", Index: 318
2018-01-09 15:06:44.588 dzVents: Info: Level at OFF = 86
2018-01-09 15:06:44.589 dzVents: Info: ------ Finished Test
2018-01-09 15:06:44.590 EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2018-01-09 15:06:46.312 dzVents: Info: Handling events for: "Trapphus Innerdörr", value: "Open"
2018-01-09 15:06:46.312 dzVents: Info: ------ Start internal script: Test: Device: "Trapphus Innerdörr (Zwave)", Index: 318
2018-01-09 15:06:46.314 dzVents: Info: Level at ON = 80
2018-01-09 15:06:46.315 dzVents: Info: ------ Finished Test
2018-01-09 15:06:46.316 EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2018-01-09 15:06:48.549 dzVents: Info: Handling events for: "Trapphus Innerdörr", value: "Closed"
2018-01-09 15:06:48.549 dzVents: Info: ------ Start internal script: Test: Device: "Trapphus Innerdörr (Zwave)", Index: 318
2018-01-09 15:06:48.550 dzVents: Info: Level at OFF = 80
2018-01-09 15:06:48.551 dzVents: Info: ------ Finished Test
Starting at 13%. The first time I close the door, the lamp is still lit, but the second time it is dark and in domoticz I can see that it is not active, i.e. not 6%

Code: Select all

2018-01-09 15:16:18.398 dzVents: Info: Handling events for: "Trapphus Innerdörr", value: "Open"
2018-01-09 15:16:18.398 dzVents: Info: ------ Start internal script: Test: Device: "Trapphus Innerdörr (Zwave)", Index: 318
2018-01-09 15:16:18.400 dzVents: Info: Level at ON = 13
2018-01-09 15:16:18.400 dzVents: Info: ------ Finished Test
2018-01-09 15:16:18.402 EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2018-01-09 15:16:21.464 dzVents: Info: Handling events for: "Trapphus Innerdörr", value: "Closed"
2018-01-09 15:16:21.464 dzVents: Info: ------ Start internal script: Test: Device: "Trapphus Innerdörr (Zwave)", Index: 318
2018-01-09 15:16:21.466 dzVents: Info: Level at OFF = 13
2018-01-09 15:16:21.467 dzVents: Info: ------ Finished Test
2018-01-09 15:16:21.469 EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2018-01-09 15:16:24.387 dzVents: Info: Handling events for: "Trapphus Innerdörr", value: "Open"
2018-01-09 15:16:24.387 dzVents: Info: ------ Start internal script: Test: Device: "Trapphus Innerdörr (Zwave)", Index: 318
2018-01-09 15:16:24.388 dzVents: Info: Level at ON = 6
2018-01-09 15:16:24.388 dzVents: Info: ------ Finished Test
2018-01-09 15:16:24.389 EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2018-01-09 15:16:27.312 dzVents: Info: Handling events for: "Trapphus Innerdörr", value: "Closed"
2018-01-09 15:16:27.312 dzVents: Info: ------ Start internal script: Test: Device: "Trapphus Innerdörr (Zwave)", Index: 318
2018-01-09 15:16:27.314 dzVents: Info: Level at OFF = 6
2018-01-09 15:16:27.317 dzVents: Info: ------ Finished Test
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Issue with persistent data

Post by waaren »

Probably not the optimal solution but sets the dim values back to multiples of 20 (20,40,60,80 or 100)

Code: Select all

return {
	active = true,
	on = {
		devices = { 'Trapphus Innerdörr' },
	     },

	data = { OldLevel = {initial=0} },

	 
	execute = function(domoticz, TriggerDevice, info)
		function SpecialRound(n)
            		return  math.min(math.max((20 * math.floor(n/20 + 0.5)),20),100) 
        	end
		
	
		DimmableDevice = domoticz.devices('Kök Fönsterlampa')
		
	  if (TriggerDevice.state == 'Open') then
	        domoticz.data.OldLevel = DimmableDevice.level
	        domoticz.log('Level at ON = ' .. tostring(domoticz.data.OldLevel), domoticz.LOG_INFO)
	        DimmableDevice.dimTo(100)
        else
        	RoundedValue = SpecialRound(domoticz.data.OldLevel)
	        DimmableDevice.dimTo(RoundedValue)
	        domoticz.log('Level at OFF = ' .. tostring(RoundedValue), domoticz.LOG_INFO)
        end
		
		
	end
}





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