Introducing dzVents - Domoticz Lua file based event scripting made e-z!

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

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Post by dannybloe » Wednesday 12 October 2016 15:14

Actually, you should try to prevent at all times to manipulate globals in your library functions. Always a huge risk. Just pass whatever you need as parameters to you function. A function is ideally unaware and independent of its environment.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

User avatar
BakSeeDaa
Posts: 336
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi
Domoticz version: beta
Contact:

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Post by BakSeeDaa » Thursday 13 October 2016 9:33

dannybloe wrote:Actually, you should try to prevent at all times to manipulate globals in your library functions. Always a huge risk. Just pass whatever you need as parameters to you function. A function is ideally unaware and independent of its environment.
Thanks @dannybloe, You are correct, no question about that.

I've spent a few hours trying to figure out how to best use dzVents together with my own custom functions.

My custom functions are quite few but they are crucial for my system and heavily used in my scripts. My functions need a way to access the Domoticz commandArray object, read device status and user defined variables. I could of course pass the needed parameters (domoticz.commandArray, domoticz.variables and/or domoticz.devices etc) to my custom functions as You suggested but it kind of bloats my scripts.

Currently I've solved the problem by inserting my own functions into Domoticz.lua and Utils.lua. I'm aware that my changes will get lost and maybe also incompatible whenever i upgrade dzVents. (They are kept inserted at a single position in the file though so I don't think it would cause me so much work to add them back when needed) If someone has a better idea please let me know.
Spoiler: show

Code: Select all

	-- Below	- My own custom functions

	function self.homeAwake()
		return (self.devices['Z1 Alarm'].text == self.SECURITY_DISARMED and true or false)
	end

	function self.AutoRemote(arMessage, deliverTo)
		local url
		if ((deliverTo == "") or (deliverTo == nil) or (deliverTo == "Narva2")) then deliverTo = "Default" end

		if (self.devices['Internet'].state == 'On') then
			-- We have Internet access, use GCM Server for delivery
			url = "https://autoremotejoaomgcd.appspot.com/sendmessage?key="
			url = url .. (deliverTo == "MastersCellPhone" and self.variables['ARKey1'].value or (deliverTo == "SonsCellPhone" and self.variables['ARKey2'].value or (deliverTo == "Uth" and self.variables['ARKey4'].value or self.variables['ARKey3'].value))) .. "&message=" .. utils.urlencode(arMessage) .. "&password=" .. self.variables['ARPassw'].value .. "&sender=Domoticz"
		elseif (deliverTo == "Default") then -- No Internet but Use LAN access to device for inhouse Speaker
			url = "http://vera-voice.private.narva2:1817/?message=" .. utils.urlencode(arMessage) .. "&password=" .. self.variables['ARPassw'].value .. "&sender=Domoticz"
		else
			print('No Internet access. Communicating with external host isn\'t possible.')
			return
		end
		self.openURL(url)
	end

	function self.speak(speakMessage, deliverTo, canSpeakAtNight)
		local NOTHINGTOSAY = 'Nothing to say'
		speakMessage = speakMessage or NOTHINGTOSAY
		canSpeakAtNight = canSpeakAtNight or 0
		if ((not self.homeAwake()) and canSpeakAtNight == 0)
		or (self.devices['Mobiltelefon'].state  == 'Off' and deliverTo == 'SonsCellPhone' and canSpeakAtNight == 0) then
			return -- Not a good time to speak
		end
		if (speakMessage ~= '') then
			self.AutoRemote('SpeakIt=:=' .. speakMessage, deliverTo)
		end
	end

	-- SMS messages are sent by making HTTP calls through the Clickatell API
	function self.sendSMS(SMSText, PhoneNumber)
		if ((PhoneNumber == '') or (PhoneNumber == nil) or (PhoneNumber == 'SMS-Away')) then PhoneNumber = self.variables['SMS-Away'].value end
		self.openURL('https://api.clickatell.com/http/sendmsg?user='..self.variables['ClickatellAPIUser'].value.."&password="..self.variables['ClickatellAPIPassw'].value.."&api_id="..self.variables['ClickatellAPIId'].value.."&from=+"..self.variables['ClickatellSender'].value.."&to="..PhoneNumber.."&text=".. utils.smsEncode(SMSText))
		print('Sending out a SMS to ' .. PhoneNumber)
	end

	-- End		- My own custom functions
Best wishes

// บักสีดา

User avatar
remb0
Posts: 792
Joined: Thursday 11 July 2013 22:21
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Post by remb0 » Thursday 13 October 2016 20:49

I have also some functions that you maybe can include in dzentz:
round numbers, log to csv, bestaat_variable,

can you tell when the framework will be merged? and if it is possible to use the online editor/ (makes wrtinging / testing and debugging a lot easier :P)

Glatzi
Posts: 15
Joined: Monday 19 September 2016 8:12
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Post by Glatzi » Friday 14 October 2016 1:30

dannybloe wrote:
Glatzi wrote:dzvEnts is an awesome framework :-) Thx a lot @dannybloe !!!!

Is it possible to use a function in the timer section too?

I've got the problem that I use several scripts for my blinds which should only be executed when it is sunrise or sunset. I'm checking the domoticz.time.isDayTime or domoticz.time.isNightTime attribute

Code: Select all

return {
        active = true,
        on = {
                ['timer'] = 'at sunset',
                ['timer'] = 'at sunrise'

...

if ( domoticz.time.isNightTime and myLevelBlinds > 20) then
                                domoticz.devices['Blinds'].dimTo(10)
But I've got the problem that not all of them get executed everytime. Sometimes my blinds will open or close, sometimes not because the event is missed by Domoticz. So I want to run the script again e.g. 3 minutes after sunrise/sunset. I've written a lua script which is able to calculate that time but how can I include this with dzevents timer?

I've already tried to put the value in a globalData Variable & use this with the timer event, but no luck. Alternatively something like '3 minutes after sunset' would be great :-)

Any help is appreciated...
So why don't you just set a timer for let's say every couple of minutes and in the script check if it is sunrise or sunset and check the state of your blinds and act accordingly? That shouldn't be too hard I think.
I've already done so & this is working. But then it is not possible anymore to control the blinds during the day though the physical buttons because after a minute the script will move them to the position which is defined in the script. Then there exists a huge problem with the WAF factor if you know what I'm talking about :D

Or do you know a way to identify if a event was triggered through script or through a physical button?!
RPi3+UZB1,FIBARO FGMS001(9x),FIBARO FGD212,FIBARO FGRM222(6x),FIBARO FGSD002(1x),FIBARO FGWPE (3x),Neo CoolCam Power plug(5x),NodOn CRC-3-1 Remote(2x),Qubino ZMNHADx(1x),Qubino ZMNHDD1(2x),Z-Wave Weather Sensor(1x),Z-Wave ZME_WALLC-S,FIBARO System FGK10x

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

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Post by dannybloe » Friday 14 October 2016 8:06

Glatzi wrote:
dannybloe wrote:
Glatzi wrote:dzvEnts is an awesome framework :-) Thx a lot @dannybloe !!!!

Is it possible to use a function in the timer section too?

I've got the problem that I use several scripts for my blinds which should only be executed when it is sunrise or sunset. I'm checking the domoticz.time.isDayTime or domoticz.time.isNightTime attribute

Code: Select all

return {
        active = true,
        on = {
                ['timer'] = 'at sunset',
                ['timer'] = 'at sunrise'

...

if ( domoticz.time.isNightTime and myLevelBlinds > 20) then
                                domoticz.devices['Blinds'].dimTo(10)
But I've got the problem that not all of them get executed everytime. Sometimes my blinds will open or close, sometimes not because the event is missed by Domoticz. So I want to run the script again e.g. 3 minutes after sunrise/sunset. I've written a lua script which is able to calculate that time but how can I include this with dzevents timer?

I've already tried to put the value in a globalData Variable & use this with the timer event, but no luck. Alternatively something like '3 minutes after sunset' would be great :-)

Any help is appreciated...
So why don't you just set a timer for let's say every couple of minutes and in the script check if it is sunrise or sunset and check the state of your blinds and act accordingly? That shouldn't be too hard I think.
I've already done so & this is working. But then it is not possible anymore to control the blinds during the day though the physical buttons because after a minute the script will move them to the position which is defined in the script. Then there exists a huge problem with the WAF factor if you know what I'm talking about :D

Or do you know a way to identify if a event was triggered through script or through a physical button?!
Well, what you could do is create your own flag that indicates if it is night time. You basically set a time every minute and detect if you switch from day to night and vice versa. Only at that moment you update the flag and do the script-based blind controlling :

Code: Select all

return {
	active = false, -- set to true to activate this script
	on = {
		['timer'] = 'every minute'
	},
	data = { 
		isNightTime = { initial = nil }	
	},
	execute = function(domoticz)
		
		if (domoticz.time.isNightTime and domoticz.data.isNightTime == false) then
			-- we switch from dayTime to nightTime, only at this moment we do the blinds
			domoticz.devices['Blinds'].dimTo(10)
			domoticz.data.isNightTime = true 
		end
		
		if (domoticz.time.isDayTime and domoticz.data.isNightTime == true) then
			-- we switch from nightTime to dayTime
			domoticz.devices['Blinds'].dimTo(20) -- something else than for the night
			domoticz.data.isNightTime = false
		end
	end
}

Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

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

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Post by dannybloe » Friday 14 October 2016 8:09

remb0 wrote:I have also some functions that you maybe can include in dzentz:
round numbers, log to csv, bestaat_variable,

can you tell when the framework will be merged? and if it is possible to use the online editor/ (makes wrtinging / testing and debugging a lot easier :P)
:cry: no.. I'm still waiting for Gizmocus to do stuff. But he is very hard to get in touch with. Or someone else with enough Domoticz C++ knowledge could stand up and do some integration stuff (don't think it is hard if you know C++, it's just more of what is already there).

/me looks around to see who raises a hand...
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

Glatzi
Posts: 15
Joined: Monday 19 September 2016 8:12
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Post by Glatzi » Friday 14 October 2016 12:53

dannybloe wrote:
Well, what you could do is create your own flag that indicates if it is night time. You basically set a time every minute and detect if you switch from day to night and vice versa. Only at that moment you update the flag and do the script-based blind controlling :

Code: Select all

return {
	active = false, -- set to true to activate this script
	on = {
		['timer'] = 'every minute'
	},
	data = { 
		isNightTime = { initial = nil }	
	},
	execute = function(domoticz)
		
		if (domoticz.time.isNightTime and domoticz.data.isNightTime == false) then
			-- we switch from dayTime to nightTime, only at this moment we do the blinds
			domoticz.devices['Blinds'].dimTo(10)
			domoticz.data.isNightTime = true 
		end
		
		if (domoticz.time.isDayTime and domoticz.data.isNightTime == true) then
			-- we switch from nightTime to dayTime
			domoticz.devices['Blinds'].dimTo(20) -- something else than for the night
			domoticz.data.isNightTime = false
		end
	end
}

Thank you for this input, that idea sounds really interesting. But in the above example the "isNightTime" is initially not set so none of the IF statements will become true?! Am I missing something? Perhaps I should enjoy my vacation on the seaside and just drink another beer :-)
RPi3+UZB1,FIBARO FGMS001(9x),FIBARO FGD212,FIBARO FGRM222(6x),FIBARO FGSD002(1x),FIBARO FGWPE (3x),Neo CoolCam Power plug(5x),NodOn CRC-3-1 Remote(2x),Qubino ZMNHADx(1x),Qubino ZMNHDD1(2x),Z-Wave Weather Sensor(1x),Z-Wave ZME_WALLC-S,FIBARO System FGK10x

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

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Post by dannybloe » Friday 14 October 2016 13:59

Yea you can set it to true if you start the stuff in th evening for the first time. That's an easy one ;)
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

User avatar
remb0
Posts: 792
Joined: Thursday 11 July 2013 22:21
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Post by remb0 » Monday 17 October 2016 9:22

I get:
2016-10-16 20:32:59.685 LUA: Cannot find the device. Skipping: Beneden 5
2016-10-16 20:33:00.487 LUA: Cannot find the device. Skipping: boven 4
2016-10-16 20:33:00.487 LUA: Cannot find the device. Skipping: Beneden 5
2016-10-16 20:33:00.487 LUA: Cannot find the device. Skipping: Alert 9
2016-10-16 20:33:00.487 LUA: Cannot find the device. Skipping: Rolluiken Nacht 10
2016-10-16 20:33:00.487 LUA: Cannot find the device. Skipping: Tuingroep 7
2016-10-16 20:33:00.487 LUA: Cannot find the device. Skipping: filmpje 8

since last domoticz update. I think that it's because of https://github.com/domoticz/domoticz/pull/919
because of a new lua table for scenes and groups.

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

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Post by dannybloe » Monday 17 October 2016 10:16

remb0 wrote:I get:
2016-10-16 20:32:59.685 LUA: Cannot find the device. Skipping: Beneden 5
2016-10-16 20:33:00.487 LUA: Cannot find the device. Skipping: boven 4
2016-10-16 20:33:00.487 LUA: Cannot find the device. Skipping: Beneden 5
2016-10-16 20:33:00.487 LUA: Cannot find the device. Skipping: Alert 9
2016-10-16 20:33:00.487 LUA: Cannot find the device. Skipping: Rolluiken Nacht 10
2016-10-16 20:33:00.487 LUA: Cannot find the device. Skipping: Tuingroep 7
2016-10-16 20:33:00.487 LUA: Cannot find the device. Skipping: filmpje 8

since last domoticz update. I think that it's because of https://github.com/domoticz/domoticz/pull/919
because of a new lua table for scenes and groups.
Strange. I'll have to dive into this (or someone else of course ;-) )
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

Doler
Posts: 115
Joined: Friday 31 July 2015 21:02
Target OS: Raspberry Pi
Domoticz version: v3.8153
Location: Sint-Oedenrode, Netherlands
Contact:

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Post by Doler » Monday 17 October 2016 12:03

dannybloe wrote:
remb0 wrote:I get:
2016-10-16 20:32:59.685 LUA: Cannot find the device. Skipping: Beneden 5
2016-10-16 20:33:00.487 LUA: Cannot find the device. Skipping: boven 4
2016-10-16 20:33:00.487 LUA: Cannot find the device. Skipping: Beneden 5
2016-10-16 20:33:00.487 LUA: Cannot find the device. Skipping: Alert 9
2016-10-16 20:33:00.487 LUA: Cannot find the device. Skipping: Rolluiken Nacht 10
2016-10-16 20:33:00.487 LUA: Cannot find the device. Skipping: Tuingroep 7
2016-10-16 20:33:00.487 LUA: Cannot find the device. Skipping: filmpje 8

since last domoticz update. I think that it's because of https://github.com/domoticz/domoticz/pull/919
because of a new lua table for scenes and groups.
Strange. I'll have to dive into this (or someone else of course ;-) )
Got the same problem and also thought it had to do with the new table for scenes and groups idx. Looked around in the dzVents code and changed something that at least made the messages disappear: in Domoticz.lua, line 315 I added 'and tableName ~= 'otherdevices_scenesgroups_idx' to the test. Don't know if this is the real solution but as said, the messages are gone. Maybe @dannybloe can give the answer whether it is the right solution.
Gr. Mark
Mark: Domoticz Beta on Raspberry Pi 3 running Debian Jessie - RaZberry - RFXCom - MySensors - SolarEdge - Dahua - dzVents

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

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Post by dannybloe » Monday 17 October 2016 21:35

I created a new release (v1.1.2):
  • More robust way of updating devices.lua
  • Added device level information for non-dimmer-like devices
  • Support for Nefit Easy thermostat (SetPoint)
  • Fixed problem with the new device table otherdevices_scenesgroups_idx.
Enjoy (again) :)
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

Wroopy
Posts: 10
Joined: Tuesday 18 October 2016 22:06
Target OS: Linux
Domoticz version: v3.5745
Contact:

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Post by Wroopy » Tuesday 18 October 2016 22:24

I have a light sensor (1Wire) that the readings flickers for. The historical values API in dzVents seamed to be a good choice to handle the flickering.

But now I have a strange problem, the number of items in the history never increase. To limit the reasons to fault I made the testscript below.
What am I missing/doing wrong?

Code: Select all

return {
        active = true,
        on = {
                'Lightsensor'
        },
        data = {
                history = {history = true, maxItems=10},
        },
        execute = function(domoticz, sensor)
                local reading = tonumber(sensor.rawData[1])
                domoticz.data.history.add(reading)
                local avg = domoticz.data.history.avg()
                local size = domoticz.data.history.size
                print (sensor.name .. ": last=" .. reading .. ", avg=" .. avg .. ", size= " .. size)
        end
}
Versions used:
Domoticz: v3.5745
dzVents: v1.1.2

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

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Post by dannybloe » Wednesday 19 October 2016 8:13

Wroopy wrote:I have a light sensor (1Wire) that the readings flickers for. The historical values API in dzVents seamed to be a good choice to handle the flickering.

But now I have a strange problem, the number of items in the history never increase. To limit the reasons to fault I made the testscript below.
What am I missing/doing wrong?

Code: Select all

return {
        active = true,
        on = {
                'Lightsensor'
        },
        data = {
                history = {history = true, maxItems=10},
        },
        execute = function(domoticz, sensor)
                local reading = tonumber(sensor.rawData[1])
                domoticz.data.history.add(reading)
                local avg = domoticz.data.history.avg()
                local size = domoticz.data.history.size
                print (sensor.name .. ": last=" .. reading .. ", avg=" .. avg .. ", size= " .. size)
        end
}
Versions used:
Domoticz: v3.5745
dzVents: v1.1.2
What exactly do you mean by 'never increase'? It should hold 10 readings at most. You can check this by opening the side-car file for this script (in scripts/storage). Sometimes it helps to remove that file completely when it behaves weird. This file holds the persistent data for the variables defined in the data section.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

Wroopy
Posts: 10
Joined: Tuesday 18 October 2016 22:06
Target OS: Linux
Domoticz version: v3.5745
Contact:

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Post by Wroopy » Wednesday 19 October 2016 9:26

dannybloe wrote: What exactly do you mean by 'never increase'? It should hold 10 readings at most. You can check this by opening the side-car file for this script (in scripts/storage). Sometimes it helps to remove that file completely when it behaves weird. This file holds the persistent data for the variables defined in the data section.
Number of items in the history table is always one.
I've tested to remove the file in storage and restart domoticz. But no change.
Between the updates I did no change to files, restart.

Storage file after first sensor update

Code: Select all

-- Persistent Data
local multiRefObjects = {

} -- multiRefObjects
local obj1 = {
        ["history"] = {
                [1] = {
                        ["time"] = "2016-10-19 06:27:59";
                        ["data"] = 7;
                };
        };
}
return obj1
Storage file after second sensor update

Code: Select all

-- Persistent Data
local multiRefObjects = {

} -- multiRefObjects
local obj1 = {
        ["history"] = {
                [1] = {
                        ["time"] = "2016-10-19 06:28:52";
                        ["data"] = 7.1;
                };
        };
}
return obj1
As I understand it there should be two record in the storage file after the second update?

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

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Post by dannybloe » Wednesday 19 October 2016 10:08

Wroopy wrote:
dannybloe wrote: What exactly do you mean by 'never increase'? It should hold 10 readings at most. You can check this by opening the side-car file for this script (in scripts/storage). Sometimes it helps to remove that file completely when it behaves weird. This file holds the persistent data for the variables defined in the data section.
Number of items in the history table is always one.
I've tested to remove the file in storage and restart domoticz. But no change.
Between the updates I did no change to files, restart.

Storage file after first sensor update

Code: Select all

-- Persistent Data
local multiRefObjects = {

} -- multiRefObjects
local obj1 = {
        ["history"] = {
                [1] = {
                        ["time"] = "2016-10-19 06:27:59";
                        ["data"] = 7;
                };
        };
}
return obj1
Storage file after second sensor update

Code: Select all

-- Persistent Data
local multiRefObjects = {

} -- multiRefObjects
local obj1 = {
        ["history"] = {
                [1] = {
                        ["time"] = "2016-10-19 06:28:52";
                        ["data"] = 7.1;
                };
        };
}
return obj1
As I understand it there should be two record in the storage file after the second update?
Could you try to throw away that data file and try again. And, if that doesn't work, rename the variable history to something else.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

Wroopy
Posts: 10
Joined: Tuesday 18 October 2016 22:06
Target OS: Linux
Domoticz version: v3.5745
Contact:

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Post by Wroopy » Wednesday 19 October 2016 11:32

dannybloe wrote: Could you try to throw away that data file and try again. And, if that doesn't work, rename the variable history to something else.
I've removed the file and checked after ½ hour, according to the log I got several updates, but still one item in the list.
Tested to rename the varaible to "light" but still the same.

When/how is the storage file read?
When/how is the content cleared?

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

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Post by dannybloe » Wednesday 19 October 2016 11:37

Wroopy wrote:
dannybloe wrote: Could you try to throw away that data file and try again. And, if that doesn't work, rename the variable history to something else.
I've removed the file and checked after ½ hour, according to the log I got several updates, but still one item in the list.
Tested to rename the varaible to "light" but still the same.

When/how is the storage file read?
When/how is the content cleared?
Every time the script is executed it reads the persistent data and stores it when the script is finished. So each time the switch changes.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

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

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Post by dannybloe » Wednesday 19 October 2016 11:48

dannybloe wrote:
Wroopy wrote:
dannybloe wrote: Could you try to throw away that data file and try again. And, if that doesn't work, rename the variable history to something else.
I've removed the file and checked after ½ hour, according to the log I got several updates, but still one item in the list.
Tested to rename the varaible to "light" but still the same.

When/how is the storage file read?
When/how is the content cleared?
Every time the script is executed it reads the persistent data and stores it when the script is finished. So each time the switch changes.
I just tested it here and it works with this script:

Code: Select all

return {
	active = true,
	on = {
		'mySensor'
	},
	data = {
		history = {history = true, maxItems = 10}
	},
	execute = function(domoticz, sensor)
		local reading = sensor.temperature
		domoticz.data.history.add(reading)
		local avg = domoticz.data.history.avg()
		local size = domoticz.data.history.size
		print(sensor.name .. ": last=" .. reading .. ", avg=" .. avg .. ", size= " .. size)
	end
}
and the data file is like this:

Code: Select all

-- Persistent Data
local multiRefObjects = {

} -- multiRefObjects
local obj1 = {
	["history"] = {
		[1] = {
			["time"] = "2016-10-19 09:45:59";
			["data"] = 16.799999237061;
		};
		[2] = {
			["time"] = "2016-10-19 09:45:58";
			["data"] = 16.799999237061;
		};
		[3] = {
			["time"] = "2016-10-19 09:45:30";
			["data"] = 16.799999237061;
		};
		[4] = {
			["time"] = "2016-10-19 09:45:29";
			["data"] = 16.799999237061;
		};
		[5] = {
			["time"] = "2016-10-19 09:44:59";
			["data"] = 16.799999237061;
		};
		[6] = {
			["time"] = "2016-10-19 09:44:58";
			["data"] = 16.799999237061;
		};
		[7] = {
			["time"] = "2016-10-19 09:44:29";
			["data"] = 16.799999237061;
		};
		[8] = {
			["time"] = "2016-10-19 09:44:28";
			["data"] = 16.799999237061;
		};
		[9] = {
			["time"] = "2016-10-19 09:43:59";
			["data"] = 16.799999237061;
		};
		[10] = {
			["time"] = "2016-10-19 09:43:58";
			["data"] = 16.89999961853;
		};
	};
}
return obj1

Are you sure that the script is called multiple times?
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

Wroopy
Posts: 10
Joined: Tuesday 18 October 2016 22:06
Target OS: Linux
Domoticz version: v3.5745
Contact:

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!

Post by Wroopy » Wednesday 19 October 2016 14:47

dannybloe wrote:
dannybloe wrote:
Wroopy wrote:
I've removed the file and checked after ½ hour, according to the log I got several updates, but still one item in the list.
Tested to rename the varaible to "light" but still the same.

When/how is the storage file read?
When/how is the content cleared?
Every time the script is executed it reads the persistent data and stores it when the script is finished. So each time the switch changes.
I just tested it here and it works with this script:

Code: Select all

return {
	active = true,
	on = {
		'mySensor'
	},
	data = {
		history = {history = true, maxItems = 10}
	},
	execute = function(domoticz, sensor)
		local reading = sensor.temperature
		domoticz.data.history.add(reading)
		local avg = domoticz.data.history.avg()
		local size = domoticz.data.history.size
		print(sensor.name .. ": last=" .. reading .. ", avg=" .. avg .. ", size= " .. size)
	end
}
and the data file is like this:

Code: Select all

-- Persistent Data
local multiRefObjects = {

} -- multiRefObjects
local obj1 = {
	["history"] = {
		[1] = {
			["time"] = "2016-10-19 09:45:59";
			["data"] = 16.799999237061;
		};
		[2] = {
			["time"] = "2016-10-19 09:45:58";
			["data"] = 16.799999237061;
		};
		[3] = {
			["time"] = "2016-10-19 09:45:30";
			["data"] = 16.799999237061;
		};
		[4] = {
			["time"] = "2016-10-19 09:45:29";
			["data"] = 16.799999237061;
		};
		[5] = {
			["time"] = "2016-10-19 09:44:59";
			["data"] = 16.799999237061;
		};
		[6] = {
			["time"] = "2016-10-19 09:44:58";
			["data"] = 16.799999237061;
		};
		[7] = {
			["time"] = "2016-10-19 09:44:29";
			["data"] = 16.799999237061;
		};
		[8] = {
			["time"] = "2016-10-19 09:44:28";
			["data"] = 16.799999237061;
		};
		[9] = {
			["time"] = "2016-10-19 09:43:59";
			["data"] = 16.799999237061;
		};
		[10] = {
			["time"] = "2016-10-19 09:43:58";
			["data"] = 16.89999961853;
		};
	};
}
return obj1

Are you sure that the script is called multiple times?
Yes I'm sure it is calles several times. I can see it both in Domoticz log and the timestamp of the item is changed in the storage-file.

I've tested to increase the logging, i.e. change ['Log level'] = 3 in dzVents_settings.lua too see if I would get som extra useful information. Got some very confusing result

1. The size of the history variable where increasing. So finally I got 10 items as expected.
2. Changed back to ['Log level'] = 2 and now I get only one hisory item.
3. Switch to level 3 again and it worked as expected.
4. Changed back to 2 and got only one history item.

Does that give any hint on what the problem could be?

Locked

Who is online

Users browsing this forum: No registered users and 4 guests