If....do problem

Moderator: leecollings

Post Reply
Bert84
Posts: 13
Joined: Monday 25 May 2015 15:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10278
Location: Assen
Contact:

If....do problem

Post 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 1756 times
testdicht.JPG
testdicht.JPG (23.23 KiB) Viewed 1756 times
jannl
Posts: 673
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: If....do problem

Post by jannl »

Do you not need to check for On and Off instead of open and closed?
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: If....do problem

Post 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.
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
Bert84
Posts: 13
Joined: Monday 25 May 2015 15:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10278
Location: Assen
Contact:

Re: If....do problem

Post 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 1739 times
Thnx for that. Now to figure out how to handle "Closed"
markk
Posts: 267
Joined: Tuesday 14 January 2014 14:50
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: If....do problem

Post 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.
Running Domoticz on Pi3 with RFXtrx433e. LWRF power sockets and dimmer switches. Integrated my existing wirefree alarm PIRs and door contacts with domoticz. Geofencing with Pilot. Harmony Hub. Tado for Heating. Now playing with mysensors.
mrf68

Re: If....do problem

Post 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. ;)
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: If....do problem

Post 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
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
Bert84
Posts: 13
Joined: Monday 25 May 2015 15:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10278
Location: Assen
Contact:

Re: If....do problem

Post 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!
Evelen
Posts: 234
Joined: Thursday 09 July 2015 12:03
Target OS: Linux
Domoticz version: 2.4538
Location: Norway
Contact:

Re: If....do problem

Post 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?
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest