Page 1 of 1

Maybe dzVents is to difficult for me. Again copy from video fail.

Posted: Monday 07 October 2019 18:05
by KroontjesPen
 
Again is taking an example from the video not working. 16:56
The difference's is the name of the device in the way that I have a space in that name.
Also I have skipped the log part.

Code: Select all

return {
	on = {
		devices = {
			'Lichtsensor Binnen'
		},
	},
	execute = function(domoticz, 'Lichtsensor Binnen')        <-- error
		
		if ('Lichtsensor Binnen'.state == 'On') then
		    local KSR = domoticz.devices('Kamer Schoorsteen Rechts')
		   KSR.switchOn()

		end       

	end
}
2019-10-07 17:40:17.898 Status: EventSystem: reset all events...
2019-10-07 17:40:17.900 Status: dzVents: Write file: C:\Program Files (x86)\Domoticz\scripts\dzVents\generated_scripts\4 mei.lua
2019-10-07 17:40:17.901 Status: dzVents: Write file: C:\Program Files (x86)\Domoticz\scripts\dzVents\generated_scripts\Lichtsensor binnen.lua
2019-10-07 17:40:49.416 Status: dzVents: Error (2.4.19): error loading module 'Lichtsensor binnen' from file 'C:\Program Files (x86)\Domoticz\scripts\dzVents\generated_scripts/Lichtsensor binnen.lua':
2019-10-07 17:40:49.416 ...scripts\dzVents\generated_scripts/Lichtsensor binnen.lua:7: <name> or '...' expected near ''Lichtsensor Binnen''

(BTW why is also write my 4 mei.lua script every time.)

This make me not looking smart. :')

Re: Maybe dzVents is to difficult for me. Again copy from video fail.

Posted: Monday 07 October 2019 18:19
by domogijs
Try },
execute = function(domoticz, Lichtsensor Binnen)

Re: Maybe dzVents is to difficult for me. Again copy from video fail.

Posted: Monday 07 October 2019 19:18
by waaren
KroontjesPen wrote: Monday 07 October 2019 18:05 Again is taking an example from the video not working. 16:56
The difference's is the name of the device in the way that I have a space in that name.
The

Code: Select all

execute = function()
Is what it claims to be; a function.
it's a function with the name execute and with 0-3 parameters.

The 1st. parameter is the domoticz object which is a table with all devices, uservars, settings, etc
The 2nd. parameter is the object that triggered the script. So in this case a device with all of its attributes and connected functions.
The 3rd parameter is an object with some additional information. Not relevant for this script. If you are interested you can find the details in the wiki

so if you use

Code: Select all

 execute = function(domoticz, 'Lichtsensor Binnen')
dzVents (Lua) will try to interpret the parameters as described above but cannot handle a quoted string as parameter and presents you the errormessage.

I always use the call with parameters dz and item

Code: Select all

execute = function(dz, item) 
dz because I am too lazy to type domoticz many times and item because I often use multiple triggers for a script like devices and timers and httpResponses. In the script body you can figure out what triggered the script by structures like

Code: Select all

if item.isDevice then
  do stuff
elseif item.isTimer then
  do otherStuff
end
If you are not feeling comfortable yet with dzVents maybe start by searching the internet for "Lua getting started"
or go directly to the official Lua page

Lua is not difficult to learn but without any programming experience the way it uses tables might feel a bit overwhelming.

Re: Maybe dzVents is to difficult for me. Again copy from video fail.

Posted: Monday 07 October 2019 19:25
by waaren
domogijs wrote: Monday 07 October 2019 18:19 execute = function(domoticz, Lichtsensor Binnen)
@domogijs, this is not going to work; Lua does not allow a space in a parameter name.

Re: Maybe dzVents is to difficult for me. Again copy from video fail.

Posted: Monday 07 October 2019 19:29
by KroontjesPen
 

Nope. ;)

2019-10-07 19:07:00.187 Status: dzVents: Error (2.4.19): error loading module 'Lichtsensor binnen' from file 'C:\Program Files (x86)\Domoticz\scripts\dzVents\generated_scripts/Lichtsensor binnen.lua':
2019-10-07 19:07:00.187 ...scripts\dzVents\generated_scripts/Lichtsensor binnen.lua:7: ')' expected near 'Binnen'

Re: Maybe dzVents is to difficult for me. Again copy from video fail.

Posted: Monday 07 October 2019 19:34
by waaren
KroontjesPen wrote: Monday 07 October 2019 19:29 2019-10-07 19:07:00.187 Status: dzVents: Error (2.4.19): error loading module 'Lichtsensor binnen' from file 'C:\Program Files (x86)\Domoticz\scripts\dzVents\generated_scripts/Lichtsensor binnen.lua':
2019-10-07 19:07:00.187 ...scripts\dzVents\generated_scripts/Lichtsensor binnen.lua:7: ')' expected near 'Binnen'
As described in my previous post try

Code: Select all

return {
	on = {
		devices = {
			'Lichtsensor Binnen'
		},
	},
	execute = function(domoticz, item) -- item is here the device 'Lichtsensor Binnen' with all of it attributes and functions
		
		if (item.state == 'On') then
		    local KSR = domoticz.devices('Kamer Schoorsteen Rechts')
		    KSR.switchOn()
		end       
	end
}

Re: Maybe dzVents is to difficult for me. Again copy from video fail.

Posted: Monday 07 October 2019 19:52
by KroontjesPen
 
Lichtsensor Binnen is a normal switch with on/off. (KaKu/COCO type.)

What is then difference between 'Living' and 'Lichtsensor Binnen' the space.
How do we overcome this.

Re: Maybe dzVents is to difficult for me. Again copy from video fail.

Posted: Monday 07 October 2019 22:41
by KroontjesPen
 
In the back of my head I know the risk that a name of a device could have. Misspelling is easy to make.


This is my solution:

Code: Select all


return {
        on = {
    		devices = {
    			idx = 25
    		},
    	},
    	execute = function(domoticz, idx )

            local ksr = domoticz.devices('Kamer Schoorsteen Rechts')
            
    		if (idx.state == 'Off') then
    		   ksr.switchOff()
		   elseif (idx.state == 'On') then
    		   ksr.switchOn()

    		end       
	end
}

Kaku/COCO reacting little later. ;)

From here I hope to put delay timers and cancelQueuedCommands(): Function.


The question about saving my 4 mei.lua script every time this script get saved is still open.

 How get this with more scripts.
 

Re: Maybe dzVents is to difficult for me. Again copy from video fail.

Posted: Tuesday 08 October 2019 0:05
by waaren
KroontjesPen wrote: Monday 07 October 2019 22:41 This is my solution:
I am sorry to be the messenger of bad news but your solution will not do the job. Lua does not understand the idx = 25 the way you expect and will not trigger the script if device 25 changes state. If you want that to happen you will have to change that part to

Code: Select all

on = 
{
    devices = 
    {
         25
    },
},

Re: Maybe dzVents is to difficult for me. Again copy from video fail.

Posted: Tuesday 08 October 2019 0:09
by domogijs
waaren wrote: Monday 07 October 2019 19:25
domogijs wrote: Monday 07 October 2019 18:19 execute = function(domoticz, Lichtsensor Binnen)
@domogijs, this is not going to work; Lua does not allow a space in a parameter name.
Thanks! I did not notice

Greets Gijs

Re: Maybe dzVents is to difficult for me. Again copy from video fail.

Posted: Tuesday 08 October 2019 0:37
by waaren
KroontjesPen wrote: Monday 07 October 2019 22:41 The question about saving my 4 mei.lua script every time this script get saved is still open.
If you use the domoticz internal editor, the scripts are stored in the domoticz database. dzVents needs to access the scripts from an OS level to be able to process them. Because there is no way for the event system to know which files are not touched on the OS level the choice is made to write all scripts from the database to the OS level every time the event system is reset. A reset is generated by a number of events, one of them being saving a script from the internal editor.
If you don't want that during testing of a new script every time you save an updated version, you can write your scripts with an external editor and store them in the directory
<domoticzdir>/scripts/dzVents/scripts with the extension .lua

Re: Maybe dzVents is to difficult for me. Again copy from video fail.

Posted: Tuesday 08 October 2019 7:35
by KroontjesPen
 
waaren wrote: Tuesday 08 October 2019 0:05 I am sorry to be the messenger of bad news but your solution will not do the job. Lua does not understand the idx = 25 the way you expect and will not trigger the script if device 25 changes state. If you want that to happen you will have to change that part to
But ...
wiki/DzVents https://www.domoticz.com/wiki/DzVents:_next_generation_LUA_scripting#Device_object_API wrote:
Writing scripts

devices = { ... }

A list of device-names or indexes. If a device in your system was changed (e.g. switch was triggered or a new temperature was received) and it is listed in this section then the execute function is executed. Each device can be:

The name of your device between string quotes. You can use the asterisk (*) wild-card here e.g. PIR_* or *_PIR. E.g.: devices = { 'myDevice', 'anotherDevice', 123, 'pir*' }
The index (idx) of your device (as the name may change, the index will usually stay the same, the index can be found in the devices section in Domoticz). Note that idx is a number;

-------------------------------------
Device attributes and methods for all devices
.
.
.
id: Number. Index of the device. You can find the index in the device list (idx column) in Domoticz settings. It's not truly an index but is unique enough for dzVents to be treated as an id.
idx: Number. Same as id: index of the device
.
.

----------------------------------------
Domoticz attributes and methods

devices(idx/name): Function. A function returning a device by idx or name: domoticz.devices(123) or domoticz.devices('My switch'). .....

Is that wiki not trusted then?
 

Re: Maybe dzVents is to difficult for me. Again copy from video fail.

Posted: Tuesday 08 October 2019 9:29
by waaren
KroontjesPen wrote: Tuesday 08 October 2019 7:35 Is that wiki not trusted then?
 
Sorry but you misunderstand what you red. You can use the name of the device within quotes like 'keukentafel licht' or you can use the device id number like 123 without quotes. You tried

Code: Select all

        on = {
    		devices = {
    			idx = 25
    		},	
    },
and that is not what's in the wiki.

Re: Maybe dzVents is to difficult for me. Again copy from video fail.  [Solved]

Posted: Tuesday 08 October 2019 9:50
by KroontjesPen
 
Flowing your advice I looked up by Lua and found this and it's working. :D


3.4 – Concatenation
Lua denotes the string concatenation operator by ".." (two dots). If any of its operands is a number, Lua converts that number to a string.

print("Hello " .. "World") --> Hello World
print(0 .. 1) --> 01

Code: Select all

rreturn {
        on = {
    		devices = {
    		
                Dev = "Lichtsensor " .. "Binnen"
    		
    		},
    	},
    	execute = function(domoticz, Dev )

            local Doel = domoticz.devices('Kamer Schoorsteen Rechts')
            
    		if (Dev.state == 'Off') then
    		   Doel.switchOff()
		   elseif (Dev.state == 'On') then
    		   Doel.switchOn()

    		end       
    	end
}


BTW that IS the dzVents wiki

https://www.domoticz.com/wiki/DzVents:_ ... _scripting

Edit:

Make the script more flexible to use for other scripts.
For testing I can set -- for Dev and Doel and put others on a new line.