Page 6 of 17
Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!
Posted: Sunday 20 March 2016 10:56
by dakipro
yeah, I made a simple script that turns on and off every minute to test one of the new switches I made, so I am doing an
if(livingRoomLights.state == 'On') then
livingRoomLights.switchOff()
end
and else for switching on.
In Json there is json.htm?type=command¶m=switchlight&idx=46&switchcmd=Toggle
so I was thinking maybe there is a similar function in Lua. But it is not a missing critical, just an very cool convenient feature

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!
Posted: Sunday 20 March 2016 10:58
by dannybloe
I think the json call does the same as what you would do manually with Lua.
Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!
Posted: Sunday 20 March 2016 13:23
by commodore white
I thought this:
Code: Select all
test = domoticz.devices["vIsDark"]
test.setState( not test.state)
should work to toggle a variable, but got this:
2016-03-20 12:19:00.376 LUA: An error occured when calling event handler Night-and-Day
2016-03-20 12:19:00.377 LUA: /home/pi/domoticz/scripts/lua/Domoticz.lua:98: invalid value (boolean) at index 1 in table for 'concat'
... and little trace back. Any ideas?
Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!
Posted: Sunday 20 March 2016 13:29
by dannybloe
Well, the state attribute is a string (like 'On' or 'Off'). You cannot negate strings, only booleans. So unfortunately this will not work.
But I'll add the necessary logic to dzVents soon. For now you have to write a few lines:
Code: Select all
if (device.state == 'On') then
device.switchOff()
else
device.switchOn()
end
I'll add
that checks the current state and finds the matching opposing state.
Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!
Posted: Sunday 20 March 2016 14:29
by dakipro
sounds great. Maybe it can be called device.switchToggle(), to be consistent to switchOn and switchOff
Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!
Posted: Sunday 20 March 2016 14:30
by dannybloe
Agreed.
Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!
Posted: Sunday 20 March 2016 14:38
by commodore white
.. or could write:
Code: Select all
test.setState(test.bState and "Off" or "On")

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!
Posted: Sunday 20 March 2016 15:38
by commodore white
dannybloe wrote:Agreed.
While you're it, could you add Normal and Alarm as opposites for my marmitek door/window sensors
Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!
Posted: Sunday 20 March 2016 15:44
by dannybloe
You mean something like this: switchToNormal(), switchToAlarm()
And the toggles of course.
Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!
Posted: Sunday 20 March 2016 20:12
by commodore white
dannybloe wrote:You mean something like this: switchToNormal(), switchToAlarm()
And the toggles of course.
Hmmm. Probably not since the devices are read-only.
I'm looking in domoticz.lua about line 170 where you booleanize some strings. I'm suggesting you add:
and
... pretty please.
BTW I'm thinking you're going to be a hostage to fortune trying to implement your negate function because a) you'll find it hard to get a complete list of things to negate, and b) find it even harder to produce a correct list of their inverts for all devices.
By all means have a capability to Invert On to Off and vis versa but anything else is only going to complicate things, not necessarily the code but the documentation. Where do you stop in providing capabilities that can be done in native LUA fairly easily? Are you going to provide and a .increment and .decrement function because it takes two extra lines of code, or perhaps provide a .area or .circumference function? Where do you stop?
What might be handy, however, is a capability whereby users can have a library of functions that are accessible to each of their scripts. Such a library might include a users very own Negate function. I used to have LUA scripts that used the require function but I'm not sure if dzVents would preclude this. Used to be neat because I could write code that created LUA modules on the fly and then include them conditionally or at some future time. It proved useful to provide persistence before variables were introduced.
Keep up the good work. I really don't want to go back to native LUA.
Peter
Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!
Posted: Sunday 20 March 2016 20:14
by dannybloe
commodore white wrote:dannybloe wrote:You mean something like this: switchToNormal(), switchToAlarm()
And the toggles of course.
Hmmm. Probably not since the devices are read-only.
I'm looking in domoticz.lua about line 170 where you booleanize some strings. I'm suggesting you add:
and
... pretty please.
Oh, yeah, already added that today. I recognize all the states I could find in the domoticz code.. I hope

Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!
Posted: Monday 21 March 2016 9:25
by NietGiftig
commodore white wrote:
What might be handy, however, is a capability whereby users can have a library of functions that are accessible to each of their scripts. Such a library might include a users very own Negate function.
.......
Keep up the good work. I really don't want to go back to native LUA.
Peter
Looks like a nice addition, i like structure.
But is this not already possible with :
Code: Select all
myLua = require("MyLUACode") -- Include the module with common functions
Beware, I'm just beginning to discover lua
Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!
Posted: Monday 21 March 2016 10:08
by Westcott
Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!
Posted: Monday 21 March 2016 10:09
by commodore white
Looks like a nice addition, i like structure.
But is this not already possible with :
Code: Select all
myLua = require("MyLUACode") -- Include the module with common functions
Beware, I'm just beginning to discover lua[/quote]
Yes, I'm already aware of the require function. However, I'm also aware that dzVents does things differently so I'm not sure where I'd have to put the require statement so that it didnt get executed repeatedly.
Danny already make the point of not putting too much code in the "active" clause since it gets executed every time a device triggers so I don't want to make matters worse.
Guess I'll just try it. Whats the worse that can happen, eh?
Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!
Posted: Monday 21 March 2016 10:26
by woody4165
Hi all
sorry for my "stupid" question.
To install dzVents from the dzVents-master zip files, do I need to copy only those files to the domoticz/scripts/lua folder
event_helpers.lua
script_time_main.lua
script_device_main.lua
Domoticz.lua
dzVents_settings.lua
and create a scripts folder within domoticz/scripts/lua ?
What about all the other files in the zip under dzVents folder?
Thanks
Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!
Posted: Monday 21 March 2016 12:04
by NietGiftig
I think danny is busy with some changes in github
You only need those files, your own files go in the scripts folder inside the lua
folder
And do not forget to have the examples folder ready, make the jump somewhat easier
Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!
Posted: Monday 21 March 2016 12:10
by NietGiftig
commodore white wrote:
Danny already make the point of not putting too much code in the "active" clause since it gets executed every time a device triggers so I don't want to make matters worse.
Guess I'll just try it. Whats the worse that can happen, eh?
Yeah, read that to
Maybe you can place the require outside the execute function, in the beginning of your function.
But I don't know what the effect on the whole business is
Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!
Posted: Monday 21 March 2016 12:26
by woody4165
Thanks
If I put those files in the domoticz/scripts/lua I get all these errors in the log:
Code: Select all
2016-03-21 12:23:50.600 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_device_main.lua: /home/pi/domoticz/scripts/lua/EventHelpers.lua:4: module 'utils' not found:
no field package.preload['utils']
no file '/usr/local/share/lua/5.2/utils.lua'
no file '/usr/local/share/lua/5.2/utils/init.lua'
no file '/usr/local/lib/lua/5.2/utils.lua'
no file '/usr/local/lib/lua/5.2/utils/init.lua'
no file './utils.lua'
no file '/home/pi/domoticz/scripts/lua/utils.lua'
no file '/home/pi/domoticz/scripts/lua//dzVents/utils.lua'
no file '/usr/local/lib/lua/5.2/utils.so'
no file '/usr/local/lib/lua/5.2/loadall.so'
no file './utils.so'
And it keeps going this way.
I tried with naming the event_helper.lua (as written in GitHub installation description, as well renaming it as EventHelpers.lua overwriting the actual files.
What am I doing wrong?
Thanks
Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!
Posted: Monday 21 March 2016 14:03
by NietGiftig
use branch 0.9.13 master is rebuild i think
Re: Introducing dzVents - Domoticz Lua file based event scripting made e-z!
Posted: Monday 21 March 2016 14:10
by woody4165
I just downloaded the zip from github, but I get the same error....
