Getting value of an Indigo variable with a name in variable

Posted on
Wed Oct 23, 2019 1:29 pm
mgolden50 offline
User avatar
Posts: 259
Joined: Jan 29, 2007
Location: Chandler, AZ

Getting value of an Indigo variable with a name in variable

Hi,
I'm stumped! I'm still in the process of converting AppleScripts to Python. These lines worked in AppleScript but are failing when converted to Python. I have to use a variable name rather than an ID because I need to calculate the Indigo variable's name.

Here's the code that fails:

Code: Select all
StepNumber = "1" # Temp overide

WateringDayCountVarName = "WateringDaysBetweenCounterLawnZone" + StepNumber # Assign WateringDayCountVarName  to Name of Indigo's Watering Days  Counter variable

WateringDaysBetweenCounterZoneX = indigo.variables["WateringDayCountVarName"].value
indigo.server.log (WateringDaysBetweenCounterZoneX)

Here is the error message:

Script Error Watering Next Step Triggered.py: 'key name WateringDayCountVarName not found in database'
Script Error Exception Traceback (most recent call shown last):
Watering Next Step Triggered.py, line 8, at top level
KeyError: 'key name WateringDayCountVarName not found in database'

Thanks for any help you can provide,
Mike

Posted on
Wed Oct 23, 2019 2:02 pm
jay (support) offline
Site Admin
User avatar
Posts: 18212
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Getting value of an Indigo variable with a name in vari

The problem is that you're asking for a variable named "WateringDayCountVarName", not "WateringDaysBetweenCounterLawnZone1" which I believe is the intent. Remove the quotes in the second to last line. I've make a tweak to make the script a bit more future proof:

Code: Select all
StepNumber = "1" # Temp overide

# This is the preferred way to manipulate a string
WateringDayCountVarName = "WateringDaysBetweenCounterLawnZone{}".format(StepNumber) # Assign WateringDayCountVarName  to Name of Indigo's Watering Days  Counter variable

# Note the lack of quotes around WateringDayCountVarName - it's a Python variable, not a literal string
WateringDaysBetweenCounterZoneX = indigo.variables[WateringDayCountVarName].value
indigo.server.log (WateringDaysBetweenCounterZoneX)


While using the name of a variable in indigo.variables[] works, it's relatively unsafe as a change to the variable name would break the script. Given how it appears you're using them, though, I suspect that may not be as big an issue for this use-case as it is with other scripts.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed Oct 23, 2019 2:21 pm
mgolden50 offline
User avatar
Posts: 259
Joined: Jan 29, 2007
Location: Chandler, AZ

Re: Getting value of an Indigo variable with a name in vari

Jay,

Thanks that worked!
I would've never figured that our on my own.

I tried to post the code as a code block using the code button, but I see the code was not posted as a block. Why?

Posted on
Wed Oct 23, 2019 2:27 pm
FlyingDiver offline
User avatar
Posts: 7210
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Getting value of an Indigo variable with a name in vari

mgolden50 wrote:
I tried to post the code as a code block using the code button, but I see the code was not posted as a block. Why?


The code was, the error messages were not.

Are you using Tapatalk? It doesn't handle code blocks at all.

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

Posted on
Wed Oct 23, 2019 2:34 pm
mgolden50 offline
User avatar
Posts: 259
Joined: Jan 29, 2007
Location: Chandler, AZ

Re: Getting value of an Indigo variable with a name in vari

No, I was using Indigo forum reply form.
But the code didn't show up as a block when I looked at my own post.

Posted on
Wed Oct 23, 2019 2:36 pm
FlyingDiver offline
User avatar
Posts: 7210
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Getting value of an Indigo variable with a name in vari

mgolden50 wrote:
No, I was using Indigo forum reply form.
But the code didn't show up as a block when I looked at my own post.


That's weird. Never seen that behavior.

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

Posted on
Wed Oct 23, 2019 3:07 pm
jay (support) offline
Site Admin
User avatar
Posts: 18212
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Getting value of an Indigo variable with a name in vari

You had BBCode turned off for your post. I enabled it... :D

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed Oct 23, 2019 3:08 pm
mgolden50 offline
User avatar
Posts: 259
Joined: Jan 29, 2007
Location: Chandler, AZ

Re: Getting value of an Indigo variable with a name in vari

Thanks

Posted on
Wed Oct 23, 2019 4:32 pm
mgolden50 offline
User avatar
Posts: 259
Joined: Jan 29, 2007
Location: Chandler, AZ

Re: Getting value of an Indigo variable with a name in vari

How do I concatenate additional text into the string using this format?

[code]
WateringZoneXOn = "Watering Zone {}".format(StepNumber)+" Om"
[/code]

Posted on
Wed Oct 23, 2019 4:34 pm
FlyingDiver offline
User avatar
Posts: 7210
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Getting value of an Indigo variable with a name in vari

Code: Select all
WateringZoneXOn = "Watering Zone {}{}".format(StepNumber, " Om")


Or better

Code: Select all
WateringZoneXOn = "Watering Zone {} Om".format(StepNumber)

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

Posted on
Wed Oct 23, 2019 4:41 pm
mgolden50 offline
User avatar
Posts: 259
Joined: Jan 29, 2007
Location: Chandler, AZ

Re: Getting value of an Indigo variable with a name in vari

Thanks Joe,
Worked great. Still trying to master all of these indirect references to objects.

Posted on
Wed Oct 23, 2019 5:21 pm
jay (support) offline
Site Admin
User avatar
Posts: 18212
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Getting value of an Indigo variable with a name in vari

Please enable BBCode on your posts (I only turned it on for the top post). It appears that you have it disabled in your account settings (it's on by default otherwise).

If this is going to be the name of an Indigo variable, which can't contain spaces, I think this is what you want:

Code: Select all
WateringZoneXOn = "WateringZone{}Om".format(StepNumber)

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 3 guests