Page 1 of 1

Blocky to Lua

Posted: Thursday 08 June 2017 17:32
by theo69
Hi there I am a total n00b and just starting, now i have a blocky script i would like to translate into Lua (because I need it more often and Lua is easy to copy) Tried a lot but failed, maybe somebody can push me into the right direction :?
screenshot_41.png
screenshot_41.png (79.42 KiB) Viewed 1649 times
Thanks a lot

Re: Blocky to Lua

Posted: Thursday 08 June 2017 17:42
by emme

Code: Select all

commandArray = {}
   if otherdevices['Einfachschalter'] == 'On' and otherdevices['Testschalter'] == 'Off' then
      commandArray['Testschalter'] = 'On AFTER 1'
   elseif otherdevices['Einfachschalter'] == 'On' and otherdevices['Testschalter'] == 'On' then
      commandArray['Testschalter'] = 'Off AFTER 1'
   end
return commandArray
but a smarter way would be:

Code: Select all

commandArray = {}
   if otherdevices['Einfachschalter'] == 'On' then
      if otherdevices['Testschalter'] == 'Off' then 
         commandArray['Testschalter'] = 'On AFTER 1' 
      else
         commandArray['Testschalter'] = 'Off AFTER 1' 
      end
   end
return commandArray
ciao
M

Re: Blocky to Lua

Posted: Friday 09 June 2017 14:24
by theo69
emme wrote:

Code: Select all

commandArray = {}
   if otherdevices['Einfachschalter'] == 'On' and otherdevices['Testschalter'] == 'Off' then
      commandArray['Testschalter'] = 'On AFTER 1'
   elseif otherdevices['Einfachschalter'] == 'On' and otherdevices['Testschalter'] == 'On' then
      commandArray['Testschalter'] = 'Off AFTER 1'
   end
return commandArray
but a smarter way would be:

Code: Select all

commandArray = {}
   if otherdevices['Einfachschalter'] == 'On' then
      if otherdevices['Testschalter'] == 'Off' then 
         commandArray['Testschalter'] = 'On AFTER 1' 
      else
         commandArray['Testschalter'] = 'Off AFTER 1' 
      end
   end
return commandArray
ciao
M
Wow thanky a lot trying to understand but still a noob, i tried to get rid of the delay and did:

Code: Select all

commandArray = {}
   if otherdevices['Einfachschalter'] == 'On' then
      if otherdevices['Testschalter'] == 'Off' then 
         commandArray['Einfachschalter'] = 'Off' and commandArray['Testschalter'] = 'On'
      elseif otherdevices['Testschalter'] == 'On' then 
         commandArray['Einfachschalter'] = 'Off' and commandArray['Testschalter'] = 'Off'
      end
   end
return commandArray
however its not working, where did i go wrong?

Re: Blocky to Lua

Posted: Friday 09 June 2017 14:31
by GJKNL
Try this. But I do not understand your logic. Now you switch something off and on and then you switch both devices off again??

Code: Select all

commandArray = {}
   if otherdevices['Einfachschalter'] == 'On' then
      if otherdevices['Testschalter'] == 'Off' then 
         commandArray['Einfachschalter'] = 'Off' 
         commandArray['Testschalter'] = 'On'
      elseif otherdevices['Testschalter'] == 'On' then 
         commandArray['Einfachschalter'] = 'Off''
         commandArray['Testschalter'] = 'Off'
      end
   end
return commandArray

Re: Blocky to Lua

Posted: Friday 09 June 2017 14:42
by jvdz
emme wrote: but a smarter way would be:

Code: Select all

commandArray = {}
   if otherdevices['Einfachschalter'] == 'On' then
      if otherdevices['Testschalter'] == 'Off' then 
         commandArray['Testschalter'] = 'On AFTER 1' 
      else
         commandArray['Testschalter'] = 'Off AFTER 1' 
      end
   end
return commandArray
This code feels flawed to me and will constantly trigger Testschalter On and Off while Einfachschalter is On... or am I seeing that wrong?
Jos

Re: Blocky to Lua

Posted: Friday 09 June 2017 14:57
by emme
jvdz wrote:This code feels flawed to me and will constantly trigger Testschalter on and of while Einfachschalter is On... or am I seeing that wrong?
my understanding of the Blockly script is to toggle Testschalter after 1 sec when Einfaschalter is on....

yes... Testschalter will flash 1sec while Einfachschalter id ON....

Wasn't this the intention? :oops: :oops:

Re: Blocky to Lua

Posted: Friday 09 June 2017 15:07
by jvdz
The difference with the Blockly code is that it will only act of a devicechange event and your script will trigger each time it is run by an event.
So to reflect the exact code you need to use devicechanged:

Code: Select all

commandArray = {}
   if devicechanged['Einfachschalter'] == 'On' then
      if otherdevices['Testschalter'] == 'Off' then 
         commandArray['Testschalter'] = 'On AFTER 1' 
      else
         commandArray['Testschalter'] = 'Off AFTER 1' 
      end
   end
return commandArray
Jos

Re: Blocky to Lua

Posted: Saturday 10 June 2017 9:45
by theo69
Thanks to all, my problem is I am using a wireless Xiaomi Aqara Wall Switch, I have to set the off delay to 1 sec otherwise its not switching off again. Therefore I have to use a dummy switch to use the switch in my homekit (Apple). Maybe I went wrong with my script and thinking....

I would like to get rid of the 1 sec delay.

Re: Blocky to Lua

Posted: Saturday 10 June 2017 9:50
by theo69
I think I start to understand all these:

Code: Select all

commandArray = {}
   if devicechanged['Einfachschalter'] == 'On' then
      if otherdevices['Testschalter'] == 'Off' then 
         commandArray['Testschalter'] = 'On' 
      else
         commandArray['Testschalter'] = 'Off' 
      end
   end
return commandArray
Its now working perfekt, thanks this a really a great community!!!!