When True is not true

Posted on
Wed Jan 10, 2018 9:49 am
CliveS offline
Posts: 770
Joined: Jan 10, 2016
Location: Medomsley, County Durham, UK

When True is not true

Not sure if this should be here or in the Python section......

Little snippet of code I wrote that had me baffled for ages due to case of True and False on Indigo Timers.

Why does the Indigo Timer Device need to be True (as opposed to true) when the states output show all the bool states as false or true (lower case) but to test it needs to be True (Upper case)?

states : States : (dict)
longStatusString : inactive (string)
timeLeftDays : 0 (integer)
timeLeftHours : 0 (string)
timeLeftMinutes : 0 (string)
timeLeftSeconds : 0 (string)
timerStartValueSeconds : 14400 (string)
timerStatus : inactive (string)
timerStatus.active : false (bool)
timerStatus.inactive : true (bool)
timerStatus.paused : false (bool)


varDrier3 = indigo.variables[983248758] # "varDrier3"
dev4HrTimer = indigo.devices[871078152] # "Bedroom 3 Drier 4 Hour Timer"

if varDrier3.value == "true" and (unicode(dev4HrTimer.states["timerStatus.inactive"])) == "True":
indigo.server.log("true and True")
indigo.server.log(str(varDrier3.value))
indigo.server.log(unicode(dev4HrTimer.states["timerStatus.active"]))
else:
indigo.server.log("not true")
indigo.server.log(str(varDrier3.value))
indigo.server.log(unicode(dev4HrTimer.states["timerStatus.active"]))

CliveS

Indigo 2023.2.0 : macOS Ventura 13.6.3 : Mac Mini M2 : 8‑core CPU and 10‑core GPU : 8 GB : 256GB SSD
----------------------------------------------------------------------------------
The best way to get the right answer on the Internet is not to ask a question, it's to post the wrong answer

Posted on
Wed Jan 10, 2018 9:57 am
Colorado4Wheeler offline
User avatar
Posts: 2794
Joined: Jul 20, 2009
Location: Colorado

Re: When True is not true

Proper case True and False are Python requirements, if you use Python code then they must be used like that:

Code: Select all
if varDrier3.value == "true" and (unicode(dev4HrTimer.states["timerStatus.inactive"])) == "True":

Isn't needed when dealing with an actual boolean field (now if you are dealing with a string field, then perhaps):

Code: Select all
if varDrier3.value == True:
if varDrier3.value:

Both of these statements resolve the same, no need for double quotes (that is for strings)

Code: Select all
if varDrier3.value == True:
if varDrier3.value:
if varDrier3.value == False:
if varDrier3.value != True:
if dev4HrTimer.states["timerStatus.inactive"]:
if dev4HrTimer.states["timerStatus.inactive"] == True:


If you Unicode them then you are turning them essentially into a string, so you would then have to use "True" or "true" (or str.lower()). Does that help?

My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here

Posted on
Wed Jan 10, 2018 10:04 am
CliveS offline
Posts: 770
Joined: Jan 10, 2016
Location: Medomsley, County Durham, UK

Re: When True is not true

Yes, thanks for that, I have a long way to go!!!!!!! but I will add that to the 'snippets' folder I am building up.
So if I stick to Upper case I will be fine but lower case can (and did) cause problems.

CliveS

Indigo 2023.2.0 : macOS Ventura 13.6.3 : Mac Mini M2 : 8‑core CPU and 10‑core GPU : 8 GB : 256GB SSD
----------------------------------------------------------------------------------
The best way to get the right answer on the Internet is not to ask a question, it's to post the wrong answer

Posted on
Wed Jan 10, 2018 10:09 am
Colorado4Wheeler offline
User avatar
Posts: 2794
Joined: Jul 20, 2009
Location: Colorado

Re: When True is not true

CliveS wrote:
So if I stick to Upper case I will be fine but lower case can (and did) cause problems.


Well, not UPPER, but Proper or Mixed would be a better term because those mean something too. True in upper is TRUE and in lower is true and in mixed/proper is True. You can do it the way you have been doing it using unicode and that's fine, but to make your life easier do something like this:

Code: Select all
if varDrier3.value.lower() == "true" and (unicode(dev4HrTimer.states["timerStatus.inactive"])).lower() == "true":


If you use the .lower() then you only ever have to resolve to a single value of "true" rather than having to worry about if it is "True" or if it is "true". That being said, it's easier to simply deal with boolean values in their native form of simply True or False (without any quotes whatsoever).

My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here

Posted on
Wed Jan 10, 2018 10:15 am
CliveS offline
Posts: 770
Joined: Jan 10, 2016
Location: Medomsley, County Durham, UK

Re: When True is not true

Thanks again, I like the .lower() solution, I can understand that better. :D

CliveS

Indigo 2023.2.0 : macOS Ventura 13.6.3 : Mac Mini M2 : 8‑core CPU and 10‑core GPU : 8 GB : 256GB SSD
----------------------------------------------------------------------------------
The best way to get the right answer on the Internet is not to ask a question, it's to post the wrong answer

Posted on
Wed Jan 10, 2018 10:18 am
Colorado4Wheeler offline
User avatar
Posts: 2794
Joined: Jul 20, 2009
Location: Colorado

Re: When True is not true

Awesome :). If you have a small snippet of code that you want to re-do then let me know and I'll fix it up so you can use it as an example going forward.

My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here

Posted on
Wed Jan 10, 2018 10:20 am
jay (support) offline
Site Admin
User avatar
Posts: 18212
Joined: Mar 19, 2008
Location: Austin, Texas

Re: When True is not true

A few more tips::

Code: Select all
a_variable = true    # Syntax error since true isn't a built-in
a_variable = True    # Correct since True is the built-in boolean value
a_variable = 'true'  # a_variable will evaluate to true because it's not an empty string or None
a_variable = 'false' # a_variable will evaluate to true because it's not an empty string or None

# Whenever you see a device state value listed as 'bool', then you compare the state directly to
# the built-in boolean representations in Python. In reality, you just inspect the value directly
# (no need to specify == True) since it will evaluate correctly:

varDrier3 = indigo.variables[983248758] # "varDrier3"
dev4HrTimer = indigo.devices[871078152] # "Bedroom 3 Drier 4 Hour Timer"

if dev4HrTimer.states['timerStatus.active']:   # Will directly evaluate the state's boolean value
    # Will evaluate correctly since the state is a boolean
    # do true things here when true

# On the other hand, the value of Indigo variables is always a string since there's no
# way for a user to specify what the correct type is.

if varDrier3.value.lower() == 'true':
   # correct way to make sure the value of the variable is equal to the string 'true'
   # ignoring case
   # do things here when true

# An alternate way to ask Indigo to attempt to coerce the variable value for you:

if varDrier3.getValue(bool):
    # Will evaluate correctly because Indigo will convert it to a boolean for you
    # do things here when true


[MODERATOR NOTE] moved to the Python Scripting forum

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed Jan 10, 2018 10:36 am
CliveS offline
Posts: 770
Joined: Jan 10, 2016
Location: Medomsley, County Durham, UK

Re: When True is not true

Thanks to both of you, I have read through all of that several times and it is now starting to make sense, I can now see where I was getting confused and now have a load more for the snippets folder :)

CliveS

Indigo 2023.2.0 : macOS Ventura 13.6.3 : Mac Mini M2 : 8‑core CPU and 10‑core GPU : 8 GB : 256GB SSD
----------------------------------------------------------------------------------
The best way to get the right answer on the Internet is not to ask a question, it's to post the wrong answer

Posted on
Wed Jan 10, 2018 10:40 am
Colorado4Wheeler offline
User avatar
Posts: 2794
Joined: Jul 20, 2009
Location: Colorado

Re: When True is not true

Py is pretty easy to wrap your head around, but your question can be difficult at first because when you are talking booleans there are a number of ways to deal with them, as both Jay and I posted in our examples. The way you are handling it is probably the way an awful lot of people who are new to Python do it, comparing strings is something we can all wrap our head around, but once you start to do a lot of lines of code you'll evolve out of necessity to doing it other ways too. Just the fact that you are doing it at all is awesome!

My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest