Page 3 of 5

Re: Script editor

Posted: Saturday 02 January 2016 10:32
by schulpr
This is the LUA script as it worked so far.

Code: Select all

-- Automatische keukenverlichting


now=os.time()
tijd=(os.date("%X", now))

commandArray = {}

if tijd > '19:00:00' and otherdevices['Schemersensor']== 'On'

	then if otherdevices['$keuken']== 'On' and otherdevices['Keuken-auto-uit']== 'Off' and otherdevices['Keukenverlichting']== 'Off'

		then
			commandArray['Keukenverlichting']= 'Set Level 10'
			commandArray['Variable:var_keuken_auto']= 'On'
	end
end

if otherdevices['$keuken']== 'Off' and uservariables['var_keuken_auto']== 'On'

		then
			commandArray['Keukenverlichting']= 'Off'
			commandArray['Variable:var_keuken_auto']= 'Off'
end

if devicechanged['Keukenverlichting']== 'Off'

		then
			commandArray['$keuken']= 'Off'
			commandArray['Variable:var_keuken_auto']= 'Off'
end

return commandArray

Re: Script editor

Posted: Saturday 02 January 2016 10:52
by jvdz
2016-01-02 10:06:00
Looking at the time of the error: Is it possibly ran as a TIME event in stead of DEVICE Event?
That would cause this error as that array would not be available.

Jos

Re: Script editor

Posted: Saturday 02 January 2016 10:56
by Dnpwwo
I suspect there will be quite a few cases where people had failing scripts but didn't know.

Firstly, I assume your file based script was named so that it ran only when devices change? If so, did you explicitly set that to match in the 2nd drop down by setting you event type to 'Device'? You will need to.

Even if you did, this script will normally fail, and it should have failed in your current set up ever since you created it except when the 'Keukenverlichting' device changed.

'devicechange' is only supplied to events when devices change and it only contains the name of the device(s) that have changed. This means that the devicechanged table does not always contain a value for 'Keukenverlichting' and the script will fail with a 'nil' value when a value is not present.

You need to 'guard' the table access with a structure like:

Code: Select all

if tostring(devicechanged['Keukenverlichting'])== 'Off' then
         commandArray['$keuken']= 'Off'
         commandArray['Variable:var_keuken_auto']= 'Off'
end
the tostring() will stop the error because it will return the string 'nil' which will not equal 'Off' when a different device has changed.

The Domoticz Lua capabilities are the same for file and database based scripts,only where they are stored is different.

EDIT: I didn't notice the log time, Looks like my 'firstly' was the cause :)

Re: Script editor

Posted: Saturday 02 January 2016 11:12
by schulpr
The script was saved as "All". I changed it to "Device" and now it's wotking fine. Thanks for the help!

Rob

Re: Script editor

Posted: Saturday 02 January 2016 12:54
by Mediacj
Thanks really a nice addition!

Transferred my scripts to it and they work perfect!

Re: Script editor

Posted: Saturday 02 January 2016 20:23
by SweetPants
Just transferred some of my scripts too, and they work fine, but if one of the scripts generates an error, the log does not show which of the scripts generates it. Is it possible to add the script name in the log?

Re: Script editor

Posted: Sunday 03 January 2016 1:29
by G3rard
The script editor works great!

Just transferred some of my time scripts and it works nice.
Some suggestions:
* Add a possibility that only LUA scripts or Blockly's are shown in Saved Events list. I have now started the name of the LUA scripts with LUA, but a selection option would be nice (just as vil1driver suggested).
* When a LUA script is shown in the editor with many lines, it shows the end of the script (this happens not with the first script, but after selecting a second script). I think it's more logic to show the beginning of the script.
* If Blockly is selected (field under Event Name) it seems logic to disable the All, Device etc dropdown menu.

Really nice work!

Re: Script editor

Posted: Sunday 03 January 2016 11:15
by dijkdj
Justed tested the script editor, very nice addition. The opens the road for easier scripting.

I have the same suggestions as G3rard!

Keep it up!

Re: Script editor

Posted: Sunday 03 January 2016 14:38
by dannybloe
Wouldn't it be nice to be able to say to a Blockly script 'convert to Lua'? I think that would be awesome. Often people start with a Blockly and then discover that their if-scenario is becoming to complex for Blockly. If you can convert a script to Lua if allows users to easily grow from being script-novices to more advanced users.

I think that a blockly script can easily be converted to Lua. It's a limited command set and fairly predictable.

Re: Script editor

Posted: Sunday 03 January 2016 22:12
by Dnpwwo
I looked into a Blockly -> Lua converter doing an XML/XSL transform in the browser and it wouldn't be too hard. I decided to focus on getting thethe base functionality out there else it would never get released.

I will be looking at this soon :D

Re: Script editor

Posted: Monday 04 January 2016 21:06
by G3rard
@Dnpwwo, in the LUA editor you can press Ctrl+, which gives a menu with Ace settings.
I noticed that these settings aren't saved. So when you change settings and then go to another Domoticz page and return to the editor, the settings are back to the original.
Can you please check that?
Thanks again for this nice feature!

Re: Script editor

Posted: Tuesday 05 January 2016 11:41
by Brutus
Yesterday installed the latest Windows version of Domoticz with the new lua editor in Domoticz.
Nice feature!! Everything in one webpage.
I have already pasted all my LUA scripts in the editor en removed the old one's in the LUA script folder.

So thnx for this.

Greetings Brutus.

Re: Script editor

Posted: Sunday 10 January 2016 14:36
by sincze
Great addition.
Just moved alll my lua scripts to it.

First had some problems

Code: Select all

The error is attempt to index global 'devicechanged' (a nil value)
However reading through this topic I found out I had to set 'time' & 'device' as wel for each individual lua script..
After modification no more errors in my error log!
This enables me to create scripts while not having access to the system via putty :D

And helping others remotely!

tnx. :D

Re: Script editor

Posted: Monday 11 January 2016 15:34
by marin849
Excellent, thank you!!

Only drawback as I see is that the log doesn't show which scripts that are executed, like this:

Code: Select all

2016-01-11 15:30:00.437 EventSystem: Script event triggered: /home/pi/dev-domoticz/scripts/lua/script_time_dpcalc.lua
And it doesn't show in which script there was en error, if any:

Code: Select all

2016-01-11 15:30:00.456 Error: EventSystem: [string "-- ..."]:25: syntax error near 'crapshiterror'

Re: Script editor

Posted: Monday 25 January 2016 14:03
by MaartenB
Ok, just wanted to let you know that.
1) Error for LUA are improved (prints out the event name). It was not so difficult to do this, fork https://github.com/domoticz/domoticz , then make changes to the fork (can be done with the online editor, or clone the fork to you local disk, and push them back), and then do a pull request. The changes were in main/EventSystem.cpp

2) Python support is added as well, which is off by default (should work on linux and osx). Run as:
$ cmake -DUSE_PYTHON=On
or toggle it using ccmake.

This was the pull request: https://github.com/domoticz/domoticz/pull/363

Re: Script editor

Posted: Wednesday 10 February 2016 8:58
by Brutus
Yesterday I noticed that you can change the theme of the editor.
I like the Cobalt theme so I selected this one.
But when you close and open the page or change between tabs the theme is reset to the default theme (XCode).

Can't this be saved?

EDIT:
Thnx for the saving option! From the Commits page:

Commits on Feb 20, 2016
Added saving of editor theme to Event page
dnpwwo committed 10 hours ago

Re: Script editor

Posted: Sunday 21 February 2016 11:55
by remb0
how can the theme be changed? I see no options for this (running latest beta)

Re: Script editor

Posted: Sunday 21 February 2016 12:16
by Dnpwwo
<Ctrl>, gives a popup window or on Firefox you can right mouse click on the editor and there is a menu.

Re: Script editor

Posted: Sunday 21 February 2016 14:03
by G3rard
Dnpwwo wrote:<Ctrl>, gives a popup window or on Firefox you can right mouse click on the editor and there is a menu.
Saving the theme works now with the latest beta :mrgreen:
I had to delete the cache in Chrome and after that it works okay.

Re: Script editor

Posted: Sunday 21 February 2016 14:41
by Brutus
Someone tested this on Firefox?

Today the new Windows release of Domoticz with this option was available so I installed it and deleted the cache in Firefox.

After starting Firefox I headed to the events page, right clicked and changed the theme. After switching to the dashboard and back the settings are already lost. Even when closing Firefox.

In Edge en Internet Explorer the popup window doesn't show "change theme".