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

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

Moderator: leecollings

Locked
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

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

Post 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
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
NietGiftig
Posts: 121
Joined: Sunday 11 October 2015 8:50
Target OS: Raspberry Pi / ODroid
Domoticz version: V3.6224
Location: Holland
Contact:

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

Post 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
RPI-2 + SSD / ESPEasy Sensors & Switches / Sonoff / RFLink / Action Switches / TP-Link switch / Node-Red / Reacticz
User avatar
olegdt
Posts: 3
Joined: Thursday 31 March 2016 10:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Kyiv
Contact:

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

Post 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
User avatar
olegdt
Posts: 3
Joined: Thursday 31 March 2016 10:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Kyiv
Contact:

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

Post 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
NietGiftig
Posts: 121
Joined: Sunday 11 October 2015 8:50
Target OS: Raspberry Pi / ODroid
Domoticz version: V3.6224
Location: Holland
Contact:

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

Post 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(.. ?
RPI-2 + SSD / ESPEasy Sensors & Switches / Sonoff / RFLink / Action Switches / TP-Link switch / Node-Red / Reacticz
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

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

Post 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).
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

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

Post 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
}

Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
thegritche
Posts: 8
Joined: Thursday 23 January 2014 22:23
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

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

Post 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
Domoticz blog & forum in french
http://easydomoticz.com/
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

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

Post 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:
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
thegritche
Posts: 8
Joined: Thursday 23 January 2014 22:23
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

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

Post 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
Domoticz blog & forum in french
http://easydomoticz.com/
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

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

Post by dannybloe »

Obviously curl isn't declared anywhere. I don't know how you did that before. Maybe you required it somehow. Dunno.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
thegritche
Posts: 8
Joined: Thursday 23 January 2014 22:23
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

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

Post by thegritche »

Ok
I keep my old but nice lua script
Thanks
Domoticz blog & forum in french
http://easydomoticz.com/
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

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

Post 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.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Eduard
Posts: 139
Joined: Monday 19 January 2015 9:14
Target OS: -
Domoticz version:

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

Post 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? ;)
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

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

Post by dannybloe »

I'll take a look at it.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Eduard
Posts: 139
Joined: Monday 19 January 2015 9:14
Target OS: -
Domoticz version:

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

Post by Eduard »

dannybloe wrote:I'll take a look at it.
Great!
commodore white
Posts: 63
Joined: Sunday 14 July 2013 11:19
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Ipswich, UK
Contact:

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

Post 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?
commodore white
Posts: 63
Joined: Sunday 14 July 2013 11:19
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Ipswich, UK
Contact:

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

Post 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?
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

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

Post by dannybloe »

I'll look into this tonight or tomorrow :)
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

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

Post 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.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Locked

Who is online

Users browsing this forum: No registered users and 1 guest