Script editor Topic is solved

Use this forum to discuss possible implementation of a new feature before opening a ticket.
A developer shall edit the topic title with "[xxx]" where xxx is the id of the accompanying tracker id.
Duplicate posts about the same id. +1 posts are not allowed.

Moderators: leecollings, remb0

schulpr
Posts: 137
Joined: Thursday 01 January 2015 9:10
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Re: Script editor

Post 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
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Script editor

Post 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
Last edited by jvdz on Saturday 02 January 2016 14:54, edited 1 time in total.
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
User avatar
Dnpwwo
Posts: 819
Joined: Sunday 23 March 2014 9:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

Re: Script editor

Post 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 :)
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw
schulpr
Posts: 137
Joined: Thursday 01 January 2015 9:10
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Re: Script editor

Post by schulpr »

The script was saved as "All". I changed it to "Device" and now it's wotking fine. Thanks for the help!

Rob
User avatar
Mediacj
Posts: 74
Joined: Wednesday 11 February 2015 16:09
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands
Contact:

Re: Script editor

Post by Mediacj »

Thanks really a nice addition!

Transferred my scripts to it and they work perfect!
SweetPants

Re: Script editor

Post 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?
User avatar
G3rard
Posts: 669
Joined: Wednesday 04 March 2015 22:15
Target OS: -
Domoticz version: No
Location: The Netherlands
Contact:

Re: Script editor

Post 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!
Not using Domoticz anymore
dijkdj
Posts: 63
Joined: Saturday 07 March 2015 22:10
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Script editor

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

Re: Script editor

Post 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.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
User avatar
Dnpwwo
Posts: 819
Joined: Sunday 23 March 2014 9:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

Re: Script editor

Post 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
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw
User avatar
G3rard
Posts: 669
Joined: Wednesday 04 March 2015 22:15
Target OS: -
Domoticz version: No
Location: The Netherlands
Contact:

Re: Script editor

Post 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!
Not using Domoticz anymore
User avatar
Brutus
Posts: 249
Joined: Friday 26 September 2014 9:33
Target OS: Windows
Domoticz version:
Location: Netherlands
Contact:

Re: Script editor

Post 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.
1x Intel NUC8i5BEK (Windows 10 x64) Domoticz on Virtualbox with DietPi.
1x Aeon Labs USB Z-Stick S2
1x P1 Smart Meter USB
28x Fibaro Modules
SMA Solar System
Daikin Airco / Heating
Denon DHT-S716H & DSW-1H
User avatar
sincze
Posts: 1299
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands / Breda Area
Contact:

Re: Script editor

Post 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
Pass2php
LAN: RFLink, P1, OTGW, MySensors
USB: RFXCom, ZWave, Sonoff 3
MQTT: ZIgbee2MQTT,
ZWAVE: Zwave-JS-UI
WIFI: Mi-light, Tasmota, Xiaomi Shelly
Solar: Omnik, PVOutput
Video: Kodi, Harmony HUB, Chromecast
Sensors: You name it I got 1.
marin849
Posts: 43
Joined: Tuesday 27 May 2014 10:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.4xxx
Location: Sweden
Contact:

Re: Script editor

Post 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'
MaartenB
Posts: 7
Joined: Monday 16 March 2015 19:51
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: The Netherlands (Groningen)
Contact:

Re: Script editor

Post 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
User avatar
Brutus
Posts: 249
Joined: Friday 26 September 2014 9:33
Target OS: Windows
Domoticz version:
Location: Netherlands
Contact:

Re: Script editor

Post 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
1x Intel NUC8i5BEK (Windows 10 x64) Domoticz on Virtualbox with DietPi.
1x Aeon Labs USB Z-Stick S2
1x P1 Smart Meter USB
28x Fibaro Modules
SMA Solar System
Daikin Airco / Heating
Denon DHT-S716H & DSW-1H
User avatar
remb0
Posts: 499
Joined: Thursday 11 July 2013 22:21
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Script editor

Post by remb0 »

how can the theme be changed? I see no options for this (running latest beta)
User avatar
Dnpwwo
Posts: 819
Joined: Sunday 23 March 2014 9:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

Re: Script editor

Post by Dnpwwo »

<Ctrl>, gives a popup window or on Firefox you can right mouse click on the editor and there is a menu.
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw
User avatar
G3rard
Posts: 669
Joined: Wednesday 04 March 2015 22:15
Target OS: -
Domoticz version: No
Location: The Netherlands
Contact:

Re: Script editor

Post 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.
Not using Domoticz anymore
User avatar
Brutus
Posts: 249
Joined: Friday 26 September 2014 9:33
Target OS: Windows
Domoticz version:
Location: Netherlands
Contact:

Re: Script editor

Post 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".
1x Intel NUC8i5BEK (Windows 10 x64) Domoticz on Virtualbox with DietPi.
1x Aeon Labs USB Z-Stick S2
1x P1 Smart Meter USB
28x Fibaro Modules
SMA Solar System
Daikin Airco / Heating
Denon DHT-S716H & DSW-1H
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests