Blocky script, IF else if question

Moderator: leecollings

Post Reply
pcardon
Posts: 6
Joined: Friday 22 January 2016 21:33
Target OS: NAS (Synology & others)
Domoticz version: 2.4298
Location: France
Contact:

Blocky script, IF else if question

Post by pcardon »

Hello in the above sample I can't understand why the if block is executing, and just after the else if executes too ?
blocky.jpg
blocky.jpg (77.5 KiB) Viewed 11346 times
and here is the log that shows the problem :
2016-01-22 21:39:00.428 SUN handler : SUNSET <====================================
end ngRepeat: item in filtered = (logitems | filter:filterExpr) track by $index
2016-01-22 21:39:00.429 EventSystem: Event triggered: SUN handler_1
end ngRepeat: item in filtered = (logitems | filter:filterExpr) track by $index
2016-01-22 21:39:00.430 SUN handler : SUNRISE <====================================
end ngRepeat: item in filtered = (log items | filter:filterExpr) track by $index

I'm sorry, i'm a starter with Domoticz and I'm sure I'm missing something

Regards
Pascal
tozzke
Posts: 135
Joined: Friday 02 January 2015 9:22
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands
Contact:

Re: Blocky script, IF else if question

Post by tozzke »

that's because when it's later than sunset, it's also still later than sunrise ;)

You should do the following:
If [Time > Sunset] or* [Time < Sunrise]
Do [Set var Jour_Nuit = "Nuit"]
Else if [Time > Sunrise] and [Time < Sunset]
Do [Set var Jour_Nuit = "Jour"]

* Domoticz sees every time related event from and to midnight. So if you're crossing midnight with an event, you'll have to use 'or'
pcardon
Posts: 6
Joined: Friday 22 January 2016 21:33
Target OS: NAS (Synology & others)
Domoticz version: 2.4298
Location: France
Contact:

Re: Blocky script, IF else if question

Post by pcardon »

Hello,

Thank you for your answer,
I understand your answer, however, as far as I understand the "ELSE IF", this block should not execute if the "IF' block just executed ?
IF this is SUNSET ... set variable to "NIGHT" (NUIT in french) and quit ...
I'm missing something here ...
Please can you help me
tozzke
Posts: 135
Joined: Friday 02 January 2015 9:22
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands
Contact:

Re: Blocky script, IF else if question

Post by tozzke »

it triggers just fine, no need to worry ;)
Even better is this to prevent retriggering:
If [[Time > Sunset] or* [Time < Sunrise]] and [var Jour_Nuit = "Jour"]
Do [Set var Jour_Nuit = "Nuit"]
Else if [[Time > Sunrise] and [Time < Sunset]] and [var Jour_Nuit = "Nuit"]
Do [Set var Jour_Nuit = "Jour"]
pcardon
Posts: 6
Joined: Friday 22 January 2016 21:33
Target OS: NAS (Synology & others)
Domoticz version: 2.4298
Location: France
Contact:

Re: Blocky script, IF else if question

Post by pcardon »

Sorry to insist …
I understand that adding just supplementary tests will prevent retriggering, thats OK ...

But, I think the "ELSE IF" block should not execute if the "IF" block has executed ? Am I wrong ? Who can explain me that point ?
If I'm wrong, what is the "ELSE IF" utility ? this is something I don't understand here ...
Thanks for your help
Pascal
tozzke
Posts: 135
Joined: Friday 02 January 2015 9:22
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands
Contact:

Re: Blocky script, IF else if question

Post by tozzke »

You are wrong ;).
see here for instance

the If-statement as well as the Else if-statement will trigger when conditions are met
alfred_j_kwak
Posts: 110
Joined: Friday 20 September 2013 18:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.3530
Location: Finland
Contact:

Re: Blocky script, IF else if question

Post by alfred_j_kwak »

Did you read what it says?


An if statement follows this sort of structure:

if (condition) {
// executed only if "condition" is true
} else if (other condition) {
// executed only if "condition" was false and "other condition" is true
} else {
// executed only if both "condition" and "other condition" were false
}


As it says: executed only if "condition" was false and "other condition" is true
pcardon
Posts: 6
Joined: Friday 22 January 2016 21:33
Target OS: NAS (Synology & others)
Domoticz version: 2.4298
Location: France
Contact:

Re: Blocky script, IF else if question

Post by pcardon »

Yes I read what it says :
In my case, if (condition){
} is just true, it's 2016-01-22 21:39:00.428, so "IF" block is executed SUNSET, thats OK ...

BUT after I read :
else if (other condition) {
// executed only if "condition" was false and "other condition" is true
}

I read executed only if "condition" was false ... I think I understand ... So, if "condition" was true as seen above ... SO I UNDERSTAND the "else if" block should not execute ...
where am I wrong ?

later in the provided sample I read this :
/* If-ElseIf-Else Example */
if(condition_is_true){
do_this
}else if(different_condition_is_true){
do_this_only_if_first_condition_was_false_and_different_condition_was_true
}

I'm still blocking on your "explanations" … please
alfred_j_kwak
Posts: 110
Joined: Friday 20 September 2013 18:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.3530
Location: Finland
Contact:

Re: Blocky script, IF else if question

Post by alfred_j_kwak »

Sorry pcardon. I wasn't clear. I mean that you are right with your problem. And tozzke made mistake.
pcardon
Posts: 6
Joined: Friday 22 January 2016 21:33
Target OS: NAS (Synology & others)
Domoticz version: 2.4298
Location: France
Contact:

Re: Blocky script, IF else if question

Post by pcardon »

Hello @alfred_j_kwak,

Thanks for your answer … does that mean there is a bug in the Blocky script engine ?
Is it possible to have a so big bug ?
Can somebody test on his Domoticz and see if he has the same problem …
I'm running Domoticz on a Synology, perhaps it's a specific problem to this distribution ...
Regards
User avatar
bizziebis
Posts: 182
Joined: Saturday 19 October 2013 14:00
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8805
Location: The Netherlands
Contact:

Re: Blocky script, IF else if question

Post by bizziebis »

It has always been this way. It would be better if the name ELSE was not there. Because it only functions as IF, then.. IF, then etc.

This was the reason I switched to LUA scripting. When you want to solve it in Blockly you have to make large scripts which are sometimes hard to debug. To have one ELSE statement, you now have to copy your whole Blockly and add one different parameter so it will only trigger then.
pcardon
Posts: 6
Joined: Friday 22 January 2016 21:33
Target OS: NAS (Synology & others)
Domoticz version: 2.4298
Location: France
Contact:

Re: Blocky script, IF else if question

Post by pcardon »

bizziebis wrote:It has always been this way. It would be better if the name ELSE was not there. Because it only functions as IF, then.. IF, then etc.

This was the reason I switched to LUA scripting. When you want to solve it in Blockly you have to make large scripts which are sometimes hard to debug. To have one ELSE statement, you now have to copy your whole Blockly and add one different parameter so it will only trigger then.
Thanks you for this explanation …
So you know why I didn't understand … and that is different of all other "else if" implementations I ever seen
nickb512
Posts: 4
Joined: Wednesday 18 January 2017 19:46
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Blocky script, IF else if question

Post by nickb512 »

How has this issue not been fixed yet? Nearly a year later and one of the fundamental pieces of a programming language still doesn't work as expected. Is Domoticz still actively supported? Was considering moving from SmartThings until i discovered this.

Nick
Spitfire
Posts: 20
Joined: Thursday 29 December 2016 9:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Blocky script, IF else if question

Post by Spitfire »

Yes, it' s strange that an obvious bug like this isn't taken care off. Add to that...it would be nice if "Else" was implemented.
blauwebuis
Posts: 331
Joined: Wednesday 21 December 2016 9:11
Target OS: Raspberry Pi / ODroid
Domoticz version: current
Contact:

Re: Blocky script, IF else if question

Post by blauwebuis »

Ah, this explains some weird behaviour..

Is there a fix on the horizon?
motejowi
Posts: 4
Joined: Tuesday 12 September 2017 4:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Blocky script, IF else if question

Post by motejowi »

YES this is a big misunderstandig of programming fundamental rule!
I also discovered another bug, like in this code:

Code: Select all

If button = On
Do
  If light = Off and a = 0
  Do
    Set a = 1
    Set light = On
  Else if light = On and a = 0
  Do
    Set light = Off
  Set a = 0
In this example not only Else executes but also (if light = On AND a = 0) executes despite the 'a' was set to 1 in previous statement!
What's going on?
Domoticz, is this really kind of joke? This is not funny.
motejowi
Posts: 4
Joined: Tuesday 12 September 2017 4:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Blocky script, IF else if question

Post by motejowi »

I committed an issue on Domoticz Github, if there's anyone interested / annoyed with it, can vote.
regards!
https://github.com/domoticz/domoticz/issues/1804
pvm
Posts: 550
Joined: Tuesday 17 June 2014 22:14
Target OS: NAS (Synology & others)
Domoticz version: 4.10538
Location: NL
Contact:

Re: Blocky script, IF else if question

Post by pvm »

I do not see a vote button
Synology NAS, slave PI3, ZWave (Fibaro), Xiaomi zigbee devices, BTLE plant sensor, DzVents, Dashticz on tablet, Logitech Media Server
motejowi
Posts: 4
Joined: Tuesday 12 September 2017 4:12
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Blocky script, IF else if question

Post by motejowi »

After signing in you can see a gray smiley with "Add your reaction" tooltip (top right corner of message frame).
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests