mrf68 wrote:I think your blockly, as shown in Testopen.JPG, is not built the correct way. Simonrg has explained it well, I want to make it a bit more visual:
Great job of visual explanation.
I sometimes wonder whether Blockly (and similar visual approaches) aren't a bit of a confidence trick, in that they appear to be very simple, but really aren't because as soon as you try to do something real it is very complex.
On the other hand Lua appears very complex, but really is a bit complex but not very complex.
I guess this problem is a good case in point.
Obviously, this is only my perception and Blockly does help you avoid spelling things wrong etc., but when you do make a mistake in Blockly it can be less obvious than in Lua. However trying to tell a computer to do something is also prone to errors / misunderstanding, in the same way that giving somebody directions to your house can be.
The equivalent Lua code would be:
Code: Select all
-- /home/pi/domoticz/scripts/lua/script_device_tester.lua
commandArray = {}
if devicechanged["Test 1"] or devicechanged["Test 2"] then
if (otherdevices["Test 1"] == "Open" or (otherdevices["Test 2"] == "Open") then
commandArray["A3"] = "On"
elseif (otherdevices["Test 1"] == "Off" and (otherdevices["Test 2"] == "Off") then
commandArray["A3"] = "Off"
end
end
return commandArray
and with comments (anything after --):
Code: Select all
-- /home/pi/domoticz/scripts/lua/script_device_tester.lua
-- Create an empty array to store the commands to be returned to Domoticz at the end of the script
commandArray = {}
-- Only do the other tests if one of the switches has changed
if devicechanged["Test 1"] or devicechanged["Test 2"] then
-- Test whether either of the switches is open
if (otherdevices["Test 1"] == "Open" or (otherdevices["Test 2"] == "Open") then
-- If either of the switches is open, then turn A3 on
commandArray["A3"] = "On"
-- Neither of the switches are on, so test if they are both off
elseif (otherdevices["Test 1"] == "Off" and (otherdevices["Test 2"] == "Off") then
-- If both switches off then turn A3 off
commandArray["A3"] = "Off"
end
end
-- Return the commandArray to Domoticz with the command to carry out.
return commandArray
You do need to read the event wiki page to even get started with Lua in Domoticz, whereas you can use Blockly without any pre-reading, but will I guess end up doing more post-reading as errors will be likely to catch you out.
Events -
http://www.domoticz.com/wiki/Events
Lua Scripts -
http://www.domoticz.com/wiki/Scripts#Lua_Scripts