Page 9 of 17

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

Posted: Thursday 31 March 2016 13:13
by dannybloe
Very nice. In your last script, for better readability I'd do something like this:

Code: Select all

local fan = domoticz.devices['Fan']
local ventForce = domoticz.devices['Ventilation_Force']
local ventOff = domoticz.devices['Ventilation_Off']
local vFan = domoticz.devices['V_Fan']

if fan.state == 'Off' and ventForce.state=='On' and ventOff.state=='On' and vFan.state=='On' then   
	fan.switchOn()
....	
But that's just a matter of taste.

I'm currently working on some enhancements that makes the uservariables obsolete for doing statistics inside the script (like detecting humidity rises etc). You can access historical data for devices etc. inside your script. Quite awesome. More on that later... :D

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

Posted: Thursday 31 March 2016 17:40
by NietGiftig
dannybloe wrote: ....
But that's just a matter of taste.
....
I'm currently working on some enhancements that makes the uservariables obsolete for doing statistics inside the script (like detecting humidity rises etc). You can access historical data for devices etc. inside your script. Quite awesome. More on that later... :D
Good reading.
I like to stick with the Idx from the devices, the name changes more often then the ID, for me that is.

Nice to read the next development for the uservariables and historical data.
I hope it won't slowdown the execution speed and/or the "Domoticz Experience" . :D

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

Posted: Thursday 31 March 2016 20:45
by olegdt
olegdt wrote: All variables in script declared as local except variable current. Then current is declared as local it's value become 0 in following check:

Code: Select all

-- check if the sensor is on or has some weird reading
	if (current == 0 or current == nil) then
		domoticz.log('current is 0 or nil. Skipping this reading', domoticz.LOG_ERROR)
		return 
	end

Otherwise it's working as it should. Looks very strange for me.
i have found mistake in my humidity script i was worried about - variable can't be defined as local inside if-else statement. Correct part of the code:

Code: Select all

	-- get the current humidity value
	local current = 0
	if (TESTMODE) then
		current = domoticz.variables[TESTMODEHUMVAR].nValue
	else
		current = domoticz.devices[SENSORNAME].humidity
	end

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

Posted: Thursday 31 March 2016 21:05
by olegdt
dannybloe wrote:Very nice. In your last script, for better readability I'd do something like this:

Code: Select all

local fan = domoticz.devices['Fan']
local ventForce = domoticz.devices['Ventilation_Force']
local ventOff = domoticz.devices['Ventilation_Off']
local vFan = domoticz.devices['V_Fan']

if fan.state == 'Off' and ventForce.state=='On' and ventOff.state=='On' and vFan.state=='On' then   
	fan.switchOn()
....	
It simplify code a lot. Thanks!
Should be in Readme.md

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

Posted: Thursday 31 March 2016 22:33
by NietGiftig
In search for best practice (and learning) :

Where to declare local vars, in or outside the execute = function(.. ?

In case of one or more self-made (general) functions where to place them:
1. in the start of the script
2. in the execute = function( ..

Is it possible for general self-made functions to use a require and bundle them in an external script?
And where should the require statement be placed, in or outside the execute = function(.. ?

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

Posted: Friday 01 April 2016 13:30
by dannybloe
olegdt wrote:i have found mistake in my humidity script i was worried about - variable can't be defined as local inside if-else statement.
Well, you can but they are scoped to the if block. They don't exist outside the block (unlike in javascript where it is hoisted to the function level unless ur using es6 of course).

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

Posted: Friday 01 April 2016 13:35
by dannybloe
NietGiftig wrote:In search for best practice (and learning) :

Where to declare local vars, in or outside the execute = function(.. ?

In case of one or more self-made (general) functions where to place them:
1. in the start of the script
2. in the execute = function( ..

Is it possible for general self-made functions to use a require and bundle them in an external script?
And where should the require statement be placed, in or outside the execute = function(.. ?
I usually declare them in the scope where they are needed so usually inside the function itself. Unless there is a need to use the variable elsewhere too (outside the function).

Self made functions can easily be required. Just create a file (I wouldn't do it in the same folder as where you put the scripts coz dzVents will open it in search for execute/on/active keys. But you can put them in the Domoticz scripts/lua folder and then require them from your dzVents event scripts:

Code: Select all

	local myUtilities = require('myutilities') -- the parent folder of dzVents is already in the package path
	local sum = myUtilities.sum(1,2)
your utility file 'myutilities.lua' could be like this:

Code: Select all


local function mySum(a, b)
	return a+b
end

-- only export those function you want to be exported:
return {
	sum = mySum
}


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

Posted: Friday 01 April 2016 19:21
by thegritche
Hello
I'm playing with dzVents and I'm trying to put the http response in a local variable called saint
In Lua I use

Code: Select all

local cmd = curl .. ' http://domogeek.entropialux.com/feastedsaint/now'
local saint = os.capture(cmd)
In DzVents I try

Code: Select all

local saint = domoticz.openURL('http://domogeek.entropialux.com/feastedsaint/now')
without success

Is it possible ?
Thanks for your responses

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

Posted: Friday 01 April 2016 19:25
by dannybloe
thegritche wrote:

Code: Select all

local saint = domoticz.openURL('http://domogeek.entropialux.com/feastedsaint/now')
without success

Is it possible ?
Thanks for your responses
Well, the domoticz.openURL() command tells Domoticz to open that URL after your script has been completed. It creates an OpenURL command in the commandArray.So no processing is done here by dzVents. So curl up and do it yourself like you did before :lol:

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

Posted: Friday 01 April 2016 19:59
by thegritche
Ok, so I try

Code: Select all

return {
    active = true,
    on = {
        'Bourse'
    },
  execute = function(domoticz, switch)
        if (switch.state == 'On') then
		local cmd = curl .. ' http://domogeek.entropialux.com/feastedsaint/now'
local saint = os.capture(cmd);
	
	 domoticz.log (saint) .....
	 
and I got a
ndling events for: "Bourse", value: "On"
LUA: =====================================================
LUA: >>> Handler: text
LUA: >>> Device: "Bourse" Index: 211
LUA: .....................................................
LUA: An error occured when calling event handler text
LUA: /home/pi/domoticz/scripts/lua/scripts/text.lua:8: attempt to call global 'curl' (a nil value)
LUA: .....................................................
LUA: <<< Done
LUA: -----------------------------------------------------
line 8 is my curl function
I don't understand where is the problem with this line

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

Posted: Friday 01 April 2016 20:03
by dannybloe
Obviously curl isn't declared anywhere. I don't know how you did that before. Maybe you required it somehow. Dunno.

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

Posted: Friday 01 April 2016 22:39
by thegritche
Ok
I keep my old but nice lua script
Thanks

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

Posted: Saturday 02 April 2016 8:43
by dannybloe
thegritche wrote:Ok
I keep my old but nice lua script
Thanks
Sure, whatever you want but I don't see what your curl stuf has to do with dzVents. Two completely unrelated things IMHO.

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

Posted: Monday 04 April 2016 16:42
by Eduard
May i suggest to suppress logging if no action (logging or switching) has to be taken by dzVents to prevent useless logging...

Code: Select all

2016-04-04 16:37:00.509 LUA: =====================================================
2016-04-04 16:37:00.510 LUA: >>> Handler: CheckOpenDoors
2016-04-04 16:37:00.510 LUA: .....................................................
2016-04-04 16:37:00.510 LUA: .....................................................
2016-04-04 16:37:00.510 LUA: <<< Done
2016-04-04 16:37:00.510 LUA: -----------------------------------------------------
This is in my logfile every minute, and i have more script that trigger every minute. I would like to have this logging only when there is something to log or to switch.

Agree? ;)

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

Posted: Tuesday 05 April 2016 8:42
by dannybloe
I'll take a look at it.

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

Posted: Tuesday 05 April 2016 9:17
by Eduard
dannybloe wrote:I'll take a look at it.
Great!

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

Posted: Wednesday 06 April 2016 21:09
by commodore white
I am trying to write a script that relies on a device id. According to the documentation the method is ".id"

Here is a fragment of my code:

Code: Select all

 (53)              printf("Device is: %s (%s)", _device.name, _device.id)
 (54)              print("Alarm "..domoticz.devices['Alarm'].id)
When the script executes while dzVent logging level is set to 2, the log shows the following:
2016-04-06 18:07:54.252 LUA: Handling events for: "envKitchen_Temperature", value: "20.39999961853"
2016-04-06 18:07:54.252 LUA: =====================================================
2016-04-06 18:07:54.252 LUA: >>> Handler: heating
2016-04-06 18:07:54.253 LUA: >>> Device: "envKitchen" Index: nil
2016-04-06 18:07:54.253 LUA: .....................................................
2016-04-06 18:07:54.253 LUA: heating: Boiler is : On - Heating set to On
2016-04-06 18:07:54.253 LUA: heating: current: 20.39999961853
2016-04-06 18:07:54.254 LUA: heating: target: 20
2016-04-06 18:07:54.254 LUA: heating: Device is: envKitchen (nil)
2016-04-06 18:07:54.254 LUA: An error occured when calling event handler heating
2016-04-06 18:07:54.255 LUA: /home/pi/domoticz/scripts/lua/scripts/heating.lua:54: attempt to index global 'device' (a nil value)
2016-04-06 18:07:54.255 LUA: .....................................................
2016-04-06 18:07:54.255 LUA: <<< Done
2016-04-06 18:07:54.255 LUA: -----------------------------------------------------
The script bombs executing line 54.

This highlights two problems:

1) the debug log entry fails to decode the device id when scripts run when a device has interrupted. c.f
2016-04-06 18:07:54.252 LUA: >>> Handler: heating
2016-04-06 18:07:54.253 LUA: >>> Device: "envKitchen" Index: nil
and
2) the ".id" method doesn't seem to work and bombs with a strange device name referred to:
2016-04-06 18:07:54.258 LUA: An error occured when calling event handler heating
2016-04-06 18:07:54.258 LUA: /home/pi/domoticz/scripts/lua/scripts/heating.lua:54: attempt to index global 'device' (a nil value)
Is the issue in dzVents or with the bio-degradeable carbon-based life-form pressing the buttons?

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

Posted: Wednesday 06 April 2016 22:14
by commodore white
One of the triggers for my dzVents central heating script uses events driven from a room thermometer.

The script gets to run twice for each device interrupt: once with the name envKitchen, and again with the name envKitchen_Temperature. The upshot is that things that my script is supposed to do once per temperature measurement are happening twice. I can't check for this in the script because the two invocations are queued to run sequentially and the commandArray isn't processed until all the scripts have run to their conclusion.

Here's a snippet from the log:
2016-04-06 20:36:35.226 LUA: Handling events for: "envKitchen", value: ""
2016-04-06 20:36:35.226 LUA: =====================================================
2016-04-06 20:36:35.227 LUA: >>> Handler: heating
2016-04-06 20:36:35.227 LUA: >>> Device: "envKitchen" Index: nil
2016-04-06 20:36:35.227 LUA: .....................................................
2016-04-06 20:36:35.227 LUA: heating: Boiler is : Off - Heating set to On
2016-04-06 20:36:35.228 LUA: heating: current: 19.799999237061
2016-04-06 20:36:35.228 LUA: heating: target: 16
2016-04-06 20:36:35.228 LUA: heating: Too hot! Switching boiler Off
2016-04-06 20:36:35.228 LUA: heating: Thermometer updated 0m ago
2016-04-06 20:36:35.229 LUA: heating: Boiler last updated 35s ago
2016-04-06 20:36:35.229 LUA: .....................................................
2016-04-06 20:36:35.229 LUA: <<< Done
2016-04-06 20:36:35.230 LUA: -----------------------------------------------------
2016-04-06 20:36:35.230 LUA: Handling events for: "envKitchen_Temperature", value: "19.799999237061"
2016-04-06 20:36:35.231 LUA: =====================================================
2016-04-06 20:36:35.231 LUA: >>> Handler: heating
2016-04-06 20:36:35.231 LUA: >>> Device: "envKitchen" Index: nil
2016-04-06 20:36:35.232 LUA: .....................................................
2016-04-06 20:36:35.232 LUA: heating: Boiler is : Off - Heating set to On
2016-04-06 20:36:35.232 LUA: heating: current: 19.799999237061
2016-04-06 20:36:35.232 LUA: heating: target: 16
2016-04-06 20:36:35.232 LUA: heating: Too hot! Switching boiler Off
2016-04-06 20:36:35.233 LUA: heating: Thermometer updated 0m ago
2016-04-06 20:36:35.233 LUA: heating: Boiler last updated 35s ago
2016-04-06 20:36:35.233 LUA: .....................................................
2016-04-06 20:36:35.234 LUA: <<< Done
2016-04-06 20:36:35.234 LUA: -----------------------------------------------------
2016-04-06 20:36:35.234 LUA: [1] = boiler: Off
2016-04-06 20:36:35.234 LUA: [2] = boiler: Off
Any thoughts?

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

Posted: Friday 08 April 2016 8:40
by dannybloe
I'll look into this tonight or tomorrow :)

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

Posted: Friday 08 April 2016 13:13
by dannybloe
commodore white wrote:One of the triggers for my dzVents central heating script uses events driven from a room thermometer.

The script gets to run twice for each device interrupt: once with the name envKitchen, and again with the name envKitchen_Temperature. The upshot is that things that my script is supposed to do once per temperature measurement are happening twice. I can't check for this in the script because the two invocations are queued to run sequentially and the commandArray isn't processed until all the scripts have run to their conclusion.

Any thoughts?

In the dev branch I made a check for this (dev is still a bit in flux.. almost ready though). Thing is that Domoticz puts MySensor and MySensor_Temperature both in the devicechanged table. Both entries result in the same device which triggers a device script twice. So, that is fixed but only if Domoticz puts them both in the devicechanged tables at once. If they passed to dzVents in two separate event loops/cycles then there is nothing I can do.