Page 1 of 1

[ANSWERED]: if statements not working need help

PostPosted: Sat Jul 05, 2014 12:05 pm
by Jpaction
I have the following if statement inside a trigger and the first part of the statement should be true and set "AllCloseToHome" to true and "AllNotCloseToHome" to false. However, it keeps setting both to false. I have even tried just using one variable value and it still sets both variables to false.

if value of variable "1JCloseToHome" is true and value of variable "1PCloseToHome" is true and value of variable "1ECloseToHome" is true and value of variable "1SCloseToHome" is true then
set value of variable "AllCloseToHome" to true
set value of variable "AllNotCloseToHome" to false
else if not (value of variable "1JCloseToHome") and not (value of variable "1PCloseToHome") and not (value of variable "1ECloseToHome") and not (value of variable "1SCloseToHome") then
set value of variable "AllCloseToHome" to false
set value of variable "AllNotCloseToHome" to true
else
set value of variable "AllCloseToHome" to false
set value of variable "AllNotCloseToHome" to false
end if

Re: if statements not working need help

PostPosted: Sat Jul 05, 2014 4:24 pm
by howartp
Can you screenshot the code - is it AppleScript or Python? (It isn't right for Python, but it doesn't look like what little I know of AppleScript)

Re: if statements not working need help

PostPosted: Sat Jul 05, 2014 4:29 pm
by howartp
(Do you need 'is true' in each else if() section, like you have in the if() sections?)

Re: if statements not working need help

PostPosted: Sat Jul 05, 2014 7:03 pm
by FlyingDiver
Or cut and past the actual code into a "Code" block so we can see the formatting. Indentation is critical to Python.

Re: if statements not working need help

PostPosted: Sun Jul 06, 2014 4:35 am
by DaveL17
Not sure why you would need variable [AllNotCloseToHome] since:
[AllCloseToHome] = true means that all are close to home, and
[AllCloseToHome] = false means that all are not close to home.

In other words, one variable can handle both conditions (either all are close to home or they're not.) If I understand what you're trying to do, I'd try this instead:

Code: Select all
if value of variable "1JCloseToHome" is "true" and value of variable "1PCloseToHome" is "true" and value of variable "1ECloseToHome" is "true" and value of variable "1SCloseToHome" is "true" then
     set value of variable "AllCloseToHome" to "true"
else
     set value of variable "AllCloseToHome" to "false"
end if
Be sure to indent your "sets" and put the true and false values in quotes.

Re: if statements not working need help

PostPosted: Sun Jul 06, 2014 10:28 am
by howartp
DaveL17 wrote:
Not sure why you would need variable [AllNotCloseToHome] since:
[AllCloseToHome] = true means that all are close to home, and
[AllCloseToHome] = false means that all are not close to home.

Actually, they're not mutually exclusive.

AllCloseToHome=True means all family members are at home.

AllCloseToHome=False means at least one family member is out and at least one family member is at home.

You need AllNotCloseToHome to determine that no family members are at home - in which case, the HVAC might be switched to low level - which you don't want if at least one family member is at home.

Re: if statements not working need help

PostPosted: Mon Jul 07, 2014 3:56 am
by DaveL17
Got it. I'd suggest reversing the second and third tests thusly:

Code: Select all
if value of variable "Foo01" is "true" and value of variable "Foo02" is "true" then
   -- All are true   
   set value of variable "Foo03" to "true"
   set value of variable "Foo04" to "false"
   
else if not (value of variable "Foo01") and not (value of variable "Foo02") then
   -- All are false
   set value of variable "Foo03" to "false"
   set value of variable "Foo04" to "false"
   
else
   -- At least one, but not all are true.
   set value of variable "Foo03" to "false"
   set value of variable "Foo04" to "true"
end if
First condition runs if all are true. Second condition runs if all are false. Third condition runs if any (but not all) are true.

Re: if statements not working need help

PostPosted: Tue Jul 08, 2014 7:37 pm
by Jpaction
Thanks for everyone's help! I think the problem was a couple of things but the biggest issue was not putting true into quotations "true". Something must have changed with Applescript because this used to work.

Re: if statements not working need help

PostPosted: Wed Jul 09, 2014 10:15 am
by howartp
Jpaction wrote:
Thanks for everyone's help! I think the problem was a couple of things but the biggest issue was not putting true into quotations "true". Something must have changed with Applescript because this used to work.


I don't know Applescript, but I suspect this isn't a change in Applescript - it's a change in whatever is setting the value of your variables.

if is true is testing if the value is Boolean True, as per the purple highlighting.
if is "true" is testing if the value is equal to the string "true", as per the black highlighting.

It looks like previously you had a trigger/action/event setting the variable to Boolean true, whereas now something (maybe a new trigger you've created based on a new device?) is setting the variable to string "true"?

Peter

Re: if statements not working need help

PostPosted: Wed Jul 09, 2014 1:46 pm
by matt (support)
Indigo hasn't changed (intentionally at least) how it handles variable values — they are always strings. So to test if a variable value is "true" you have to compare it to the string "true." If you compare it to the AppleScript boolean True then AppleScript will always evaluate it to False since the string "true" does not equal the boolean True.

You can cast/force the variable value to a boolean type like this:
Code: Select all
   if (value of variable "foo") as boolean is true then log "is true"
   if (value of variable "foo") as boolean is false then log "is false"

but normally it is best to just compare it to the raw string value, IMO.

If it worked before, then perhaps there was a subtle change in how AppleScript does indirect casting to booleans.

Re: if statements not working need help

PostPosted: Wed Jul 09, 2014 1:55 pm
by howartp
matt (support) wrote:
...but normally it is best to just compare it to the raw string value, IMO.

IMO, it would be best to allow variables to BE boolean values if that is what they are.

The UI for actions implies they are boolean as it offers true, false or free text.

Maybe you could change UI to have quote marks around the "Set to" variable actions for now?

Code: Select all
Set to "true"
Set to "false"
Set to: _____
Increment by 1
Decrement by 1


Peter

Re: if statements not working need help

PostPosted: Wed Jul 09, 2014 2:32 pm
by jay (support)
howartp wrote:
IMO, it would be best to allow variables to BE boolean values if that is what they are.


That's something we want to do at some point (add data type specifications to variables and device states) - however Indigo currently stores variable values as strings with no regard to what they "could" be coerced to. We do some coercion in actions to make it convenient for users, but those coercions are always done after the fact (in the action/trigger execution). AppleScript and Python get the raw variable values, which are always strings.