Page 1 of 1

Problem with table.insert in commandArray

Posted: Sunday 30 September 2018 20:32
by petercal
Hi,

I want these commands executed in sequence, but it works only if i have one line in commandArray not if
there are 2 lines. - What am i doing wrong ?

Code: Select all

commandArray = {}

if (devicechanged['vTest1'] == 'On') then
   print('vTest Called...')
   table.insert (commandArray, { ['Relay1'] = 'Off FOR 2 SECONDS REPEAT 8 INTERVAL 10 SECONDS' } )
   table.insert (commandArray, { ['Relay1'] = 'On AFTER 5 SECONDS' } )  
   print('vTest Exit...')    
end

return commandArray
I also tried this, but same problem, this time no lines gets executed

Code: Select all

if (devicechanged['vDoor1'] == 'On') then
   print('vDoor1 Called...')
   commandArray[#commandArray + 0] = {['Relay1'] ='On AFTER 5 SECONDS' }
   commandArray[#commandArray + 1] = {['Relay1'] ='Off FOR 2 SECONDS REPEAT 3 INTERVAL 15 SECONDS' }
   commandArray[#commandArray + 2] = {['Relay1'] ='On AFTER 5 SECONDS' }

end
I run newest Domoticz version

thanks Peter

Re: Problem with table.insert in commandArray

Posted: Monday 01 October 2018 1:15
by waaren
petercal wrote: Sunday 30 September 2018 20:32 Hi,

I want these commands executed in sequence, but it works only if i have one line in commandArray not if
there are 2 lines. - What am i doing wrong ?

Code: Select all

commandArray = {}

if (devicechanged['vTest1'] == 'On') then
   print('vTest Called...')
   table.insert (commandArray, { ['Relay1'] = 'Off FOR 2 SECONDS REPEAT 8 INTERVAL 10 SECONDS' } )
   table.insert (commandArray, { ['Relay1'] = 'On AFTER 5 SECONDS' } )  
   print('vTest Exit...')    
end

return commandArray
I also tried this, but same problem, this time no lines gets executed

Code: Select all

if (devicechanged['vDoor1'] == 'On') then
   print('vDoor1 Called...')
   commandArray[#commandArray + 0] = {['Relay1'] ='On AFTER 5 SECONDS' }
   commandArray[#commandArray + 1] = {['Relay1'] ='Off FOR 2 SECONDS REPEAT 3 INTERVAL 15 SECONDS' }
   commandArray[#commandArray + 2] = {['Relay1'] ='On AFTER 5 SECONDS' }

end
I run newest Domoticz version

thanks Peter
Some observations:
all commands in the commandArray will be evaluated immediate after the return so commandArray[1] will not wait until all commands in commandArray[0] are finished.
'Off FOR 2 SECONDS REPEAT 3 INTERVAL 15 SECONDS' will switch Off your device but will not send On signals.
the On AFTER 5 SECONDS conflict with the Off FOR and will not be send to the device
#commandArray + 0 = 0 + 0 ===>> #commandArray = 0
#commandArray + 1 = 0 + 1 ===>> #commandArray = 1
#commandArray + 2 = 1 + 2 ===>> #commandArray = 3

Try this:

Code: Select all

commandArray[#commandArray + 1] = {['Relay1'] ='On AFTER 35 SECONDS' } -- will be executed 4th
commandArray[#commandArray + 1] = {['Relay1'] ='On FOR 2 SECONDS REPEAT 3 INTERVAL 3 SECONDS' } -- will be executed 1st and finish in 15 seconds
commandArray[#commandArray + 1] = {['Relay1'] ='On AFTER 25 SECONDS' } -- will be executed 2nd
commandArray[#commandArray + 1] = {['Relay1'] ='Off AFTER 29 SECONDS' } -- will be executed 3rd

Re: Problem with table.insert in commandArray

Posted: Monday 01 October 2018 9:20
by petercal
Thank you very much, that makes more sense to me.

Are there other types of scripts, where lines are executed sequence ?

I know my commands are a little confusing, but the relay in fact goes ON when i send OFF and vice versa.
it is a cheap eBay 433 relay.

So what i want is to :

Turn relay ON in 2 secs then OFF
Wait 10 secs
Repeat x times

I have also noticed that the 'Off FOR 2 SECONDS REPEAT 4 INTERVAL 10 SECONDS' always starts with the INTERVAL delay of 10 secs.
So when script triggers the first ON comes after 10 Secs.
And the REPEAT 4 gives only 3 repeats (REPEAT -1)
- Could this be bugs ?

thanks,
Peter

Re: Problem with table.insert in commandArray

Posted: Monday 01 October 2018 14:41
by waaren
petercal wrote: Monday 01 October 2018 9:20 Are there other types of scripts, where lines are executed sequence ?
Probably not within domoticz. The event system in domoticz is single threaded and when a script waits for a command to
finish, all other script activity has to wait as well.
So what i want is to :

Turn relay ON in 2 secs then OFF
Wait 10 secs
Repeat x times
Sounds quite complicated so maybe it is easier to just spell it out to domoticz.

Code: Select all

local repeats        = 3
 local waitSeconds    = 10
 
 for i=0,repeats - 1 do
    commandArray[#commandArray + 1] = {['Relay1'] ='On AFTER ' .. tostring(i * waitSeconds) .. ' SECONDS' }
    commandArray[#commandArray + 1] = {['Relay1'] ='Off AFTER ' .. tostring(i * waitSeconds + 2) .. ' SECONDS' } 
 end
Switch 'On' to 'Off'and vice versa if your device wants "Off" to switch On

Re: Problem with table.insert in commandArray

Posted: Monday 01 October 2018 21:40
by petercal
Thank you very much, i'll try this tomorrow evening..
>
"The event system in domoticz is single threaded and when a script waits for a command to
finish, all other script activity has to wait as well. "
<

Here i learned more about Domoticz :
I was calling a LUA script, which included another LUA script and i did not work, this explains..

I'm quite new to Domoticz, and one thing is to install and get it running, which is easy, another thing is to
implement all the stuff you want in a proper way :)

Best Regards,
Peter

Re: Problem with table.insert in commandArray

Posted: Tuesday 02 October 2018 21:19
by petercal
Hello again,

Your solution works just PERFECT :D

The earlier tests was not really stable and the relay did stange things from time to time.
But this works stable, i have tested at least 20 times.
It is also simple, only 2 lines. - Great Work. :D

Best Regards
Peter