[ANSWERED]: if statements not working need help

Posted on
Sat Jul 05, 2014 12:05 pm
Jpaction offline
Posts: 36
Joined: Jun 18, 2011

[ANSWERED]: if statements not working need help

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

Posted on
Sat Jul 05, 2014 4:24 pm
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: if statements not working need help

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)

Posted on
Sat Jul 05, 2014 4:29 pm
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: if statements not working need help

(Do you need 'is true' in each else if() section, like you have in the if() sections?)

Posted on
Sat Jul 05, 2014 7:03 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: if statements not working need help

Or cut and past the actual code into a "Code" block so we can see the formatting. Indentation is critical to Python.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sun Jul 06, 2014 4:35 am
DaveL17 offline
User avatar
Posts: 6741
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: if statements not working need help

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.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sun Jul 06, 2014 10:28 am
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: if statements not working need help

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.

Posted on
Mon Jul 07, 2014 3:56 am
DaveL17 offline
User avatar
Posts: 6741
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: if statements not working need help

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.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Tue Jul 08, 2014 7:37 pm
Jpaction offline
Posts: 36
Joined: Jun 18, 2011

Re: if statements not working need help

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.
Attachments
Screen Shot 2014-07-08 at 6.24.08 PM.png
Screen Shot 2014-07-08 at 6.24.08 PM.png (68.22 KiB) Viewed 5254 times
Screen Shot 2014-07-08 at 6.23.31 PM.png
Screen Shot 2014-07-08 at 6.23.31 PM.png (66.03 KiB) Viewed 5254 times

Posted on
Wed Jul 09, 2014 10:15 am
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: if statements not working need help

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

Posted on
Wed Jul 09, 2014 1:46 pm
matt (support) offline
Site Admin
User avatar
Posts: 21411
Joined: Jan 27, 2003
Location: Texas

Re: if statements not working need help

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.

Image

Posted on
Wed Jul 09, 2014 1:55 pm
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: if statements not working need help

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

Posted on
Wed Jul 09, 2014 2:32 pm
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: if statements not working need help

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.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests