Blocky to Lua

Moderator: leecollings

Post Reply
theo69
Posts: 60
Joined: Sunday 04 June 2017 12:57
Target OS: Raspberry Pi / ODroid
Domoticz version: la. beta
Location: World
Contact:

Blocky to Lua

Post 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 1640 times
Thanks a lot
Love my Xiaomi Gateway in connection with homebridge and domoticz on my PI3
Xiaomi Gateway Domoticz Wiki
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: Blocky to Lua

Post 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
The most dangerous phrase in any language is:
"We always done this way"
theo69
Posts: 60
Joined: Sunday 04 June 2017 12:57
Target OS: Raspberry Pi / ODroid
Domoticz version: la. beta
Location: World
Contact:

Re: Blocky to Lua

Post 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?
Love my Xiaomi Gateway in connection with homebridge and domoticz on my PI3
Xiaomi Gateway Domoticz Wiki
GJKNL
Posts: 41
Joined: Monday 31 October 2016 9:33
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Blocky to Lua

Post 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
Last edited by GJKNL on Friday 09 June 2017 14:45, edited 1 time in total.
User avatar
jvdz
Posts: 2334
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Blocky to Lua

Post 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
Last edited by jvdz on Friday 09 June 2017 15:03, edited 1 time in total.
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: Blocky to Lua

Post 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:
The most dangerous phrase in any language is:
"We always done this way"
User avatar
jvdz
Posts: 2334
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Blocky to Lua

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
theo69
Posts: 60
Joined: Sunday 04 June 2017 12:57
Target OS: Raspberry Pi / ODroid
Domoticz version: la. beta
Location: World
Contact:

Re: Blocky to Lua

Post 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.
Love my Xiaomi Gateway in connection with homebridge and domoticz on my PI3
Xiaomi Gateway Domoticz Wiki
theo69
Posts: 60
Joined: Sunday 04 June 2017 12:57
Target OS: Raspberry Pi / ODroid
Domoticz version: la. beta
Location: World
Contact:

Re: Blocky to Lua

Post 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!!!!
Last edited by theo69 on Saturday 10 June 2017 11:30, edited 1 time in total.
Love my Xiaomi Gateway in connection with homebridge and domoticz on my PI3
Xiaomi Gateway Domoticz Wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest