Page 1 of 1

If....do problem

Posted: Monday 22 February 2016 21:18
by Bert84
Hello,

I reckon myself as a starter with Domoticz. I have been looking al over this forum for an answer. I hope anyone can help me.

I have two contact switches. If one of them is "open", I want Domoticz to switch on a light. If they are both closed, the light must be switched of. I have 2 blockies, but they are not working. What am I doing wrong?
Testopen.JPG
Testopen.JPG (22.39 KiB) Viewed 1766 times
testdicht.JPG
testdicht.JPG (23.23 KiB) Viewed 1766 times

Re: If....do problem

Posted: Monday 22 February 2016 21:28
by jannl
Do you not need to check for On and Off instead of open and closed?

Re: If....do problem

Posted: Monday 22 February 2016 21:35
by simonrg
This isn't really a Domoticz issue, you would find this easier if you had a play with blockly itself and then made your Domoticz event.
https://developers.google.com/blockly/

Your event as written means:

Code: Select all

Test 1 = (open or (Test 2 = Open))
not what I think you want which is

Code: Select all

(Test 1 = open) or (Test 2 = open)
To get the second you need to add the blocks in the right order not just add left to right as you have done in your code.

So first add if block, then put the or block inside it, then put = blocks on each side of the or block, then fill in the variables and on / open etc..

You should then find your events will work.

Re: If....do problem

Posted: Monday 22 February 2016 21:57
by Bert84
Thx for the reactions. I checked is and "Open" and "closed" are right.
Mr simonrg: You put me in the right direction. For "Open" I now have the following Blocky, wich is working now:
Testopen1.JPG
Testopen1.JPG (21.97 KiB) Viewed 1749 times
Thnx for that. Now to figure out how to handle "Closed"

Re: If....do problem

Posted: Monday 22 February 2016 22:11
by markk
Bert84 wrote:Hello,

I reckon myself as a starter with Domoticz. I have been looking al over this forum for an answer. I hope anyone can help me.

I have two contact switches. If one of them is "open", I want Domoticz to switch on a light. If they are both closed, the light must be switched of. I have 2 blockies, but they are not working. What am I doing wrong?
Testopen.JPG
testdicht.JPG
I remember reading somewhere that the "if do" blocks with the "+" don't work if there isn't an "else" command present. For this you need a normal "if do" block.

Re: If....do problem

Posted: Tuesday 23 February 2016 17:02
by mrf68
Hi Bert84,

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:

First make seperate statements for each thing that needs to be true. In this case you want to check Test 1 and Test 2. So build those 2 statements:

Image

Image

Then combine them in the 'or' statement:

Image

Image

Then take an 'If' block, create a 'Set' statement and there you have it:

Image

And finally you create this:

Image

When you read the text, you will say it sounds exactly what you did in Testopen.JPG, but there is a difference. ;)

Re: If....do problem

Posted: Tuesday 23 February 2016 17:55
by simonrg
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

Re: If....do problem

Posted: Tuesday 23 February 2016 21:25
by Bert84
You' ve made it very clear. The blocky' s are working now.
Indeed, it looks simple, but if you make a mistake, its hard to find the mistake.
Maybe its time to take a good look at Lua for me.
Thank you all!

Re: If....do problem

Posted: Tuesday 23 February 2016 21:55
by Evelen
Bert84 wrote: Maybe its time to take a good look at Lua for me.
me2.
Are there any domoticz specific instructions for lua?