trying to understand LUA script but get in problem

Moderator: leecollings

Post Reply
zacken542
Posts: 7
Joined: Wednesday 21 October 2015 21:32
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

trying to understand LUA script but get in problem

Post by zacken542 »

I have renamed scrit_time_demo.lua script to scrit_time_test1.lua
I changed in 1 line, put in some more text in print to see it ii log better
print('test1=file===this will end up in the domoticz log')
this only change in this file from version 2.3530 (on Raspberry pi)

Code: Select all

-- demo time script
-- script names have three name components: script_trigger_name.lua
-- trigger can be 'time' or 'device', name can be any string
-- domoticz will execute all time and device triggers when the relevant trigger occurs
-- 
-- copy this script and change the "name" part, all scripts named "demo" are ignored. 
--
-- Make sure the encoding is UTF8 of the file
--
-- ingests tables: otherdevices,otherdevices_svalues

What is wrong?

Sven Åke
-- 
-- otherdevices and otherdevices_svalues are two item array for all devices: 
--   otherdevices['yourotherdevicename']="On"
--	otherdevices_svalues['yourotherthermometer'] = string of svalues
--
-- Based on your logic, fill the commandArray with device commands. Device name is case sensitive. 
--
-- Always, and I repeat ALWAYS start by checking for a state.
-- If you would only specify commandArray['AnotherDevice']='On', every time trigger (e.g. every minute) will switch AnotherDevice on.
--
-- The print command will output lua print statements to the domoticz log for debugging.
-- List all otherdevices states for debugging: 
--   for i, v in pairs(otherdevices) do print(i, v) end
-- List all otherdevices svalues for debugging: 
--   for i, v in pairs(otherdevices_svalues) do print(i, v) end

print('test1=file===this will end up in the domoticz log')

t1 = os.time()
s = otherdevices_lastupdate['Garagedoor']
-- returns a date time like 2013-07-11 17:23:12

year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)

commandArray = {}

t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
difference = (os.difftime (t1, t2))
if (otherdevices['Garagedoor'] == 'Open' and difference > 600 and difference < 700) then
	commandArray['SendNotification']='Garage door alert#The garage door has been open for more than 10 minutes!'

end 

return commandArray
output in logfile is

Code: Select all

015-12-24 22:16:00.022 LUA: test1=file===this will end up in the domoticz log
2015-12-24 22:16:00.022 Error: EventSystem: /home/pi/domoticz/scripts/lua/script_time_test1.lua:33: bad argument #1 to 'sub' (string expected, got nil) 
should not this Work from start !
in an other file

Code: Select all

f_light =12.345
commandArray["Variable:Ljus"] = tostring(f_light)
gives me this error

Code: Select all

 Error: EventSystem: /home/pi/domoticz/scripts/lua/script_time_sac.lua:43: attempt to index global 'commandArray' (a nil value)
User avatar
jvdz
Posts: 2330
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: trying to understand LUA script but get in problem

Post by jvdz »

For the first error: did you use the exact /proper name Garagedoor? This is case sensitive.
For the second error: Do you have a line in the script to define. commandArray and return it at the end of the script?

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
zacken542
Posts: 7
Joined: Wednesday 21 October 2015 21:32
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: trying to understand LUA script but get in problem

Post by zacken542 »

For the first error: did you use the exact /proper name Garagedoor? This is case sensitive.
you are right I have not change nae ! now it's OK
For the second error: Do you have a line in the script to define. commandArray and return it at the end of the script?
I give you hole second file

Code: Select all

-- demo time script
-- script names have three name components: script_trigger_name.lua
-- trigger can be 'time' or 'device', name can be any string
-- domoticz will execute all time and device triggers when the relevant trigger occurs
-- 
-- copy this script and change the "name" part, all scripts named "demo" are ignored. 
--
-- Make sure the encoding is UTF8 of the file
--
-- ingests tables: otherdevices,otherdevices_svalues
-- 
-- otherdevices and otherdevices_svalues are two item array for all devices: 
--   otherdevices['yourotherdevicename']="On"
--	otherdevices_svalues['yourotherthermometer'] = string of svalues
--
-- Based on your logic, fill the commandArray with device commands. Device name is case sensitive. 
--
-- Always, and I repeat ALWAYS start by checking for a state.
-- If you would only specify commandArray['AnotherDevice']='On', every time trigger (e.g. every minute) will switch AnotherDevice on.
--
-- The print command will output lua print statements to the domoticz log for debugging.
-- List all otherdevices states for debugging: 
--   for i, v in pairs(otherdevices) do print(i, v) end
-- List all otherdevices svalues for debugging: 
--   for i, v in pairs(otherdevices_svalues) do print(i, v) end
--commandArray = {}
f_temp=uservariables["Ljus"]
print(string.format('1====%f======-this will end up in the domoticz log',f_temp))

t1 = os.time()
-- returns a date time like 2013-07-11 17:23:12

file = io.open("/home/pi/solar.txt", "r")
io.input(file)
f_light=tonumber(io.read())
io.close(file)
f_light =12.345
print(string.format("f_light is >%f< %f",f_light,f_temp))
commandArray["Variable:Ljus"] = tostring(f_light)

if (f_light < 0.14) then
  print ('3a= It is  dark ')
commandArray['Variable:dark'] = tostring( 1)
else
  print ('3b= It is NOT dark ') 
 commandArray['Variable:dark'] =tostring( 0)
end
print('4==========-this will end up in the domoticz log')


return commandArray
and from logfile

Code: Select all

2015-12-25 09:44:00.073 LUA: 1====12.345600======-this will end up in the domoticz log
2015-12-25 09:44:00.074 LUA: f_light is >12.345000< 12.345600
2015-12-25 09:44:00.074 Error: EventSystem: /home/pi/domoticz/scripts/lua/script_time_sac.lua:39: attempt to index global 'commandArray' (a nil value) 
this Domoticz UserVariables

Code: Select all

2	Ljus	Flyttal	12.3456	2015-12-25 07:14:47
1	dark	Heltal	0	2015-12-24 13:19:09
heltal = integer
flyttal= float or decimal
Sven Åke
User avatar
jvdz
Posts: 2330
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: trying to understand LUA script but get in problem

Post by jvdz »

You have 2 hyphens infront of your Array definition making it a comment. ;)

so this:

Code: Select all

--commandArray = {}
should be:

Code: Select all

commandArray = {}
Merry Christmas,
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
zacken542
Posts: 7
Joined: Wednesday 21 October 2015 21:32
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: trying to understand LUA script but get in problem

Post by zacken542 »

THANKS now it's working
Merry chrimas

Sven Åke
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest