Page 1 of 1

Blocky code events or LUA script?

Posted: Wednesday 13 November 2013 21:36
by mobisat
At the moment I have just been playing with blocky code events to do what I want (basic on/offs and timers, just learning).
Is there a benefit to using one method over the other? Does one function quicker etc?

Re: Blocky code events or LUA script?

Posted: Thursday 14 November 2013 14:23
by commodore white
Blockly is good for simple situations and entry level programmers. As soon as things get complicated then lua is the way to go.

I found a book called "beginning lua programming". It's always open on my tablet - I've learnt everything Lua reading it.

To test things out I create a script called script_device_play.lua on my RasPi happy in the knowledge it'll get called every 30 seconds and I can look at the log to see the results of my endeavours. If i mess up, domoticz will try it again 30 sec later.

Some of the names the developers choose for data tables are very long winded and result in code where you can't see wood for trees. All my scripts start with some VERY SHORT aliases for these arrays. It cuts down on typing and results in clean looking code. For example, I create an alias called "odsv' which is my shorthand for "otherdevices_svalues", similarly, I abbreviate "commandArray" to "set" as in "Set switch On". You need to do whatever you are comfortable with and what results in code you can read in a few weeks time and still understand it. I've also relearnt the old addage "if it ain't broke, don't fix it". Get your code working the way you first figured out how to solve the problem. Don't optimise/improve unless you have to.

Just my opinion, perhaps others could chip in.

Re: Blocky code events or LUA script?

Posted: Friday 15 November 2013 8:02
by Shunt
I agree that lua is the way to go. It has its quirks, but is fairly simple to learn. There are loads of lua examples and tips online, and the Domoticz-specific commands aren't that many so you'll learn them fast.

If you take a simple Blockly example and "convert" it to lua, you'll learn as you go along. You'll also get a lot more freedom in how you want to control things, as Blockly is quite limited. And if you get stuck you can always ask here.

I'm not a big fan of KISS (Keep It Simple, Stuipd), probably due to being on a tight leash when coding at work, so I tend to write quick messy scripts with lots of bells and whistles (notifications etc) then come back after a few days when I know it works and clean up the code.
I wish I could use aliases, but I've found that if it's been more than a few weeks since I read a script, I spend more time relearning the abbreviations than reading the script. But that's me. :)

Re: Blocky code events or LUA script?

Posted: Friday 15 November 2013 21:39
by commodore white
Good advice - I hadn't realised blockly could export to lua. That said, I pretty much code on my headless RasPi using a terminal emulator from my el cheapo android tablet. Blockly doesn't work on some tablet based browsers - mine included - so l didn't have much choice.

I should also say that there is hardly a line of code I haven't changed but mostly that's because I learned better ways while reading the book I mentioned.

Re: Blocky code events or LUA script?

Posted: Tuesday 19 November 2013 18:12
by mobisat
@Commodore white, I have just found that book too, in PDF. I might give it a glance through when I get time :)

Re: Blocky code events or LUA script?

Posted: Tuesday 15 December 2015 10:28
by elmortero
commodore white wrote:Good advice - I hadn't realised blockly could export to lua.
I want to do that too, but cannot find it.
After just two weeks with Domoticz blocky is restricting me and I don't have time to start studying LUA right now.
Converting them to lua would help a lot.