Only one if...then working

Moderator: leecollings

Post Reply
Thelion
Posts: 54
Joined: Saturday 08 October 2016 12:15
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: The Netherlands
Contact:

Only one if...then working

Post 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
freijn
Posts: 536
Joined: Friday 23 December 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands Purmerend
Contact:

Re: Only one if...then working

Post 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
Thelion
Posts: 54
Joined: Saturday 08 October 2016 12:15
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: The Netherlands
Contact:

Re: Only one if...then working

Post 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.
freijn
Posts: 536
Joined: Friday 23 December 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands Purmerend
Contact:

Re: Only one if...then working

Post 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
SweetPants

Re: Only one if...then working

Post 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?
Thelion
Posts: 54
Joined: Saturday 08 October 2016 12:15
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: The Netherlands
Contact:

Re: Only one if...then working

Post 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?
Thelion
Posts: 54
Joined: Saturday 08 October 2016 12:15
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: The Netherlands
Contact:

Re: Only one if...then working

Post 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.
Thelion
Posts: 54
Joined: Saturday 08 October 2016 12:15
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: The Netherlands
Contact:

Re: Only one if...then working

Post by Thelion »

Anybody any thoughts on this?
freijn
Posts: 536
Joined: Friday 23 December 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands Purmerend
Contact:

Re: Only one if...then working

Post by freijn »

Have you tried my suggestion made a few posts back ?
Thelion
Posts: 54
Joined: Saturday 08 October 2016 12:15
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: The Netherlands
Contact:

Re: Only one if...then working

Post 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?
freijn
Posts: 536
Joined: Friday 23 December 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands Purmerend
Contact:

Re: Only one if...then working

Post 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
Thelion
Posts: 54
Joined: Saturday 08 October 2016 12:15
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: The Netherlands
Contact:

Re: Only one if...then working

Post by Thelion »

Ah. I understand now.

Thanks. Will try that.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest