Page 1 of 1

Only one if...then working

Posted: Saturday 27 January 2018 14:16
by Thelion
Hi!

I don't know if this is really an issue, or just the standard way that LUA works, but I found out that script that contains multiple if...then functions, only the last one seems to be triggered.

I have a script like this:

Code: Select all

commandArray = {}


sNestSetpointHeatOn = tonumber(uservariables["uNestSetpointHeatOn"])
sNestSetpointHeatOff = tonumber(uservariables["uNestSetpointHeatOff"])

if ( uservariables["DV_Keuken_Heat"] == 1 or uservariables["DV_Woonkamer_Heat"] == 1 or uservariables["DV_Hal_Heat"] == 1 or uservariables["DV_PC-Kamer_Heat"] == 1  or uservariables["DV_BadKamer_Heat"] == 1  or uservariables["DV_KleedKamer_Heat"] == 1 ) and uservariables["DV_Nest_Heat"] == 0 then
   commandArray['Variable:DV_Nest_Heat']='1'
   commandArray['Nest Verwarmen'] = "On"   
   commandArray['SendNotification']='[Nest Verwarmen Aan#[Nest Verwarmen Aan]#0#Classical'
   print('<font color="RED">[DV Nest Aan]</font>')
   commandArray [ 'UpdateDevice'] = "29 | 0 | "..sNestSetpointHeatOn
   end
if ( uservariables["DV_Keuken_Heat"] == 0 and uservariables["DV_Woonkamer_Heat"] == 0 and uservariables["DV_Hal_Heat"] == 0 and uservariables["DV_PC-Kamer_Heat"] == 0  and uservariables["DV_Badkamer_Heat"] == 0  and uservariables["DV_Kleedkamer_Heat"] == 0 ) and uservariables["DV_Nest_Heat"] == 1 then
   commandArray['Variable:DV_Nest_Heat']='0'
   commandArray['Nest Verwarmen'] = "Off"      
   commandArray['SendNotification']='[Nest Verwarmen Uit]#[Nest Verwarmen Uit]#0#Classical' 
   print('<font color="RED">[DV Nest Uit]</font>')
   commandArray [ 'UpdateDevice'] = "29 | 0 | "..sNestSetpointHeatOff   
   end
return commandArray
In this script only the last if..then is ever triggered.

When i split it into 2 seperate scripts, like:

Code: Select all

commandArray = {}

sNestSetpointHeatOn = tonumber(uservariables["uNestSetpointHeatOn"])
sNestSetpointHeatOff = tonumber(uservariables["uNestSetpointHeatOff"])

if (uservariables["DV_Keuken_Heat"] == 1 or uservariables["DV_Woonkamer_Heat"] == 1 or uservariables["DV_Hal_Heat"] == 1 or uservariables["DV_PC-Kamer_Heat"] == 1 or uservariables["DV_Badkamer_Heat"] == 1 or uservariables["DV_Kleedkamer_Heat"] == 1) and uservariables["DV_Nest_Heat"] == 0 then
   commandArray['Variable:DV_Nest_Heat']='1'
   commandArray['Nest Verwarmen'] = "On"   
   commandArray['SendNotification']='[Nest Verwarmen Aan#[Nest Verwarmen Aan]#0#Classical'
   print('<font color="RED">[DV Nest Aan]</font>')
   commandArray [ 'UpdateDevice'] = "29 | 0 | "..sNestSetpointHeatOn
end
return commandArray
and

Code: Select all

commandArray = {}

sNestSetpointHeatOn = tonumber(uservariables["uNestSetpointHeatOn"])
sNestSetpointHeatOff = tonumber(uservariables["uNestSetpointHeatOff"])

if (uservariables["DV_Keuken_Heat"] == 0 and uservariables["DV_Woonkamer_Heat"] == 0 and uservariables["DV_Hal_Heat"] == 0 and uservariables["DV_PC-Kamer_Heat"] == 0 and uservariables["DV_Badkamer_Heat"] == 0 and uservariables["DV_Kleedkamer_Heat"] == 0) and uservariables["DV_Nest_Heat"] == 1 then
   commandArray['Variable:DV_Nest_Heat']='0'
   commandArray['Nest Verwarmen'] = "Off"      
   commandArray['SendNotification']='[Nest Verwarmen Uit]#[Nest Verwarmen Uit]#0#Classical' 
   print('<font color="RED">[DV Nest Uit]</font>')
   commandArray [ 'UpdateDevice'] = "29 | 0 | "..sNestSetpointHeatOff   
end
return commandArray
They both are triggered when they are supposed to and all works wel.

Is there an explanation for this?

Martijn

Re: Only one if...then working

Posted: Saturday 27 January 2018 14:24
by freijn
First thing to try...

if ( I only see the opening ( not the closing ) (or do I need to clean my Bril ? )

I suggest to put the ) just before the then and try it again.

Cheers,

Frank

Re: Only one if...then working

Posted: Saturday 27 January 2018 14:31
by Thelion
Hi freijn,

I think you need to clean your bril ;-) There is a closing bracket, but it is before the last "and".

And the brackets are EXACTLY the same in the separate scripts and there is works flawlessly.

Re: Only one if...then working

Posted: Saturday 27 January 2018 14:36
by freijn
Oeps :-)

then try it with only 1 ==

and put a print statement in the both if's and see which one is selected...

if <= haha that is working start building the other == with it.

try and error... sorry..

Frank

Re: Only one if...then working

Posted: Saturday 27 January 2018 14:48
by SweetPants
If both your 'if' statements are valid, then only the last commandArray will be executed. The first is overridden. So your conclusion is correct.
Maybe change the lase if to elseif?

Re: Only one if...then working

Posted: Saturday 27 January 2018 15:30
by Thelion
Ah. So I’m not losing my mind. :roll:

I don't mind splitting it in two separate scripts, or using elseif for that matter, but I was wondering if this was a bug or by design.

Thanks SweetPants.

Does this also go for DzVents? Or is it possible to have two if..then statements there?

Re: Only one if...then working

Posted: Saturday 27 January 2018 15:37
by Thelion
Erhm. I just re-read SweetPant's message, and I noticed something. Both if statements are never both valid. So it shouldn't be a problem.

Re: Only one if...then working

Posted: Wednesday 31 January 2018 19:11
by Thelion
Anybody any thoughts on this?

Re: Only one if...then working

Posted: Thursday 01 February 2018 13:38
by freijn
Have you tried my suggestion made a few posts back ?

Re: Only one if...then working

Posted: Thursday 01 February 2018 13:42
by Thelion
Partly. There is already a print statement in both and only the last one is ever selected.

I didn't know exactly what you meant by
then try it with only 1 ==
Care to explain?

Re: Only one if...then working

Posted: Thursday 01 February 2018 20:50
by freijn
At least that is my approach if I can't get it to work.. Simplify it and see what happens

So my suggestion :

if ( uservariables["DV_Keuken_Heat"] == 1 or uservariables["DV_Woonkamer_Heat"] == 1 )then
commandArray['Variable:DV_Nest_Heat']='1'
commandArray['Nest Verwarmen'] = "On"
commandArray['SendNotification']='[Nest Verwarmen Aan#[Nest Verwarmen Aan]#0#Classical'
print('<font color="RED">[DV Nest Aan]</font>')
commandArray [ 'UpdateDevice'] = "29 | 0 | "..sNestSetpointHeatOn
end
if ( uservariables["DV_Keuken_Heat"] == 0 and uservariables["DV_Woonkamer_Heat"] == 0 )then
commandArray['Variable:DV_Nest_Heat']='0'
commandArray['Nest Verwarmen'] = "Off"
commandArray['SendNotification']='[Nest Verwarmen Uit]#[Nest Verwarmen Uit]#0#Classical'
print('<font color="RED">[DV Nest Uit]</font>')
commandArray [ 'UpdateDevice'] = "29 | 0 | "..sNestSetpointHeatOff
end

Like this or even a single uservariables["DV_Keuken_Heat"] == 1 without an 'or' and 'and'

Strip down the complexity and try if it works, then slowly start building up.

Please let me know if you got my suggestion now, otherwise we switch to pm's and in dutch :-)

Cheers,
Frank

Re: Only one if...then working

Posted: Thursday 01 February 2018 20:57
by Thelion
Ah. I understand now.

Thanks. Will try that.